blob: dd89328dd0152d0ec7eb8539bb81152470852996 [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. */
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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -0700314
Jeff Brownb88102f2010-09-08 11:49:43 -0700315 virtual void dump(String8& dump);
316
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown01ce2e92010-09-26 22:20:12 -0700378 inline bool isInjected() { return injectionState != NULL; }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700379 };
380
381 struct ConfigurationChangedEntry : EventEntry {
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -0700403 };
404
405 struct MotionSample {
406 MotionSample* next;
407
408 nsecs_t eventTime;
409 PointerCoords pointerCoords[MAX_POINTERS];
410 };
411
412 struct MotionEntry : EventEntry {
413 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800414 uint32_t source;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700415 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -0700416 int32_t flags;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700417 int32_t metaState;
418 int32_t edgeFlags;
419 float xPrecision;
420 float yPrecision;
421 nsecs_t downTime;
422 uint32_t pointerCount;
423 int32_t pointerIds[MAX_POINTERS];
424
425 // Linked list of motion samples associated with this motion event.
426 MotionSample firstSample;
427 MotionSample* lastSample;
Jeff Brownae9fc032010-08-18 15:51:08 -0700428
429 uint32_t countSamples() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700430 };
431
Jeff Brown9c3cda02010-06-15 01:31:58 -0700432 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700433 struct DispatchEntry : Link<DispatchEntry> {
434 EventEntry* eventEntry; // the event to dispatch
435 int32_t targetFlags;
436 float xOffset;
437 float yOffset;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400438 float scaleFactor;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700439
440 // True if dispatch has started.
441 bool inProgress;
442
443 // For motion events:
444 // Pointer to the first motion sample to dispatch in this cycle.
445 // Usually NULL to indicate that the list of motion samples begins at
446 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
447 // cycle and this pointer indicates the location of the first remainining sample
448 // to dispatch during the current cycle.
449 MotionSample* headMotionSample;
450 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
451 // unable to send all motion samples during this cycle. On the next cycle,
452 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
453 // will be set to NULL.
454 MotionSample* tailMotionSample;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700455
Jeff Brown519e0242010-09-15 15:18:56 -0700456 inline bool hasForegroundTarget() const {
457 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Brownb88102f2010-09-08 11:49:43 -0700458 }
Jeff Brown01ce2e92010-09-26 22:20:12 -0700459
460 inline bool isSplit() const {
461 return targetFlags & InputTarget::FLAG_SPLIT;
462 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700463 };
464
Jeff Brown9c3cda02010-06-15 01:31:58 -0700465 // A command entry captures state and behavior for an action to be performed in the
466 // dispatch loop after the initial processing has taken place. It is essentially
467 // a kind of continuation used to postpone sensitive policy interactions to a point
468 // in the dispatch loop where it is safe to release the lock (generally after finishing
469 // the critical parts of the dispatch cycle).
470 //
471 // The special thing about commands is that they can voluntarily release and reacquire
472 // the dispatcher lock at will. Initially when the command starts running, the
473 // dispatcher lock is held. However, if the command needs to call into the policy to
474 // do some work, it can release the lock, do the work, then reacquire the lock again
475 // before returning.
476 //
477 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
478 // never calls into the policy while holding its lock.
479 //
480 // Commands are implicitly 'LockedInterruptible'.
481 struct CommandEntry;
482 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
483
Jeff Brown7fbdc842010-06-17 20:52:56 -0700484 class Connection;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700485 struct CommandEntry : Link<CommandEntry> {
486 CommandEntry();
487 ~CommandEntry();
488
489 Command command;
490
491 // parameters for the command (usage varies by command)
Jeff Brown7fbdc842010-06-17 20:52:56 -0700492 sp<Connection> connection;
Jeff Brownb88102f2010-09-08 11:49:43 -0700493 nsecs_t eventTime;
494 KeyEntry* keyEntry;
495 sp<InputChannel> inputChannel;
496 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800497 sp<InputWindowHandle> inputWindowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700498 int32_t userActivityEventType;
Jeff Brown3915bb82010-11-05 15:02:16 -0700499 bool handled;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700500 };
501
502 // Generic queue implementation.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700503 template <typename T>
504 struct Queue {
Jeff Brownb88102f2010-09-08 11:49:43 -0700505 T headSentinel;
506 T tailSentinel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700507
508 inline Queue() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700509 headSentinel.prev = NULL;
510 headSentinel.next = & tailSentinel;
511 tailSentinel.prev = & headSentinel;
512 tailSentinel.next = NULL;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700513 }
514
Jeff Brownb88102f2010-09-08 11:49:43 -0700515 inline bool isEmpty() const {
516 return headSentinel.next == & tailSentinel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700517 }
518
519 inline void enqueueAtTail(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700520 T* last = tailSentinel.prev;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700521 last->next = entry;
522 entry->prev = last;
Jeff Brownb88102f2010-09-08 11:49:43 -0700523 entry->next = & tailSentinel;
524 tailSentinel.prev = entry;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700525 }
526
527 inline void enqueueAtHead(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700528 T* first = headSentinel.next;
529 headSentinel.next = entry;
530 entry->prev = & headSentinel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700531 entry->next = first;
532 first->prev = entry;
533 }
534
535 inline void dequeue(T* entry) {
536 entry->prev->next = entry->next;
537 entry->next->prev = entry->prev;
538 }
539
540 inline T* dequeueAtHead() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700541 T* first = headSentinel.next;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700542 dequeue(first);
543 return first;
544 }
Jeff Brown519e0242010-09-15 15:18:56 -0700545
546 uint32_t count() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700547 };
548
549 /* Allocates queue entries and performs reference counting as needed. */
550 class Allocator {
551 public:
552 Allocator();
553
Jeff Brown01ce2e92010-09-26 22:20:12 -0700554 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700555 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
556 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brown58a2da82011-01-25 16:02:22 -0800557 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700558 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
559 int32_t repeatCount, nsecs_t downTime);
560 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brown58a2da82011-01-25 16:02:22 -0800561 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700562 int32_t flags, int32_t metaState, int32_t edgeFlags,
563 float xPrecision, float yPrecision,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700564 nsecs_t downTime, uint32_t pointerCount,
565 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Brownb88102f2010-09-08 11:49:43 -0700566 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400567 int32_t targetFlags, float xOffset, float yOffset, float scaleFactor);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700568 CommandEntry* obtainCommandEntry(Command command);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700569
Jeff Brown01ce2e92010-09-26 22:20:12 -0700570 void releaseInjectionState(InjectionState* injectionState);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700571 void releaseEventEntry(EventEntry* entry);
572 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
573 void releaseKeyEntry(KeyEntry* entry);
574 void releaseMotionEntry(MotionEntry* entry);
575 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700576 void releaseCommandEntry(CommandEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700577
Jeff Brown01ce2e92010-09-26 22:20:12 -0700578 void recycleKeyEntry(KeyEntry* entry);
579
Jeff Brown46b9ac02010-04-22 18:58:52 -0700580 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700581 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700582
583 private:
Jeff Brown01ce2e92010-09-26 22:20:12 -0700584 Pool<InjectionState> mInjectionStatePool;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700585 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
586 Pool<KeyEntry> mKeyEntryPool;
587 Pool<MotionEntry> mMotionEntryPool;
588 Pool<MotionSample> mMotionSamplePool;
589 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700590 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700591
Jeff Brownb6997262010-10-08 22:31:17 -0700592 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime,
593 uint32_t policyFlags);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700594 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700595 };
596
Jeff Brownb88102f2010-09-08 11:49:43 -0700597 /* Tracks dispatched key and motion event state so that cancelation events can be
598 * synthesized when events are dropped. */
599 class InputState {
600 public:
Jeff Brownb6997262010-10-08 22:31:17 -0700601 // Specifies the sources to cancel.
602 enum CancelationOptions {
603 CANCEL_ALL_EVENTS = 0,
604 CANCEL_POINTER_EVENTS = 1,
605 CANCEL_NON_POINTER_EVENTS = 2,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800606 CANCEL_FALLBACK_EVENTS = 3,
Jeff Brownb6997262010-10-08 22:31:17 -0700607 };
608
Jeff Brownb88102f2010-09-08 11:49:43 -0700609 InputState();
610 ~InputState();
611
612 // Returns true if there is no state to be canceled.
613 bool isNeutral() const;
614
Jeff Brownb88102f2010-09-08 11:49:43 -0700615 // Records tracking information for an event that has just been published.
Jeff Browncc0c1592011-02-19 05:07:28 -0800616 void trackEvent(const EventEntry* entry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700617
618 // Records tracking information for a key event that has just been published.
Jeff Browncc0c1592011-02-19 05:07:28 -0800619 void trackKey(const KeyEntry* entry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700620
621 // Records tracking information for a motion event that has just been published.
Jeff Browncc0c1592011-02-19 05:07:28 -0800622 void trackMotion(const MotionEntry* entry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700623
Jeff Brownb6997262010-10-08 22:31:17 -0700624 // Synthesizes cancelation events for the current state and resets the tracked state.
625 void synthesizeCancelationEvents(nsecs_t currentTime, Allocator* allocator,
626 Vector<EventEntry*>& outEvents, CancelationOptions options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700627
628 // Clears the current state.
629 void clear();
630
Jeff Brown9c9f1a32010-10-11 18:32:20 -0700631 // Copies pointer-related parts of the input state to another instance.
632 void copyPointerStateTo(InputState& other) const;
633
Jeff Brownb88102f2010-09-08 11:49:43 -0700634 private:
Jeff Brownb88102f2010-09-08 11:49:43 -0700635 struct KeyMemento {
636 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800637 uint32_t source;
Jeff Brownb88102f2010-09-08 11:49:43 -0700638 int32_t keyCode;
639 int32_t scanCode;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800640 int32_t flags;
Jeff Brownb88102f2010-09-08 11:49:43 -0700641 nsecs_t downTime;
642 };
643
644 struct MotionMemento {
645 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800646 uint32_t source;
Jeff Brownb88102f2010-09-08 11:49:43 -0700647 float xPrecision;
648 float yPrecision;
649 nsecs_t downTime;
650 uint32_t pointerCount;
651 int32_t pointerIds[MAX_POINTERS];
652 PointerCoords pointerCoords[MAX_POINTERS];
653
654 void setPointers(const MotionEntry* entry);
655 };
656
657 Vector<KeyMemento> mKeyMementos;
658 Vector<MotionMemento> mMotionMementos;
Jeff Brownb6997262010-10-08 22:31:17 -0700659
Jeff Brown49ed71d2010-12-06 17:13:33 -0800660 static bool shouldCancelKey(const KeyMemento& memento,
661 CancelationOptions options);
662 static bool shouldCancelMotion(const MotionMemento& memento,
663 CancelationOptions options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700664 };
665
Jeff Brown46b9ac02010-04-22 18:58:52 -0700666 /* Manages the dispatch state associated with a single input channel. */
667 class Connection : public RefBase {
668 protected:
669 virtual ~Connection();
670
671 public:
672 enum Status {
673 // Everything is peachy.
674 STATUS_NORMAL,
675 // An unrecoverable communication error has occurred.
676 STATUS_BROKEN,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700677 // The input channel has been unregistered.
678 STATUS_ZOMBIE
679 };
680
681 Status status;
Jeff Brown928e0542011-01-10 11:17:36 -0800682 sp<InputChannel> inputChannel; // never null
683 sp<InputWindowHandle> inputWindowHandle; // may be null
Jeff Brown46b9ac02010-04-22 18:58:52 -0700684 InputPublisher inputPublisher;
Jeff Brownb88102f2010-09-08 11:49:43 -0700685 InputState inputState;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700686 Queue<DispatchEntry> outboundQueue;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700687
688 nsecs_t lastEventTime; // the time when the event was originally captured
689 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Brownbfaf3b92011-02-22 15:00:50 -0800690 int32_t originalKeyCodeForFallback; // original keycode for fallback in progress, -1 if none
Jeff Brown46b9ac02010-04-22 18:58:52 -0700691
Jeff Brown928e0542011-01-10 11:17:36 -0800692 explicit Connection(const sp<InputChannel>& inputChannel,
693 const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700694
Jeff Brown9c3cda02010-06-15 01:31:58 -0700695 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
696
697 const char* getStatusLabel() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700698
699 // Finds a DispatchEntry in the outbound queue associated with the specified event.
700 // Returns NULL if not found.
701 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
702
Jeff Brown46b9ac02010-04-22 18:58:52 -0700703 // Gets the time since the current event was originally obtained from the input driver.
Jeff Brownb88102f2010-09-08 11:49:43 -0700704 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700705 return (currentTime - lastEventTime) / 1000000.0;
706 }
707
708 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Brownb88102f2010-09-08 11:49:43 -0700709 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700710 return (currentTime - lastDispatchTime) / 1000000.0;
711 }
712
Jeff Brown46b9ac02010-04-22 18:58:52 -0700713 status_t initialize();
714 };
715
Jeff Brownb6997262010-10-08 22:31:17 -0700716 enum DropReason {
717 DROP_REASON_NOT_DROPPED = 0,
718 DROP_REASON_POLICY = 1,
719 DROP_REASON_APP_SWITCH = 2,
720 DROP_REASON_DISABLED = 3,
Jeff Brown928e0542011-01-10 11:17:36 -0800721 DROP_REASON_BLOCKED = 4,
722 DROP_REASON_STALE = 5,
Jeff Brownb6997262010-10-08 22:31:17 -0700723 };
724
Jeff Brown9c3cda02010-06-15 01:31:58 -0700725 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700726
727 Mutex mLock;
728
Jeff Brown46b9ac02010-04-22 18:58:52 -0700729 Allocator mAllocator;
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700730 sp<Looper> mLooper;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700731
Jeff Brownb88102f2010-09-08 11:49:43 -0700732 EventEntry* mPendingEvent;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700733 Queue<EventEntry> mInboundQueue;
734 Queue<CommandEntry> mCommandQueue;
735
Jeff Brownb88102f2010-09-08 11:49:43 -0700736 Vector<EventEntry*> mTempCancelationEvents;
737
738 void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay,
739 nsecs_t* nextWakeupTime);
740
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700741 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Brownb88102f2010-09-08 11:49:43 -0700742 bool enqueueInboundEventLocked(EventEntry* entry);
743
Jeff Brownb6997262010-10-08 22:31:17 -0700744 // Cleans up input state when dropping an inbound event.
745 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
746
Jeff Brownb88102f2010-09-08 11:49:43 -0700747 // App switch latency optimization.
Jeff Brownb6997262010-10-08 22:31:17 -0700748 bool mAppSwitchSawKeyDown;
Jeff Brownb88102f2010-09-08 11:49:43 -0700749 nsecs_t mAppSwitchDueTime;
750
Jeff Brownb6997262010-10-08 22:31:17 -0700751 static bool isAppSwitchKeyCode(int32_t keyCode);
752 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700753 bool isAppSwitchPendingLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700754 void resetPendingAppSwitchLocked(bool handled);
755
Jeff Brown928e0542011-01-10 11:17:36 -0800756 // Stale event latency optimization.
757 static bool isStaleEventLocked(nsecs_t currentTime, EventEntry* entry);
758
759 // Blocked event latency optimization. Drops old events when the user intends
760 // to transfer focus to a new application.
761 EventEntry* mNextUnblockedEvent;
762
763 const InputWindow* findTouchedWindowAtLocked(int32_t x, int32_t y);
764
Jeff Brown46b9ac02010-04-22 18:58:52 -0700765 // All registered connections mapped by receive pipe file descriptor.
766 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
767
Jeff Brown519e0242010-09-15 15:18:56 -0700768 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown2cbecea2010-08-17 15:59:26 -0700769
Jeff Brown46b9ac02010-04-22 18:58:52 -0700770 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700771 // We don't use a ref-counted pointer here because we explicitly abort connections
772 // during unregistration which causes the connection's outbound queue to be cleared
773 // and the connection itself to be deactivated.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700774 Vector<Connection*> mActiveConnections;
775
Jeff Brownb88102f2010-09-08 11:49:43 -0700776 // Input channels that will receive a copy of all input events.
777 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700778
Jeff Brown7fbdc842010-06-17 20:52:56 -0700779 // Event injection and synchronization.
780 Condition mInjectionResultAvailableCondition;
Jeff Brownb6997262010-10-08 22:31:17 -0700781 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700782 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
783
Jeff Brown6ec402b2010-07-28 15:48:59 -0700784 Condition mInjectionSyncFinishedCondition;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700785 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -0700786 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown6ec402b2010-07-28 15:48:59 -0700787
Jeff Brownae9fc032010-08-18 15:51:08 -0700788 // Throttling state.
789 struct ThrottleState {
790 nsecs_t minTimeBetweenEvents;
791
792 nsecs_t lastEventTime;
793 int32_t lastDeviceId;
794 uint32_t lastSource;
795
796 uint32_t originalSampleCount; // only collected during debugging
797 } mThrottleState;
798
Jeff Brown46b9ac02010-04-22 18:58:52 -0700799 // Key repeat tracking.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700800 struct KeyRepeatState {
801 KeyEntry* lastKeyEntry; // or null if no repeat
802 nsecs_t nextRepeatTime;
803 } mKeyRepeatState;
804
805 void resetKeyRepeatLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700806 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700807
Jeff Brown9c3cda02010-06-15 01:31:58 -0700808 // Deferred command processing.
809 bool runCommandsLockedInterruptible();
810 CommandEntry* postCommandLocked(Command command);
811
Jeff Brownb88102f2010-09-08 11:49:43 -0700812 // Inbound event processing.
813 void drainInboundQueueLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700814 void releasePendingEventLocked();
815 void releaseInboundEventLocked(EventEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700816
Jeff Brownb88102f2010-09-08 11:49:43 -0700817 // Dispatch state.
818 bool mDispatchEnabled;
819 bool mDispatchFrozen;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700820
Jeff Brownb88102f2010-09-08 11:49:43 -0700821 Vector<InputWindow> mWindows;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700822
823 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Brownb88102f2010-09-08 11:49:43 -0700824
825 // Focus tracking for keys, trackball, etc.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700826 const InputWindow* mFocusedWindow;
Jeff Brownb88102f2010-09-08 11:49:43 -0700827
828 // Focus tracking for touch.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700829 struct TouchedWindow {
830 const InputWindow* window;
831 int32_t targetFlags;
Jeff Brown46e75292010-11-10 16:53:45 -0800832 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
Jeff Brown01ce2e92010-09-26 22:20:12 -0700833 sp<InputChannel> channel;
Jeff Brownb88102f2010-09-08 11:49:43 -0700834 };
Jeff Brown01ce2e92010-09-26 22:20:12 -0700835 struct TouchState {
836 bool down;
837 bool split;
Jeff Brown95712852011-01-04 19:41:59 -0800838 int32_t deviceId; // id of the device that is currently down, others are rejected
Jeff Brown58a2da82011-01-25 16:02:22 -0800839 uint32_t source; // source of the device that is current down, others are rejected
Jeff Brown01ce2e92010-09-26 22:20:12 -0700840 Vector<TouchedWindow> windows;
841
842 TouchState();
843 ~TouchState();
844 void reset();
845 void copyFrom(const TouchState& other);
846 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
847 void removeOutsideTouchWindows();
848 const InputWindow* getFirstForegroundWindow();
849 };
850
851 TouchState mTouchState;
852 TouchState mTempTouchState;
Jeff Brownb88102f2010-09-08 11:49:43 -0700853
854 // Focused application.
855 InputApplication* mFocusedApplication;
856 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
857 void releaseFocusedApplicationLocked();
858
859 // Dispatch inbound events.
860 bool dispatchConfigurationChangedLocked(
861 nsecs_t currentTime, ConfigurationChangedEntry* entry);
862 bool dispatchKeyLocked(
863 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout,
Jeff Browne20c9e02010-10-11 14:20:19 -0700864 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700865 bool dispatchMotionLocked(
866 nsecs_t currentTime, MotionEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700867 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700868 void dispatchEventToCurrentInputTargetsLocked(
869 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700870
Jeff Brownb88102f2010-09-08 11:49:43 -0700871 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
872 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
873
874 // The input targets that were most recently identified for dispatch.
Jeff Brownb88102f2010-09-08 11:49:43 -0700875 bool mCurrentInputTargetsValid; // false while targets are being recomputed
876 Vector<InputTarget> mCurrentInputTargets;
Jeff Brownb88102f2010-09-08 11:49:43 -0700877
878 enum InputTargetWaitCause {
879 INPUT_TARGET_WAIT_CAUSE_NONE,
880 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
881 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
882 };
883
884 InputTargetWaitCause mInputTargetWaitCause;
885 nsecs_t mInputTargetWaitStartTime;
886 nsecs_t mInputTargetWaitTimeoutTime;
887 bool mInputTargetWaitTimeoutExpired;
Jeff Brown928e0542011-01-10 11:17:36 -0800888 sp<InputApplicationHandle> mInputTargetWaitApplication;
Jeff Brownb88102f2010-09-08 11:49:43 -0700889
890 // Finding targets for input events.
Jeff Brown54a18252010-09-16 14:07:33 -0700891 void resetTargetsLocked();
Jeff Brown01ce2e92010-09-26 22:20:12 -0700892 void commitTargetsLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700893 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
894 const InputApplication* application, const InputWindow* window,
895 nsecs_t* nextWakeupTime);
Jeff Brown519e0242010-09-15 15:18:56 -0700896 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
897 const sp<InputChannel>& inputChannel);
898 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700899 void resetANRTimeoutsLocked();
900
Jeff Brown01ce2e92010-09-26 22:20:12 -0700901 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
902 nsecs_t* nextWakeupTime);
903 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
Jeff Browncc0c1592011-02-19 05:07:28 -0800904 nsecs_t* nextWakeupTime, bool* outConflictingPointerActions);
Jeff Brownb88102f2010-09-08 11:49:43 -0700905
Jeff Brown01ce2e92010-09-26 22:20:12 -0700906 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
907 BitSet32 pointerIds);
Jeff Brownb88102f2010-09-08 11:49:43 -0700908 void addMonitoringTargetsLocked();
Jeff Browne2fe69e2010-10-18 13:21:23 -0700909 void pokeUserActivityLocked(const EventEntry* eventEntry);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700910 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Brown19dfc832010-10-05 12:26:23 -0700911 bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const;
Jeff Brown519e0242010-09-15 15:18:56 -0700912 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown519e0242010-09-15 15:18:56 -0700913 String8 getApplicationWindowLabelLocked(const InputApplication* application,
914 const InputWindow* window);
Jeff Brownb88102f2010-09-08 11:49:43 -0700915
Jeff Brown46b9ac02010-04-22 18:58:52 -0700916 // Manage the dispatch cycle for a single connection.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700917 // These methods are deliberately not Interruptible because doing all of the work
918 // with the mutex held makes it easier to ensure that connection invariants are maintained.
919 // If needed, the methods post commands to run later once the critical bits are done.
920 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700921 EventEntry* eventEntry, const InputTarget* inputTarget,
922 bool resumeWithAppendedMotionSample);
Jeff Brown519e0242010-09-15 15:18:56 -0700923 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown3915bb82010-11-05 15:02:16 -0700924 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
925 bool handled);
Jeff Brownb88102f2010-09-08 11:49:43 -0700926 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brownb6997262010-10-08 22:31:17 -0700927 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -0700928 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700929 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700930
Jeff Brownb6997262010-10-08 22:31:17 -0700931 void synthesizeCancelationEventsForAllConnectionsLocked(
932 InputState::CancelationOptions options, const char* reason);
933 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
934 InputState::CancelationOptions options, const char* reason);
935 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
936 InputState::CancelationOptions options, const char* reason);
937
Jeff Brown01ce2e92010-09-26 22:20:12 -0700938 // Splitting motion events across windows.
939 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
940
Jeff Brown120a4592010-10-27 18:43:51 -0700941 // Reset and drop everything the dispatcher is doing.
942 void resetAndDropEverythingLocked(const char* reason);
943
Jeff Brownb88102f2010-09-08 11:49:43 -0700944 // Dump state.
945 void dumpDispatchStateLocked(String8& dump);
946 void logDispatchStateLocked();
947
Jeff Brown46b9ac02010-04-22 18:58:52 -0700948 // Add or remove a connection to the mActiveConnections vector.
949 void activateConnectionLocked(Connection* connection);
950 void deactivateConnectionLocked(Connection* connection);
951
952 // Interesting events that we might like to log or tell the framework about.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700953 void onDispatchCycleStartedLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -0700954 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700955 void onDispatchCycleFinishedLocked(
Jeff Brown3915bb82010-11-05 15:02:16 -0700956 nsecs_t currentTime, const sp<Connection>& connection, bool handled);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700957 void onDispatchCycleBrokenLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -0700958 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -0700959 void onANRLocked(
960 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
961 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700962
Jeff Brown7fbdc842010-06-17 20:52:56 -0700963 // Outbound policy interactions.
Jeff Brownb88102f2010-09-08 11:49:43 -0700964 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700965 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown519e0242010-09-15 15:18:56 -0700966 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700967 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -0700968 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700969 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -0700970 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -0700971
972 // Statistics gathering.
973 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
974 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700975};
976
977/* Enqueues and dispatches input events, endlessly. */
978class InputDispatcherThread : public Thread {
979public:
980 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
981 ~InputDispatcherThread();
982
983private:
984 virtual bool threadLoop();
985
986 sp<InputDispatcherInterface> mDispatcher;
987};
988
989} // namespace android
990
Jeff Brownb88102f2010-09-08 11:49:43 -0700991#endif // _UI_INPUT_DISPATCHER_H