blob: 39d4aeb7fb4ff9f31e3f12baa9a25408cf451c6f [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
124 /* Mask for all dispatch modes. */
125 FLAG_DISPATCH_MASK = FLAG_DISPATCH_AS_IS
126 | FLAG_DISPATCH_AS_OUTSIDE
127 | FLAG_DISPATCH_AS_HOVER_ENTER
128 | FLAG_DISPATCH_AS_HOVER_EXIT,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700129 };
130
131 // The input channel to be targeted.
132 sp<InputChannel> inputChannel;
133
134 // Flags for the input target.
135 int32_t flags;
136
Jeff Brown9c3cda02010-06-15 01:31:58 -0700137 // The x and y offset to add to a MotionEvent as it is delivered.
138 // (ignored for KeyEvents)
139 float xOffset, yOffset;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700140
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400141 // Scaling factor to apply to MotionEvent as it is delivered.
142 // (ignored for KeyEvents)
143 float scaleFactor;
144
Jeff Brown01ce2e92010-09-26 22:20:12 -0700145 // The subset of pointer ids to include in motion events dispatched to this input target
146 // if FLAG_SPLIT is set.
147 BitSet32 pointerIds;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700148};
149
Jeff Brown7fbdc842010-06-17 20:52:56 -0700150
Jeff Brown9c3cda02010-06-15 01:31:58 -0700151/*
Jeff Brown214eaf42011-05-26 19:17:02 -0700152 * Input dispatcher configuration.
153 *
154 * Specifies various options that modify the behavior of the input dispatcher.
155 */
156struct InputDispatcherConfiguration {
157 // The key repeat initial timeout.
158 nsecs_t keyRepeatTimeout;
159
160 // The key repeat inter-key delay.
161 nsecs_t keyRepeatDelay;
162
163 // The maximum suggested event delivery rate per second.
164 // This value is used to throttle motion event movement actions on a per-device
165 // basis. It is not intended to be a hard limit.
166 int32_t maxEventsPerSecond;
167
168 InputDispatcherConfiguration() :
169 keyRepeatTimeout(500 * 1000000LL),
170 keyRepeatDelay(50 * 1000000LL),
171 maxEventsPerSecond(60) { }
172};
173
174
175/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700176 * Input dispatcher policy interface.
177 *
178 * The input reader policy is used by the input reader to interact with the Window Manager
179 * and other system components.
180 *
181 * The actual implementation is partially supported by callbacks into the DVM
182 * via JNI. This interface is also mocked in the unit tests.
183 */
184class InputDispatcherPolicyInterface : public virtual RefBase {
185protected:
186 InputDispatcherPolicyInterface() { }
187 virtual ~InputDispatcherPolicyInterface() { }
188
189public:
190 /* Notifies the system that a configuration change has occurred. */
191 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
192
Jeff Brownb88102f2010-09-08 11:49:43 -0700193 /* Notifies the system that an application is not responding.
194 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
Jeff Brown519e0242010-09-15 15:18:56 -0700195 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -0800196 const sp<InputWindowHandle>& inputWindowHandle) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700197
Jeff Brown9c3cda02010-06-15 01:31:58 -0700198 /* Notifies the system that an input channel is unrecoverably broken. */
Jeff Brown928e0542011-01-10 11:17:36 -0800199 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700200
Jeff Brown214eaf42011-05-26 19:17:02 -0700201 /* Gets the input dispatcher configuration. */
202 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700203
Jeff Brown214eaf42011-05-26 19:17:02 -0700204 /* Returns true if automatic key repeating is enabled. */
205 virtual bool isKeyRepeatEnabled() = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700206
Jeff Brown0029c662011-03-30 02:25:18 -0700207 /* Filters an input event.
208 * Return true to dispatch the event unmodified, false to consume the event.
209 * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED
210 * to injectInputEvent.
211 */
212 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) = 0;
213
Jeff Brownb6997262010-10-08 22:31:17 -0700214 /* Intercepts a key event immediately before queueing it.
215 * The policy can use this method as an opportunity to perform power management functions
216 * and early event preprocessing such as updating policy flags.
217 *
218 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
219 * should be dispatched to applications.
220 */
Jeff Brown1f245102010-11-18 20:53:46 -0800221 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700222
Jeff Brown56194eb2011-03-02 19:23:13 -0800223 /* Intercepts a touch, trackball or other motion event before queueing it.
Jeff Brownb6997262010-10-08 22:31:17 -0700224 * The policy can use this method as an opportunity to perform power management functions
225 * and early event preprocessing such as updating policy flags.
226 *
227 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
228 * should be dispatched to applications.
229 */
Jeff Brown56194eb2011-03-02 19:23:13 -0800230 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700231
Jeff Brownb88102f2010-09-08 11:49:43 -0700232 /* Allows the policy a chance to intercept a key before dispatching. */
Jeff Brown928e0542011-01-10 11:17:36 -0800233 virtual bool interceptKeyBeforeDispatching(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700234 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
235
Jeff Brown49ed71d2010-12-06 17:13:33 -0800236 /* Allows the policy a chance to perform default processing for an unhandled key.
237 * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */
Jeff Brown928e0542011-01-10 11:17:36 -0800238 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800239 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0;
Jeff Brown3915bb82010-11-05 15:02:16 -0700240
Jeff Brownb6997262010-10-08 22:31:17 -0700241 /* Notifies the policy about switch events.
242 */
243 virtual void notifySwitch(nsecs_t when,
244 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
245
Jeff Brownb88102f2010-09-08 11:49:43 -0700246 /* Poke user activity for an event dispatched to a window. */
Jeff Brown01ce2e92010-09-26 22:20:12 -0700247 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700248
249 /* Checks whether a given application pid/uid has permission to inject input events
250 * into other applications.
251 *
252 * This method is special in that its implementation promises to be non-reentrant and
253 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
254 */
255 virtual bool checkInjectEventsPermissionNonReentrant(
256 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700257};
258
259
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700260/* Notifies the system about input events generated by the input reader.
261 * The dispatcher is expected to be mostly asynchronous. */
262class InputDispatcherInterface : public virtual RefBase {
263protected:
264 InputDispatcherInterface() { }
265 virtual ~InputDispatcherInterface() { }
266
267public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700268 /* Dumps the state of the input dispatcher.
269 *
270 * This method may be called on any thread (usually by the input manager). */
271 virtual void dump(String8& dump) = 0;
272
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700273 /* Runs a single iteration of the dispatch loop.
274 * Nominally processes one queued event, a timeout, or a response from an input consumer.
275 *
276 * This method should only be called on the input dispatcher thread.
277 */
278 virtual void dispatchOnce() = 0;
279
280 /* Notifies the dispatcher about new events.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700281 *
282 * These methods should only be called on the input reader thread.
283 */
Jeff Brown9c3cda02010-06-15 01:31:58 -0700284 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brown58a2da82011-01-25 16:02:22 -0800285 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700286 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
287 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
Jeff Brown58a2da82011-01-25 16:02:22 -0800288 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700289 uint32_t policyFlags, int32_t action, int32_t flags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700290 int32_t metaState, int32_t buttonState, int32_t edgeFlags,
291 uint32_t pointerCount, const PointerProperties* pointerProperties,
292 const PointerCoords* pointerCoords,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700293 float xPrecision, float yPrecision, nsecs_t downTime) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700294 virtual void notifySwitch(nsecs_t when,
295 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700296
Jeff Brown7fbdc842010-06-17 20:52:56 -0700297 /* Injects an input event and optionally waits for sync.
Jeff Brown6ec402b2010-07-28 15:48:59 -0700298 * The synchronization mode determines whether the method blocks while waiting for
299 * input injection to proceed.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700300 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
301 *
302 * This method may be called on any thread (usually by the input manager).
303 */
304 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown0029c662011-03-30 02:25:18 -0700305 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
306 uint32_t policyFlags) = 0;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700307
Jeff Brownb88102f2010-09-08 11:49:43 -0700308 /* Sets the list of input windows.
309 *
310 * This method may be called on any thread (usually by the input manager).
311 */
312 virtual void setInputWindows(const Vector<InputWindow>& inputWindows) = 0;
313
314 /* Sets the focused application.
315 *
316 * This method may be called on any thread (usually by the input manager).
317 */
318 virtual void setFocusedApplication(const InputApplication* inputApplication) = 0;
319
320 /* Sets the input dispatching mode.
321 *
322 * This method may be called on any thread (usually by the input manager).
323 */
324 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
325
Jeff Brown0029c662011-03-30 02:25:18 -0700326 /* Sets whether input event filtering is enabled.
327 * When enabled, incoming input events are sent to the policy's filterInputEvent
328 * method instead of being dispatched. The filter is expected to use
329 * injectInputEvent to inject the events it would like to have dispatched.
330 * It should include POLICY_FLAG_FILTERED in the policy flags during injection.
331 */
332 virtual void setInputFilterEnabled(bool enabled) = 0;
333
Jeff Browne6504122010-09-27 14:52:15 -0700334 /* Transfers touch focus from the window associated with one channel to the
335 * window associated with the other channel.
336 *
337 * Returns true on success. False if the window did not actually have touch focus.
338 */
339 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
340 const sp<InputChannel>& toChannel) = 0;
341
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700342 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Brownb88102f2010-09-08 11:49:43 -0700343 * If monitor is true, the channel will receive a copy of all input events.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700344 *
345 * These methods may be called on any thread (usually by the input manager).
346 */
Jeff Brown928e0542011-01-10 11:17:36 -0800347 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
348 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700349 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
350};
351
Jeff Brown9c3cda02010-06-15 01:31:58 -0700352/* Dispatches events to input targets. Some functions of the input dispatcher, such as
353 * identifying input targets, are controlled by a separate policy object.
354 *
355 * IMPORTANT INVARIANT:
356 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
357 * the input dispatcher never calls into the policy while holding its internal locks.
358 * The implementation is also carefully designed to recover from scenarios such as an
359 * input channel becoming unregistered while identifying input targets or processing timeouts.
360 *
361 * Methods marked 'Locked' must be called with the lock acquired.
362 *
363 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
364 * may during the course of their execution release the lock, call into the policy, and
365 * then reacquire the lock. The caller is responsible for recovering gracefully.
366 *
367 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
368 */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700369class InputDispatcher : public InputDispatcherInterface {
370protected:
371 virtual ~InputDispatcher();
372
373public:
Jeff Brown9c3cda02010-06-15 01:31:58 -0700374 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700375
Jeff Brownb88102f2010-09-08 11:49:43 -0700376 virtual void dump(String8& dump);
377
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700378 virtual void dispatchOnce();
379
Jeff Brown9c3cda02010-06-15 01:31:58 -0700380 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brown58a2da82011-01-25 16:02:22 -0800381 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700382 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
383 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown58a2da82011-01-25 16:02:22 -0800384 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700385 uint32_t policyFlags, int32_t action, int32_t flags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700386 int32_t metaState, int32_t buttonState, int32_t edgeFlags,
387 uint32_t pointerCount, const PointerProperties* pointerProperties,
388 const PointerCoords* pointerCoords,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700389 float xPrecision, float yPrecision, nsecs_t downTime);
Jeff Brownb6997262010-10-08 22:31:17 -0700390 virtual void notifySwitch(nsecs_t when,
391 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) ;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700392
Jeff Brown7fbdc842010-06-17 20:52:56 -0700393 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown0029c662011-03-30 02:25:18 -0700394 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
395 uint32_t policyFlags);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700396
Jeff Brownb88102f2010-09-08 11:49:43 -0700397 virtual void setInputWindows(const Vector<InputWindow>& inputWindows);
398 virtual void setFocusedApplication(const InputApplication* inputApplication);
399 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown0029c662011-03-30 02:25:18 -0700400 virtual void setInputFilterEnabled(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700401
Jeff Browne6504122010-09-27 14:52:15 -0700402 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
403 const sp<InputChannel>& toChannel);
404
Jeff Brown928e0542011-01-10 11:17:36 -0800405 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
406 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700407 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
408
409private:
410 template <typename T>
411 struct Link {
412 T* next;
413 T* prev;
414 };
415
Jeff Brown01ce2e92010-09-26 22:20:12 -0700416 struct InjectionState {
417 mutable int32_t refCount;
418
419 int32_t injectorPid;
420 int32_t injectorUid;
421 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
422 bool injectionIsAsync; // set to true if injection is not waiting for the result
423 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
424 };
425
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700426 struct EventEntry : Link<EventEntry> {
427 enum {
428 TYPE_SENTINEL,
429 TYPE_CONFIGURATION_CHANGED,
430 TYPE_KEY,
431 TYPE_MOTION
432 };
433
Jeff Brown01ce2e92010-09-26 22:20:12 -0700434 mutable int32_t refCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700435 int32_t type;
436 nsecs_t eventTime;
Jeff Brownb6997262010-10-08 22:31:17 -0700437 uint32_t policyFlags;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700438 InjectionState* injectionState;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700439
Jeff Brown9c3cda02010-06-15 01:31:58 -0700440 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown7fbdc842010-06-17 20:52:56 -0700441
Jeff Brown4e91a182011-04-07 11:38:09 -0700442 inline bool isInjected() const { return injectionState != NULL; }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700443 };
444
445 struct ConfigurationChangedEntry : EventEntry {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700446 };
447
448 struct KeyEntry : EventEntry {
449 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800450 uint32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700451 int32_t action;
452 int32_t flags;
453 int32_t keyCode;
454 int32_t scanCode;
455 int32_t metaState;
456 int32_t repeatCount;
457 nsecs_t downTime;
Jeff Brownb88102f2010-09-08 11:49:43 -0700458
459 bool syntheticRepeat; // set to true for synthetic key repeats
460
461 enum InterceptKeyResult {
462 INTERCEPT_KEY_RESULT_UNKNOWN,
463 INTERCEPT_KEY_RESULT_SKIP,
464 INTERCEPT_KEY_RESULT_CONTINUE,
465 };
466 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700467 };
468
469 struct MotionSample {
470 MotionSample* next;
471
Jeff Brown4e91a182011-04-07 11:38:09 -0700472 nsecs_t eventTime; // may be updated during coalescing
473 nsecs_t eventTimeBeforeCoalescing; // not updated during coalescing
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700474 PointerCoords pointerCoords[MAX_POINTERS];
475 };
476
477 struct MotionEntry : EventEntry {
478 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800479 uint32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700480 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -0700481 int32_t flags;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700482 int32_t metaState;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700483 int32_t buttonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700484 int32_t edgeFlags;
485 float xPrecision;
486 float yPrecision;
487 nsecs_t downTime;
488 uint32_t pointerCount;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700489 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700490
491 // Linked list of motion samples associated with this motion event.
492 MotionSample firstSample;
493 MotionSample* lastSample;
Jeff Brownae9fc032010-08-18 15:51:08 -0700494
495 uint32_t countSamples() const;
Jeff Brown4e91a182011-04-07 11:38:09 -0700496
497 // Checks whether we can append samples, assuming the device id and source are the same.
498 bool canAppendSamples(int32_t action, uint32_t pointerCount,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700499 const PointerProperties* pointerProperties) const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700500 };
501
Jeff Brown9c3cda02010-06-15 01:31:58 -0700502 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700503 struct DispatchEntry : Link<DispatchEntry> {
504 EventEntry* eventEntry; // the event to dispatch
505 int32_t targetFlags;
506 float xOffset;
507 float yOffset;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400508 float scaleFactor;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700509
510 // True if dispatch has started.
511 bool inProgress;
512
513 // For motion events:
514 // Pointer to the first motion sample to dispatch in this cycle.
515 // Usually NULL to indicate that the list of motion samples begins at
516 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
517 // cycle and this pointer indicates the location of the first remainining sample
518 // to dispatch during the current cycle.
519 MotionSample* headMotionSample;
520 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
521 // unable to send all motion samples during this cycle. On the next cycle,
522 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
523 // will be set to NULL.
524 MotionSample* tailMotionSample;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700525
Jeff Brown519e0242010-09-15 15:18:56 -0700526 inline bool hasForegroundTarget() const {
527 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Brownb88102f2010-09-08 11:49:43 -0700528 }
Jeff Brown01ce2e92010-09-26 22:20:12 -0700529
530 inline bool isSplit() const {
531 return targetFlags & InputTarget::FLAG_SPLIT;
532 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700533 };
534
Jeff Brown9c3cda02010-06-15 01:31:58 -0700535 // A command entry captures state and behavior for an action to be performed in the
536 // dispatch loop after the initial processing has taken place. It is essentially
537 // a kind of continuation used to postpone sensitive policy interactions to a point
538 // in the dispatch loop where it is safe to release the lock (generally after finishing
539 // the critical parts of the dispatch cycle).
540 //
541 // The special thing about commands is that they can voluntarily release and reacquire
542 // the dispatcher lock at will. Initially when the command starts running, the
543 // dispatcher lock is held. However, if the command needs to call into the policy to
544 // do some work, it can release the lock, do the work, then reacquire the lock again
545 // before returning.
546 //
547 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
548 // never calls into the policy while holding its lock.
549 //
550 // Commands are implicitly 'LockedInterruptible'.
551 struct CommandEntry;
552 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
553
Jeff Brown7fbdc842010-06-17 20:52:56 -0700554 class Connection;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700555 struct CommandEntry : Link<CommandEntry> {
556 CommandEntry();
557 ~CommandEntry();
558
559 Command command;
560
561 // parameters for the command (usage varies by command)
Jeff Brown7fbdc842010-06-17 20:52:56 -0700562 sp<Connection> connection;
Jeff Brownb88102f2010-09-08 11:49:43 -0700563 nsecs_t eventTime;
564 KeyEntry* keyEntry;
565 sp<InputChannel> inputChannel;
566 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800567 sp<InputWindowHandle> inputWindowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700568 int32_t userActivityEventType;
Jeff Brown3915bb82010-11-05 15:02:16 -0700569 bool handled;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700570 };
571
572 // Generic queue implementation.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700573 template <typename T>
574 struct Queue {
Jeff Brownb88102f2010-09-08 11:49:43 -0700575 T headSentinel;
576 T tailSentinel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700577
578 inline Queue() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700579 headSentinel.prev = NULL;
580 headSentinel.next = & tailSentinel;
581 tailSentinel.prev = & headSentinel;
582 tailSentinel.next = NULL;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700583 }
584
Jeff Brownb88102f2010-09-08 11:49:43 -0700585 inline bool isEmpty() const {
586 return headSentinel.next == & tailSentinel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700587 }
588
589 inline void enqueueAtTail(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700590 T* last = tailSentinel.prev;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700591 last->next = entry;
592 entry->prev = last;
Jeff Brownb88102f2010-09-08 11:49:43 -0700593 entry->next = & tailSentinel;
594 tailSentinel.prev = entry;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700595 }
596
597 inline void enqueueAtHead(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700598 T* first = headSentinel.next;
599 headSentinel.next = entry;
600 entry->prev = & headSentinel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700601 entry->next = first;
602 first->prev = entry;
603 }
604
605 inline void dequeue(T* entry) {
606 entry->prev->next = entry->next;
607 entry->next->prev = entry->prev;
608 }
609
610 inline T* dequeueAtHead() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700611 T* first = headSentinel.next;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700612 dequeue(first);
613 return first;
614 }
Jeff Brown519e0242010-09-15 15:18:56 -0700615
616 uint32_t count() const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700617 };
618
619 /* Allocates queue entries and performs reference counting as needed. */
620 class Allocator {
621 public:
622 Allocator();
623
Jeff Brown01ce2e92010-09-26 22:20:12 -0700624 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700625 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
626 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brown58a2da82011-01-25 16:02:22 -0800627 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700628 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
629 int32_t repeatCount, nsecs_t downTime);
630 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brown58a2da82011-01-25 16:02:22 -0800631 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700632 int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
Jeff Brown85a31762010-09-01 17:01:00 -0700633 float xPrecision, float yPrecision,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700634 nsecs_t downTime, uint32_t pointerCount,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700635 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords);
Jeff Brownb88102f2010-09-08 11:49:43 -0700636 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400637 int32_t targetFlags, float xOffset, float yOffset, float scaleFactor);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700638 CommandEntry* obtainCommandEntry(Command command);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700639
Jeff Brown01ce2e92010-09-26 22:20:12 -0700640 void releaseInjectionState(InjectionState* injectionState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700641 void releaseEventEntry(EventEntry* entry);
642 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
643 void releaseKeyEntry(KeyEntry* entry);
644 void releaseMotionEntry(MotionEntry* entry);
Jeff Browna032cc02011-03-07 16:56:21 -0800645 void freeMotionSample(MotionSample* sample);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700646 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700647 void releaseCommandEntry(CommandEntry* entry);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700648
Jeff Brown01ce2e92010-09-26 22:20:12 -0700649 void recycleKeyEntry(KeyEntry* entry);
650
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700651 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700652 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700653
654 private:
Jeff Brown01ce2e92010-09-26 22:20:12 -0700655 Pool<InjectionState> mInjectionStatePool;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700656 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
657 Pool<KeyEntry> mKeyEntryPool;
658 Pool<MotionEntry> mMotionEntryPool;
659 Pool<MotionSample> mMotionSamplePool;
660 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700661 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700662
Jeff Brownb6997262010-10-08 22:31:17 -0700663 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime,
664 uint32_t policyFlags);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700665 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700666 };
667
Jeff Brownda3d5a92011-03-29 15:11:34 -0700668 /* Specifies which events are to be canceled and why. */
669 struct CancelationOptions {
670 enum Mode {
Jeff Brownb6997262010-10-08 22:31:17 -0700671 CANCEL_ALL_EVENTS = 0,
672 CANCEL_POINTER_EVENTS = 1,
673 CANCEL_NON_POINTER_EVENTS = 2,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800674 CANCEL_FALLBACK_EVENTS = 3,
Jeff Brownb6997262010-10-08 22:31:17 -0700675 };
676
Jeff Brownda3d5a92011-03-29 15:11:34 -0700677 // The criterion to use to determine which events should be canceled.
678 Mode mode;
679
680 // Descriptive reason for the cancelation.
681 const char* reason;
682
683 // The specific keycode of the key event to cancel, or -1 to cancel any key event.
684 int32_t keyCode;
685
686 CancelationOptions(Mode mode, const char* reason) :
687 mode(mode), reason(reason), keyCode(-1) { }
688 };
689
690 /* Tracks dispatched key and motion event state so that cancelation events can be
691 * synthesized when events are dropped. */
692 class InputState {
693 public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700694 InputState();
695 ~InputState();
696
697 // Returns true if there is no state to be canceled.
698 bool isNeutral() const;
699
Jeff Brownb88102f2010-09-08 11:49:43 -0700700 // Records tracking information for an event that has just been published.
Jeff Browna032cc02011-03-07 16:56:21 -0800701 void trackEvent(const EventEntry* entry, int32_t action);
Jeff Brownb88102f2010-09-08 11:49:43 -0700702
703 // Records tracking information for a key event that has just been published.
Jeff Browna032cc02011-03-07 16:56:21 -0800704 void trackKey(const KeyEntry* entry, int32_t action);
Jeff Brownb88102f2010-09-08 11:49:43 -0700705
706 // Records tracking information for a motion event that has just been published.
Jeff Browna032cc02011-03-07 16:56:21 -0800707 void trackMotion(const MotionEntry* entry, int32_t action);
Jeff Brownb88102f2010-09-08 11:49:43 -0700708
Jeff Brownb6997262010-10-08 22:31:17 -0700709 // Synthesizes cancelation events for the current state and resets the tracked state.
710 void synthesizeCancelationEvents(nsecs_t currentTime, Allocator* allocator,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700711 Vector<EventEntry*>& outEvents, const CancelationOptions& options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700712
713 // Clears the current state.
714 void clear();
715
Jeff Brown9c9f1a32010-10-11 18:32:20 -0700716 // Copies pointer-related parts of the input state to another instance.
717 void copyPointerStateTo(InputState& other) const;
718
Jeff Brownda3d5a92011-03-29 15:11:34 -0700719 // Gets the fallback key associated with a keycode.
720 // Returns -1 if none.
721 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
722 int32_t getFallbackKey(int32_t originalKeyCode);
723
724 // Sets the fallback key for a particular keycode.
725 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
726
727 // Removes the fallback key for a particular keycode.
728 void removeFallbackKey(int32_t originalKeyCode);
729
730 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const {
731 return mFallbackKeys;
732 }
733
Jeff Brownb88102f2010-09-08 11:49:43 -0700734 private:
Jeff Brownb88102f2010-09-08 11:49:43 -0700735 struct KeyMemento {
736 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800737 uint32_t source;
Jeff Brownb88102f2010-09-08 11:49:43 -0700738 int32_t keyCode;
739 int32_t scanCode;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800740 int32_t flags;
Jeff Brownb88102f2010-09-08 11:49:43 -0700741 nsecs_t downTime;
742 };
743
744 struct MotionMemento {
745 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800746 uint32_t source;
Jeff Brownb88102f2010-09-08 11:49:43 -0700747 float xPrecision;
748 float yPrecision;
749 nsecs_t downTime;
750 uint32_t pointerCount;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700751 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownb88102f2010-09-08 11:49:43 -0700752 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Browna032cc02011-03-07 16:56:21 -0800753 bool hovering;
Jeff Brownb88102f2010-09-08 11:49:43 -0700754
755 void setPointers(const MotionEntry* entry);
756 };
757
758 Vector<KeyMemento> mKeyMementos;
759 Vector<MotionMemento> mMotionMementos;
Jeff Brownda3d5a92011-03-29 15:11:34 -0700760 KeyedVector<int32_t, int32_t> mFallbackKeys;
Jeff Brownb6997262010-10-08 22:31:17 -0700761
Jeff Brown49ed71d2010-12-06 17:13:33 -0800762 static bool shouldCancelKey(const KeyMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700763 const CancelationOptions& options);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800764 static bool shouldCancelMotion(const MotionMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700765 const CancelationOptions& options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700766 };
767
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700768 /* Manages the dispatch state associated with a single input channel. */
769 class Connection : public RefBase {
770 protected:
771 virtual ~Connection();
772
773 public:
774 enum Status {
775 // Everything is peachy.
776 STATUS_NORMAL,
777 // An unrecoverable communication error has occurred.
778 STATUS_BROKEN,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700779 // The input channel has been unregistered.
780 STATUS_ZOMBIE
781 };
782
783 Status status;
Jeff Brown928e0542011-01-10 11:17:36 -0800784 sp<InputChannel> inputChannel; // never null
785 sp<InputWindowHandle> inputWindowHandle; // may be null
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700786 InputPublisher inputPublisher;
Jeff Brownb88102f2010-09-08 11:49:43 -0700787 InputState inputState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700788 Queue<DispatchEntry> outboundQueue;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700789
790 nsecs_t lastEventTime; // the time when the event was originally captured
791 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700792
Jeff Brown928e0542011-01-10 11:17:36 -0800793 explicit Connection(const sp<InputChannel>& inputChannel,
794 const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700795
Jeff Brown9c3cda02010-06-15 01:31:58 -0700796 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
797
798 const char* getStatusLabel() const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700799
800 // Finds a DispatchEntry in the outbound queue associated with the specified event.
801 // Returns NULL if not found.
802 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
803
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700804 // Gets the time since the current event was originally obtained from the input driver.
Jeff Brownb88102f2010-09-08 11:49:43 -0700805 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700806 return (currentTime - lastEventTime) / 1000000.0;
807 }
808
809 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Brownb88102f2010-09-08 11:49:43 -0700810 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700811 return (currentTime - lastDispatchTime) / 1000000.0;
812 }
813
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700814 status_t initialize();
815 };
816
Jeff Brownb6997262010-10-08 22:31:17 -0700817 enum DropReason {
818 DROP_REASON_NOT_DROPPED = 0,
819 DROP_REASON_POLICY = 1,
820 DROP_REASON_APP_SWITCH = 2,
821 DROP_REASON_DISABLED = 3,
Jeff Brown928e0542011-01-10 11:17:36 -0800822 DROP_REASON_BLOCKED = 4,
823 DROP_REASON_STALE = 5,
Jeff Brownb6997262010-10-08 22:31:17 -0700824 };
825
Jeff Brown9c3cda02010-06-15 01:31:58 -0700826 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Brown214eaf42011-05-26 19:17:02 -0700827 InputDispatcherConfiguration mConfig;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700828
829 Mutex mLock;
830
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700831 Allocator mAllocator;
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700832 sp<Looper> mLooper;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700833
Jeff Brownb88102f2010-09-08 11:49:43 -0700834 EventEntry* mPendingEvent;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700835 Queue<EventEntry> mInboundQueue;
836 Queue<CommandEntry> mCommandQueue;
837
Jeff Brownb88102f2010-09-08 11:49:43 -0700838 Vector<EventEntry*> mTempCancelationEvents;
839
Jeff Brown214eaf42011-05-26 19:17:02 -0700840 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700841
Jeff Brown4e91a182011-04-07 11:38:09 -0700842 // Batches a new sample onto a motion entry.
843 // Assumes that the we have already checked that we can append samples.
844 void batchMotionLocked(MotionEntry* entry, nsecs_t eventTime, int32_t metaState,
845 const PointerCoords* pointerCoords, const char* eventDescription);
846
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700847 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Brownb88102f2010-09-08 11:49:43 -0700848 bool enqueueInboundEventLocked(EventEntry* entry);
849
Jeff Brownb6997262010-10-08 22:31:17 -0700850 // Cleans up input state when dropping an inbound event.
851 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
852
Jeff Brownb88102f2010-09-08 11:49:43 -0700853 // App switch latency optimization.
Jeff Brownb6997262010-10-08 22:31:17 -0700854 bool mAppSwitchSawKeyDown;
Jeff Brownb88102f2010-09-08 11:49:43 -0700855 nsecs_t mAppSwitchDueTime;
856
Jeff Brownb6997262010-10-08 22:31:17 -0700857 static bool isAppSwitchKeyCode(int32_t keyCode);
858 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700859 bool isAppSwitchPendingLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700860 void resetPendingAppSwitchLocked(bool handled);
861
Jeff Brown928e0542011-01-10 11:17:36 -0800862 // Stale event latency optimization.
863 static bool isStaleEventLocked(nsecs_t currentTime, EventEntry* entry);
864
865 // Blocked event latency optimization. Drops old events when the user intends
866 // to transfer focus to a new application.
867 EventEntry* mNextUnblockedEvent;
868
869 const InputWindow* findTouchedWindowAtLocked(int32_t x, int32_t y);
870
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700871 // All registered connections mapped by receive pipe file descriptor.
872 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
873
Jeff Brown519e0242010-09-15 15:18:56 -0700874 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown2cbecea2010-08-17 15:59:26 -0700875
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700876 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700877 // We don't use a ref-counted pointer here because we explicitly abort connections
878 // during unregistration which causes the connection's outbound queue to be cleared
879 // and the connection itself to be deactivated.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700880 Vector<Connection*> mActiveConnections;
881
Jeff Brownb88102f2010-09-08 11:49:43 -0700882 // Input channels that will receive a copy of all input events.
883 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700884
Jeff Brown7fbdc842010-06-17 20:52:56 -0700885 // Event injection and synchronization.
886 Condition mInjectionResultAvailableCondition;
Jeff Brownb6997262010-10-08 22:31:17 -0700887 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700888 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
889
Jeff Brown6ec402b2010-07-28 15:48:59 -0700890 Condition mInjectionSyncFinishedCondition;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700891 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -0700892 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown6ec402b2010-07-28 15:48:59 -0700893
Jeff Brownae9fc032010-08-18 15:51:08 -0700894 // Throttling state.
895 struct ThrottleState {
896 nsecs_t minTimeBetweenEvents;
897
898 nsecs_t lastEventTime;
899 int32_t lastDeviceId;
900 uint32_t lastSource;
901
902 uint32_t originalSampleCount; // only collected during debugging
903 } mThrottleState;
904
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700905 // Key repeat tracking.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700906 struct KeyRepeatState {
907 KeyEntry* lastKeyEntry; // or null if no repeat
908 nsecs_t nextRepeatTime;
909 } mKeyRepeatState;
910
911 void resetKeyRepeatLocked();
Jeff Brown214eaf42011-05-26 19:17:02 -0700912 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700913
Jeff Brown9c3cda02010-06-15 01:31:58 -0700914 // Deferred command processing.
915 bool runCommandsLockedInterruptible();
916 CommandEntry* postCommandLocked(Command command);
917
Jeff Brownb88102f2010-09-08 11:49:43 -0700918 // Inbound event processing.
919 void drainInboundQueueLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700920 void releasePendingEventLocked();
921 void releaseInboundEventLocked(EventEntry* entry);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700922
Jeff Brownb88102f2010-09-08 11:49:43 -0700923 // Dispatch state.
924 bool mDispatchEnabled;
925 bool mDispatchFrozen;
Jeff Brown0029c662011-03-30 02:25:18 -0700926 bool mInputFilterEnabled;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700927
Jeff Brownb88102f2010-09-08 11:49:43 -0700928 Vector<InputWindow> mWindows;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700929
930 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Brownb88102f2010-09-08 11:49:43 -0700931
932 // Focus tracking for keys, trackball, etc.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700933 const InputWindow* mFocusedWindow;
Jeff Brownb88102f2010-09-08 11:49:43 -0700934
935 // Focus tracking for touch.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700936 struct TouchedWindow {
937 const InputWindow* window;
938 int32_t targetFlags;
Jeff Brown46e75292010-11-10 16:53:45 -0800939 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
Jeff Brown01ce2e92010-09-26 22:20:12 -0700940 sp<InputChannel> channel;
Jeff Brownb88102f2010-09-08 11:49:43 -0700941 };
Jeff Brown01ce2e92010-09-26 22:20:12 -0700942 struct TouchState {
943 bool down;
944 bool split;
Jeff Brown95712852011-01-04 19:41:59 -0800945 int32_t deviceId; // id of the device that is currently down, others are rejected
Jeff Brown58a2da82011-01-25 16:02:22 -0800946 uint32_t source; // source of the device that is current down, others are rejected
Jeff Brown01ce2e92010-09-26 22:20:12 -0700947 Vector<TouchedWindow> windows;
948
949 TouchState();
950 ~TouchState();
951 void reset();
952 void copyFrom(const TouchState& other);
953 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
Jeff Browna032cc02011-03-07 16:56:21 -0800954 void filterNonAsIsTouchWindows();
Jeff Brown01ce2e92010-09-26 22:20:12 -0700955 const InputWindow* getFirstForegroundWindow();
956 };
957
958 TouchState mTouchState;
959 TouchState mTempTouchState;
Jeff Brownb88102f2010-09-08 11:49:43 -0700960
961 // Focused application.
962 InputApplication* mFocusedApplication;
963 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
964 void releaseFocusedApplicationLocked();
965
966 // Dispatch inbound events.
967 bool dispatchConfigurationChangedLocked(
968 nsecs_t currentTime, ConfigurationChangedEntry* entry);
969 bool dispatchKeyLocked(
Jeff Brown214eaf42011-05-26 19:17:02 -0700970 nsecs_t currentTime, KeyEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700971 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700972 bool dispatchMotionLocked(
973 nsecs_t currentTime, MotionEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700974 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700975 void dispatchEventToCurrentInputTargetsLocked(
976 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700977
Jeff Brownb88102f2010-09-08 11:49:43 -0700978 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
979 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
980
981 // The input targets that were most recently identified for dispatch.
Jeff Brownb88102f2010-09-08 11:49:43 -0700982 bool mCurrentInputTargetsValid; // false while targets are being recomputed
983 Vector<InputTarget> mCurrentInputTargets;
Jeff Brownb88102f2010-09-08 11:49:43 -0700984
985 enum InputTargetWaitCause {
986 INPUT_TARGET_WAIT_CAUSE_NONE,
987 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
988 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
989 };
990
991 InputTargetWaitCause mInputTargetWaitCause;
992 nsecs_t mInputTargetWaitStartTime;
993 nsecs_t mInputTargetWaitTimeoutTime;
994 bool mInputTargetWaitTimeoutExpired;
Jeff Brown928e0542011-01-10 11:17:36 -0800995 sp<InputApplicationHandle> mInputTargetWaitApplication;
Jeff Brownb88102f2010-09-08 11:49:43 -0700996
Jeff Browna032cc02011-03-07 16:56:21 -0800997 // Contains the last window which received a hover event.
998 const InputWindow* mLastHoverWindow;
999
Jeff Brownb88102f2010-09-08 11:49:43 -07001000 // Finding targets for input events.
Jeff Brown54a18252010-09-16 14:07:33 -07001001 void resetTargetsLocked();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001002 void commitTargetsLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -07001003 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
1004 const InputApplication* application, const InputWindow* window,
1005 nsecs_t* nextWakeupTime);
Jeff Brown519e0242010-09-15 15:18:56 -07001006 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
1007 const sp<InputChannel>& inputChannel);
1008 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07001009 void resetANRTimeoutsLocked();
1010
Jeff Brown01ce2e92010-09-26 22:20:12 -07001011 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
1012 nsecs_t* nextWakeupTime);
1013 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
Jeff Browna032cc02011-03-07 16:56:21 -08001014 nsecs_t* nextWakeupTime, bool* outConflictingPointerActions,
1015 const MotionSample** outSplitBatchAfterSample);
Jeff Brownb88102f2010-09-08 11:49:43 -07001016
Jeff Brown01ce2e92010-09-26 22:20:12 -07001017 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
1018 BitSet32 pointerIds);
Jeff Brownb88102f2010-09-08 11:49:43 -07001019 void addMonitoringTargetsLocked();
Jeff Browne2fe69e2010-10-18 13:21:23 -07001020 void pokeUserActivityLocked(const EventEntry* eventEntry);
Jeff Brown01ce2e92010-09-26 22:20:12 -07001021 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Brown19dfc832010-10-05 12:26:23 -07001022 bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const;
Jeff Brown519e0242010-09-15 15:18:56 -07001023 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown519e0242010-09-15 15:18:56 -07001024 String8 getApplicationWindowLabelLocked(const InputApplication* application,
1025 const InputWindow* window);
Jeff Brownb88102f2010-09-08 11:49:43 -07001026
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001027 // Manage the dispatch cycle for a single connection.
Jeff Brown7fbdc842010-06-17 20:52:56 -07001028 // These methods are deliberately not Interruptible because doing all of the work
1029 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1030 // If needed, the methods post commands to run later once the critical bits are done.
1031 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001032 EventEntry* eventEntry, const InputTarget* inputTarget,
1033 bool resumeWithAppendedMotionSample);
Jeff Browna032cc02011-03-07 16:56:21 -08001034 void enqueueDispatchEntryLocked(const sp<Connection>& connection,
1035 EventEntry* eventEntry, const InputTarget* inputTarget,
1036 bool resumeWithAppendedMotionSample, int32_t dispatchMode);
Jeff Brown519e0242010-09-15 15:18:56 -07001037 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown3915bb82010-11-05 15:02:16 -07001038 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
1039 bool handled);
Jeff Brownb88102f2010-09-08 11:49:43 -07001040 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brownb6997262010-10-08 22:31:17 -07001041 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001042 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07001043 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001044
Jeff Brownb6997262010-10-08 22:31:17 -07001045 void synthesizeCancelationEventsForAllConnectionsLocked(
Jeff Brownda3d5a92011-03-29 15:11:34 -07001046 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001047 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Jeff Brownda3d5a92011-03-29 15:11:34 -07001048 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001049 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Jeff Brownda3d5a92011-03-29 15:11:34 -07001050 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001051
Jeff Brown01ce2e92010-09-26 22:20:12 -07001052 // Splitting motion events across windows.
1053 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1054
Jeff Brown120a4592010-10-27 18:43:51 -07001055 // Reset and drop everything the dispatcher is doing.
1056 void resetAndDropEverythingLocked(const char* reason);
1057
Jeff Brownb88102f2010-09-08 11:49:43 -07001058 // Dump state.
1059 void dumpDispatchStateLocked(String8& dump);
1060 void logDispatchStateLocked();
1061
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001062 // Add or remove a connection to the mActiveConnections vector.
1063 void activateConnectionLocked(Connection* connection);
1064 void deactivateConnectionLocked(Connection* connection);
1065
1066 // Interesting events that we might like to log or tell the framework about.
Jeff Brown9c3cda02010-06-15 01:31:58 -07001067 void onDispatchCycleStartedLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001068 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001069 void onDispatchCycleFinishedLocked(
Jeff Brown3915bb82010-11-05 15:02:16 -07001070 nsecs_t currentTime, const sp<Connection>& connection, bool handled);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001071 void onDispatchCycleBrokenLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001072 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001073 void onANRLocked(
1074 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
1075 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001076
Jeff Brown7fbdc842010-06-17 20:52:56 -07001077 // Outbound policy interactions.
Jeff Brownb88102f2010-09-08 11:49:43 -07001078 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001079 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown519e0242010-09-15 15:18:56 -07001080 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07001081 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001082 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001083 bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
1084 DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled);
1085 bool afterMotionEventLockedInterruptible(const sp<Connection>& connection,
1086 DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled);
Jeff Brownb88102f2010-09-08 11:49:43 -07001087 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001088 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -07001089
1090 // Statistics gathering.
1091 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1092 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001093};
1094
1095/* Enqueues and dispatches input events, endlessly. */
1096class InputDispatcherThread : public Thread {
1097public:
1098 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1099 ~InputDispatcherThread();
1100
1101private:
1102 virtual bool threadLoop();
1103
1104 sp<InputDispatcherInterface> mDispatcher;
1105};
1106
1107} // namespace android
1108
Jeff Brownb88102f2010-09-08 11:49:43 -07001109#endif // _UI_INPUT_DISPATCHER_H