blob: 15fd274f5e50f3682e1c587805e1452ac6655f7a [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. */
Jeff Browna032cc02011-03-07 16:56:21 -080089 FLAG_FOREGROUND = 1 << 0,
Jeff Brown9c3cda02010-06-15 01:31:58 -070090
Jeff Brown85a31762010-09-01 17:01:00 -070091 /* This flag indicates that the target of a MotionEvent is partly or wholly
92 * obscured by another visible window above it. The motion event should be
93 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
Jeff Browna032cc02011-03-07 16:56:21 -080094 FLAG_WINDOW_IS_OBSCURED = 1 << 1,
Jeff Brown01ce2e92010-09-26 22:20:12 -070095
96 /* This flag indicates that a motion event is being split across multiple windows. */
Jeff Browna032cc02011-03-07 16:56:21 -080097 FLAG_SPLIT = 1 << 2,
98
Kenny Root7a9db182011-06-02 15:16:05 -070099 /* This flag indicates that the pointer coordinates dispatched to the application
100 * will be zeroed out to avoid revealing information to an application. This is
101 * used in conjunction with FLAG_DISPATCH_AS_OUTSIDE to prevent apps not sharing
102 * the same UID from watching all touches. */
103 FLAG_ZERO_COORDS = 1 << 3,
104
Jeff Browna032cc02011-03-07 16:56:21 -0800105 /* This flag indicates that the event should be sent as is.
106 * Should always be set unless the event is to be transmuted. */
107 FLAG_DISPATCH_AS_IS = 1 << 8,
108
109 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
110 * of the area of this target and so should instead be delivered as an
111 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
112 FLAG_DISPATCH_AS_OUTSIDE = 1 << 9,
113
114 /* This flag indicates that a hover sequence is starting in the given window.
115 * The event is transmuted into ACTION_HOVER_ENTER. */
116 FLAG_DISPATCH_AS_HOVER_ENTER = 1 << 10,
117
118 /* This flag indicates that a hover event happened outside of a window which handled
119 * previous hover events, signifying the end of the current hover sequence for that
120 * window.
121 * The event is transmuted into ACTION_HOVER_ENTER. */
122 FLAG_DISPATCH_AS_HOVER_EXIT = 1 << 11,
123
Jeff Brown98db5fa2011-06-08 15:37:10 -0700124 /* This flag indicates that the event should be canceled.
125 * It is used to transmute ACTION_MOVE into ACTION_CANCEL when a touch slips
126 * outside of a window. */
127 FLAG_DISPATCH_AS_SLIPPERY_EXIT = 1 << 12,
128
129 /* This flag indicates that the event should be dispatched as an initial down.
130 * It is used to transmute ACTION_MOVE into ACTION_DOWN when a touch slips
131 * into a new window. */
132 FLAG_DISPATCH_AS_SLIPPERY_ENTER = 1 << 13,
133
Jeff Browna032cc02011-03-07 16:56:21 -0800134 /* Mask for all dispatch modes. */
135 FLAG_DISPATCH_MASK = FLAG_DISPATCH_AS_IS
136 | FLAG_DISPATCH_AS_OUTSIDE
137 | FLAG_DISPATCH_AS_HOVER_ENTER
Jeff Brown98db5fa2011-06-08 15:37:10 -0700138 | FLAG_DISPATCH_AS_HOVER_EXIT
139 | FLAG_DISPATCH_AS_SLIPPERY_EXIT
140 | FLAG_DISPATCH_AS_SLIPPERY_ENTER,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700141 };
142
143 // The input channel to be targeted.
144 sp<InputChannel> inputChannel;
145
146 // Flags for the input target.
147 int32_t flags;
148
Jeff Brown9c3cda02010-06-15 01:31:58 -0700149 // The x and y offset to add to a MotionEvent as it is delivered.
150 // (ignored for KeyEvents)
151 float xOffset, yOffset;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700152
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400153 // Scaling factor to apply to MotionEvent as it is delivered.
154 // (ignored for KeyEvents)
155 float scaleFactor;
156
Jeff Brown01ce2e92010-09-26 22:20:12 -0700157 // The subset of pointer ids to include in motion events dispatched to this input target
158 // if FLAG_SPLIT is set.
159 BitSet32 pointerIds;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700160};
161
Jeff Brown7fbdc842010-06-17 20:52:56 -0700162
Jeff Brown9c3cda02010-06-15 01:31:58 -0700163/*
Jeff Brown214eaf42011-05-26 19:17:02 -0700164 * Input dispatcher configuration.
165 *
166 * Specifies various options that modify the behavior of the input dispatcher.
167 */
168struct InputDispatcherConfiguration {
169 // The key repeat initial timeout.
170 nsecs_t keyRepeatTimeout;
171
172 // The key repeat inter-key delay.
173 nsecs_t keyRepeatDelay;
174
175 // The maximum suggested event delivery rate per second.
176 // This value is used to throttle motion event movement actions on a per-device
177 // basis. It is not intended to be a hard limit.
178 int32_t maxEventsPerSecond;
179
180 InputDispatcherConfiguration() :
181 keyRepeatTimeout(500 * 1000000LL),
182 keyRepeatDelay(50 * 1000000LL),
183 maxEventsPerSecond(60) { }
184};
185
186
187/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700188 * Input dispatcher policy interface.
189 *
190 * The input reader policy is used by the input reader to interact with the Window Manager
191 * and other system components.
192 *
193 * The actual implementation is partially supported by callbacks into the DVM
194 * via JNI. This interface is also mocked in the unit tests.
195 */
196class InputDispatcherPolicyInterface : public virtual RefBase {
197protected:
198 InputDispatcherPolicyInterface() { }
199 virtual ~InputDispatcherPolicyInterface() { }
200
201public:
202 /* Notifies the system that a configuration change has occurred. */
203 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
204
Jeff Brownb88102f2010-09-08 11:49:43 -0700205 /* Notifies the system that an application is not responding.
206 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
Jeff Brown519e0242010-09-15 15:18:56 -0700207 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -0800208 const sp<InputWindowHandle>& inputWindowHandle) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700209
Jeff Brown9c3cda02010-06-15 01:31:58 -0700210 /* Notifies the system that an input channel is unrecoverably broken. */
Jeff Brown928e0542011-01-10 11:17:36 -0800211 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700212
Jeff Brown214eaf42011-05-26 19:17:02 -0700213 /* Gets the input dispatcher configuration. */
214 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700215
Jeff Brown214eaf42011-05-26 19:17:02 -0700216 /* Returns true if automatic key repeating is enabled. */
217 virtual bool isKeyRepeatEnabled() = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700218
Jeff Brown0029c662011-03-30 02:25:18 -0700219 /* Filters an input event.
220 * Return true to dispatch the event unmodified, false to consume the event.
221 * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED
222 * to injectInputEvent.
223 */
224 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) = 0;
225
Jeff Brownb6997262010-10-08 22:31:17 -0700226 /* Intercepts a key event immediately before queueing it.
227 * The policy can use this method as an opportunity to perform power management functions
228 * and early event preprocessing such as updating policy flags.
229 *
230 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
231 * should be dispatched to applications.
232 */
Jeff Brown1f245102010-11-18 20:53:46 -0800233 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700234
Jeff Brown56194eb2011-03-02 19:23:13 -0800235 /* Intercepts a touch, trackball or other motion event before queueing it.
Jeff Brownb6997262010-10-08 22:31:17 -0700236 * The policy can use this method as an opportunity to perform power management functions
237 * and early event preprocessing such as updating policy flags.
238 *
239 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
240 * should be dispatched to applications.
241 */
Jeff Brown56194eb2011-03-02 19:23:13 -0800242 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700243
Jeff Brownb88102f2010-09-08 11:49:43 -0700244 /* Allows the policy a chance to intercept a key before dispatching. */
Jeff Brown928e0542011-01-10 11:17:36 -0800245 virtual bool interceptKeyBeforeDispatching(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700246 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
247
Jeff Brown49ed71d2010-12-06 17:13:33 -0800248 /* Allows the policy a chance to perform default processing for an unhandled key.
249 * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */
Jeff Brown928e0542011-01-10 11:17:36 -0800250 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800251 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0;
Jeff Brown3915bb82010-11-05 15:02:16 -0700252
Jeff Brownb6997262010-10-08 22:31:17 -0700253 /* Notifies the policy about switch events.
254 */
255 virtual void notifySwitch(nsecs_t when,
256 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
257
Jeff Brownb88102f2010-09-08 11:49:43 -0700258 /* Poke user activity for an event dispatched to a window. */
Jeff Brown01ce2e92010-09-26 22:20:12 -0700259 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700260
261 /* Checks whether a given application pid/uid has permission to inject input events
262 * into other applications.
263 *
264 * This method is special in that its implementation promises to be non-reentrant and
265 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
266 */
267 virtual bool checkInjectEventsPermissionNonReentrant(
268 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700269};
270
271
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700272/* Notifies the system about input events generated by the input reader.
273 * The dispatcher is expected to be mostly asynchronous. */
274class InputDispatcherInterface : public virtual RefBase {
275protected:
276 InputDispatcherInterface() { }
277 virtual ~InputDispatcherInterface() { }
278
279public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700280 /* Dumps the state of the input dispatcher.
281 *
282 * This method may be called on any thread (usually by the input manager). */
283 virtual void dump(String8& dump) = 0;
284
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700285 /* Runs a single iteration of the dispatch loop.
286 * Nominally processes one queued event, a timeout, or a response from an input consumer.
287 *
288 * This method should only be called on the input dispatcher thread.
289 */
290 virtual void dispatchOnce() = 0;
291
292 /* Notifies the dispatcher about new events.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700293 *
294 * These methods should only be called on the input reader thread.
295 */
Jeff Brown9c3cda02010-06-15 01:31:58 -0700296 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brown58a2da82011-01-25 16:02:22 -0800297 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700298 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
299 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
Jeff Brown58a2da82011-01-25 16:02:22 -0800300 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700301 uint32_t policyFlags, int32_t action, int32_t flags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700302 int32_t metaState, int32_t buttonState, int32_t edgeFlags,
303 uint32_t pointerCount, const PointerProperties* pointerProperties,
304 const PointerCoords* pointerCoords,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700305 float xPrecision, float yPrecision, nsecs_t downTime) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700306 virtual void notifySwitch(nsecs_t when,
307 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700308
Jeff Brown7fbdc842010-06-17 20:52:56 -0700309 /* Injects an input event and optionally waits for sync.
Jeff Brown6ec402b2010-07-28 15:48:59 -0700310 * The synchronization mode determines whether the method blocks while waiting for
311 * input injection to proceed.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700312 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
313 *
314 * This method may be called on any thread (usually by the input manager).
315 */
316 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown0029c662011-03-30 02:25:18 -0700317 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
318 uint32_t policyFlags) = 0;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700319
Jeff Brownb88102f2010-09-08 11:49:43 -0700320 /* Sets the list of input windows.
321 *
322 * This method may be called on any thread (usually by the input manager).
323 */
Jeff Brown9302c872011-07-13 22:51:29 -0700324 virtual void setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700325
326 /* Sets the focused application.
327 *
328 * This method may be called on any thread (usually by the input manager).
329 */
Jeff Brown9302c872011-07-13 22:51:29 -0700330 virtual void setFocusedApplication(
331 const sp<InputApplicationHandle>& inputApplicationHandle) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700332
333 /* Sets the input dispatching mode.
334 *
335 * This method may be called on any thread (usually by the input manager).
336 */
337 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
338
Jeff Brown0029c662011-03-30 02:25:18 -0700339 /* Sets whether input event filtering is enabled.
340 * When enabled, incoming input events are sent to the policy's filterInputEvent
341 * method instead of being dispatched. The filter is expected to use
342 * injectInputEvent to inject the events it would like to have dispatched.
343 * It should include POLICY_FLAG_FILTERED in the policy flags during injection.
344 */
345 virtual void setInputFilterEnabled(bool enabled) = 0;
346
Jeff Browne6504122010-09-27 14:52:15 -0700347 /* Transfers touch focus from the window associated with one channel to the
348 * window associated with the other channel.
349 *
350 * Returns true on success. False if the window did not actually have touch focus.
351 */
352 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
353 const sp<InputChannel>& toChannel) = 0;
354
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700355 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Brownb88102f2010-09-08 11:49:43 -0700356 * If monitor is true, the channel will receive a copy of all input events.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700357 *
358 * These methods may be called on any thread (usually by the input manager).
359 */
Jeff Brown928e0542011-01-10 11:17:36 -0800360 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
361 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700362 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
363};
364
Jeff Brown9c3cda02010-06-15 01:31:58 -0700365/* Dispatches events to input targets. Some functions of the input dispatcher, such as
366 * identifying input targets, are controlled by a separate policy object.
367 *
368 * IMPORTANT INVARIANT:
369 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
370 * the input dispatcher never calls into the policy while holding its internal locks.
371 * The implementation is also carefully designed to recover from scenarios such as an
372 * input channel becoming unregistered while identifying input targets or processing timeouts.
373 *
374 * Methods marked 'Locked' must be called with the lock acquired.
375 *
376 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
377 * may during the course of their execution release the lock, call into the policy, and
378 * then reacquire the lock. The caller is responsible for recovering gracefully.
379 *
380 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
381 */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700382class InputDispatcher : public InputDispatcherInterface {
383protected:
384 virtual ~InputDispatcher();
385
386public:
Jeff Brown9c3cda02010-06-15 01:31:58 -0700387 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700388
Jeff Brownb88102f2010-09-08 11:49:43 -0700389 virtual void dump(String8& dump);
390
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700391 virtual void dispatchOnce();
392
Jeff Brown9c3cda02010-06-15 01:31:58 -0700393 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brown58a2da82011-01-25 16:02:22 -0800394 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700395 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
396 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown58a2da82011-01-25 16:02:22 -0800397 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700398 uint32_t policyFlags, int32_t action, int32_t flags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700399 int32_t metaState, int32_t buttonState, int32_t edgeFlags,
400 uint32_t pointerCount, const PointerProperties* pointerProperties,
401 const PointerCoords* pointerCoords,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700402 float xPrecision, float yPrecision, nsecs_t downTime);
Jeff Brownb6997262010-10-08 22:31:17 -0700403 virtual void notifySwitch(nsecs_t when,
404 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) ;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700405
Jeff Brown7fbdc842010-06-17 20:52:56 -0700406 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown0029c662011-03-30 02:25:18 -0700407 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
408 uint32_t policyFlags);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700409
Jeff Brown9302c872011-07-13 22:51:29 -0700410 virtual void setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles);
411 virtual void setFocusedApplication(const sp<InputApplicationHandle>& inputApplicationHandle);
Jeff Brownb88102f2010-09-08 11:49:43 -0700412 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown0029c662011-03-30 02:25:18 -0700413 virtual void setInputFilterEnabled(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700414
Jeff Browne6504122010-09-27 14:52:15 -0700415 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
416 const sp<InputChannel>& toChannel);
417
Jeff Brown928e0542011-01-10 11:17:36 -0800418 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
419 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700420 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
421
422private:
423 template <typename T>
424 struct Link {
425 T* next;
426 T* prev;
427 };
428
Jeff Brown01ce2e92010-09-26 22:20:12 -0700429 struct InjectionState {
430 mutable int32_t refCount;
431
432 int32_t injectorPid;
433 int32_t injectorUid;
434 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
435 bool injectionIsAsync; // set to true if injection is not waiting for the result
436 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
437 };
438
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700439 struct EventEntry : Link<EventEntry> {
440 enum {
441 TYPE_SENTINEL,
442 TYPE_CONFIGURATION_CHANGED,
443 TYPE_KEY,
444 TYPE_MOTION
445 };
446
Jeff Brown01ce2e92010-09-26 22:20:12 -0700447 mutable int32_t refCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700448 int32_t type;
449 nsecs_t eventTime;
Jeff Brownb6997262010-10-08 22:31:17 -0700450 uint32_t policyFlags;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700451 InjectionState* injectionState;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700452
Jeff Brown9c3cda02010-06-15 01:31:58 -0700453 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown7fbdc842010-06-17 20:52:56 -0700454
Jeff Brown4e91a182011-04-07 11:38:09 -0700455 inline bool isInjected() const { return injectionState != NULL; }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700456 };
457
458 struct ConfigurationChangedEntry : EventEntry {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700459 };
460
461 struct KeyEntry : EventEntry {
462 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800463 uint32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700464 int32_t action;
465 int32_t flags;
466 int32_t keyCode;
467 int32_t scanCode;
468 int32_t metaState;
469 int32_t repeatCount;
470 nsecs_t downTime;
Jeff Brownb88102f2010-09-08 11:49:43 -0700471
472 bool syntheticRepeat; // set to true for synthetic key repeats
473
474 enum InterceptKeyResult {
475 INTERCEPT_KEY_RESULT_UNKNOWN,
476 INTERCEPT_KEY_RESULT_SKIP,
477 INTERCEPT_KEY_RESULT_CONTINUE,
478 };
479 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700480 };
481
482 struct MotionSample {
483 MotionSample* next;
484
Jeff Brown4e91a182011-04-07 11:38:09 -0700485 nsecs_t eventTime; // may be updated during coalescing
486 nsecs_t eventTimeBeforeCoalescing; // not updated during coalescing
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700487 PointerCoords pointerCoords[MAX_POINTERS];
488 };
489
490 struct MotionEntry : EventEntry {
491 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800492 uint32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700493 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -0700494 int32_t flags;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700495 int32_t metaState;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700496 int32_t buttonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700497 int32_t edgeFlags;
498 float xPrecision;
499 float yPrecision;
500 nsecs_t downTime;
501 uint32_t pointerCount;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700502 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700503
504 // Linked list of motion samples associated with this motion event.
505 MotionSample firstSample;
506 MotionSample* lastSample;
Jeff Brownae9fc032010-08-18 15:51:08 -0700507
508 uint32_t countSamples() const;
Jeff Brown4e91a182011-04-07 11:38:09 -0700509
510 // Checks whether we can append samples, assuming the device id and source are the same.
511 bool canAppendSamples(int32_t action, uint32_t pointerCount,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700512 const PointerProperties* pointerProperties) const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700513 };
514
Jeff Brown9c3cda02010-06-15 01:31:58 -0700515 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700516 struct DispatchEntry : Link<DispatchEntry> {
517 EventEntry* eventEntry; // the event to dispatch
518 int32_t targetFlags;
519 float xOffset;
520 float yOffset;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400521 float scaleFactor;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700522
523 // True if dispatch has started.
524 bool inProgress;
525
Jeff Brown81346812011-06-28 20:08:48 -0700526 // Set to the resolved action and flags when the event is enqueued.
527 int32_t resolvedAction;
528 int32_t resolvedFlags;
529
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700530 // For motion events:
531 // Pointer to the first motion sample to dispatch in this cycle.
532 // Usually NULL to indicate that the list of motion samples begins at
533 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
534 // cycle and this pointer indicates the location of the first remainining sample
535 // to dispatch during the current cycle.
536 MotionSample* headMotionSample;
537 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
538 // unable to send all motion samples during this cycle. On the next cycle,
539 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
540 // will be set to NULL.
541 MotionSample* tailMotionSample;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700542
Jeff Brown519e0242010-09-15 15:18:56 -0700543 inline bool hasForegroundTarget() const {
544 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Brownb88102f2010-09-08 11:49:43 -0700545 }
Jeff Brown01ce2e92010-09-26 22:20:12 -0700546
547 inline bool isSplit() const {
548 return targetFlags & InputTarget::FLAG_SPLIT;
549 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700550 };
551
Jeff Brown9c3cda02010-06-15 01:31:58 -0700552 // A command entry captures state and behavior for an action to be performed in the
553 // dispatch loop after the initial processing has taken place. It is essentially
554 // a kind of continuation used to postpone sensitive policy interactions to a point
555 // in the dispatch loop where it is safe to release the lock (generally after finishing
556 // the critical parts of the dispatch cycle).
557 //
558 // The special thing about commands is that they can voluntarily release and reacquire
559 // the dispatcher lock at will. Initially when the command starts running, the
560 // dispatcher lock is held. However, if the command needs to call into the policy to
561 // do some work, it can release the lock, do the work, then reacquire the lock again
562 // before returning.
563 //
564 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
565 // never calls into the policy while holding its lock.
566 //
567 // Commands are implicitly 'LockedInterruptible'.
568 struct CommandEntry;
569 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
570
Jeff Brown7fbdc842010-06-17 20:52:56 -0700571 class Connection;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700572 struct CommandEntry : Link<CommandEntry> {
573 CommandEntry();
574 ~CommandEntry();
575
576 Command command;
577
578 // parameters for the command (usage varies by command)
Jeff Brown7fbdc842010-06-17 20:52:56 -0700579 sp<Connection> connection;
Jeff Brownb88102f2010-09-08 11:49:43 -0700580 nsecs_t eventTime;
581 KeyEntry* keyEntry;
Jeff Brownb88102f2010-09-08 11:49:43 -0700582 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800583 sp<InputWindowHandle> inputWindowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700584 int32_t userActivityEventType;
Jeff Brown3915bb82010-11-05 15:02:16 -0700585 bool handled;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700586 };
587
588 // Generic queue implementation.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700589 template <typename T>
590 struct Queue {
Jeff Brownb88102f2010-09-08 11:49:43 -0700591 T headSentinel;
592 T tailSentinel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700593
594 inline Queue() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700595 headSentinel.prev = NULL;
596 headSentinel.next = & tailSentinel;
597 tailSentinel.prev = & headSentinel;
598 tailSentinel.next = NULL;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700599 }
600
Jeff Brownb88102f2010-09-08 11:49:43 -0700601 inline bool isEmpty() const {
602 return headSentinel.next == & tailSentinel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700603 }
604
605 inline void enqueueAtTail(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700606 T* last = tailSentinel.prev;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700607 last->next = entry;
608 entry->prev = last;
Jeff Brownb88102f2010-09-08 11:49:43 -0700609 entry->next = & tailSentinel;
610 tailSentinel.prev = entry;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700611 }
612
613 inline void enqueueAtHead(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700614 T* first = headSentinel.next;
615 headSentinel.next = entry;
616 entry->prev = & headSentinel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700617 entry->next = first;
618 first->prev = entry;
619 }
620
621 inline void dequeue(T* entry) {
622 entry->prev->next = entry->next;
623 entry->next->prev = entry->prev;
624 }
625
626 inline T* dequeueAtHead() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700627 T* first = headSentinel.next;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700628 dequeue(first);
629 return first;
630 }
Jeff Brown519e0242010-09-15 15:18:56 -0700631
632 uint32_t count() const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700633 };
634
635 /* Allocates queue entries and performs reference counting as needed. */
636 class Allocator {
637 public:
638 Allocator();
639
Jeff Brown01ce2e92010-09-26 22:20:12 -0700640 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700641 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
642 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brown58a2da82011-01-25 16:02:22 -0800643 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700644 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
645 int32_t repeatCount, nsecs_t downTime);
646 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brown58a2da82011-01-25 16:02:22 -0800647 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700648 int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
Jeff Brown85a31762010-09-01 17:01:00 -0700649 float xPrecision, float yPrecision,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700650 nsecs_t downTime, uint32_t pointerCount,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700651 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords);
Jeff Brownb88102f2010-09-08 11:49:43 -0700652 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400653 int32_t targetFlags, float xOffset, float yOffset, float scaleFactor);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700654 CommandEntry* obtainCommandEntry(Command command);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700655
Jeff Brown01ce2e92010-09-26 22:20:12 -0700656 void releaseInjectionState(InjectionState* injectionState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700657 void releaseEventEntry(EventEntry* entry);
658 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
659 void releaseKeyEntry(KeyEntry* entry);
660 void releaseMotionEntry(MotionEntry* entry);
Jeff Browna032cc02011-03-07 16:56:21 -0800661 void freeMotionSample(MotionSample* sample);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700662 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700663 void releaseCommandEntry(CommandEntry* entry);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700664
Jeff Brown01ce2e92010-09-26 22:20:12 -0700665 void recycleKeyEntry(KeyEntry* entry);
666
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700667 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700668 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700669
670 private:
Jeff Brown01ce2e92010-09-26 22:20:12 -0700671 Pool<InjectionState> mInjectionStatePool;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700672 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
673 Pool<KeyEntry> mKeyEntryPool;
674 Pool<MotionEntry> mMotionEntryPool;
675 Pool<MotionSample> mMotionSamplePool;
676 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700677 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700678
Jeff Brownb6997262010-10-08 22:31:17 -0700679 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime,
680 uint32_t policyFlags);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700681 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700682 };
683
Jeff Brownda3d5a92011-03-29 15:11:34 -0700684 /* Specifies which events are to be canceled and why. */
685 struct CancelationOptions {
686 enum Mode {
Jeff Brownb6997262010-10-08 22:31:17 -0700687 CANCEL_ALL_EVENTS = 0,
688 CANCEL_POINTER_EVENTS = 1,
689 CANCEL_NON_POINTER_EVENTS = 2,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800690 CANCEL_FALLBACK_EVENTS = 3,
Jeff Brownb6997262010-10-08 22:31:17 -0700691 };
692
Jeff Brownda3d5a92011-03-29 15:11:34 -0700693 // The criterion to use to determine which events should be canceled.
694 Mode mode;
695
696 // Descriptive reason for the cancelation.
697 const char* reason;
698
699 // The specific keycode of the key event to cancel, or -1 to cancel any key event.
700 int32_t keyCode;
701
702 CancelationOptions(Mode mode, const char* reason) :
703 mode(mode), reason(reason), keyCode(-1) { }
704 };
705
706 /* Tracks dispatched key and motion event state so that cancelation events can be
707 * synthesized when events are dropped. */
708 class InputState {
709 public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700710 InputState();
711 ~InputState();
712
713 // Returns true if there is no state to be canceled.
714 bool isNeutral() const;
715
Jeff Brown81346812011-06-28 20:08:48 -0700716 // Returns true if the specified source is known to have received a hover enter
717 // motion event.
718 bool isHovering(int32_t deviceId, uint32_t source) const;
Jeff Brownb88102f2010-09-08 11:49:43 -0700719
720 // Records tracking information for a key event that has just been published.
Jeff Brown81346812011-06-28 20:08:48 -0700721 // Returns true if the event should be delivered, false if it is inconsistent
722 // and should be skipped.
723 bool trackKey(const KeyEntry* entry, int32_t action, int32_t flags);
Jeff Brownb88102f2010-09-08 11:49:43 -0700724
725 // Records tracking information for a motion event that has just been published.
Jeff Brown81346812011-06-28 20:08:48 -0700726 // Returns true if the event should be delivered, false if it is inconsistent
727 // and should be skipped.
728 bool trackMotion(const MotionEntry* entry, int32_t action, int32_t flags);
Jeff Brownb88102f2010-09-08 11:49:43 -0700729
Jeff Brownb6997262010-10-08 22:31:17 -0700730 // Synthesizes cancelation events for the current state and resets the tracked state.
731 void synthesizeCancelationEvents(nsecs_t currentTime, Allocator* allocator,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700732 Vector<EventEntry*>& outEvents, const CancelationOptions& options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700733
734 // Clears the current state.
735 void clear();
736
Jeff Brown9c9f1a32010-10-11 18:32:20 -0700737 // Copies pointer-related parts of the input state to another instance.
738 void copyPointerStateTo(InputState& other) const;
739
Jeff Brownda3d5a92011-03-29 15:11:34 -0700740 // Gets the fallback key associated with a keycode.
741 // Returns -1 if none.
742 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
743 int32_t getFallbackKey(int32_t originalKeyCode);
744
745 // Sets the fallback key for a particular keycode.
746 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
747
748 // Removes the fallback key for a particular keycode.
749 void removeFallbackKey(int32_t originalKeyCode);
750
751 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const {
752 return mFallbackKeys;
753 }
754
Jeff Brownb88102f2010-09-08 11:49:43 -0700755 private:
Jeff Brownb88102f2010-09-08 11:49:43 -0700756 struct KeyMemento {
757 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800758 uint32_t source;
Jeff Brownb88102f2010-09-08 11:49:43 -0700759 int32_t keyCode;
760 int32_t scanCode;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800761 int32_t flags;
Jeff Brownb88102f2010-09-08 11:49:43 -0700762 nsecs_t downTime;
763 };
764
765 struct MotionMemento {
766 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800767 uint32_t source;
Jeff Brown81346812011-06-28 20:08:48 -0700768 int32_t flags;
Jeff Brownb88102f2010-09-08 11:49:43 -0700769 float xPrecision;
770 float yPrecision;
771 nsecs_t downTime;
772 uint32_t pointerCount;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700773 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownb88102f2010-09-08 11:49:43 -0700774 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Browna032cc02011-03-07 16:56:21 -0800775 bool hovering;
Jeff Brownb88102f2010-09-08 11:49:43 -0700776
777 void setPointers(const MotionEntry* entry);
778 };
779
780 Vector<KeyMemento> mKeyMementos;
781 Vector<MotionMemento> mMotionMementos;
Jeff Brownda3d5a92011-03-29 15:11:34 -0700782 KeyedVector<int32_t, int32_t> mFallbackKeys;
Jeff Brownb6997262010-10-08 22:31:17 -0700783
Jeff Brown81346812011-06-28 20:08:48 -0700784 ssize_t findKeyMemento(const KeyEntry* entry) const;
785 ssize_t findMotionMemento(const MotionEntry* entry, bool hovering) const;
786
787 void addKeyMemento(const KeyEntry* entry, int32_t flags);
788 void addMotionMemento(const MotionEntry* entry, int32_t flags, bool hovering);
789
Jeff Brown49ed71d2010-12-06 17:13:33 -0800790 static bool shouldCancelKey(const KeyMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700791 const CancelationOptions& options);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800792 static bool shouldCancelMotion(const MotionMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700793 const CancelationOptions& options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700794 };
795
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700796 /* Manages the dispatch state associated with a single input channel. */
797 class Connection : public RefBase {
798 protected:
799 virtual ~Connection();
800
801 public:
802 enum Status {
803 // Everything is peachy.
804 STATUS_NORMAL,
805 // An unrecoverable communication error has occurred.
806 STATUS_BROKEN,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700807 // The input channel has been unregistered.
808 STATUS_ZOMBIE
809 };
810
811 Status status;
Jeff Brown928e0542011-01-10 11:17:36 -0800812 sp<InputChannel> inputChannel; // never null
813 sp<InputWindowHandle> inputWindowHandle; // may be null
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700814 InputPublisher inputPublisher;
Jeff Brownb88102f2010-09-08 11:49:43 -0700815 InputState inputState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700816 Queue<DispatchEntry> outboundQueue;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700817
818 nsecs_t lastEventTime; // the time when the event was originally captured
819 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700820
Jeff Brown928e0542011-01-10 11:17:36 -0800821 explicit Connection(const sp<InputChannel>& inputChannel,
822 const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700823
Jeff Brown9c3cda02010-06-15 01:31:58 -0700824 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
825
826 const char* getStatusLabel() const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700827
828 // Finds a DispatchEntry in the outbound queue associated with the specified event.
829 // Returns NULL if not found.
830 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
831
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700832 // Gets the time since the current event was originally obtained from the input driver.
Jeff Brownb88102f2010-09-08 11:49:43 -0700833 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700834 return (currentTime - lastEventTime) / 1000000.0;
835 }
836
837 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Brownb88102f2010-09-08 11:49:43 -0700838 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700839 return (currentTime - lastDispatchTime) / 1000000.0;
840 }
841
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700842 status_t initialize();
843 };
844
Jeff Brownb6997262010-10-08 22:31:17 -0700845 enum DropReason {
846 DROP_REASON_NOT_DROPPED = 0,
847 DROP_REASON_POLICY = 1,
848 DROP_REASON_APP_SWITCH = 2,
849 DROP_REASON_DISABLED = 3,
Jeff Brown928e0542011-01-10 11:17:36 -0800850 DROP_REASON_BLOCKED = 4,
851 DROP_REASON_STALE = 5,
Jeff Brownb6997262010-10-08 22:31:17 -0700852 };
853
Jeff Brown9c3cda02010-06-15 01:31:58 -0700854 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Brown214eaf42011-05-26 19:17:02 -0700855 InputDispatcherConfiguration mConfig;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700856
857 Mutex mLock;
858
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700859 Allocator mAllocator;
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700860 sp<Looper> mLooper;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700861
Jeff Brownb88102f2010-09-08 11:49:43 -0700862 EventEntry* mPendingEvent;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700863 Queue<EventEntry> mInboundQueue;
864 Queue<CommandEntry> mCommandQueue;
865
Jeff Brownb88102f2010-09-08 11:49:43 -0700866 Vector<EventEntry*> mTempCancelationEvents;
867
Jeff Brown214eaf42011-05-26 19:17:02 -0700868 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700869
Jeff Brown4e91a182011-04-07 11:38:09 -0700870 // Batches a new sample onto a motion entry.
871 // Assumes that the we have already checked that we can append samples.
872 void batchMotionLocked(MotionEntry* entry, nsecs_t eventTime, int32_t metaState,
873 const PointerCoords* pointerCoords, const char* eventDescription);
874
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700875 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Brownb88102f2010-09-08 11:49:43 -0700876 bool enqueueInboundEventLocked(EventEntry* entry);
877
Jeff Brownb6997262010-10-08 22:31:17 -0700878 // Cleans up input state when dropping an inbound event.
879 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
880
Jeff Brownb88102f2010-09-08 11:49:43 -0700881 // App switch latency optimization.
Jeff Brownb6997262010-10-08 22:31:17 -0700882 bool mAppSwitchSawKeyDown;
Jeff Brownb88102f2010-09-08 11:49:43 -0700883 nsecs_t mAppSwitchDueTime;
884
Jeff Brownb6997262010-10-08 22:31:17 -0700885 static bool isAppSwitchKeyCode(int32_t keyCode);
886 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700887 bool isAppSwitchPendingLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700888 void resetPendingAppSwitchLocked(bool handled);
889
Jeff Brown928e0542011-01-10 11:17:36 -0800890 // Stale event latency optimization.
891 static bool isStaleEventLocked(nsecs_t currentTime, EventEntry* entry);
892
893 // Blocked event latency optimization. Drops old events when the user intends
894 // to transfer focus to a new application.
895 EventEntry* mNextUnblockedEvent;
896
Jeff Brown9302c872011-07-13 22:51:29 -0700897 sp<InputWindowHandle> findTouchedWindowAtLocked(int32_t x, int32_t y);
Jeff Brown928e0542011-01-10 11:17:36 -0800898
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700899 // All registered connections mapped by receive pipe file descriptor.
900 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
901
Jeff Brown519e0242010-09-15 15:18:56 -0700902 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown2cbecea2010-08-17 15:59:26 -0700903
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700904 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700905 // We don't use a ref-counted pointer here because we explicitly abort connections
906 // during unregistration which causes the connection's outbound queue to be cleared
907 // and the connection itself to be deactivated.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700908 Vector<Connection*> mActiveConnections;
909
Jeff Brownb88102f2010-09-08 11:49:43 -0700910 // Input channels that will receive a copy of all input events.
911 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700912
Jeff Brown7fbdc842010-06-17 20:52:56 -0700913 // Event injection and synchronization.
914 Condition mInjectionResultAvailableCondition;
Jeff Brownb6997262010-10-08 22:31:17 -0700915 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700916 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
917
Jeff Brown6ec402b2010-07-28 15:48:59 -0700918 Condition mInjectionSyncFinishedCondition;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700919 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -0700920 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown6ec402b2010-07-28 15:48:59 -0700921
Jeff Brownae9fc032010-08-18 15:51:08 -0700922 // Throttling state.
923 struct ThrottleState {
924 nsecs_t minTimeBetweenEvents;
925
926 nsecs_t lastEventTime;
927 int32_t lastDeviceId;
928 uint32_t lastSource;
929
930 uint32_t originalSampleCount; // only collected during debugging
931 } mThrottleState;
932
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700933 // Key repeat tracking.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700934 struct KeyRepeatState {
935 KeyEntry* lastKeyEntry; // or null if no repeat
936 nsecs_t nextRepeatTime;
937 } mKeyRepeatState;
938
939 void resetKeyRepeatLocked();
Jeff Brown214eaf42011-05-26 19:17:02 -0700940 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700941
Jeff Brown9c3cda02010-06-15 01:31:58 -0700942 // Deferred command processing.
943 bool runCommandsLockedInterruptible();
944 CommandEntry* postCommandLocked(Command command);
945
Jeff Brownb88102f2010-09-08 11:49:43 -0700946 // Inbound event processing.
947 void drainInboundQueueLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700948 void releasePendingEventLocked();
949 void releaseInboundEventLocked(EventEntry* entry);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700950
Jeff Brownb88102f2010-09-08 11:49:43 -0700951 // Dispatch state.
952 bool mDispatchEnabled;
953 bool mDispatchFrozen;
Jeff Brown0029c662011-03-30 02:25:18 -0700954 bool mInputFilterEnabled;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700955
Jeff Brown9302c872011-07-13 22:51:29 -0700956 Vector<sp<InputWindowHandle> > mWindowHandles;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700957
Jeff Brown9302c872011-07-13 22:51:29 -0700958 sp<InputWindowHandle> getWindowHandleLocked(const sp<InputChannel>& inputChannel) const;
959 bool hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const;
Jeff Brownb88102f2010-09-08 11:49:43 -0700960
961 // Focus tracking for keys, trackball, etc.
Jeff Brown9302c872011-07-13 22:51:29 -0700962 sp<InputWindowHandle> mFocusedWindowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700963
964 // Focus tracking for touch.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700965 struct TouchedWindow {
Jeff Brown9302c872011-07-13 22:51:29 -0700966 sp<InputWindowHandle> windowHandle;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700967 int32_t targetFlags;
Jeff Brown46e75292010-11-10 16:53:45 -0800968 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
Jeff Brownb88102f2010-09-08 11:49:43 -0700969 };
Jeff Brown01ce2e92010-09-26 22:20:12 -0700970 struct TouchState {
971 bool down;
972 bool split;
Jeff Brown95712852011-01-04 19:41:59 -0800973 int32_t deviceId; // id of the device that is currently down, others are rejected
Jeff Brown58a2da82011-01-25 16:02:22 -0800974 uint32_t source; // source of the device that is current down, others are rejected
Jeff Brown01ce2e92010-09-26 22:20:12 -0700975 Vector<TouchedWindow> windows;
976
977 TouchState();
978 ~TouchState();
979 void reset();
980 void copyFrom(const TouchState& other);
Jeff Brown9302c872011-07-13 22:51:29 -0700981 void addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle,
982 int32_t targetFlags, BitSet32 pointerIds);
Jeff Browna032cc02011-03-07 16:56:21 -0800983 void filterNonAsIsTouchWindows();
Jeff Brown9302c872011-07-13 22:51:29 -0700984 sp<InputWindowHandle> getFirstForegroundWindowHandle() const;
Jeff Brown98db5fa2011-06-08 15:37:10 -0700985 bool isSlippery() const;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700986 };
987
988 TouchState mTouchState;
989 TouchState mTempTouchState;
Jeff Brownb88102f2010-09-08 11:49:43 -0700990
991 // Focused application.
Jeff Brown9302c872011-07-13 22:51:29 -0700992 sp<InputApplicationHandle> mFocusedApplicationHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700993
994 // Dispatch inbound events.
995 bool dispatchConfigurationChangedLocked(
996 nsecs_t currentTime, ConfigurationChangedEntry* entry);
997 bool dispatchKeyLocked(
Jeff Brown214eaf42011-05-26 19:17:02 -0700998 nsecs_t currentTime, KeyEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700999 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07001000 bool dispatchMotionLocked(
1001 nsecs_t currentTime, MotionEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -07001002 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001003 void dispatchEventToCurrentInputTargetsLocked(
1004 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001005
Jeff Brownb88102f2010-09-08 11:49:43 -07001006 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
1007 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
1008
1009 // The input targets that were most recently identified for dispatch.
Jeff Brownb88102f2010-09-08 11:49:43 -07001010 bool mCurrentInputTargetsValid; // false while targets are being recomputed
1011 Vector<InputTarget> mCurrentInputTargets;
Jeff Brownb88102f2010-09-08 11:49:43 -07001012
1013 enum InputTargetWaitCause {
1014 INPUT_TARGET_WAIT_CAUSE_NONE,
1015 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
1016 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
1017 };
1018
1019 InputTargetWaitCause mInputTargetWaitCause;
1020 nsecs_t mInputTargetWaitStartTime;
1021 nsecs_t mInputTargetWaitTimeoutTime;
1022 bool mInputTargetWaitTimeoutExpired;
Jeff Brown9302c872011-07-13 22:51:29 -07001023 sp<InputApplicationHandle> mInputTargetWaitApplicationHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -07001024
Jeff Browna032cc02011-03-07 16:56:21 -08001025 // Contains the last window which received a hover event.
Jeff Brown9302c872011-07-13 22:51:29 -07001026 sp<InputWindowHandle> mLastHoverWindowHandle;
Jeff Browna032cc02011-03-07 16:56:21 -08001027
Jeff Brownb88102f2010-09-08 11:49:43 -07001028 // Finding targets for input events.
Jeff Brown54a18252010-09-16 14:07:33 -07001029 void resetTargetsLocked();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001030 void commitTargetsLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -07001031 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
Jeff Brown9302c872011-07-13 22:51:29 -07001032 const sp<InputApplicationHandle>& applicationHandle,
1033 const sp<InputWindowHandle>& windowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -07001034 nsecs_t* nextWakeupTime);
Jeff Brown519e0242010-09-15 15:18:56 -07001035 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
1036 const sp<InputChannel>& inputChannel);
1037 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07001038 void resetANRTimeoutsLocked();
1039
Jeff Brown01ce2e92010-09-26 22:20:12 -07001040 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
1041 nsecs_t* nextWakeupTime);
1042 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
Jeff Browna032cc02011-03-07 16:56:21 -08001043 nsecs_t* nextWakeupTime, bool* outConflictingPointerActions,
1044 const MotionSample** outSplitBatchAfterSample);
Jeff Brownb88102f2010-09-08 11:49:43 -07001045
Jeff Brown9302c872011-07-13 22:51:29 -07001046 void addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle,
1047 int32_t targetFlags, BitSet32 pointerIds);
Jeff Brownb88102f2010-09-08 11:49:43 -07001048 void addMonitoringTargetsLocked();
Jeff Browne2fe69e2010-10-18 13:21:23 -07001049 void pokeUserActivityLocked(const EventEntry* eventEntry);
Jeff Brown9302c872011-07-13 22:51:29 -07001050 bool checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
1051 const InjectionState* injectionState);
1052 bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle,
1053 int32_t x, int32_t y) const;
1054 bool isWindowFinishedWithPreviousInputLocked(const sp<InputWindowHandle>& windowHandle);
1055 String8 getApplicationWindowLabelLocked(const sp<InputApplicationHandle>& applicationHandle,
1056 const sp<InputWindowHandle>& windowHandle);
Jeff Brownb88102f2010-09-08 11:49:43 -07001057
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001058 // Manage the dispatch cycle for a single connection.
Jeff Brown7fbdc842010-06-17 20:52:56 -07001059 // These methods are deliberately not Interruptible because doing all of the work
1060 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1061 // If needed, the methods post commands to run later once the critical bits are done.
1062 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001063 EventEntry* eventEntry, const InputTarget* inputTarget,
1064 bool resumeWithAppendedMotionSample);
Jeff Browna032cc02011-03-07 16:56:21 -08001065 void enqueueDispatchEntryLocked(const sp<Connection>& connection,
1066 EventEntry* eventEntry, const InputTarget* inputTarget,
1067 bool resumeWithAppendedMotionSample, int32_t dispatchMode);
Jeff Brown519e0242010-09-15 15:18:56 -07001068 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown3915bb82010-11-05 15:02:16 -07001069 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
1070 bool handled);
Jeff Brownb88102f2010-09-08 11:49:43 -07001071 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brownb6997262010-10-08 22:31:17 -07001072 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001073 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07001074 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001075
Jeff Brownb6997262010-10-08 22:31:17 -07001076 void synthesizeCancelationEventsForAllConnectionsLocked(
Jeff Brownda3d5a92011-03-29 15:11:34 -07001077 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001078 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Jeff Brownda3d5a92011-03-29 15:11:34 -07001079 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001080 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Jeff Brownda3d5a92011-03-29 15:11:34 -07001081 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001082
Jeff Brown01ce2e92010-09-26 22:20:12 -07001083 // Splitting motion events across windows.
1084 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1085
Jeff Brown120a4592010-10-27 18:43:51 -07001086 // Reset and drop everything the dispatcher is doing.
1087 void resetAndDropEverythingLocked(const char* reason);
1088
Jeff Brownb88102f2010-09-08 11:49:43 -07001089 // Dump state.
1090 void dumpDispatchStateLocked(String8& dump);
1091 void logDispatchStateLocked();
1092
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001093 // Add or remove a connection to the mActiveConnections vector.
1094 void activateConnectionLocked(Connection* connection);
1095 void deactivateConnectionLocked(Connection* connection);
1096
1097 // Interesting events that we might like to log or tell the framework about.
Jeff Brown9c3cda02010-06-15 01:31:58 -07001098 void onDispatchCycleStartedLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001099 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001100 void onDispatchCycleFinishedLocked(
Jeff Brown3915bb82010-11-05 15:02:16 -07001101 nsecs_t currentTime, const sp<Connection>& connection, bool handled);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001102 void onDispatchCycleBrokenLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001103 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001104 void onANRLocked(
Jeff Brown9302c872011-07-13 22:51:29 -07001105 nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
1106 const sp<InputWindowHandle>& windowHandle,
Jeff Brown519e0242010-09-15 15:18:56 -07001107 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001108
Jeff Brown7fbdc842010-06-17 20:52:56 -07001109 // Outbound policy interactions.
Jeff Brownb88102f2010-09-08 11:49:43 -07001110 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001111 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown519e0242010-09-15 15:18:56 -07001112 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07001113 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001114 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001115 bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
1116 DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled);
1117 bool afterMotionEventLockedInterruptible(const sp<Connection>& connection,
1118 DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled);
Jeff Brownb88102f2010-09-08 11:49:43 -07001119 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001120 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -07001121
1122 // Statistics gathering.
1123 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1124 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001125};
1126
1127/* Enqueues and dispatches input events, endlessly. */
1128class InputDispatcherThread : public Thread {
1129public:
1130 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1131 ~InputDispatcherThread();
1132
1133private:
1134 virtual bool threadLoop();
1135
1136 sp<InputDispatcherInterface> mDispatcher;
1137};
1138
1139} // namespace android
1140
Jeff Brownb88102f2010-09-08 11:49:43 -07001141#endif // _UI_INPUT_DISPATCHER_H