blob: 1d39b2e6cbb82de9e13cbff45531672a2b0251d8 [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _UI_INPUT_DISPATCHER_H
18#define _UI_INPUT_DISPATCHER_H
19
20#include <ui/Input.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070021#include <ui/InputTransport.h>
22#include <utils/KeyedVector.h>
23#include <utils/Vector.h>
24#include <utils/threads.h>
25#include <utils/Timers.h>
26#include <utils/RefBase.h>
27#include <utils/String8.h>
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070028#include <utils/Looper.h>
Jeff Brown01ce2e92010-09-26 22:20:12 -070029#include <utils/BitSet.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070030
31#include <stddef.h>
32#include <unistd.h>
Jeff Brownb88102f2010-09-08 11:49:43 -070033#include <limits.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070034
Jeff Brown928e0542011-01-10 11:17:36 -080035#include "InputWindow.h"
36#include "InputApplication.h"
37
Jeff Brown46b9ac02010-04-22 18:58:52 -070038
39namespace android {
40
Jeff Brown9c3cda02010-06-15 01:31:58 -070041/*
Jeff Brown7fbdc842010-06-17 20:52:56 -070042 * Constants used to report the outcome of input event injection.
43 */
44enum {
45 /* (INTERNAL USE ONLY) Specifies that injection is pending and its outcome is unknown. */
46 INPUT_EVENT_INJECTION_PENDING = -1,
47
48 /* Injection succeeded. */
49 INPUT_EVENT_INJECTION_SUCCEEDED = 0,
50
51 /* Injection failed because the injector did not have permission to inject
52 * into the application with input focus. */
53 INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1,
54
55 /* Injection failed because there were no available input targets. */
56 INPUT_EVENT_INJECTION_FAILED = 2,
57
58 /* Injection failed due to a timeout. */
59 INPUT_EVENT_INJECTION_TIMED_OUT = 3
60};
61
Jeff Brown6ec402b2010-07-28 15:48:59 -070062/*
63 * Constants used to determine the input event injection synchronization mode.
64 */
65enum {
66 /* Injection is asynchronous and is assumed always to be successful. */
67 INPUT_EVENT_INJECTION_SYNC_NONE = 0,
68
69 /* Waits for previous events to be dispatched so that the input dispatcher can determine
70 * whether input event injection willbe permitted based on the current input focus.
71 * Does not wait for the input event to finish processing. */
72 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1,
73
74 /* Waits for the input event to be completely processed. */
75 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED = 2,
76};
77
Jeff Brown7fbdc842010-06-17 20:52:56 -070078
79/*
Jeff Brown9c3cda02010-06-15 01:31:58 -070080 * An input target specifies how an input event is to be dispatched to a particular window
81 * including the window's input channel, control flags, a timeout, and an X / Y offset to
82 * be added to input event coordinates to compensate for the absolute position of the
83 * window area.
84 */
85struct InputTarget {
86 enum {
Jeff Brown519e0242010-09-15 15:18:56 -070087 /* This flag indicates that the event is being delivered to a foreground application. */
Jeff Browna032cc02011-03-07 16:56:21 -080088 FLAG_FOREGROUND = 1 << 0,
Jeff Brown9c3cda02010-06-15 01:31:58 -070089
Jeff Brown85a31762010-09-01 17:01:00 -070090 /* This flag indicates that the target of a MotionEvent is partly or wholly
91 * obscured by another visible window above it. The motion event should be
92 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
Jeff Browna032cc02011-03-07 16:56:21 -080093 FLAG_WINDOW_IS_OBSCURED = 1 << 1,
Jeff Brown01ce2e92010-09-26 22:20:12 -070094
95 /* This flag indicates that a motion event is being split across multiple windows. */
Jeff Browna032cc02011-03-07 16:56:21 -080096 FLAG_SPLIT = 1 << 2,
97
Kenny Root7a9db182011-06-02 15:16:05 -070098 /* This flag indicates that the pointer coordinates dispatched to the application
99 * will be zeroed out to avoid revealing information to an application. This is
100 * used in conjunction with FLAG_DISPATCH_AS_OUTSIDE to prevent apps not sharing
101 * the same UID from watching all touches. */
102 FLAG_ZERO_COORDS = 1 << 3,
103
Jeff Browna032cc02011-03-07 16:56:21 -0800104 /* This flag indicates that the event should be sent as is.
105 * Should always be set unless the event is to be transmuted. */
106 FLAG_DISPATCH_AS_IS = 1 << 8,
107
108 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
109 * of the area of this target and so should instead be delivered as an
110 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
111 FLAG_DISPATCH_AS_OUTSIDE = 1 << 9,
112
113 /* This flag indicates that a hover sequence is starting in the given window.
114 * The event is transmuted into ACTION_HOVER_ENTER. */
115 FLAG_DISPATCH_AS_HOVER_ENTER = 1 << 10,
116
117 /* This flag indicates that a hover event happened outside of a window which handled
118 * previous hover events, signifying the end of the current hover sequence for that
119 * window.
120 * The event is transmuted into ACTION_HOVER_ENTER. */
121 FLAG_DISPATCH_AS_HOVER_EXIT = 1 << 11,
122
Jeff Brown98db5fa2011-06-08 15:37:10 -0700123 /* This flag indicates that the event should be canceled.
124 * It is used to transmute ACTION_MOVE into ACTION_CANCEL when a touch slips
125 * outside of a window. */
126 FLAG_DISPATCH_AS_SLIPPERY_EXIT = 1 << 12,
127
128 /* This flag indicates that the event should be dispatched as an initial down.
129 * It is used to transmute ACTION_MOVE into ACTION_DOWN when a touch slips
130 * into a new window. */
131 FLAG_DISPATCH_AS_SLIPPERY_ENTER = 1 << 13,
132
Jeff Browna032cc02011-03-07 16:56:21 -0800133 /* Mask for all dispatch modes. */
134 FLAG_DISPATCH_MASK = FLAG_DISPATCH_AS_IS
135 | FLAG_DISPATCH_AS_OUTSIDE
136 | FLAG_DISPATCH_AS_HOVER_ENTER
Jeff Brown98db5fa2011-06-08 15:37:10 -0700137 | FLAG_DISPATCH_AS_HOVER_EXIT
138 | FLAG_DISPATCH_AS_SLIPPERY_EXIT
139 | FLAG_DISPATCH_AS_SLIPPERY_ENTER,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700140 };
141
142 // The input channel to be targeted.
143 sp<InputChannel> inputChannel;
144
145 // Flags for the input target.
146 int32_t flags;
147
Jeff Brown9c3cda02010-06-15 01:31:58 -0700148 // The x and y offset to add to a MotionEvent as it is delivered.
149 // (ignored for KeyEvents)
150 float xOffset, yOffset;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700151
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400152 // Scaling factor to apply to MotionEvent as it is delivered.
153 // (ignored for KeyEvents)
154 float scaleFactor;
155
Jeff Brown01ce2e92010-09-26 22:20:12 -0700156 // The subset of pointer ids to include in motion events dispatched to this input target
157 // if FLAG_SPLIT is set.
158 BitSet32 pointerIds;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700159};
160
Jeff Brown7fbdc842010-06-17 20:52:56 -0700161
Jeff Brown9c3cda02010-06-15 01:31:58 -0700162/*
Jeff Brown214eaf42011-05-26 19:17:02 -0700163 * Input dispatcher configuration.
164 *
165 * Specifies various options that modify the behavior of the input dispatcher.
166 */
167struct InputDispatcherConfiguration {
168 // The key repeat initial timeout.
169 nsecs_t keyRepeatTimeout;
170
171 // The key repeat inter-key delay.
172 nsecs_t keyRepeatDelay;
173
174 // The maximum suggested event delivery rate per second.
175 // This value is used to throttle motion event movement actions on a per-device
176 // basis. It is not intended to be a hard limit.
177 int32_t maxEventsPerSecond;
178
179 InputDispatcherConfiguration() :
180 keyRepeatTimeout(500 * 1000000LL),
181 keyRepeatDelay(50 * 1000000LL),
182 maxEventsPerSecond(60) { }
183};
184
185
186/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700187 * Input dispatcher policy interface.
188 *
189 * The input reader policy is used by the input reader to interact with the Window Manager
190 * and other system components.
191 *
192 * The actual implementation is partially supported by callbacks into the DVM
193 * via JNI. This interface is also mocked in the unit tests.
194 */
195class InputDispatcherPolicyInterface : public virtual RefBase {
196protected:
197 InputDispatcherPolicyInterface() { }
198 virtual ~InputDispatcherPolicyInterface() { }
199
200public:
201 /* Notifies the system that a configuration change has occurred. */
202 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
203
Jeff Brownb88102f2010-09-08 11:49:43 -0700204 /* Notifies the system that an application is not responding.
205 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
Jeff Brown519e0242010-09-15 15:18:56 -0700206 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -0800207 const sp<InputWindowHandle>& inputWindowHandle) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700208
Jeff Brown9c3cda02010-06-15 01:31:58 -0700209 /* Notifies the system that an input channel is unrecoverably broken. */
Jeff Brown928e0542011-01-10 11:17:36 -0800210 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700211
Jeff Brown214eaf42011-05-26 19:17:02 -0700212 /* Gets the input dispatcher configuration. */
213 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700214
Jeff Brown214eaf42011-05-26 19:17:02 -0700215 /* Returns true if automatic key repeating is enabled. */
216 virtual bool isKeyRepeatEnabled() = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700217
Jeff Brown0029c662011-03-30 02:25:18 -0700218 /* Filters an input event.
219 * Return true to dispatch the event unmodified, false to consume the event.
220 * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED
221 * to injectInputEvent.
222 */
223 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) = 0;
224
Jeff Brownb6997262010-10-08 22:31:17 -0700225 /* Intercepts a key event immediately before queueing it.
226 * The policy can use this method as an opportunity to perform power management functions
227 * and early event preprocessing such as updating policy flags.
228 *
229 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
230 * should be dispatched to applications.
231 */
Jeff Brown1f245102010-11-18 20:53:46 -0800232 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700233
Jeff Brown56194eb2011-03-02 19:23:13 -0800234 /* Intercepts a touch, trackball or other motion event before queueing it.
Jeff Brownb6997262010-10-08 22:31:17 -0700235 * The policy can use this method as an opportunity to perform power management functions
236 * and early event preprocessing such as updating policy flags.
237 *
238 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
239 * should be dispatched to applications.
240 */
Jeff Brown56194eb2011-03-02 19:23:13 -0800241 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700242
Jeff Brownb88102f2010-09-08 11:49:43 -0700243 /* Allows the policy a chance to intercept a key before dispatching. */
Jeff Brown928e0542011-01-10 11:17:36 -0800244 virtual bool interceptKeyBeforeDispatching(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700245 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
246
Jeff Brown49ed71d2010-12-06 17:13:33 -0800247 /* Allows the policy a chance to perform default processing for an unhandled key.
248 * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */
Jeff Brown928e0542011-01-10 11:17:36 -0800249 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800250 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0;
Jeff Brown3915bb82010-11-05 15:02:16 -0700251
Jeff Brownb6997262010-10-08 22:31:17 -0700252 /* Notifies the policy about switch events.
253 */
254 virtual void notifySwitch(nsecs_t when,
255 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
256
Jeff Brownb88102f2010-09-08 11:49:43 -0700257 /* Poke user activity for an event dispatched to a window. */
Jeff Brown01ce2e92010-09-26 22:20:12 -0700258 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700259
260 /* Checks whether a given application pid/uid has permission to inject input events
261 * into other applications.
262 *
263 * This method is special in that its implementation promises to be non-reentrant and
264 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
265 */
266 virtual bool checkInjectEventsPermissionNonReentrant(
267 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700268};
269
270
Jeff Brown46b9ac02010-04-22 18:58:52 -0700271/* Notifies the system about input events generated by the input reader.
272 * The dispatcher is expected to be mostly asynchronous. */
273class InputDispatcherInterface : public virtual RefBase {
274protected:
275 InputDispatcherInterface() { }
276 virtual ~InputDispatcherInterface() { }
277
278public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700279 /* Dumps the state of the input dispatcher.
280 *
281 * This method may be called on any thread (usually by the input manager). */
282 virtual void dump(String8& dump) = 0;
283
Jeff Brown46b9ac02010-04-22 18:58:52 -0700284 /* Runs a single iteration of the dispatch loop.
285 * Nominally processes one queued event, a timeout, or a response from an input consumer.
286 *
287 * This method should only be called on the input dispatcher thread.
288 */
289 virtual void dispatchOnce() = 0;
290
291 /* Notifies the dispatcher about new events.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700292 *
293 * These methods should only be called on the input reader thread.
294 */
Jeff Brown9c3cda02010-06-15 01:31:58 -0700295 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brown58a2da82011-01-25 16:02:22 -0800296 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700297 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
298 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
Jeff Brown58a2da82011-01-25 16:02:22 -0800299 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700300 uint32_t policyFlags, int32_t action, int32_t flags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700301 int32_t metaState, int32_t buttonState, int32_t edgeFlags,
302 uint32_t pointerCount, const PointerProperties* pointerProperties,
303 const PointerCoords* pointerCoords,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700304 float xPrecision, float yPrecision, nsecs_t downTime) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700305 virtual void notifySwitch(nsecs_t when,
306 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700307
Jeff Brown7fbdc842010-06-17 20:52:56 -0700308 /* Injects an input event and optionally waits for sync.
Jeff Brown6ec402b2010-07-28 15:48:59 -0700309 * The synchronization mode determines whether the method blocks while waiting for
310 * input injection to proceed.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700311 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
312 *
313 * This method may be called on any thread (usually by the input manager).
314 */
315 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown0029c662011-03-30 02:25:18 -0700316 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
317 uint32_t policyFlags) = 0;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700318
Jeff Brownb88102f2010-09-08 11:49:43 -0700319 /* Sets the list of input windows.
320 *
321 * This method may be called on any thread (usually by the input manager).
322 */
Jeff Brown9302c872011-07-13 22:51:29 -0700323 virtual void setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700324
325 /* Sets the focused application.
326 *
327 * This method may be called on any thread (usually by the input manager).
328 */
Jeff Brown9302c872011-07-13 22:51:29 -0700329 virtual void setFocusedApplication(
330 const sp<InputApplicationHandle>& inputApplicationHandle) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700331
332 /* Sets the input dispatching mode.
333 *
334 * This method may be called on any thread (usually by the input manager).
335 */
336 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
337
Jeff Brown0029c662011-03-30 02:25:18 -0700338 /* Sets whether input event filtering is enabled.
339 * When enabled, incoming input events are sent to the policy's filterInputEvent
340 * method instead of being dispatched. The filter is expected to use
341 * injectInputEvent to inject the events it would like to have dispatched.
342 * It should include POLICY_FLAG_FILTERED in the policy flags during injection.
343 */
344 virtual void setInputFilterEnabled(bool enabled) = 0;
345
Jeff Browne6504122010-09-27 14:52:15 -0700346 /* Transfers touch focus from the window associated with one channel to the
347 * window associated with the other channel.
348 *
349 * Returns true on success. False if the window did not actually have touch focus.
350 */
351 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
352 const sp<InputChannel>& toChannel) = 0;
353
Jeff Brown46b9ac02010-04-22 18:58:52 -0700354 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Brownb88102f2010-09-08 11:49:43 -0700355 * If monitor is true, the channel will receive a copy of all input events.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700356 *
357 * These methods may be called on any thread (usually by the input manager).
358 */
Jeff Brown928e0542011-01-10 11:17:36 -0800359 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
360 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700361 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
362};
363
Jeff Brown9c3cda02010-06-15 01:31:58 -0700364/* Dispatches events to input targets. Some functions of the input dispatcher, such as
365 * identifying input targets, are controlled by a separate policy object.
366 *
367 * IMPORTANT INVARIANT:
368 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
369 * the input dispatcher never calls into the policy while holding its internal locks.
370 * The implementation is also carefully designed to recover from scenarios such as an
371 * input channel becoming unregistered while identifying input targets or processing timeouts.
372 *
373 * Methods marked 'Locked' must be called with the lock acquired.
374 *
375 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
376 * may during the course of their execution release the lock, call into the policy, and
377 * then reacquire the lock. The caller is responsible for recovering gracefully.
378 *
379 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
380 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700381class InputDispatcher : public InputDispatcherInterface {
382protected:
383 virtual ~InputDispatcher();
384
385public:
Jeff Brown9c3cda02010-06-15 01:31:58 -0700386 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700387
Jeff Brownb88102f2010-09-08 11:49:43 -0700388 virtual void dump(String8& dump);
389
Jeff Brown46b9ac02010-04-22 18:58:52 -0700390 virtual void dispatchOnce();
391
Jeff Brown9c3cda02010-06-15 01:31:58 -0700392 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brown58a2da82011-01-25 16:02:22 -0800393 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700394 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
395 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown58a2da82011-01-25 16:02:22 -0800396 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700397 uint32_t policyFlags, int32_t action, int32_t flags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700398 int32_t metaState, int32_t buttonState, int32_t edgeFlags,
399 uint32_t pointerCount, const PointerProperties* pointerProperties,
400 const PointerCoords* pointerCoords,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700401 float xPrecision, float yPrecision, nsecs_t downTime);
Jeff Brownb6997262010-10-08 22:31:17 -0700402 virtual void notifySwitch(nsecs_t when,
403 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) ;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700404
Jeff Brown7fbdc842010-06-17 20:52:56 -0700405 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown0029c662011-03-30 02:25:18 -0700406 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
407 uint32_t policyFlags);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700408
Jeff Brown9302c872011-07-13 22:51:29 -0700409 virtual void setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles);
410 virtual void setFocusedApplication(const sp<InputApplicationHandle>& inputApplicationHandle);
Jeff Brownb88102f2010-09-08 11:49:43 -0700411 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown0029c662011-03-30 02:25:18 -0700412 virtual void setInputFilterEnabled(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700413
Jeff Browne6504122010-09-27 14:52:15 -0700414 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
415 const sp<InputChannel>& toChannel);
416
Jeff Brown928e0542011-01-10 11:17:36 -0800417 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
418 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700419 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
420
421private:
422 template <typename T>
423 struct Link {
424 T* next;
425 T* prev;
426 };
427
Jeff Brown01ce2e92010-09-26 22:20:12 -0700428 struct InjectionState {
429 mutable int32_t refCount;
430
431 int32_t injectorPid;
432 int32_t injectorUid;
433 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
434 bool injectionIsAsync; // set to true if injection is not waiting for the result
435 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
Jeff Brownac386072011-07-20 15:19:50 -0700436
437 InjectionState(int32_t injectorPid, int32_t injectorUid);
438 void release();
439
440 private:
441 ~InjectionState();
Jeff Brown01ce2e92010-09-26 22:20:12 -0700442 };
443
Jeff Brown46b9ac02010-04-22 18:58:52 -0700444 struct EventEntry : Link<EventEntry> {
445 enum {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700446 TYPE_CONFIGURATION_CHANGED,
447 TYPE_KEY,
448 TYPE_MOTION
449 };
450
Jeff Brown01ce2e92010-09-26 22:20:12 -0700451 mutable int32_t refCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700452 int32_t type;
453 nsecs_t eventTime;
Jeff Brownb6997262010-10-08 22:31:17 -0700454 uint32_t policyFlags;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700455 InjectionState* injectionState;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700456
Jeff Brown9c3cda02010-06-15 01:31:58 -0700457 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown7fbdc842010-06-17 20:52:56 -0700458
Jeff Brown4e91a182011-04-07 11:38:09 -0700459 inline bool isInjected() const { return injectionState != NULL; }
Jeff Brownac386072011-07-20 15:19:50 -0700460
461 void release();
462
463 protected:
464 EventEntry(int32_t type, nsecs_t eventTime, uint32_t policyFlags);
465 virtual ~EventEntry();
466 void releaseInjectionState();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700467 };
468
469 struct ConfigurationChangedEntry : EventEntry {
Jeff Brownac386072011-07-20 15:19:50 -0700470 ConfigurationChangedEntry(nsecs_t eventTime);
471
472 protected:
473 virtual ~ConfigurationChangedEntry();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700474 };
475
476 struct KeyEntry : EventEntry {
477 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800478 uint32_t source;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700479 int32_t action;
480 int32_t flags;
481 int32_t keyCode;
482 int32_t scanCode;
483 int32_t metaState;
484 int32_t repeatCount;
485 nsecs_t downTime;
Jeff Brownb88102f2010-09-08 11:49:43 -0700486
487 bool syntheticRepeat; // set to true for synthetic key repeats
488
489 enum InterceptKeyResult {
490 INTERCEPT_KEY_RESULT_UNKNOWN,
491 INTERCEPT_KEY_RESULT_SKIP,
492 INTERCEPT_KEY_RESULT_CONTINUE,
493 };
494 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Brownac386072011-07-20 15:19:50 -0700495
496 KeyEntry(nsecs_t eventTime,
497 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
498 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
499 int32_t repeatCount, nsecs_t downTime);
500 void recycle();
501
502 protected:
503 virtual ~KeyEntry();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700504 };
505
506 struct MotionSample {
507 MotionSample* next;
508
Jeff Brown4e91a182011-04-07 11:38:09 -0700509 nsecs_t eventTime; // may be updated during coalescing
510 nsecs_t eventTimeBeforeCoalescing; // not updated during coalescing
Jeff Brown46b9ac02010-04-22 18:58:52 -0700511 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownac386072011-07-20 15:19:50 -0700512
513 MotionSample(nsecs_t eventTime, const PointerCoords* pointerCoords,
514 uint32_t pointerCount);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700515 };
516
517 struct MotionEntry : EventEntry {
518 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800519 uint32_t source;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700520 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -0700521 int32_t flags;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700522 int32_t metaState;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700523 int32_t buttonState;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700524 int32_t edgeFlags;
525 float xPrecision;
526 float yPrecision;
527 nsecs_t downTime;
528 uint32_t pointerCount;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700529 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brown46b9ac02010-04-22 18:58:52 -0700530
531 // Linked list of motion samples associated with this motion event.
532 MotionSample firstSample;
533 MotionSample* lastSample;
Jeff Brownae9fc032010-08-18 15:51:08 -0700534
Jeff Brownac386072011-07-20 15:19:50 -0700535 MotionEntry(nsecs_t eventTime,
536 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
537 int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
538 float xPrecision, float yPrecision,
539 nsecs_t downTime, uint32_t pointerCount,
540 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords);
541
Jeff Brownae9fc032010-08-18 15:51:08 -0700542 uint32_t countSamples() const;
Jeff Brown4e91a182011-04-07 11:38:09 -0700543
544 // Checks whether we can append samples, assuming the device id and source are the same.
545 bool canAppendSamples(int32_t action, uint32_t pointerCount,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700546 const PointerProperties* pointerProperties) const;
Jeff Brownac386072011-07-20 15:19:50 -0700547
548 void appendSample(nsecs_t eventTime, const PointerCoords* pointerCoords);
549
550 protected:
551 virtual ~MotionEntry();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700552 };
553
Jeff Brown9c3cda02010-06-15 01:31:58 -0700554 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700555 struct DispatchEntry : Link<DispatchEntry> {
556 EventEntry* eventEntry; // the event to dispatch
557 int32_t targetFlags;
558 float xOffset;
559 float yOffset;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400560 float scaleFactor;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700561
562 // True if dispatch has started.
563 bool inProgress;
564
Jeff Brown81346812011-06-28 20:08:48 -0700565 // Set to the resolved action and flags when the event is enqueued.
566 int32_t resolvedAction;
567 int32_t resolvedFlags;
568
Jeff Brown46b9ac02010-04-22 18:58:52 -0700569 // For motion events:
570 // Pointer to the first motion sample to dispatch in this cycle.
571 // Usually NULL to indicate that the list of motion samples begins at
572 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
573 // cycle and this pointer indicates the location of the first remainining sample
574 // to dispatch during the current cycle.
575 MotionSample* headMotionSample;
576 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
577 // unable to send all motion samples during this cycle. On the next cycle,
578 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
579 // will be set to NULL.
580 MotionSample* tailMotionSample;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700581
Jeff Brownac386072011-07-20 15:19:50 -0700582 DispatchEntry(EventEntry* eventEntry,
583 int32_t targetFlags, float xOffset, float yOffset, float scaleFactor);
584 ~DispatchEntry();
585
Jeff Brown519e0242010-09-15 15:18:56 -0700586 inline bool hasForegroundTarget() const {
587 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Brownb88102f2010-09-08 11:49:43 -0700588 }
Jeff Brown01ce2e92010-09-26 22:20:12 -0700589
590 inline bool isSplit() const {
591 return targetFlags & InputTarget::FLAG_SPLIT;
592 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700593 };
594
Jeff Brown9c3cda02010-06-15 01:31:58 -0700595 // A command entry captures state and behavior for an action to be performed in the
596 // dispatch loop after the initial processing has taken place. It is essentially
597 // a kind of continuation used to postpone sensitive policy interactions to a point
598 // in the dispatch loop where it is safe to release the lock (generally after finishing
599 // the critical parts of the dispatch cycle).
600 //
601 // The special thing about commands is that they can voluntarily release and reacquire
602 // the dispatcher lock at will. Initially when the command starts running, the
603 // dispatcher lock is held. However, if the command needs to call into the policy to
604 // do some work, it can release the lock, do the work, then reacquire the lock again
605 // before returning.
606 //
607 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
608 // never calls into the policy while holding its lock.
609 //
610 // Commands are implicitly 'LockedInterruptible'.
611 struct CommandEntry;
612 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
613
Jeff Brown7fbdc842010-06-17 20:52:56 -0700614 class Connection;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700615 struct CommandEntry : Link<CommandEntry> {
Jeff Brownac386072011-07-20 15:19:50 -0700616 CommandEntry(Command command);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700617 ~CommandEntry();
618
619 Command command;
620
621 // parameters for the command (usage varies by command)
Jeff Brown7fbdc842010-06-17 20:52:56 -0700622 sp<Connection> connection;
Jeff Brownb88102f2010-09-08 11:49:43 -0700623 nsecs_t eventTime;
624 KeyEntry* keyEntry;
Jeff Brownb88102f2010-09-08 11:49:43 -0700625 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800626 sp<InputWindowHandle> inputWindowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700627 int32_t userActivityEventType;
Jeff Brown3915bb82010-11-05 15:02:16 -0700628 bool handled;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700629 };
630
631 // Generic queue implementation.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700632 template <typename T>
633 struct Queue {
Jeff Brownac386072011-07-20 15:19:50 -0700634 T* head;
635 T* tail;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700636
Jeff Brownac386072011-07-20 15:19:50 -0700637 inline Queue() : head(NULL), tail(NULL) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700638 }
639
Jeff Brownb88102f2010-09-08 11:49:43 -0700640 inline bool isEmpty() const {
Jeff Brownac386072011-07-20 15:19:50 -0700641 return !head;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700642 }
643
644 inline void enqueueAtTail(T* entry) {
Jeff Brownac386072011-07-20 15:19:50 -0700645 entry->prev = tail;
646 if (tail) {
647 tail->next = entry;
648 } else {
649 head = entry;
650 }
651 entry->next = NULL;
652 tail = entry;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700653 }
654
655 inline void enqueueAtHead(T* entry) {
Jeff Brownac386072011-07-20 15:19:50 -0700656 entry->next = head;
657 if (head) {
658 head->prev = entry;
659 } else {
660 tail = entry;
661 }
662 entry->prev = NULL;
663 head = entry;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700664 }
665
666 inline void dequeue(T* entry) {
Jeff Brownac386072011-07-20 15:19:50 -0700667 if (entry->prev) {
668 entry->prev->next = entry->next;
669 } else {
670 head = entry->next;
671 }
672 if (entry->next) {
673 entry->next->prev = entry->prev;
674 } else {
675 tail = entry->prev;
676 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700677 }
678
679 inline T* dequeueAtHead() {
Jeff Brownac386072011-07-20 15:19:50 -0700680 T* entry = head;
681 head = entry->next;
682 if (head) {
683 head->prev = NULL;
684 } else {
685 tail = NULL;
686 }
687 return entry;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700688 }
Jeff Brown519e0242010-09-15 15:18:56 -0700689
690 uint32_t count() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700691 };
692
Jeff Brownda3d5a92011-03-29 15:11:34 -0700693 /* Specifies which events are to be canceled and why. */
694 struct CancelationOptions {
695 enum Mode {
Jeff Brownb6997262010-10-08 22:31:17 -0700696 CANCEL_ALL_EVENTS = 0,
697 CANCEL_POINTER_EVENTS = 1,
698 CANCEL_NON_POINTER_EVENTS = 2,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800699 CANCEL_FALLBACK_EVENTS = 3,
Jeff Brownb6997262010-10-08 22:31:17 -0700700 };
701
Jeff Brownda3d5a92011-03-29 15:11:34 -0700702 // The criterion to use to determine which events should be canceled.
703 Mode mode;
704
705 // Descriptive reason for the cancelation.
706 const char* reason;
707
708 // The specific keycode of the key event to cancel, or -1 to cancel any key event.
709 int32_t keyCode;
710
711 CancelationOptions(Mode mode, const char* reason) :
712 mode(mode), reason(reason), keyCode(-1) { }
713 };
714
715 /* Tracks dispatched key and motion event state so that cancelation events can be
716 * synthesized when events are dropped. */
717 class InputState {
718 public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700719 InputState();
720 ~InputState();
721
722 // Returns true if there is no state to be canceled.
723 bool isNeutral() const;
724
Jeff Brown81346812011-06-28 20:08:48 -0700725 // Returns true if the specified source is known to have received a hover enter
726 // motion event.
727 bool isHovering(int32_t deviceId, uint32_t source) const;
Jeff Brownb88102f2010-09-08 11:49:43 -0700728
729 // Records tracking information for a key event that has just been published.
Jeff Brown81346812011-06-28 20:08:48 -0700730 // Returns true if the event should be delivered, false if it is inconsistent
731 // and should be skipped.
732 bool trackKey(const KeyEntry* entry, int32_t action, int32_t flags);
Jeff Brownb88102f2010-09-08 11:49:43 -0700733
734 // Records tracking information for a motion event that has just been published.
Jeff Brown81346812011-06-28 20:08:48 -0700735 // Returns true if the event should be delivered, false if it is inconsistent
736 // and should be skipped.
737 bool trackMotion(const MotionEntry* entry, int32_t action, int32_t flags);
Jeff Brownb88102f2010-09-08 11:49:43 -0700738
Jeff Brownb6997262010-10-08 22:31:17 -0700739 // Synthesizes cancelation events for the current state and resets the tracked state.
Jeff Brownac386072011-07-20 15:19:50 -0700740 void synthesizeCancelationEvents(nsecs_t currentTime,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700741 Vector<EventEntry*>& outEvents, const CancelationOptions& options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700742
743 // Clears the current state.
744 void clear();
745
Jeff Brown9c9f1a32010-10-11 18:32:20 -0700746 // Copies pointer-related parts of the input state to another instance.
747 void copyPointerStateTo(InputState& other) const;
748
Jeff Brownda3d5a92011-03-29 15:11:34 -0700749 // Gets the fallback key associated with a keycode.
750 // Returns -1 if none.
751 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
752 int32_t getFallbackKey(int32_t originalKeyCode);
753
754 // Sets the fallback key for a particular keycode.
755 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
756
757 // Removes the fallback key for a particular keycode.
758 void removeFallbackKey(int32_t originalKeyCode);
759
760 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const {
761 return mFallbackKeys;
762 }
763
Jeff Brownb88102f2010-09-08 11:49:43 -0700764 private:
Jeff Brownb88102f2010-09-08 11:49:43 -0700765 struct KeyMemento {
766 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800767 uint32_t source;
Jeff Brownb88102f2010-09-08 11:49:43 -0700768 int32_t keyCode;
769 int32_t scanCode;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800770 int32_t flags;
Jeff Brownb88102f2010-09-08 11:49:43 -0700771 nsecs_t downTime;
772 };
773
774 struct MotionMemento {
775 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800776 uint32_t source;
Jeff Brown81346812011-06-28 20:08:48 -0700777 int32_t flags;
Jeff Brownb88102f2010-09-08 11:49:43 -0700778 float xPrecision;
779 float yPrecision;
780 nsecs_t downTime;
781 uint32_t pointerCount;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700782 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownb88102f2010-09-08 11:49:43 -0700783 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Browna032cc02011-03-07 16:56:21 -0800784 bool hovering;
Jeff Brownb88102f2010-09-08 11:49:43 -0700785
786 void setPointers(const MotionEntry* entry);
787 };
788
789 Vector<KeyMemento> mKeyMementos;
790 Vector<MotionMemento> mMotionMementos;
Jeff Brownda3d5a92011-03-29 15:11:34 -0700791 KeyedVector<int32_t, int32_t> mFallbackKeys;
Jeff Brownb6997262010-10-08 22:31:17 -0700792
Jeff Brown81346812011-06-28 20:08:48 -0700793 ssize_t findKeyMemento(const KeyEntry* entry) const;
794 ssize_t findMotionMemento(const MotionEntry* entry, bool hovering) const;
795
796 void addKeyMemento(const KeyEntry* entry, int32_t flags);
797 void addMotionMemento(const MotionEntry* entry, int32_t flags, bool hovering);
798
Jeff Brown49ed71d2010-12-06 17:13:33 -0800799 static bool shouldCancelKey(const KeyMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700800 const CancelationOptions& options);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800801 static bool shouldCancelMotion(const MotionMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700802 const CancelationOptions& options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700803 };
804
Jeff Brown46b9ac02010-04-22 18:58:52 -0700805 /* Manages the dispatch state associated with a single input channel. */
806 class Connection : public RefBase {
807 protected:
808 virtual ~Connection();
809
810 public:
811 enum Status {
812 // Everything is peachy.
813 STATUS_NORMAL,
814 // An unrecoverable communication error has occurred.
815 STATUS_BROKEN,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700816 // The input channel has been unregistered.
817 STATUS_ZOMBIE
818 };
819
820 Status status;
Jeff Brown928e0542011-01-10 11:17:36 -0800821 sp<InputChannel> inputChannel; // never null
822 sp<InputWindowHandle> inputWindowHandle; // may be null
Jeff Brown46b9ac02010-04-22 18:58:52 -0700823 InputPublisher inputPublisher;
Jeff Brownb88102f2010-09-08 11:49:43 -0700824 InputState inputState;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700825 Queue<DispatchEntry> outboundQueue;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700826
827 nsecs_t lastEventTime; // the time when the event was originally captured
828 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Brown46b9ac02010-04-22 18:58:52 -0700829
Jeff Brown928e0542011-01-10 11:17:36 -0800830 explicit Connection(const sp<InputChannel>& inputChannel,
831 const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700832
Jeff Brown9c3cda02010-06-15 01:31:58 -0700833 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
834
835 const char* getStatusLabel() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700836
837 // Finds a DispatchEntry in the outbound queue associated with the specified event.
838 // Returns NULL if not found.
839 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
840
Jeff Brown46b9ac02010-04-22 18:58:52 -0700841 // Gets the time since the current event was originally obtained from the input driver.
Jeff Brownb88102f2010-09-08 11:49:43 -0700842 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700843 return (currentTime - lastEventTime) / 1000000.0;
844 }
845
846 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Brownb88102f2010-09-08 11:49:43 -0700847 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700848 return (currentTime - lastDispatchTime) / 1000000.0;
849 }
850
Jeff Brown46b9ac02010-04-22 18:58:52 -0700851 status_t initialize();
852 };
853
Jeff Brownb6997262010-10-08 22:31:17 -0700854 enum DropReason {
855 DROP_REASON_NOT_DROPPED = 0,
856 DROP_REASON_POLICY = 1,
857 DROP_REASON_APP_SWITCH = 2,
858 DROP_REASON_DISABLED = 3,
Jeff Brown928e0542011-01-10 11:17:36 -0800859 DROP_REASON_BLOCKED = 4,
860 DROP_REASON_STALE = 5,
Jeff Brownb6997262010-10-08 22:31:17 -0700861 };
862
Jeff Brown9c3cda02010-06-15 01:31:58 -0700863 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Brown214eaf42011-05-26 19:17:02 -0700864 InputDispatcherConfiguration mConfig;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700865
866 Mutex mLock;
867
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700868 sp<Looper> mLooper;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700869
Jeff Brownb88102f2010-09-08 11:49:43 -0700870 EventEntry* mPendingEvent;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700871 Queue<EventEntry> mInboundQueue;
872 Queue<CommandEntry> mCommandQueue;
873
Jeff Brownb88102f2010-09-08 11:49:43 -0700874 Vector<EventEntry*> mTempCancelationEvents;
875
Jeff Brown214eaf42011-05-26 19:17:02 -0700876 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700877
Jeff Brown4e91a182011-04-07 11:38:09 -0700878 // Batches a new sample onto a motion entry.
879 // Assumes that the we have already checked that we can append samples.
880 void batchMotionLocked(MotionEntry* entry, nsecs_t eventTime, int32_t metaState,
881 const PointerCoords* pointerCoords, const char* eventDescription);
882
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700883 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Brownb88102f2010-09-08 11:49:43 -0700884 bool enqueueInboundEventLocked(EventEntry* entry);
885
Jeff Brownb6997262010-10-08 22:31:17 -0700886 // Cleans up input state when dropping an inbound event.
887 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
888
Jeff Brownb88102f2010-09-08 11:49:43 -0700889 // App switch latency optimization.
Jeff Brownb6997262010-10-08 22:31:17 -0700890 bool mAppSwitchSawKeyDown;
Jeff Brownb88102f2010-09-08 11:49:43 -0700891 nsecs_t mAppSwitchDueTime;
892
Jeff Brownb6997262010-10-08 22:31:17 -0700893 static bool isAppSwitchKeyCode(int32_t keyCode);
894 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700895 bool isAppSwitchPendingLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700896 void resetPendingAppSwitchLocked(bool handled);
897
Jeff Brown928e0542011-01-10 11:17:36 -0800898 // Stale event latency optimization.
899 static bool isStaleEventLocked(nsecs_t currentTime, EventEntry* entry);
900
901 // Blocked event latency optimization. Drops old events when the user intends
902 // to transfer focus to a new application.
903 EventEntry* mNextUnblockedEvent;
904
Jeff Brown9302c872011-07-13 22:51:29 -0700905 sp<InputWindowHandle> findTouchedWindowAtLocked(int32_t x, int32_t y);
Jeff Brown928e0542011-01-10 11:17:36 -0800906
Jeff Brown46b9ac02010-04-22 18:58:52 -0700907 // All registered connections mapped by receive pipe file descriptor.
908 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
909
Jeff Brown519e0242010-09-15 15:18:56 -0700910 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown2cbecea2010-08-17 15:59:26 -0700911
Jeff Brown46b9ac02010-04-22 18:58:52 -0700912 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700913 // We don't use a ref-counted pointer here because we explicitly abort connections
914 // during unregistration which causes the connection's outbound queue to be cleared
915 // and the connection itself to be deactivated.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700916 Vector<Connection*> mActiveConnections;
917
Jeff Brownb88102f2010-09-08 11:49:43 -0700918 // Input channels that will receive a copy of all input events.
919 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700920
Jeff Brown7fbdc842010-06-17 20:52:56 -0700921 // Event injection and synchronization.
922 Condition mInjectionResultAvailableCondition;
Jeff Brownb6997262010-10-08 22:31:17 -0700923 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700924 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
925
Jeff Brown6ec402b2010-07-28 15:48:59 -0700926 Condition mInjectionSyncFinishedCondition;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700927 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -0700928 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown6ec402b2010-07-28 15:48:59 -0700929
Jeff Brownae9fc032010-08-18 15:51:08 -0700930 // Throttling state.
931 struct ThrottleState {
932 nsecs_t minTimeBetweenEvents;
933
934 nsecs_t lastEventTime;
935 int32_t lastDeviceId;
936 uint32_t lastSource;
937
938 uint32_t originalSampleCount; // only collected during debugging
939 } mThrottleState;
940
Jeff Brown46b9ac02010-04-22 18:58:52 -0700941 // Key repeat tracking.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700942 struct KeyRepeatState {
943 KeyEntry* lastKeyEntry; // or null if no repeat
944 nsecs_t nextRepeatTime;
945 } mKeyRepeatState;
946
947 void resetKeyRepeatLocked();
Jeff Brown214eaf42011-05-26 19:17:02 -0700948 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700949
Jeff Brown9c3cda02010-06-15 01:31:58 -0700950 // Deferred command processing.
951 bool runCommandsLockedInterruptible();
952 CommandEntry* postCommandLocked(Command command);
953
Jeff Brownb88102f2010-09-08 11:49:43 -0700954 // Inbound event processing.
955 void drainInboundQueueLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700956 void releasePendingEventLocked();
957 void releaseInboundEventLocked(EventEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700958
Jeff Brownb88102f2010-09-08 11:49:43 -0700959 // Dispatch state.
960 bool mDispatchEnabled;
961 bool mDispatchFrozen;
Jeff Brown0029c662011-03-30 02:25:18 -0700962 bool mInputFilterEnabled;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700963
Jeff Brown9302c872011-07-13 22:51:29 -0700964 Vector<sp<InputWindowHandle> > mWindowHandles;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700965
Jeff Brown9302c872011-07-13 22:51:29 -0700966 sp<InputWindowHandle> getWindowHandleLocked(const sp<InputChannel>& inputChannel) const;
967 bool hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const;
Jeff Brownb88102f2010-09-08 11:49:43 -0700968
969 // Focus tracking for keys, trackball, etc.
Jeff Brown9302c872011-07-13 22:51:29 -0700970 sp<InputWindowHandle> mFocusedWindowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700971
972 // Focus tracking for touch.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700973 struct TouchedWindow {
Jeff Brown9302c872011-07-13 22:51:29 -0700974 sp<InputWindowHandle> windowHandle;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700975 int32_t targetFlags;
Jeff Brown46e75292010-11-10 16:53:45 -0800976 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
Jeff Brownb88102f2010-09-08 11:49:43 -0700977 };
Jeff Brown01ce2e92010-09-26 22:20:12 -0700978 struct TouchState {
979 bool down;
980 bool split;
Jeff Brown95712852011-01-04 19:41:59 -0800981 int32_t deviceId; // id of the device that is currently down, others are rejected
Jeff Brown58a2da82011-01-25 16:02:22 -0800982 uint32_t source; // source of the device that is current down, others are rejected
Jeff Brown01ce2e92010-09-26 22:20:12 -0700983 Vector<TouchedWindow> windows;
984
985 TouchState();
986 ~TouchState();
987 void reset();
988 void copyFrom(const TouchState& other);
Jeff Brown9302c872011-07-13 22:51:29 -0700989 void addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle,
990 int32_t targetFlags, BitSet32 pointerIds);
Jeff Browna032cc02011-03-07 16:56:21 -0800991 void filterNonAsIsTouchWindows();
Jeff Brown9302c872011-07-13 22:51:29 -0700992 sp<InputWindowHandle> getFirstForegroundWindowHandle() const;
Jeff Brown98db5fa2011-06-08 15:37:10 -0700993 bool isSlippery() const;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700994 };
995
996 TouchState mTouchState;
997 TouchState mTempTouchState;
Jeff Brownb88102f2010-09-08 11:49:43 -0700998
999 // Focused application.
Jeff Brown9302c872011-07-13 22:51:29 -07001000 sp<InputApplicationHandle> mFocusedApplicationHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -07001001
1002 // Dispatch inbound events.
1003 bool dispatchConfigurationChangedLocked(
1004 nsecs_t currentTime, ConfigurationChangedEntry* entry);
1005 bool dispatchKeyLocked(
Jeff Brown214eaf42011-05-26 19:17:02 -07001006 nsecs_t currentTime, KeyEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -07001007 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07001008 bool dispatchMotionLocked(
1009 nsecs_t currentTime, MotionEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -07001010 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001011 void dispatchEventToCurrentInputTargetsLocked(
1012 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001013
Jeff Brownb88102f2010-09-08 11:49:43 -07001014 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
1015 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
1016
1017 // The input targets that were most recently identified for dispatch.
Jeff Brownb88102f2010-09-08 11:49:43 -07001018 bool mCurrentInputTargetsValid; // false while targets are being recomputed
1019 Vector<InputTarget> mCurrentInputTargets;
Jeff Brownb88102f2010-09-08 11:49:43 -07001020
1021 enum InputTargetWaitCause {
1022 INPUT_TARGET_WAIT_CAUSE_NONE,
1023 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
1024 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
1025 };
1026
1027 InputTargetWaitCause mInputTargetWaitCause;
1028 nsecs_t mInputTargetWaitStartTime;
1029 nsecs_t mInputTargetWaitTimeoutTime;
1030 bool mInputTargetWaitTimeoutExpired;
Jeff Brown9302c872011-07-13 22:51:29 -07001031 sp<InputApplicationHandle> mInputTargetWaitApplicationHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -07001032
Jeff Browna032cc02011-03-07 16:56:21 -08001033 // Contains the last window which received a hover event.
Jeff Brown9302c872011-07-13 22:51:29 -07001034 sp<InputWindowHandle> mLastHoverWindowHandle;
Jeff Browna032cc02011-03-07 16:56:21 -08001035
Jeff Brownb88102f2010-09-08 11:49:43 -07001036 // Finding targets for input events.
Jeff Brown54a18252010-09-16 14:07:33 -07001037 void resetTargetsLocked();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001038 void commitTargetsLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -07001039 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
Jeff Brown9302c872011-07-13 22:51:29 -07001040 const sp<InputApplicationHandle>& applicationHandle,
1041 const sp<InputWindowHandle>& windowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -07001042 nsecs_t* nextWakeupTime);
Jeff Brown519e0242010-09-15 15:18:56 -07001043 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
1044 const sp<InputChannel>& inputChannel);
1045 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07001046 void resetANRTimeoutsLocked();
1047
Jeff Brown01ce2e92010-09-26 22:20:12 -07001048 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
1049 nsecs_t* nextWakeupTime);
1050 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
Jeff Browna032cc02011-03-07 16:56:21 -08001051 nsecs_t* nextWakeupTime, bool* outConflictingPointerActions,
1052 const MotionSample** outSplitBatchAfterSample);
Jeff Brownb88102f2010-09-08 11:49:43 -07001053
Jeff Brown9302c872011-07-13 22:51:29 -07001054 void addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle,
1055 int32_t targetFlags, BitSet32 pointerIds);
Jeff Brownb88102f2010-09-08 11:49:43 -07001056 void addMonitoringTargetsLocked();
Jeff Browne2fe69e2010-10-18 13:21:23 -07001057 void pokeUserActivityLocked(const EventEntry* eventEntry);
Jeff Brown9302c872011-07-13 22:51:29 -07001058 bool checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
1059 const InjectionState* injectionState);
1060 bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle,
1061 int32_t x, int32_t y) const;
1062 bool isWindowFinishedWithPreviousInputLocked(const sp<InputWindowHandle>& windowHandle);
1063 String8 getApplicationWindowLabelLocked(const sp<InputApplicationHandle>& applicationHandle,
1064 const sp<InputWindowHandle>& windowHandle);
Jeff Brownb88102f2010-09-08 11:49:43 -07001065
Jeff Brown46b9ac02010-04-22 18:58:52 -07001066 // Manage the dispatch cycle for a single connection.
Jeff Brown7fbdc842010-06-17 20:52:56 -07001067 // These methods are deliberately not Interruptible because doing all of the work
1068 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1069 // If needed, the methods post commands to run later once the critical bits are done.
1070 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001071 EventEntry* eventEntry, const InputTarget* inputTarget,
1072 bool resumeWithAppendedMotionSample);
Jeff Browna032cc02011-03-07 16:56:21 -08001073 void enqueueDispatchEntryLocked(const sp<Connection>& connection,
1074 EventEntry* eventEntry, const InputTarget* inputTarget,
1075 bool resumeWithAppendedMotionSample, int32_t dispatchMode);
Jeff Brown519e0242010-09-15 15:18:56 -07001076 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown3915bb82010-11-05 15:02:16 -07001077 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
1078 bool handled);
Jeff Brownb88102f2010-09-08 11:49:43 -07001079 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brownb6997262010-10-08 22:31:17 -07001080 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001081 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07001082 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001083
Jeff Brownb6997262010-10-08 22:31:17 -07001084 void synthesizeCancelationEventsForAllConnectionsLocked(
Jeff Brownda3d5a92011-03-29 15:11:34 -07001085 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001086 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Jeff Brownda3d5a92011-03-29 15:11:34 -07001087 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001088 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Jeff Brownda3d5a92011-03-29 15:11:34 -07001089 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001090
Jeff Brown01ce2e92010-09-26 22:20:12 -07001091 // Splitting motion events across windows.
1092 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1093
Jeff Brown120a4592010-10-27 18:43:51 -07001094 // Reset and drop everything the dispatcher is doing.
1095 void resetAndDropEverythingLocked(const char* reason);
1096
Jeff Brownb88102f2010-09-08 11:49:43 -07001097 // Dump state.
1098 void dumpDispatchStateLocked(String8& dump);
1099 void logDispatchStateLocked();
1100
Jeff Brown46b9ac02010-04-22 18:58:52 -07001101 // Add or remove a connection to the mActiveConnections vector.
1102 void activateConnectionLocked(Connection* connection);
1103 void deactivateConnectionLocked(Connection* connection);
1104
1105 // Interesting events that we might like to log or tell the framework about.
Jeff Brown9c3cda02010-06-15 01:31:58 -07001106 void onDispatchCycleStartedLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001107 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001108 void onDispatchCycleFinishedLocked(
Jeff Brown3915bb82010-11-05 15:02:16 -07001109 nsecs_t currentTime, const sp<Connection>& connection, bool handled);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001110 void onDispatchCycleBrokenLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001111 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001112 void onANRLocked(
Jeff Brown9302c872011-07-13 22:51:29 -07001113 nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
1114 const sp<InputWindowHandle>& windowHandle,
Jeff Brown519e0242010-09-15 15:18:56 -07001115 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001116
Jeff Brown7fbdc842010-06-17 20:52:56 -07001117 // Outbound policy interactions.
Jeff Brownb88102f2010-09-08 11:49:43 -07001118 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001119 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown519e0242010-09-15 15:18:56 -07001120 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07001121 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001122 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001123 bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
1124 DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled);
1125 bool afterMotionEventLockedInterruptible(const sp<Connection>& connection,
1126 DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled);
Jeff Brownb88102f2010-09-08 11:49:43 -07001127 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001128 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -07001129
1130 // Statistics gathering.
1131 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1132 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001133};
1134
1135/* Enqueues and dispatches input events, endlessly. */
1136class InputDispatcherThread : public Thread {
1137public:
1138 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1139 ~InputDispatcherThread();
1140
1141private:
1142 virtual bool threadLoop();
1143
1144 sp<InputDispatcherInterface> mDispatcher;
1145};
1146
1147} // namespace android
1148
Jeff Brownb88102f2010-09-08 11:49:43 -07001149#endif // _UI_INPUT_DISPATCHER_H