blob: 7a2839fbbd3891aca3cac19f4a67552c95a2058b [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 Brown01ce2e92010-09-26 22:20:12 -070029#include <utils/BitSet.h>
Jeff Brown46b9ac0a2010-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 Brown46b9ac0a2010-04-22 18:58:52 -070034
Jeff Brown928e0542011-01-10 11:17:36 -080035#include "InputWindow.h"
36#include "InputApplication.h"
Jeff Brownbe1aa822011-07-27 16:04:54 -070037#include "InputListener.h"
Jeff Brown928e0542011-01-10 11:17:36 -080038
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070039
40namespace android {
41
Jeff Brown9c3cda02010-06-15 01:31:58 -070042/*
Jeff Brown7fbdc842010-06-17 20:52:56 -070043 * Constants used to report the outcome of input event injection.
44 */
45enum {
46 /* (INTERNAL USE ONLY) Specifies that injection is pending and its outcome is unknown. */
47 INPUT_EVENT_INJECTION_PENDING = -1,
48
49 /* Injection succeeded. */
50 INPUT_EVENT_INJECTION_SUCCEEDED = 0,
51
52 /* Injection failed because the injector did not have permission to inject
53 * into the application with input focus. */
54 INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1,
55
56 /* Injection failed because there were no available input targets. */
57 INPUT_EVENT_INJECTION_FAILED = 2,
58
59 /* Injection failed due to a timeout. */
60 INPUT_EVENT_INJECTION_TIMED_OUT = 3
61};
62
Jeff Brown6ec402b2010-07-28 15:48:59 -070063/*
64 * Constants used to determine the input event injection synchronization mode.
65 */
66enum {
67 /* Injection is asynchronous and is assumed always to be successful. */
68 INPUT_EVENT_INJECTION_SYNC_NONE = 0,
69
70 /* Waits for previous events to be dispatched so that the input dispatcher can determine
71 * whether input event injection willbe permitted based on the current input focus.
72 * Does not wait for the input event to finish processing. */
73 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1,
74
75 /* Waits for the input event to be completely processed. */
76 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED = 2,
77};
78
Jeff Brown7fbdc842010-06-17 20:52:56 -070079
80/*
Jeff Brown9c3cda02010-06-15 01:31:58 -070081 * An input target specifies how an input event is to be dispatched to a particular window
82 * including the window's input channel, control flags, a timeout, and an X / Y offset to
83 * be added to input event coordinates to compensate for the absolute position of the
84 * window area.
85 */
86struct InputTarget {
87 enum {
Jeff Brown519e0242010-09-15 15:18:56 -070088 /* This flag indicates that the event is being delivered to a foreground application. */
Jeff Browna032cc02011-03-07 16:56:21 -080089 FLAG_FOREGROUND = 1 << 0,
Jeff Brown9c3cda02010-06-15 01:31:58 -070090
Jeff Brown85a31762010-09-01 17:01:00 -070091 /* This flag indicates that the target of a MotionEvent is partly or wholly
92 * obscured by another visible window above it. The motion event should be
93 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
Jeff Browna032cc02011-03-07 16:56:21 -080094 FLAG_WINDOW_IS_OBSCURED = 1 << 1,
Jeff Brown01ce2e92010-09-26 22:20:12 -070095
96 /* This flag indicates that a motion event is being split across multiple windows. */
Jeff Browna032cc02011-03-07 16:56:21 -080097 FLAG_SPLIT = 1 << 2,
98
Kenny Root7a9db182011-06-02 15:16:05 -070099 /* This flag indicates that the pointer coordinates dispatched to the application
100 * will be zeroed out to avoid revealing information to an application. This is
101 * used in conjunction with FLAG_DISPATCH_AS_OUTSIDE to prevent apps not sharing
102 * the same UID from watching all touches. */
103 FLAG_ZERO_COORDS = 1 << 3,
104
Jeff Browna032cc02011-03-07 16:56:21 -0800105 /* This flag indicates that the event should be sent as is.
106 * Should always be set unless the event is to be transmuted. */
107 FLAG_DISPATCH_AS_IS = 1 << 8,
108
109 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
110 * of the area of this target and so should instead be delivered as an
111 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
112 FLAG_DISPATCH_AS_OUTSIDE = 1 << 9,
113
114 /* This flag indicates that a hover sequence is starting in the given window.
115 * The event is transmuted into ACTION_HOVER_ENTER. */
116 FLAG_DISPATCH_AS_HOVER_ENTER = 1 << 10,
117
118 /* This flag indicates that a hover event happened outside of a window which handled
119 * previous hover events, signifying the end of the current hover sequence for that
120 * window.
121 * The event is transmuted into ACTION_HOVER_ENTER. */
122 FLAG_DISPATCH_AS_HOVER_EXIT = 1 << 11,
123
Jeff Brown98db5fa2011-06-08 15:37:10 -0700124 /* This flag indicates that the event should be canceled.
125 * It is used to transmute ACTION_MOVE into ACTION_CANCEL when a touch slips
126 * outside of a window. */
127 FLAG_DISPATCH_AS_SLIPPERY_EXIT = 1 << 12,
128
129 /* This flag indicates that the event should be dispatched as an initial down.
130 * It is used to transmute ACTION_MOVE into ACTION_DOWN when a touch slips
131 * into a new window. */
132 FLAG_DISPATCH_AS_SLIPPERY_ENTER = 1 << 13,
133
Jeff Browna032cc02011-03-07 16:56:21 -0800134 /* Mask for all dispatch modes. */
135 FLAG_DISPATCH_MASK = FLAG_DISPATCH_AS_IS
136 | FLAG_DISPATCH_AS_OUTSIDE
137 | FLAG_DISPATCH_AS_HOVER_ENTER
Jeff Brown98db5fa2011-06-08 15:37:10 -0700138 | FLAG_DISPATCH_AS_HOVER_EXIT
139 | FLAG_DISPATCH_AS_SLIPPERY_EXIT
140 | FLAG_DISPATCH_AS_SLIPPERY_ENTER,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700141 };
142
143 // The input channel to be targeted.
144 sp<InputChannel> inputChannel;
145
146 // Flags for the input target.
147 int32_t flags;
148
Jeff Brown9c3cda02010-06-15 01:31:58 -0700149 // The x and y offset to add to a MotionEvent as it is delivered.
150 // (ignored for KeyEvents)
151 float xOffset, yOffset;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700152
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400153 // Scaling factor to apply to MotionEvent as it is delivered.
154 // (ignored for KeyEvents)
155 float scaleFactor;
156
Jeff Brown01ce2e92010-09-26 22:20:12 -0700157 // The subset of pointer ids to include in motion events dispatched to this input target
158 // if FLAG_SPLIT is set.
159 BitSet32 pointerIds;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700160};
161
Jeff Brown7fbdc842010-06-17 20:52:56 -0700162
Jeff Brown9c3cda02010-06-15 01:31:58 -0700163/*
Jeff Brown214eaf42011-05-26 19:17:02 -0700164 * Input dispatcher configuration.
165 *
166 * Specifies various options that modify the behavior of the input dispatcher.
167 */
168struct InputDispatcherConfiguration {
169 // The key repeat initial timeout.
170 nsecs_t keyRepeatTimeout;
171
172 // The key repeat inter-key delay.
173 nsecs_t keyRepeatDelay;
174
Jeff Brown214eaf42011-05-26 19:17:02 -0700175 InputDispatcherConfiguration() :
176 keyRepeatTimeout(500 * 1000000LL),
Jeff Brown30802802012-02-03 13:35:13 -0800177 keyRepeatDelay(50 * 1000000LL) { }
Jeff Brown214eaf42011-05-26 19:17:02 -0700178};
179
180
181/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700182 * Input dispatcher policy interface.
183 *
184 * The input reader policy is used by the input reader to interact with the Window Manager
185 * and other system components.
186 *
187 * The actual implementation is partially supported by callbacks into the DVM
188 * via JNI. This interface is also mocked in the unit tests.
189 */
190class InputDispatcherPolicyInterface : public virtual RefBase {
191protected:
192 InputDispatcherPolicyInterface() { }
193 virtual ~InputDispatcherPolicyInterface() { }
194
195public:
196 /* Notifies the system that a configuration change has occurred. */
197 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
198
Jeff Brownb88102f2010-09-08 11:49:43 -0700199 /* Notifies the system that an application is not responding.
200 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
Jeff Brown519e0242010-09-15 15:18:56 -0700201 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -0800202 const sp<InputWindowHandle>& inputWindowHandle) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700203
Jeff Brown9c3cda02010-06-15 01:31:58 -0700204 /* Notifies the system that an input channel is unrecoverably broken. */
Jeff Brown928e0542011-01-10 11:17:36 -0800205 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700206
Jeff Brown214eaf42011-05-26 19:17:02 -0700207 /* Gets the input dispatcher configuration. */
208 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700209
Jeff Brown214eaf42011-05-26 19:17:02 -0700210 /* Returns true if automatic key repeating is enabled. */
211 virtual bool isKeyRepeatEnabled() = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700212
Jeff Brown0029c662011-03-30 02:25:18 -0700213 /* Filters an input event.
214 * Return true to dispatch the event unmodified, false to consume the event.
215 * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED
216 * to injectInputEvent.
217 */
218 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) = 0;
219
Jeff Brownb6997262010-10-08 22:31:17 -0700220 /* Intercepts a key event immediately before queueing it.
221 * The policy can use this method as an opportunity to perform power management functions
222 * and early event preprocessing such as updating policy flags.
223 *
224 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
225 * should be dispatched to applications.
226 */
Jeff Brown1f245102010-11-18 20:53:46 -0800227 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700228
Jeff Brown56194eb2011-03-02 19:23:13 -0800229 /* Intercepts a touch, trackball or other motion event before queueing it.
Jeff Brownb6997262010-10-08 22:31:17 -0700230 * The policy can use this method as an opportunity to perform power management functions
231 * and early event preprocessing such as updating policy flags.
232 *
233 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
234 * should be dispatched to applications.
235 */
Jeff Brown56194eb2011-03-02 19:23:13 -0800236 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700237
Jeff Brownb88102f2010-09-08 11:49:43 -0700238 /* Allows the policy a chance to intercept a key before dispatching. */
Jeff Brown905805a2011-10-12 13:57:59 -0700239 virtual nsecs_t interceptKeyBeforeDispatching(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700240 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
241
Jeff Brown49ed71d2010-12-06 17:13:33 -0800242 /* Allows the policy a chance to perform default processing for an unhandled key.
243 * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */
Jeff Brown928e0542011-01-10 11:17:36 -0800244 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800245 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0;
Jeff Brown3915bb82010-11-05 15:02:16 -0700246
Jeff Brownb6997262010-10-08 22:31:17 -0700247 /* Notifies the policy about switch events.
248 */
249 virtual void notifySwitch(nsecs_t when,
250 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
251
Jeff Brownb88102f2010-09-08 11:49:43 -0700252 /* Poke user activity for an event dispatched to a window. */
Jeff Brown01ce2e92010-09-26 22:20:12 -0700253 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700254
255 /* Checks whether a given application pid/uid has permission to inject input events
256 * into other applications.
257 *
258 * This method is special in that its implementation promises to be non-reentrant and
259 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
260 */
261 virtual bool checkInjectEventsPermissionNonReentrant(
262 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700263};
264
265
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700266/* Notifies the system about input events generated by the input reader.
267 * The dispatcher is expected to be mostly asynchronous. */
Jeff Brownbe1aa822011-07-27 16:04:54 -0700268class InputDispatcherInterface : public virtual RefBase, public InputListenerInterface {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700269protected:
270 InputDispatcherInterface() { }
271 virtual ~InputDispatcherInterface() { }
272
273public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700274 /* Dumps the state of the input dispatcher.
275 *
276 * This method may be called on any thread (usually by the input manager). */
277 virtual void dump(String8& dump) = 0;
278
Jeff Brown89ef0722011-08-10 16:25:21 -0700279 /* Called by the heatbeat to ensures that the dispatcher has not deadlocked. */
280 virtual void monitor() = 0;
281
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700282 /* Runs a single iteration of the dispatch loop.
283 * Nominally processes one queued event, a timeout, or a response from an input consumer.
284 *
285 * This method should only be called on the input dispatcher thread.
286 */
287 virtual void dispatchOnce() = 0;
288
Jeff Brown7fbdc842010-06-17 20:52:56 -0700289 /* Injects an input event and optionally waits for sync.
Jeff Brown6ec402b2010-07-28 15:48:59 -0700290 * The synchronization mode determines whether the method blocks while waiting for
291 * input injection to proceed.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700292 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
293 *
294 * This method may be called on any thread (usually by the input manager).
295 */
296 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown0029c662011-03-30 02:25:18 -0700297 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
298 uint32_t policyFlags) = 0;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700299
Jeff Brownb88102f2010-09-08 11:49:43 -0700300 /* Sets the list of input windows.
301 *
302 * This method may be called on any thread (usually by the input manager).
303 */
Jeff Brown9302c872011-07-13 22:51:29 -0700304 virtual void setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700305
306 /* Sets the focused application.
307 *
308 * This method may be called on any thread (usually by the input manager).
309 */
Jeff Brown9302c872011-07-13 22:51:29 -0700310 virtual void setFocusedApplication(
311 const sp<InputApplicationHandle>& inputApplicationHandle) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700312
313 /* Sets the input dispatching mode.
314 *
315 * This method may be called on any thread (usually by the input manager).
316 */
317 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
318
Jeff Brown0029c662011-03-30 02:25:18 -0700319 /* Sets whether input event filtering is enabled.
320 * When enabled, incoming input events are sent to the policy's filterInputEvent
321 * method instead of being dispatched. The filter is expected to use
322 * injectInputEvent to inject the events it would like to have dispatched.
323 * It should include POLICY_FLAG_FILTERED in the policy flags during injection.
324 */
325 virtual void setInputFilterEnabled(bool enabled) = 0;
326
Jeff Browne6504122010-09-27 14:52:15 -0700327 /* Transfers touch focus from the window associated with one channel to the
328 * window associated with the other channel.
329 *
330 * Returns true on success. False if the window did not actually have touch focus.
331 */
332 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
333 const sp<InputChannel>& toChannel) = 0;
334
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700335 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Brownb88102f2010-09-08 11:49:43 -0700336 * If monitor is true, the channel will receive a copy of all input events.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700337 *
338 * These methods may be called on any thread (usually by the input manager).
339 */
Jeff Brown928e0542011-01-10 11:17:36 -0800340 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
341 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700342 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
343};
344
Jeff Brown9c3cda02010-06-15 01:31:58 -0700345/* Dispatches events to input targets. Some functions of the input dispatcher, such as
346 * identifying input targets, are controlled by a separate policy object.
347 *
348 * IMPORTANT INVARIANT:
349 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
350 * the input dispatcher never calls into the policy while holding its internal locks.
351 * The implementation is also carefully designed to recover from scenarios such as an
352 * input channel becoming unregistered while identifying input targets or processing timeouts.
353 *
354 * Methods marked 'Locked' must be called with the lock acquired.
355 *
356 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
357 * may during the course of their execution release the lock, call into the policy, and
358 * then reacquire the lock. The caller is responsible for recovering gracefully.
359 *
360 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
361 */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700362class InputDispatcher : public InputDispatcherInterface {
363protected:
364 virtual ~InputDispatcher();
365
366public:
Jeff Brown9c3cda02010-06-15 01:31:58 -0700367 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700368
Jeff Brownb88102f2010-09-08 11:49:43 -0700369 virtual void dump(String8& dump);
Jeff Brown89ef0722011-08-10 16:25:21 -0700370 virtual void monitor();
Jeff Brownb88102f2010-09-08 11:49:43 -0700371
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700372 virtual void dispatchOnce();
373
Jeff Brownbe1aa822011-07-27 16:04:54 -0700374 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args);
375 virtual void notifyKey(const NotifyKeyArgs* args);
376 virtual void notifyMotion(const NotifyMotionArgs* args);
377 virtual void notifySwitch(const NotifySwitchArgs* args);
Jeff Brown65fd2512011-08-18 11:20:58 -0700378 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700379
Jeff Brown7fbdc842010-06-17 20:52:56 -0700380 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown0029c662011-03-30 02:25:18 -0700381 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
382 uint32_t policyFlags);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700383
Jeff Brown9302c872011-07-13 22:51:29 -0700384 virtual void setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles);
385 virtual void setFocusedApplication(const sp<InputApplicationHandle>& inputApplicationHandle);
Jeff Brownb88102f2010-09-08 11:49:43 -0700386 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown0029c662011-03-30 02:25:18 -0700387 virtual void setInputFilterEnabled(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700388
Jeff Browne6504122010-09-27 14:52:15 -0700389 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
390 const sp<InputChannel>& toChannel);
391
Jeff Brown928e0542011-01-10 11:17:36 -0800392 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
393 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700394 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
395
396private:
397 template <typename T>
398 struct Link {
399 T* next;
400 T* prev;
Jeff Brown3241b6b2012-02-03 15:08:02 -0800401
402 protected:
403 inline Link() : next(NULL), prev(NULL) { }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700404 };
405
Jeff Brown01ce2e92010-09-26 22:20:12 -0700406 struct InjectionState {
407 mutable int32_t refCount;
408
409 int32_t injectorPid;
410 int32_t injectorUid;
411 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
412 bool injectionIsAsync; // set to true if injection is not waiting for the result
413 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
Jeff Brownac386072011-07-20 15:19:50 -0700414
415 InjectionState(int32_t injectorPid, int32_t injectorUid);
416 void release();
417
418 private:
419 ~InjectionState();
Jeff Brown01ce2e92010-09-26 22:20:12 -0700420 };
421
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700422 struct EventEntry : Link<EventEntry> {
423 enum {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700424 TYPE_CONFIGURATION_CHANGED,
Jeff Brown65fd2512011-08-18 11:20:58 -0700425 TYPE_DEVICE_RESET,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700426 TYPE_KEY,
427 TYPE_MOTION
428 };
429
Jeff Brown01ce2e92010-09-26 22:20:12 -0700430 mutable int32_t refCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700431 int32_t type;
432 nsecs_t eventTime;
Jeff Brownb6997262010-10-08 22:31:17 -0700433 uint32_t policyFlags;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700434 InjectionState* injectionState;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700435
Jeff Brown9c3cda02010-06-15 01:31:58 -0700436 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown7fbdc842010-06-17 20:52:56 -0700437
Jeff Brown4e91a182011-04-07 11:38:09 -0700438 inline bool isInjected() const { return injectionState != NULL; }
Jeff Brownac386072011-07-20 15:19:50 -0700439
440 void release();
441
442 protected:
443 EventEntry(int32_t type, nsecs_t eventTime, uint32_t policyFlags);
444 virtual ~EventEntry();
445 void releaseInjectionState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700446 };
447
448 struct ConfigurationChangedEntry : EventEntry {
Jeff Brownac386072011-07-20 15:19:50 -0700449 ConfigurationChangedEntry(nsecs_t eventTime);
450
451 protected:
452 virtual ~ConfigurationChangedEntry();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700453 };
454
Jeff Brown65fd2512011-08-18 11:20:58 -0700455 struct DeviceResetEntry : EventEntry {
456 int32_t deviceId;
457
458 DeviceResetEntry(nsecs_t eventTime, int32_t deviceId);
459
460 protected:
461 virtual ~DeviceResetEntry();
462 };
463
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700464 struct KeyEntry : EventEntry {
465 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800466 uint32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700467 int32_t action;
468 int32_t flags;
469 int32_t keyCode;
470 int32_t scanCode;
471 int32_t metaState;
472 int32_t repeatCount;
473 nsecs_t downTime;
Jeff Brownb88102f2010-09-08 11:49:43 -0700474
475 bool syntheticRepeat; // set to true for synthetic key repeats
476
477 enum InterceptKeyResult {
478 INTERCEPT_KEY_RESULT_UNKNOWN,
479 INTERCEPT_KEY_RESULT_SKIP,
480 INTERCEPT_KEY_RESULT_CONTINUE,
Jeff Brown905805a2011-10-12 13:57:59 -0700481 INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER,
Jeff Brownb88102f2010-09-08 11:49:43 -0700482 };
483 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Brown905805a2011-10-12 13:57:59 -0700484 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
Jeff Brownac386072011-07-20 15:19:50 -0700485
486 KeyEntry(nsecs_t eventTime,
487 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
488 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
489 int32_t repeatCount, nsecs_t downTime);
490 void recycle();
491
492 protected:
493 virtual ~KeyEntry();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700494 };
495
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700496 struct MotionEntry : EventEntry {
Jeff Brown3241b6b2012-02-03 15:08:02 -0800497 nsecs_t eventTime;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700498 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800499 uint32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700500 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -0700501 int32_t flags;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700502 int32_t metaState;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700503 int32_t buttonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700504 int32_t edgeFlags;
505 float xPrecision;
506 float yPrecision;
507 nsecs_t downTime;
508 uint32_t pointerCount;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700509 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brown3241b6b2012-02-03 15:08:02 -0800510 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownae9fc032010-08-18 15:51:08 -0700511
Jeff Brownac386072011-07-20 15:19:50 -0700512 MotionEntry(nsecs_t eventTime,
513 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
514 int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
515 float xPrecision, float yPrecision,
516 nsecs_t downTime, uint32_t pointerCount,
517 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords);
518
Jeff Brownac386072011-07-20 15:19:50 -0700519 protected:
520 virtual ~MotionEntry();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700521 };
522
Jeff Brown9c3cda02010-06-15 01:31:58 -0700523 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700524 struct DispatchEntry : Link<DispatchEntry> {
525 EventEntry* eventEntry; // the event to dispatch
526 int32_t targetFlags;
527 float xOffset;
528 float yOffset;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400529 float scaleFactor;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700530
531 // True if dispatch has started.
532 bool inProgress;
533
Jeff Brown81346812011-06-28 20:08:48 -0700534 // Set to the resolved action and flags when the event is enqueued.
535 int32_t resolvedAction;
536 int32_t resolvedFlags;
537
Jeff Brownac386072011-07-20 15:19:50 -0700538 DispatchEntry(EventEntry* eventEntry,
539 int32_t targetFlags, float xOffset, float yOffset, float scaleFactor);
540 ~DispatchEntry();
541
Jeff Brown519e0242010-09-15 15:18:56 -0700542 inline bool hasForegroundTarget() const {
543 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Brownb88102f2010-09-08 11:49:43 -0700544 }
Jeff Brown01ce2e92010-09-26 22:20:12 -0700545
546 inline bool isSplit() const {
547 return targetFlags & InputTarget::FLAG_SPLIT;
548 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700549 };
550
Jeff Brown9c3cda02010-06-15 01:31:58 -0700551 // A command entry captures state and behavior for an action to be performed in the
552 // dispatch loop after the initial processing has taken place. It is essentially
553 // a kind of continuation used to postpone sensitive policy interactions to a point
554 // in the dispatch loop where it is safe to release the lock (generally after finishing
555 // the critical parts of the dispatch cycle).
556 //
557 // The special thing about commands is that they can voluntarily release and reacquire
558 // the dispatcher lock at will. Initially when the command starts running, the
559 // dispatcher lock is held. However, if the command needs to call into the policy to
560 // do some work, it can release the lock, do the work, then reacquire the lock again
561 // before returning.
562 //
563 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
564 // never calls into the policy while holding its lock.
565 //
566 // Commands are implicitly 'LockedInterruptible'.
567 struct CommandEntry;
568 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
569
Jeff Brown7fbdc842010-06-17 20:52:56 -0700570 class Connection;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700571 struct CommandEntry : Link<CommandEntry> {
Jeff Brownac386072011-07-20 15:19:50 -0700572 CommandEntry(Command command);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700573 ~CommandEntry();
574
575 Command command;
576
577 // parameters for the command (usage varies by command)
Jeff Brown7fbdc842010-06-17 20:52:56 -0700578 sp<Connection> connection;
Jeff Brownb88102f2010-09-08 11:49:43 -0700579 nsecs_t eventTime;
580 KeyEntry* keyEntry;
Jeff Brownb88102f2010-09-08 11:49:43 -0700581 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800582 sp<InputWindowHandle> inputWindowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700583 int32_t userActivityEventType;
Jeff Brown3915bb82010-11-05 15:02:16 -0700584 bool handled;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700585 };
586
587 // Generic queue implementation.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700588 template <typename T>
589 struct Queue {
Jeff Brownac386072011-07-20 15:19:50 -0700590 T* head;
591 T* tail;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700592
Jeff Brownac386072011-07-20 15:19:50 -0700593 inline Queue() : head(NULL), tail(NULL) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700594 }
595
Jeff Brownb88102f2010-09-08 11:49:43 -0700596 inline bool isEmpty() const {
Jeff Brownac386072011-07-20 15:19:50 -0700597 return !head;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700598 }
599
600 inline void enqueueAtTail(T* entry) {
Jeff Brownac386072011-07-20 15:19:50 -0700601 entry->prev = tail;
602 if (tail) {
603 tail->next = entry;
604 } else {
605 head = entry;
606 }
607 entry->next = NULL;
608 tail = entry;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700609 }
610
611 inline void enqueueAtHead(T* entry) {
Jeff Brownac386072011-07-20 15:19:50 -0700612 entry->next = head;
613 if (head) {
614 head->prev = entry;
615 } else {
616 tail = entry;
617 }
618 entry->prev = NULL;
619 head = entry;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700620 }
621
622 inline void dequeue(T* entry) {
Jeff Brownac386072011-07-20 15:19:50 -0700623 if (entry->prev) {
624 entry->prev->next = entry->next;
625 } else {
626 head = entry->next;
627 }
628 if (entry->next) {
629 entry->next->prev = entry->prev;
630 } else {
631 tail = entry->prev;
632 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700633 }
634
635 inline T* dequeueAtHead() {
Jeff Brownac386072011-07-20 15:19:50 -0700636 T* entry = head;
637 head = entry->next;
638 if (head) {
639 head->prev = NULL;
640 } else {
641 tail = NULL;
642 }
643 return entry;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700644 }
Jeff Brown519e0242010-09-15 15:18:56 -0700645
646 uint32_t count() const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700647 };
648
Jeff Brownda3d5a92011-03-29 15:11:34 -0700649 /* Specifies which events are to be canceled and why. */
650 struct CancelationOptions {
651 enum Mode {
Jeff Brownb6997262010-10-08 22:31:17 -0700652 CANCEL_ALL_EVENTS = 0,
653 CANCEL_POINTER_EVENTS = 1,
654 CANCEL_NON_POINTER_EVENTS = 2,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800655 CANCEL_FALLBACK_EVENTS = 3,
Jeff Brownb6997262010-10-08 22:31:17 -0700656 };
657
Jeff Brownda3d5a92011-03-29 15:11:34 -0700658 // The criterion to use to determine which events should be canceled.
659 Mode mode;
660
661 // Descriptive reason for the cancelation.
662 const char* reason;
663
664 // The specific keycode of the key event to cancel, or -1 to cancel any key event.
665 int32_t keyCode;
666
Jeff Brown65fd2512011-08-18 11:20:58 -0700667 // The specific device id of events to cancel, or -1 to cancel events from any device.
668 int32_t deviceId;
669
Jeff Brownda3d5a92011-03-29 15:11:34 -0700670 CancelationOptions(Mode mode, const char* reason) :
Jeff Brown65fd2512011-08-18 11:20:58 -0700671 mode(mode), reason(reason), keyCode(-1), deviceId(-1) { }
Jeff Brownda3d5a92011-03-29 15:11:34 -0700672 };
673
674 /* Tracks dispatched key and motion event state so that cancelation events can be
675 * synthesized when events are dropped. */
676 class InputState {
677 public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700678 InputState();
679 ~InputState();
680
681 // Returns true if there is no state to be canceled.
682 bool isNeutral() const;
683
Jeff Brown81346812011-06-28 20:08:48 -0700684 // Returns true if the specified source is known to have received a hover enter
685 // motion event.
686 bool isHovering(int32_t deviceId, uint32_t source) const;
Jeff Brownb88102f2010-09-08 11:49:43 -0700687
688 // Records tracking information for a key event that has just been published.
Jeff Brown81346812011-06-28 20:08:48 -0700689 // Returns true if the event should be delivered, false if it is inconsistent
690 // and should be skipped.
691 bool trackKey(const KeyEntry* entry, int32_t action, int32_t flags);
Jeff Brownb88102f2010-09-08 11:49:43 -0700692
693 // Records tracking information for a motion event that has just been published.
Jeff Brown81346812011-06-28 20:08:48 -0700694 // Returns true if the event should be delivered, false if it is inconsistent
695 // and should be skipped.
696 bool trackMotion(const MotionEntry* entry, int32_t action, int32_t flags);
Jeff Brownb88102f2010-09-08 11:49:43 -0700697
Jeff Brownb6997262010-10-08 22:31:17 -0700698 // Synthesizes cancelation events for the current state and resets the tracked state.
Jeff Brownac386072011-07-20 15:19:50 -0700699 void synthesizeCancelationEvents(nsecs_t currentTime,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700700 Vector<EventEntry*>& outEvents, const CancelationOptions& options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700701
702 // Clears the current state.
703 void clear();
704
Jeff Brown9c9f1a32010-10-11 18:32:20 -0700705 // Copies pointer-related parts of the input state to another instance.
706 void copyPointerStateTo(InputState& other) const;
707
Jeff Brownda3d5a92011-03-29 15:11:34 -0700708 // Gets the fallback key associated with a keycode.
709 // Returns -1 if none.
710 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
711 int32_t getFallbackKey(int32_t originalKeyCode);
712
713 // Sets the fallback key for a particular keycode.
714 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
715
716 // Removes the fallback key for a particular keycode.
717 void removeFallbackKey(int32_t originalKeyCode);
718
719 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const {
720 return mFallbackKeys;
721 }
722
Jeff Brownb88102f2010-09-08 11:49:43 -0700723 private:
Jeff Brownb88102f2010-09-08 11:49:43 -0700724 struct KeyMemento {
725 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800726 uint32_t source;
Jeff Brownb88102f2010-09-08 11:49:43 -0700727 int32_t keyCode;
728 int32_t scanCode;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800729 int32_t flags;
Jeff Brownb88102f2010-09-08 11:49:43 -0700730 nsecs_t downTime;
731 };
732
733 struct MotionMemento {
734 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800735 uint32_t source;
Jeff Brown81346812011-06-28 20:08:48 -0700736 int32_t flags;
Jeff Brownb88102f2010-09-08 11:49:43 -0700737 float xPrecision;
738 float yPrecision;
739 nsecs_t downTime;
740 uint32_t pointerCount;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700741 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownb88102f2010-09-08 11:49:43 -0700742 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Browna032cc02011-03-07 16:56:21 -0800743 bool hovering;
Jeff Brownb88102f2010-09-08 11:49:43 -0700744
745 void setPointers(const MotionEntry* entry);
746 };
747
748 Vector<KeyMemento> mKeyMementos;
749 Vector<MotionMemento> mMotionMementos;
Jeff Brownda3d5a92011-03-29 15:11:34 -0700750 KeyedVector<int32_t, int32_t> mFallbackKeys;
Jeff Brownb6997262010-10-08 22:31:17 -0700751
Jeff Brown81346812011-06-28 20:08:48 -0700752 ssize_t findKeyMemento(const KeyEntry* entry) const;
753 ssize_t findMotionMemento(const MotionEntry* entry, bool hovering) const;
754
755 void addKeyMemento(const KeyEntry* entry, int32_t flags);
756 void addMotionMemento(const MotionEntry* entry, int32_t flags, bool hovering);
757
Jeff Brown49ed71d2010-12-06 17:13:33 -0800758 static bool shouldCancelKey(const KeyMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700759 const CancelationOptions& options);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800760 static bool shouldCancelMotion(const MotionMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700761 const CancelationOptions& options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700762 };
763
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700764 /* Manages the dispatch state associated with a single input channel. */
765 class Connection : public RefBase {
766 protected:
767 virtual ~Connection();
768
769 public:
770 enum Status {
771 // Everything is peachy.
772 STATUS_NORMAL,
773 // An unrecoverable communication error has occurred.
774 STATUS_BROKEN,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700775 // The input channel has been unregistered.
776 STATUS_ZOMBIE
777 };
778
779 Status status;
Jeff Brown928e0542011-01-10 11:17:36 -0800780 sp<InputChannel> inputChannel; // never null
781 sp<InputWindowHandle> inputWindowHandle; // may be null
Jeff Browncc4f7db2011-08-30 20:34:48 -0700782 bool monitor;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700783 InputPublisher inputPublisher;
Jeff Brownb88102f2010-09-08 11:49:43 -0700784 InputState inputState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700785 Queue<DispatchEntry> outboundQueue;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700786
787 nsecs_t lastEventTime; // the time when the event was originally captured
788 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700789
Jeff Brown928e0542011-01-10 11:17:36 -0800790 explicit Connection(const sp<InputChannel>& inputChannel,
Jeff Browncc4f7db2011-08-30 20:34:48 -0700791 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700792
Jeff Brown9c3cda02010-06-15 01:31:58 -0700793 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
794
795 const char* getStatusLabel() const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700796
797 // Finds a DispatchEntry in the outbound queue associated with the specified event.
798 // Returns NULL if not found.
799 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
800
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700801 // Gets the time since the current event was originally obtained from the input driver.
Jeff Brownb88102f2010-09-08 11:49:43 -0700802 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700803 return (currentTime - lastEventTime) / 1000000.0;
804 }
805
806 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Brownb88102f2010-09-08 11:49:43 -0700807 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700808 return (currentTime - lastDispatchTime) / 1000000.0;
809 }
810
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700811 status_t initialize();
812 };
813
Jeff Brownb6997262010-10-08 22:31:17 -0700814 enum DropReason {
815 DROP_REASON_NOT_DROPPED = 0,
816 DROP_REASON_POLICY = 1,
817 DROP_REASON_APP_SWITCH = 2,
818 DROP_REASON_DISABLED = 3,
Jeff Brown928e0542011-01-10 11:17:36 -0800819 DROP_REASON_BLOCKED = 4,
820 DROP_REASON_STALE = 5,
Jeff Brownb6997262010-10-08 22:31:17 -0700821 };
822
Jeff Brown9c3cda02010-06-15 01:31:58 -0700823 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Brown214eaf42011-05-26 19:17:02 -0700824 InputDispatcherConfiguration mConfig;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700825
826 Mutex mLock;
827
Jeff Brown112b5f52012-01-27 17:32:06 -0800828 Condition mDispatcherIsAliveCondition;
829
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700830 sp<Looper> mLooper;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700831
Jeff Brownb88102f2010-09-08 11:49:43 -0700832 EventEntry* mPendingEvent;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700833 Queue<EventEntry> mInboundQueue;
834 Queue<CommandEntry> mCommandQueue;
835
Jeff Brownb88102f2010-09-08 11:49:43 -0700836 Vector<EventEntry*> mTempCancelationEvents;
837
Jeff Brown214eaf42011-05-26 19:17:02 -0700838 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700839 void dispatchIdleLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700840
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700841 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Brownb88102f2010-09-08 11:49:43 -0700842 bool enqueueInboundEventLocked(EventEntry* entry);
843
Jeff Brownb6997262010-10-08 22:31:17 -0700844 // Cleans up input state when dropping an inbound event.
845 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
846
Jeff Brownb88102f2010-09-08 11:49:43 -0700847 // App switch latency optimization.
Jeff Brownb6997262010-10-08 22:31:17 -0700848 bool mAppSwitchSawKeyDown;
Jeff Brownb88102f2010-09-08 11:49:43 -0700849 nsecs_t mAppSwitchDueTime;
850
Jeff Brownb6997262010-10-08 22:31:17 -0700851 static bool isAppSwitchKeyCode(int32_t keyCode);
852 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700853 bool isAppSwitchPendingLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700854 void resetPendingAppSwitchLocked(bool handled);
855
Jeff Brown928e0542011-01-10 11:17:36 -0800856 // Stale event latency optimization.
857 static bool isStaleEventLocked(nsecs_t currentTime, EventEntry* entry);
858
859 // Blocked event latency optimization. Drops old events when the user intends
860 // to transfer focus to a new application.
861 EventEntry* mNextUnblockedEvent;
862
Jeff Brown9302c872011-07-13 22:51:29 -0700863 sp<InputWindowHandle> findTouchedWindowAtLocked(int32_t x, int32_t y);
Jeff Brown928e0542011-01-10 11:17:36 -0800864
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700865 // All registered connections mapped by receive pipe file descriptor.
866 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
867
Jeff Brown519e0242010-09-15 15:18:56 -0700868 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown2cbecea2010-08-17 15:59:26 -0700869
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700870 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700871 // We don't use a ref-counted pointer here because we explicitly abort connections
872 // during unregistration which causes the connection's outbound queue to be cleared
873 // and the connection itself to be deactivated.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700874 Vector<Connection*> mActiveConnections;
875
Jeff Brownb88102f2010-09-08 11:49:43 -0700876 // Input channels that will receive a copy of all input events.
877 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700878
Jeff Brown7fbdc842010-06-17 20:52:56 -0700879 // Event injection and synchronization.
880 Condition mInjectionResultAvailableCondition;
Jeff Brownb6997262010-10-08 22:31:17 -0700881 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700882 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
883
Jeff Brown6ec402b2010-07-28 15:48:59 -0700884 Condition mInjectionSyncFinishedCondition;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700885 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -0700886 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown6ec402b2010-07-28 15:48:59 -0700887
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700888 // Key repeat tracking.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700889 struct KeyRepeatState {
890 KeyEntry* lastKeyEntry; // or null if no repeat
891 nsecs_t nextRepeatTime;
892 } mKeyRepeatState;
893
894 void resetKeyRepeatLocked();
Jeff Brown214eaf42011-05-26 19:17:02 -0700895 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700896
Jeff Brown9c3cda02010-06-15 01:31:58 -0700897 // Deferred command processing.
898 bool runCommandsLockedInterruptible();
899 CommandEntry* postCommandLocked(Command command);
900
Jeff Brownb88102f2010-09-08 11:49:43 -0700901 // Inbound event processing.
902 void drainInboundQueueLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700903 void releasePendingEventLocked();
904 void releaseInboundEventLocked(EventEntry* entry);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700905
Jeff Brownb88102f2010-09-08 11:49:43 -0700906 // Dispatch state.
907 bool mDispatchEnabled;
908 bool mDispatchFrozen;
Jeff Brown0029c662011-03-30 02:25:18 -0700909 bool mInputFilterEnabled;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700910
Jeff Brown9302c872011-07-13 22:51:29 -0700911 Vector<sp<InputWindowHandle> > mWindowHandles;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700912
Jeff Brown9302c872011-07-13 22:51:29 -0700913 sp<InputWindowHandle> getWindowHandleLocked(const sp<InputChannel>& inputChannel) const;
914 bool hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const;
Jeff Brownb88102f2010-09-08 11:49:43 -0700915
916 // Focus tracking for keys, trackball, etc.
Jeff Brown9302c872011-07-13 22:51:29 -0700917 sp<InputWindowHandle> mFocusedWindowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700918
919 // Focus tracking for touch.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700920 struct TouchedWindow {
Jeff Brown9302c872011-07-13 22:51:29 -0700921 sp<InputWindowHandle> windowHandle;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700922 int32_t targetFlags;
Jeff Brown46e75292010-11-10 16:53:45 -0800923 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
Jeff Brownb88102f2010-09-08 11:49:43 -0700924 };
Jeff Brown01ce2e92010-09-26 22:20:12 -0700925 struct TouchState {
926 bool down;
927 bool split;
Jeff Brown95712852011-01-04 19:41:59 -0800928 int32_t deviceId; // id of the device that is currently down, others are rejected
Jeff Brown58a2da82011-01-25 16:02:22 -0800929 uint32_t source; // source of the device that is current down, others are rejected
Jeff Brown01ce2e92010-09-26 22:20:12 -0700930 Vector<TouchedWindow> windows;
931
932 TouchState();
933 ~TouchState();
934 void reset();
935 void copyFrom(const TouchState& other);
Jeff Brown9302c872011-07-13 22:51:29 -0700936 void addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle,
937 int32_t targetFlags, BitSet32 pointerIds);
Jeff Browna032cc02011-03-07 16:56:21 -0800938 void filterNonAsIsTouchWindows();
Jeff Brown9302c872011-07-13 22:51:29 -0700939 sp<InputWindowHandle> getFirstForegroundWindowHandle() const;
Jeff Brown98db5fa2011-06-08 15:37:10 -0700940 bool isSlippery() const;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700941 };
942
943 TouchState mTouchState;
944 TouchState mTempTouchState;
Jeff Brownb88102f2010-09-08 11:49:43 -0700945
946 // Focused application.
Jeff Brown9302c872011-07-13 22:51:29 -0700947 sp<InputApplicationHandle> mFocusedApplicationHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700948
949 // Dispatch inbound events.
950 bool dispatchConfigurationChangedLocked(
951 nsecs_t currentTime, ConfigurationChangedEntry* entry);
Jeff Brown65fd2512011-08-18 11:20:58 -0700952 bool dispatchDeviceResetLocked(
953 nsecs_t currentTime, DeviceResetEntry* entry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700954 bool dispatchKeyLocked(
Jeff Brown214eaf42011-05-26 19:17:02 -0700955 nsecs_t currentTime, KeyEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700956 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700957 bool dispatchMotionLocked(
958 nsecs_t currentTime, MotionEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700959 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brown3241b6b2012-02-03 15:08:02 -0800960 void dispatchEventToCurrentInputTargetsLocked(nsecs_t currentTime, EventEntry* entry);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700961
Jeff Brownb88102f2010-09-08 11:49:43 -0700962 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
963 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
964
965 // The input targets that were most recently identified for dispatch.
Jeff Brownb88102f2010-09-08 11:49:43 -0700966 bool mCurrentInputTargetsValid; // false while targets are being recomputed
967 Vector<InputTarget> mCurrentInputTargets;
Jeff Brownb88102f2010-09-08 11:49:43 -0700968
969 enum InputTargetWaitCause {
970 INPUT_TARGET_WAIT_CAUSE_NONE,
971 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
972 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
973 };
974
975 InputTargetWaitCause mInputTargetWaitCause;
976 nsecs_t mInputTargetWaitStartTime;
977 nsecs_t mInputTargetWaitTimeoutTime;
978 bool mInputTargetWaitTimeoutExpired;
Jeff Brown9302c872011-07-13 22:51:29 -0700979 sp<InputApplicationHandle> mInputTargetWaitApplicationHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700980
Jeff Browna032cc02011-03-07 16:56:21 -0800981 // Contains the last window which received a hover event.
Jeff Brown9302c872011-07-13 22:51:29 -0700982 sp<InputWindowHandle> mLastHoverWindowHandle;
Jeff Browna032cc02011-03-07 16:56:21 -0800983
Jeff Brownb88102f2010-09-08 11:49:43 -0700984 // Finding targets for input events.
Jeff Brown54a18252010-09-16 14:07:33 -0700985 void resetTargetsLocked();
Jeff Brown01ce2e92010-09-26 22:20:12 -0700986 void commitTargetsLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700987 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
Jeff Brown9302c872011-07-13 22:51:29 -0700988 const sp<InputApplicationHandle>& applicationHandle,
989 const sp<InputWindowHandle>& windowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700990 nsecs_t* nextWakeupTime);
Jeff Brown519e0242010-09-15 15:18:56 -0700991 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
992 const sp<InputChannel>& inputChannel);
993 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700994 void resetANRTimeoutsLocked();
995
Jeff Brown01ce2e92010-09-26 22:20:12 -0700996 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
997 nsecs_t* nextWakeupTime);
998 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
Jeff Brown3241b6b2012-02-03 15:08:02 -0800999 nsecs_t* nextWakeupTime, bool* outConflictingPointerActions);
Jeff Brownb88102f2010-09-08 11:49:43 -07001000
Jeff Brown9302c872011-07-13 22:51:29 -07001001 void addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle,
1002 int32_t targetFlags, BitSet32 pointerIds);
Jeff Brownb88102f2010-09-08 11:49:43 -07001003 void addMonitoringTargetsLocked();
Jeff Browne2fe69e2010-10-18 13:21:23 -07001004 void pokeUserActivityLocked(const EventEntry* eventEntry);
Jeff Brown9302c872011-07-13 22:51:29 -07001005 bool checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
1006 const InjectionState* injectionState);
1007 bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle,
1008 int32_t x, int32_t y) const;
1009 bool isWindowFinishedWithPreviousInputLocked(const sp<InputWindowHandle>& windowHandle);
1010 String8 getApplicationWindowLabelLocked(const sp<InputApplicationHandle>& applicationHandle,
1011 const sp<InputWindowHandle>& windowHandle);
Jeff Brownb88102f2010-09-08 11:49:43 -07001012
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001013 // Manage the dispatch cycle for a single connection.
Jeff Brown7fbdc842010-06-17 20:52:56 -07001014 // These methods are deliberately not Interruptible because doing all of the work
1015 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1016 // If needed, the methods post commands to run later once the critical bits are done.
1017 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001018 EventEntry* eventEntry, const InputTarget* inputTarget);
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001019 void enqueueDispatchEntriesLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001020 EventEntry* eventEntry, const InputTarget* inputTarget);
Jeff Browna032cc02011-03-07 16:56:21 -08001021 void enqueueDispatchEntryLocked(const sp<Connection>& connection,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001022 EventEntry* eventEntry, const InputTarget* inputTarget, int32_t dispatchMode);
Jeff Brown519e0242010-09-15 15:18:56 -07001023 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown3915bb82010-11-05 15:02:16 -07001024 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
1025 bool handled);
Jeff Brownb88102f2010-09-08 11:49:43 -07001026 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001027 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
1028 bool notify);
Jeff Brown519e0242010-09-15 15:18:56 -07001029 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07001030 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001031
Jeff Brownb6997262010-10-08 22:31:17 -07001032 void synthesizeCancelationEventsForAllConnectionsLocked(
Jeff Brownda3d5a92011-03-29 15:11:34 -07001033 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001034 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Jeff Brownda3d5a92011-03-29 15:11:34 -07001035 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001036 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Jeff Brownda3d5a92011-03-29 15:11:34 -07001037 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001038
Jeff Brown01ce2e92010-09-26 22:20:12 -07001039 // Splitting motion events across windows.
1040 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1041
Jeff Brown120a4592010-10-27 18:43:51 -07001042 // Reset and drop everything the dispatcher is doing.
1043 void resetAndDropEverythingLocked(const char* reason);
1044
Jeff Brownb88102f2010-09-08 11:49:43 -07001045 // Dump state.
1046 void dumpDispatchStateLocked(String8& dump);
1047 void logDispatchStateLocked();
1048
Jeff Browncc4f7db2011-08-30 20:34:48 -07001049 // Registration.
1050 void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel);
1051 status_t unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, bool notify);
1052
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001053 // Add or remove a connection to the mActiveConnections vector.
1054 void activateConnectionLocked(Connection* connection);
1055 void deactivateConnectionLocked(Connection* connection);
1056
1057 // Interesting events that we might like to log or tell the framework about.
Jeff Brown9c3cda02010-06-15 01:31:58 -07001058 void onDispatchCycleStartedLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001059 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001060 void onDispatchCycleFinishedLocked(
Jeff Brown3915bb82010-11-05 15:02:16 -07001061 nsecs_t currentTime, const sp<Connection>& connection, bool handled);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001062 void onDispatchCycleBrokenLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001063 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001064 void onANRLocked(
Jeff Brown9302c872011-07-13 22:51:29 -07001065 nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
1066 const sp<InputWindowHandle>& windowHandle,
Jeff Brown519e0242010-09-15 15:18:56 -07001067 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001068
Jeff Brown7fbdc842010-06-17 20:52:56 -07001069 // Outbound policy interactions.
Jeff Brownb88102f2010-09-08 11:49:43 -07001070 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001071 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown519e0242010-09-15 15:18:56 -07001072 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07001073 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001074 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001075 bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
1076 DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled);
1077 bool afterMotionEventLockedInterruptible(const sp<Connection>& connection,
1078 DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled);
Jeff Brownb88102f2010-09-08 11:49:43 -07001079 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001080 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -07001081
1082 // Statistics gathering.
1083 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1084 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001085};
1086
1087/* Enqueues and dispatches input events, endlessly. */
1088class InputDispatcherThread : public Thread {
1089public:
1090 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1091 ~InputDispatcherThread();
1092
1093private:
1094 virtual bool threadLoop();
1095
1096 sp<InputDispatcherInterface> mDispatcher;
1097};
1098
1099} // namespace android
1100
Jeff Brownb88102f2010-09-08 11:49:43 -07001101#endif // _UI_INPUT_DISPATCHER_H