blob: 5631ef9a4fa8a641277cdccaa25a0691ecda03d4 [file] [log] [blame]
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _UI_INPUT_DISPATCHER_H
18#define _UI_INPUT_DISPATCHER_H
19
20#include <ui/Input.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070021#include <ui/InputTransport.h>
22#include <utils/KeyedVector.h>
23#include <utils/Vector.h>
24#include <utils/threads.h>
25#include <utils/Timers.h>
26#include <utils/RefBase.h>
27#include <utils/String8.h>
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070028#include <utils/Looper.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070029#include <utils/Pool.h>
Jeff Brown01ce2e92010-09-26 22:20:12 -070030#include <utils/BitSet.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070031
32#include <stddef.h>
33#include <unistd.h>
Jeff Brownb88102f2010-09-08 11:49:43 -070034#include <limits.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070035
Jeff Brown928e0542011-01-10 11:17:36 -080036#include "InputWindow.h"
37#include "InputApplication.h"
38
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070039
40namespace android {
41
Jeff Brown9c3cda02010-06-15 01:31:58 -070042/*
Jeff Brown7fbdc842010-06-17 20:52:56 -070043 * Constants used to report the outcome of input event injection.
44 */
45enum {
46 /* (INTERNAL USE ONLY) Specifies that injection is pending and its outcome is unknown. */
47 INPUT_EVENT_INJECTION_PENDING = -1,
48
49 /* Injection succeeded. */
50 INPUT_EVENT_INJECTION_SUCCEEDED = 0,
51
52 /* Injection failed because the injector did not have permission to inject
53 * into the application with input focus. */
54 INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1,
55
56 /* Injection failed because there were no available input targets. */
57 INPUT_EVENT_INJECTION_FAILED = 2,
58
59 /* Injection failed due to a timeout. */
60 INPUT_EVENT_INJECTION_TIMED_OUT = 3
61};
62
Jeff Brown6ec402b2010-07-28 15:48:59 -070063/*
64 * Constants used to determine the input event injection synchronization mode.
65 */
66enum {
67 /* Injection is asynchronous and is assumed always to be successful. */
68 INPUT_EVENT_INJECTION_SYNC_NONE = 0,
69
70 /* Waits for previous events to be dispatched so that the input dispatcher can determine
71 * whether input event injection willbe permitted based on the current input focus.
72 * Does not wait for the input event to finish processing. */
73 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1,
74
75 /* Waits for the input event to be completely processed. */
76 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED = 2,
77};
78
Jeff Brown7fbdc842010-06-17 20:52:56 -070079
80/*
Jeff Brown9c3cda02010-06-15 01:31:58 -070081 * An input target specifies how an input event is to be dispatched to a particular window
82 * including the window's input channel, control flags, a timeout, and an X / Y offset to
83 * be added to input event coordinates to compensate for the absolute position of the
84 * window area.
85 */
86struct InputTarget {
87 enum {
Jeff Brown519e0242010-09-15 15:18:56 -070088 /* This flag indicates that the event is being delivered to a foreground application. */
89 FLAG_FOREGROUND = 0x01,
Jeff Brown9c3cda02010-06-15 01:31:58 -070090
Jeff Brown85a31762010-09-01 17:01:00 -070091 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
92 * of the area of this target and so should instead be delivered as an
93 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
Jeff Brown9c3cda02010-06-15 01:31:58 -070094 FLAG_OUTSIDE = 0x02,
95
Jeff Brown85a31762010-09-01 17:01:00 -070096 /* This flag indicates that the target of a MotionEvent is partly or wholly
97 * obscured by another visible window above it. The motion event should be
98 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
Jeff Brown01ce2e92010-09-26 22:20:12 -070099 FLAG_WINDOW_IS_OBSCURED = 0x04,
100
101 /* This flag indicates that a motion event is being split across multiple windows. */
102 FLAG_SPLIT = 0x08,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700103 };
104
105 // The input channel to be targeted.
106 sp<InputChannel> inputChannel;
107
108 // Flags for the input target.
109 int32_t flags;
110
Jeff Brown9c3cda02010-06-15 01:31:58 -0700111 // The x and y offset to add to a MotionEvent as it is delivered.
112 // (ignored for KeyEvents)
113 float xOffset, yOffset;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700114
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400115 // Scaling factor to apply to MotionEvent as it is delivered.
116 // (ignored for KeyEvents)
117 float scaleFactor;
118
Jeff Brown01ce2e92010-09-26 22:20:12 -0700119 // The subset of pointer ids to include in motion events dispatched to this input target
120 // if FLAG_SPLIT is set.
121 BitSet32 pointerIds;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700122};
123
Jeff Brown7fbdc842010-06-17 20:52:56 -0700124
Jeff Brown9c3cda02010-06-15 01:31:58 -0700125/*
Jeff Brown214eaf42011-05-26 19:17:02 -0700126 * Input dispatcher configuration.
127 *
128 * Specifies various options that modify the behavior of the input dispatcher.
129 */
130struct InputDispatcherConfiguration {
131 // The key repeat initial timeout.
132 nsecs_t keyRepeatTimeout;
133
134 // The key repeat inter-key delay.
135 nsecs_t keyRepeatDelay;
136
137 // The maximum suggested event delivery rate per second.
138 // This value is used to throttle motion event movement actions on a per-device
139 // basis. It is not intended to be a hard limit.
140 int32_t maxEventsPerSecond;
141
142 InputDispatcherConfiguration() :
143 keyRepeatTimeout(500 * 1000000LL),
144 keyRepeatDelay(50 * 1000000LL),
145 maxEventsPerSecond(60) { }
146};
147
148
149/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700150 * Input dispatcher policy interface.
151 *
152 * The input reader policy is used by the input reader to interact with the Window Manager
153 * and other system components.
154 *
155 * The actual implementation is partially supported by callbacks into the DVM
156 * via JNI. This interface is also mocked in the unit tests.
157 */
158class InputDispatcherPolicyInterface : public virtual RefBase {
159protected:
160 InputDispatcherPolicyInterface() { }
161 virtual ~InputDispatcherPolicyInterface() { }
162
163public:
164 /* Notifies the system that a configuration change has occurred. */
165 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
166
Jeff Brownb88102f2010-09-08 11:49:43 -0700167 /* Notifies the system that an application is not responding.
168 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
Jeff Brown519e0242010-09-15 15:18:56 -0700169 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -0800170 const sp<InputWindowHandle>& inputWindowHandle) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700171
Jeff Brown9c3cda02010-06-15 01:31:58 -0700172 /* Notifies the system that an input channel is unrecoverably broken. */
Jeff Brown928e0542011-01-10 11:17:36 -0800173 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700174
Jeff Brown214eaf42011-05-26 19:17:02 -0700175 /* Gets the input dispatcher configuration. */
176 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700177
Jeff Brown214eaf42011-05-26 19:17:02 -0700178 /* Returns true if automatic key repeating is enabled. */
179 virtual bool isKeyRepeatEnabled() = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700180
Jeff Brownb6997262010-10-08 22:31:17 -0700181 /* Intercepts a key event immediately before queueing it.
182 * The policy can use this method as an opportunity to perform power management functions
183 * and early event preprocessing such as updating policy flags.
184 *
185 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
186 * should be dispatched to applications.
187 */
Jeff Brown1f245102010-11-18 20:53:46 -0800188 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700189
Jeff Brown56194eb2011-03-02 19:23:13 -0800190 /* Intercepts a touch, trackball or other motion event before queueing it.
Jeff Brownb6997262010-10-08 22:31:17 -0700191 * The policy can use this method as an opportunity to perform power management functions
192 * and early event preprocessing such as updating policy flags.
193 *
194 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
195 * should be dispatched to applications.
196 */
Jeff Brown56194eb2011-03-02 19:23:13 -0800197 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700198
Jeff Brownb88102f2010-09-08 11:49:43 -0700199 /* Allows the policy a chance to intercept a key before dispatching. */
Jeff Brown928e0542011-01-10 11:17:36 -0800200 virtual bool interceptKeyBeforeDispatching(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700201 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
202
Jeff Brown49ed71d2010-12-06 17:13:33 -0800203 /* Allows the policy a chance to perform default processing for an unhandled key.
204 * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */
Jeff Brown928e0542011-01-10 11:17:36 -0800205 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800206 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0;
Jeff Brown3915bb82010-11-05 15:02:16 -0700207
Jeff Brownb6997262010-10-08 22:31:17 -0700208 /* Notifies the policy about switch events.
209 */
210 virtual void notifySwitch(nsecs_t when,
211 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
212
Jeff Brownb88102f2010-09-08 11:49:43 -0700213 /* Poke user activity for an event dispatched to a window. */
Jeff Brown01ce2e92010-09-26 22:20:12 -0700214 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700215
216 /* Checks whether a given application pid/uid has permission to inject input events
217 * into other applications.
218 *
219 * This method is special in that its implementation promises to be non-reentrant and
220 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
221 */
222 virtual bool checkInjectEventsPermissionNonReentrant(
223 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700224};
225
226
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700227/* Notifies the system about input events generated by the input reader.
228 * The dispatcher is expected to be mostly asynchronous. */
229class InputDispatcherInterface : public virtual RefBase {
230protected:
231 InputDispatcherInterface() { }
232 virtual ~InputDispatcherInterface() { }
233
234public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700235 /* Dumps the state of the input dispatcher.
236 *
237 * This method may be called on any thread (usually by the input manager). */
238 virtual void dump(String8& dump) = 0;
239
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700240 /* Runs a single iteration of the dispatch loop.
241 * Nominally processes one queued event, a timeout, or a response from an input consumer.
242 *
243 * This method should only be called on the input dispatcher thread.
244 */
245 virtual void dispatchOnce() = 0;
246
247 /* Notifies the dispatcher about new events.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700248 *
249 * These methods should only be called on the input reader thread.
250 */
Jeff Brown9c3cda02010-06-15 01:31:58 -0700251 virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0;
Jeff Brown58a2da82011-01-25 16:02:22 -0800252 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700253 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
254 int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0;
Jeff Brown58a2da82011-01-25 16:02:22 -0800255 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700256 uint32_t policyFlags, int32_t action, int32_t flags,
257 int32_t metaState, int32_t edgeFlags,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700258 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
259 float xPrecision, float yPrecision, nsecs_t downTime) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700260 virtual void notifySwitch(nsecs_t when,
261 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700262
Jeff Brown7fbdc842010-06-17 20:52:56 -0700263 /* Injects an input event and optionally waits for sync.
Jeff Brown6ec402b2010-07-28 15:48:59 -0700264 * The synchronization mode determines whether the method blocks while waiting for
265 * input injection to proceed.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700266 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
267 *
268 * This method may be called on any thread (usually by the input manager).
269 */
270 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown6ec402b2010-07-28 15:48:59 -0700271 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis) = 0;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700272
Jeff Brownb88102f2010-09-08 11:49:43 -0700273 /* Sets the list of input windows.
274 *
275 * This method may be called on any thread (usually by the input manager).
276 */
277 virtual void setInputWindows(const Vector<InputWindow>& inputWindows) = 0;
278
279 /* Sets the focused application.
280 *
281 * This method may be called on any thread (usually by the input manager).
282 */
283 virtual void setFocusedApplication(const InputApplication* inputApplication) = 0;
284
285 /* Sets the input dispatching mode.
286 *
287 * This method may be called on any thread (usually by the input manager).
288 */
289 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
290
Jeff Browne6504122010-09-27 14:52:15 -0700291 /* Transfers touch focus from the window associated with one channel to the
292 * window associated with the other channel.
293 *
294 * Returns true on success. False if the window did not actually have touch focus.
295 */
296 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
297 const sp<InputChannel>& toChannel) = 0;
298
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700299 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Brownb88102f2010-09-08 11:49:43 -0700300 * If monitor is true, the channel will receive a copy of all input events.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700301 *
302 * These methods may be called on any thread (usually by the input manager).
303 */
Jeff Brown928e0542011-01-10 11:17:36 -0800304 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
305 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700306 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
307};
308
Jeff Brown9c3cda02010-06-15 01:31:58 -0700309/* Dispatches events to input targets. Some functions of the input dispatcher, such as
310 * identifying input targets, are controlled by a separate policy object.
311 *
312 * IMPORTANT INVARIANT:
313 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
314 * the input dispatcher never calls into the policy while holding its internal locks.
315 * The implementation is also carefully designed to recover from scenarios such as an
316 * input channel becoming unregistered while identifying input targets or processing timeouts.
317 *
318 * Methods marked 'Locked' must be called with the lock acquired.
319 *
320 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
321 * may during the course of their execution release the lock, call into the policy, and
322 * then reacquire the lock. The caller is responsible for recovering gracefully.
323 *
324 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
325 */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700326class InputDispatcher : public InputDispatcherInterface {
327protected:
328 virtual ~InputDispatcher();
329
330public:
Jeff Brown9c3cda02010-06-15 01:31:58 -0700331 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700332
Jeff Brownb88102f2010-09-08 11:49:43 -0700333 virtual void dump(String8& dump);
334
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700335 virtual void dispatchOnce();
336
Jeff Brown9c3cda02010-06-15 01:31:58 -0700337 virtual void notifyConfigurationChanged(nsecs_t eventTime);
Jeff Brown58a2da82011-01-25 16:02:22 -0800338 virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700339 uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode,
340 int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown58a2da82011-01-25 16:02:22 -0800341 virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, uint32_t source,
Jeff Brown85a31762010-09-01 17:01:00 -0700342 uint32_t policyFlags, int32_t action, int32_t flags,
343 int32_t metaState, int32_t edgeFlags,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700344 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords,
345 float xPrecision, float yPrecision, nsecs_t downTime);
Jeff Brownb6997262010-10-08 22:31:17 -0700346 virtual void notifySwitch(nsecs_t when,
347 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) ;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700348
Jeff Brown7fbdc842010-06-17 20:52:56 -0700349 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown6ec402b2010-07-28 15:48:59 -0700350 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700351
Jeff Brownb88102f2010-09-08 11:49:43 -0700352 virtual void setInputWindows(const Vector<InputWindow>& inputWindows);
353 virtual void setFocusedApplication(const InputApplication* inputApplication);
354 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700355
Jeff Browne6504122010-09-27 14:52:15 -0700356 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
357 const sp<InputChannel>& toChannel);
358
Jeff Brown928e0542011-01-10 11:17:36 -0800359 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
360 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700361 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
362
363private:
364 template <typename T>
365 struct Link {
366 T* next;
367 T* prev;
368 };
369
Jeff Brown01ce2e92010-09-26 22:20:12 -0700370 struct InjectionState {
371 mutable int32_t refCount;
372
373 int32_t injectorPid;
374 int32_t injectorUid;
375 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
376 bool injectionIsAsync; // set to true if injection is not waiting for the result
377 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
378 };
379
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700380 struct EventEntry : Link<EventEntry> {
381 enum {
382 TYPE_SENTINEL,
383 TYPE_CONFIGURATION_CHANGED,
384 TYPE_KEY,
385 TYPE_MOTION
386 };
387
Jeff Brown01ce2e92010-09-26 22:20:12 -0700388 mutable int32_t refCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700389 int32_t type;
390 nsecs_t eventTime;
Jeff Brownb6997262010-10-08 22:31:17 -0700391 uint32_t policyFlags;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700392 InjectionState* injectionState;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700393
Jeff Brown9c3cda02010-06-15 01:31:58 -0700394 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown7fbdc842010-06-17 20:52:56 -0700395
Jeff Brown5ced76a2011-05-24 11:23:27 -0700396 inline bool isInjected() const { return injectionState != NULL; }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700397 };
398
399 struct ConfigurationChangedEntry : EventEntry {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700400 };
401
402 struct KeyEntry : EventEntry {
403 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800404 uint32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700405 int32_t action;
406 int32_t flags;
407 int32_t keyCode;
408 int32_t scanCode;
409 int32_t metaState;
410 int32_t repeatCount;
411 nsecs_t downTime;
Jeff Brownb88102f2010-09-08 11:49:43 -0700412
413 bool syntheticRepeat; // set to true for synthetic key repeats
414
415 enum InterceptKeyResult {
416 INTERCEPT_KEY_RESULT_UNKNOWN,
417 INTERCEPT_KEY_RESULT_SKIP,
418 INTERCEPT_KEY_RESULT_CONTINUE,
419 };
420 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700421 };
422
423 struct MotionSample {
424 MotionSample* next;
425
Jeff Brown5ced76a2011-05-24 11:23:27 -0700426 nsecs_t eventTime; // may be updated during coalescing
427 nsecs_t eventTimeBeforeCoalescing; // not updated during coalescing
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700428 PointerCoords pointerCoords[MAX_POINTERS];
429 };
430
431 struct MotionEntry : EventEntry {
432 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800433 uint32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700434 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -0700435 int32_t flags;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700436 int32_t metaState;
437 int32_t edgeFlags;
438 float xPrecision;
439 float yPrecision;
440 nsecs_t downTime;
441 uint32_t pointerCount;
442 int32_t pointerIds[MAX_POINTERS];
443
444 // Linked list of motion samples associated with this motion event.
445 MotionSample firstSample;
446 MotionSample* lastSample;
Jeff Brownae9fc032010-08-18 15:51:08 -0700447
448 uint32_t countSamples() const;
Jeff Brown5ced76a2011-05-24 11:23:27 -0700449
450 // Checks whether we can append samples, assuming the device id and source are the same.
451 bool canAppendSamples(int32_t action, uint32_t pointerCount,
452 const int32_t* pointerIds) const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700453 };
454
Jeff Brown9c3cda02010-06-15 01:31:58 -0700455 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700456 struct DispatchEntry : Link<DispatchEntry> {
457 EventEntry* eventEntry; // the event to dispatch
458 int32_t targetFlags;
459 float xOffset;
460 float yOffset;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400461 float scaleFactor;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700462
463 // True if dispatch has started.
464 bool inProgress;
465
466 // For motion events:
467 // Pointer to the first motion sample to dispatch in this cycle.
468 // Usually NULL to indicate that the list of motion samples begins at
469 // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous
470 // cycle and this pointer indicates the location of the first remainining sample
471 // to dispatch during the current cycle.
472 MotionSample* headMotionSample;
473 // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was
474 // unable to send all motion samples during this cycle. On the next cycle,
475 // headMotionSample will be initialized to tailMotionSample and tailMotionSample
476 // will be set to NULL.
477 MotionSample* tailMotionSample;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700478
Jeff Brown519e0242010-09-15 15:18:56 -0700479 inline bool hasForegroundTarget() const {
480 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Brownb88102f2010-09-08 11:49:43 -0700481 }
Jeff Brown01ce2e92010-09-26 22:20:12 -0700482
483 inline bool isSplit() const {
484 return targetFlags & InputTarget::FLAG_SPLIT;
485 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700486 };
487
Jeff Brown9c3cda02010-06-15 01:31:58 -0700488 // A command entry captures state and behavior for an action to be performed in the
489 // dispatch loop after the initial processing has taken place. It is essentially
490 // a kind of continuation used to postpone sensitive policy interactions to a point
491 // in the dispatch loop where it is safe to release the lock (generally after finishing
492 // the critical parts of the dispatch cycle).
493 //
494 // The special thing about commands is that they can voluntarily release and reacquire
495 // the dispatcher lock at will. Initially when the command starts running, the
496 // dispatcher lock is held. However, if the command needs to call into the policy to
497 // do some work, it can release the lock, do the work, then reacquire the lock again
498 // before returning.
499 //
500 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
501 // never calls into the policy while holding its lock.
502 //
503 // Commands are implicitly 'LockedInterruptible'.
504 struct CommandEntry;
505 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
506
Jeff Brown7fbdc842010-06-17 20:52:56 -0700507 class Connection;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700508 struct CommandEntry : Link<CommandEntry> {
509 CommandEntry();
510 ~CommandEntry();
511
512 Command command;
513
514 // parameters for the command (usage varies by command)
Jeff Brown7fbdc842010-06-17 20:52:56 -0700515 sp<Connection> connection;
Jeff Brownb88102f2010-09-08 11:49:43 -0700516 nsecs_t eventTime;
517 KeyEntry* keyEntry;
518 sp<InputChannel> inputChannel;
519 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800520 sp<InputWindowHandle> inputWindowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700521 int32_t userActivityEventType;
Jeff Brown3915bb82010-11-05 15:02:16 -0700522 bool handled;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700523 };
524
525 // Generic queue implementation.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700526 template <typename T>
527 struct Queue {
Jeff Brownb88102f2010-09-08 11:49:43 -0700528 T headSentinel;
529 T tailSentinel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700530
531 inline Queue() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700532 headSentinel.prev = NULL;
533 headSentinel.next = & tailSentinel;
534 tailSentinel.prev = & headSentinel;
535 tailSentinel.next = NULL;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700536 }
537
Jeff Brownb88102f2010-09-08 11:49:43 -0700538 inline bool isEmpty() const {
539 return headSentinel.next == & tailSentinel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700540 }
541
542 inline void enqueueAtTail(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700543 T* last = tailSentinel.prev;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700544 last->next = entry;
545 entry->prev = last;
Jeff Brownb88102f2010-09-08 11:49:43 -0700546 entry->next = & tailSentinel;
547 tailSentinel.prev = entry;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700548 }
549
550 inline void enqueueAtHead(T* entry) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700551 T* first = headSentinel.next;
552 headSentinel.next = entry;
553 entry->prev = & headSentinel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700554 entry->next = first;
555 first->prev = entry;
556 }
557
558 inline void dequeue(T* entry) {
559 entry->prev->next = entry->next;
560 entry->next->prev = entry->prev;
561 }
562
563 inline T* dequeueAtHead() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700564 T* first = headSentinel.next;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700565 dequeue(first);
566 return first;
567 }
Jeff Brown519e0242010-09-15 15:18:56 -0700568
569 uint32_t count() const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700570 };
571
572 /* Allocates queue entries and performs reference counting as needed. */
573 class Allocator {
574 public:
575 Allocator();
576
Jeff Brown01ce2e92010-09-26 22:20:12 -0700577 InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700578 ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime);
579 KeyEntry* obtainKeyEntry(nsecs_t eventTime,
Jeff Brown58a2da82011-01-25 16:02:22 -0800580 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700581 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
582 int32_t repeatCount, nsecs_t downTime);
583 MotionEntry* obtainMotionEntry(nsecs_t eventTime,
Jeff Brown58a2da82011-01-25 16:02:22 -0800584 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700585 int32_t flags, int32_t metaState, int32_t edgeFlags,
586 float xPrecision, float yPrecision,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700587 nsecs_t downTime, uint32_t pointerCount,
588 const int32_t* pointerIds, const PointerCoords* pointerCoords);
Jeff Brownb88102f2010-09-08 11:49:43 -0700589 DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400590 int32_t targetFlags, float xOffset, float yOffset, float scaleFactor);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700591 CommandEntry* obtainCommandEntry(Command command);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700592
Jeff Brown01ce2e92010-09-26 22:20:12 -0700593 void releaseInjectionState(InjectionState* injectionState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700594 void releaseEventEntry(EventEntry* entry);
595 void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry);
596 void releaseKeyEntry(KeyEntry* entry);
597 void releaseMotionEntry(MotionEntry* entry);
598 void releaseDispatchEntry(DispatchEntry* entry);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700599 void releaseCommandEntry(CommandEntry* entry);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700600
Jeff Brown01ce2e92010-09-26 22:20:12 -0700601 void recycleKeyEntry(KeyEntry* entry);
602
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700603 void appendMotionSample(MotionEntry* motionEntry,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700604 nsecs_t eventTime, const PointerCoords* pointerCoords);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700605
606 private:
Jeff Brown01ce2e92010-09-26 22:20:12 -0700607 Pool<InjectionState> mInjectionStatePool;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700608 Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool;
609 Pool<KeyEntry> mKeyEntryPool;
610 Pool<MotionEntry> mMotionEntryPool;
611 Pool<MotionSample> mMotionSamplePool;
612 Pool<DispatchEntry> mDispatchEntryPool;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700613 Pool<CommandEntry> mCommandEntryPool;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700614
Jeff Brownb6997262010-10-08 22:31:17 -0700615 void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime,
616 uint32_t policyFlags);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700617 void releaseEventEntryInjectionState(EventEntry* entry);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700618 };
619
Jeff Brown524ee642011-03-29 15:11:34 -0700620 /* Specifies which events are to be canceled and why. */
621 struct CancelationOptions {
622 enum Mode {
Jeff Brownb6997262010-10-08 22:31:17 -0700623 CANCEL_ALL_EVENTS = 0,
624 CANCEL_POINTER_EVENTS = 1,
625 CANCEL_NON_POINTER_EVENTS = 2,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800626 CANCEL_FALLBACK_EVENTS = 3,
Jeff Brownb6997262010-10-08 22:31:17 -0700627 };
628
Jeff Brown524ee642011-03-29 15:11:34 -0700629 // The criterion to use to determine which events should be canceled.
630 Mode mode;
631
632 // Descriptive reason for the cancelation.
633 const char* reason;
634
635 // The specific keycode of the key event to cancel, or -1 to cancel any key event.
636 int32_t keyCode;
637
638 CancelationOptions(Mode mode, const char* reason) :
639 mode(mode), reason(reason), keyCode(-1) { }
640 };
641
642 /* Tracks dispatched key and motion event state so that cancelation events can be
643 * synthesized when events are dropped. */
644 class InputState {
645 public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700646 InputState();
647 ~InputState();
648
649 // Returns true if there is no state to be canceled.
650 bool isNeutral() const;
651
Jeff Brownb88102f2010-09-08 11:49:43 -0700652 // Records tracking information for an event that has just been published.
Jeff Browncc0c1592011-02-19 05:07:28 -0800653 void trackEvent(const EventEntry* entry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700654
655 // Records tracking information for a key event that has just been published.
Jeff Browncc0c1592011-02-19 05:07:28 -0800656 void trackKey(const KeyEntry* entry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700657
658 // Records tracking information for a motion event that has just been published.
Jeff Browncc0c1592011-02-19 05:07:28 -0800659 void trackMotion(const MotionEntry* entry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700660
Jeff Brownb6997262010-10-08 22:31:17 -0700661 // Synthesizes cancelation events for the current state and resets the tracked state.
662 void synthesizeCancelationEvents(nsecs_t currentTime, Allocator* allocator,
Jeff Brown524ee642011-03-29 15:11:34 -0700663 Vector<EventEntry*>& outEvents, const CancelationOptions& options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700664
665 // Clears the current state.
666 void clear();
667
Jeff Brown9c9f1a32010-10-11 18:32:20 -0700668 // Copies pointer-related parts of the input state to another instance.
669 void copyPointerStateTo(InputState& other) const;
670
Jeff Brown524ee642011-03-29 15:11:34 -0700671 // Gets the fallback key associated with a keycode.
672 // Returns -1 if none.
673 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
674 int32_t getFallbackKey(int32_t originalKeyCode);
675
676 // Sets the fallback key for a particular keycode.
677 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
678
679 // Removes the fallback key for a particular keycode.
680 void removeFallbackKey(int32_t originalKeyCode);
681
682 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const {
683 return mFallbackKeys;
684 }
685
Jeff Brownb88102f2010-09-08 11:49:43 -0700686 private:
Jeff Brownb88102f2010-09-08 11:49:43 -0700687 struct KeyMemento {
688 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800689 uint32_t source;
Jeff Brownb88102f2010-09-08 11:49:43 -0700690 int32_t keyCode;
691 int32_t scanCode;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800692 int32_t flags;
Jeff Brownb88102f2010-09-08 11:49:43 -0700693 nsecs_t downTime;
694 };
695
696 struct MotionMemento {
697 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800698 uint32_t source;
Jeff Brownb88102f2010-09-08 11:49:43 -0700699 float xPrecision;
700 float yPrecision;
701 nsecs_t downTime;
702 uint32_t pointerCount;
703 int32_t pointerIds[MAX_POINTERS];
704 PointerCoords pointerCoords[MAX_POINTERS];
705
706 void setPointers(const MotionEntry* entry);
707 };
708
709 Vector<KeyMemento> mKeyMementos;
710 Vector<MotionMemento> mMotionMementos;
Jeff Brown524ee642011-03-29 15:11:34 -0700711 KeyedVector<int32_t, int32_t> mFallbackKeys;
Jeff Brownb6997262010-10-08 22:31:17 -0700712
Jeff Brown49ed71d2010-12-06 17:13:33 -0800713 static bool shouldCancelKey(const KeyMemento& memento,
Jeff Brown524ee642011-03-29 15:11:34 -0700714 const CancelationOptions& options);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800715 static bool shouldCancelMotion(const MotionMemento& memento,
Jeff Brown524ee642011-03-29 15:11:34 -0700716 const CancelationOptions& options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700717 };
718
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700719 /* Manages the dispatch state associated with a single input channel. */
720 class Connection : public RefBase {
721 protected:
722 virtual ~Connection();
723
724 public:
725 enum Status {
726 // Everything is peachy.
727 STATUS_NORMAL,
728 // An unrecoverable communication error has occurred.
729 STATUS_BROKEN,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700730 // The input channel has been unregistered.
731 STATUS_ZOMBIE
732 };
733
734 Status status;
Jeff Brown928e0542011-01-10 11:17:36 -0800735 sp<InputChannel> inputChannel; // never null
736 sp<InputWindowHandle> inputWindowHandle; // may be null
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700737 InputPublisher inputPublisher;
Jeff Brownb88102f2010-09-08 11:49:43 -0700738 InputState inputState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700739 Queue<DispatchEntry> outboundQueue;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700740
741 nsecs_t lastEventTime; // the time when the event was originally captured
742 nsecs_t lastDispatchTime; // the time when the last event was dispatched
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700743
Jeff Brown928e0542011-01-10 11:17:36 -0800744 explicit Connection(const sp<InputChannel>& inputChannel,
745 const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700746
Jeff Brown9c3cda02010-06-15 01:31:58 -0700747 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
748
749 const char* getStatusLabel() const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700750
751 // Finds a DispatchEntry in the outbound queue associated with the specified event.
752 // Returns NULL if not found.
753 DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const;
754
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700755 // Gets the time since the current event was originally obtained from the input driver.
Jeff Brownb88102f2010-09-08 11:49:43 -0700756 inline double getEventLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700757 return (currentTime - lastEventTime) / 1000000.0;
758 }
759
760 // Gets the time since the current event entered the outbound dispatch queue.
Jeff Brownb88102f2010-09-08 11:49:43 -0700761 inline double getDispatchLatencyMillis(nsecs_t currentTime) const {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700762 return (currentTime - lastDispatchTime) / 1000000.0;
763 }
764
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700765 status_t initialize();
766 };
767
Jeff Brownb6997262010-10-08 22:31:17 -0700768 enum DropReason {
769 DROP_REASON_NOT_DROPPED = 0,
770 DROP_REASON_POLICY = 1,
771 DROP_REASON_APP_SWITCH = 2,
772 DROP_REASON_DISABLED = 3,
Jeff Brown928e0542011-01-10 11:17:36 -0800773 DROP_REASON_BLOCKED = 4,
774 DROP_REASON_STALE = 5,
Jeff Brownb6997262010-10-08 22:31:17 -0700775 };
776
Jeff Brown9c3cda02010-06-15 01:31:58 -0700777 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Brown214eaf42011-05-26 19:17:02 -0700778 InputDispatcherConfiguration mConfig;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700779
780 Mutex mLock;
781
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700782 Allocator mAllocator;
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700783 sp<Looper> mLooper;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700784
Jeff Brownb88102f2010-09-08 11:49:43 -0700785 EventEntry* mPendingEvent;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700786 Queue<EventEntry> mInboundQueue;
787 Queue<CommandEntry> mCommandQueue;
788
Jeff Brownb88102f2010-09-08 11:49:43 -0700789 Vector<EventEntry*> mTempCancelationEvents;
790
Jeff Brown214eaf42011-05-26 19:17:02 -0700791 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700792
Jeff Brown5ced76a2011-05-24 11:23:27 -0700793 // Batches a new sample onto a motion entry.
794 // Assumes that the we have already checked that we can append samples.
795 void batchMotionLocked(MotionEntry* entry, nsecs_t eventTime, int32_t metaState,
796 const PointerCoords* pointerCoords, const char* eventDescription);
797
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700798 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Brownb88102f2010-09-08 11:49:43 -0700799 bool enqueueInboundEventLocked(EventEntry* entry);
800
Jeff Brownb6997262010-10-08 22:31:17 -0700801 // Cleans up input state when dropping an inbound event.
802 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
803
Jeff Brownb88102f2010-09-08 11:49:43 -0700804 // App switch latency optimization.
Jeff Brownb6997262010-10-08 22:31:17 -0700805 bool mAppSwitchSawKeyDown;
Jeff Brownb88102f2010-09-08 11:49:43 -0700806 nsecs_t mAppSwitchDueTime;
807
Jeff Brownb6997262010-10-08 22:31:17 -0700808 static bool isAppSwitchKeyCode(int32_t keyCode);
809 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700810 bool isAppSwitchPendingLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700811 void resetPendingAppSwitchLocked(bool handled);
812
Jeff Brown928e0542011-01-10 11:17:36 -0800813 // Stale event latency optimization.
814 static bool isStaleEventLocked(nsecs_t currentTime, EventEntry* entry);
815
816 // Blocked event latency optimization. Drops old events when the user intends
817 // to transfer focus to a new application.
818 EventEntry* mNextUnblockedEvent;
819
820 const InputWindow* findTouchedWindowAtLocked(int32_t x, int32_t y);
821
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700822 // All registered connections mapped by receive pipe file descriptor.
823 KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd;
824
Jeff Brown519e0242010-09-15 15:18:56 -0700825 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown2cbecea2010-08-17 15:59:26 -0700826
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700827 // Active connections are connections that have a non-empty outbound queue.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700828 // We don't use a ref-counted pointer here because we explicitly abort connections
829 // during unregistration which causes the connection's outbound queue to be cleared
830 // and the connection itself to be deactivated.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700831 Vector<Connection*> mActiveConnections;
832
Jeff Brownb88102f2010-09-08 11:49:43 -0700833 // Input channels that will receive a copy of all input events.
834 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700835
Jeff Brown7fbdc842010-06-17 20:52:56 -0700836 // Event injection and synchronization.
837 Condition mInjectionResultAvailableCondition;
Jeff Brownb6997262010-10-08 22:31:17 -0700838 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700839 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
840
Jeff Brown6ec402b2010-07-28 15:48:59 -0700841 Condition mInjectionSyncFinishedCondition;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700842 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -0700843 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown6ec402b2010-07-28 15:48:59 -0700844
Jeff Brownae9fc032010-08-18 15:51:08 -0700845 // Throttling state.
846 struct ThrottleState {
847 nsecs_t minTimeBetweenEvents;
848
849 nsecs_t lastEventTime;
850 int32_t lastDeviceId;
851 uint32_t lastSource;
852
853 uint32_t originalSampleCount; // only collected during debugging
854 } mThrottleState;
855
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700856 // Key repeat tracking.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700857 struct KeyRepeatState {
858 KeyEntry* lastKeyEntry; // or null if no repeat
859 nsecs_t nextRepeatTime;
860 } mKeyRepeatState;
861
862 void resetKeyRepeatLocked();
Jeff Brown214eaf42011-05-26 19:17:02 -0700863 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700864
Jeff Brown9c3cda02010-06-15 01:31:58 -0700865 // Deferred command processing.
866 bool runCommandsLockedInterruptible();
867 CommandEntry* postCommandLocked(Command command);
868
Jeff Brownb88102f2010-09-08 11:49:43 -0700869 // Inbound event processing.
870 void drainInboundQueueLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700871 void releasePendingEventLocked();
872 void releaseInboundEventLocked(EventEntry* entry);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700873
Jeff Brownb88102f2010-09-08 11:49:43 -0700874 // Dispatch state.
875 bool mDispatchEnabled;
876 bool mDispatchFrozen;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700877
Jeff Brownb88102f2010-09-08 11:49:43 -0700878 Vector<InputWindow> mWindows;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700879
880 const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel);
Jeff Brownb88102f2010-09-08 11:49:43 -0700881
882 // Focus tracking for keys, trackball, etc.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700883 const InputWindow* mFocusedWindow;
Jeff Brownb88102f2010-09-08 11:49:43 -0700884
885 // Focus tracking for touch.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700886 struct TouchedWindow {
887 const InputWindow* window;
888 int32_t targetFlags;
Jeff Brown46e75292010-11-10 16:53:45 -0800889 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
Jeff Brown01ce2e92010-09-26 22:20:12 -0700890 sp<InputChannel> channel;
Jeff Brownb88102f2010-09-08 11:49:43 -0700891 };
Jeff Brown01ce2e92010-09-26 22:20:12 -0700892 struct TouchState {
893 bool down;
894 bool split;
Jeff Brown95712852011-01-04 19:41:59 -0800895 int32_t deviceId; // id of the device that is currently down, others are rejected
Jeff Brown58a2da82011-01-25 16:02:22 -0800896 uint32_t source; // source of the device that is current down, others are rejected
Jeff Brown01ce2e92010-09-26 22:20:12 -0700897 Vector<TouchedWindow> windows;
898
899 TouchState();
900 ~TouchState();
901 void reset();
902 void copyFrom(const TouchState& other);
903 void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds);
904 void removeOutsideTouchWindows();
905 const InputWindow* getFirstForegroundWindow();
906 };
907
908 TouchState mTouchState;
909 TouchState mTempTouchState;
Jeff Brownb88102f2010-09-08 11:49:43 -0700910
911 // Focused application.
912 InputApplication* mFocusedApplication;
913 InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication
914 void releaseFocusedApplicationLocked();
915
916 // Dispatch inbound events.
917 bool dispatchConfigurationChangedLocked(
918 nsecs_t currentTime, ConfigurationChangedEntry* entry);
919 bool dispatchKeyLocked(
Jeff Brown214eaf42011-05-26 19:17:02 -0700920 nsecs_t currentTime, KeyEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700921 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700922 bool dispatchMotionLocked(
923 nsecs_t currentTime, MotionEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700924 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700925 void dispatchEventToCurrentInputTargetsLocked(
926 nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700927
Jeff Brownb88102f2010-09-08 11:49:43 -0700928 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
929 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
930
931 // The input targets that were most recently identified for dispatch.
Jeff Brownb88102f2010-09-08 11:49:43 -0700932 bool mCurrentInputTargetsValid; // false while targets are being recomputed
933 Vector<InputTarget> mCurrentInputTargets;
Jeff Brownb88102f2010-09-08 11:49:43 -0700934
935 enum InputTargetWaitCause {
936 INPUT_TARGET_WAIT_CAUSE_NONE,
937 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
938 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
939 };
940
941 InputTargetWaitCause mInputTargetWaitCause;
942 nsecs_t mInputTargetWaitStartTime;
943 nsecs_t mInputTargetWaitTimeoutTime;
944 bool mInputTargetWaitTimeoutExpired;
Jeff Brown928e0542011-01-10 11:17:36 -0800945 sp<InputApplicationHandle> mInputTargetWaitApplication;
Jeff Brownb88102f2010-09-08 11:49:43 -0700946
947 // Finding targets for input events.
Jeff Brown54a18252010-09-16 14:07:33 -0700948 void resetTargetsLocked();
Jeff Brown01ce2e92010-09-26 22:20:12 -0700949 void commitTargetsLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700950 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
951 const InputApplication* application, const InputWindow* window,
952 nsecs_t* nextWakeupTime);
Jeff Brown519e0242010-09-15 15:18:56 -0700953 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
954 const sp<InputChannel>& inputChannel);
955 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700956 void resetANRTimeoutsLocked();
957
Jeff Brown01ce2e92010-09-26 22:20:12 -0700958 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
959 nsecs_t* nextWakeupTime);
960 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
Jeff Browncc0c1592011-02-19 05:07:28 -0800961 nsecs_t* nextWakeupTime, bool* outConflictingPointerActions);
Jeff Brownb88102f2010-09-08 11:49:43 -0700962
Jeff Brown01ce2e92010-09-26 22:20:12 -0700963 void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags,
964 BitSet32 pointerIds);
Jeff Brownb88102f2010-09-08 11:49:43 -0700965 void addMonitoringTargetsLocked();
Jeff Browne2fe69e2010-10-18 13:21:23 -0700966 void pokeUserActivityLocked(const EventEntry* eventEntry);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700967 bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState);
Jeff Brown19dfc832010-10-05 12:26:23 -0700968 bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const;
Jeff Brown519e0242010-09-15 15:18:56 -0700969 bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window);
Jeff Brown519e0242010-09-15 15:18:56 -0700970 String8 getApplicationWindowLabelLocked(const InputApplication* application,
971 const InputWindow* window);
Jeff Brownb88102f2010-09-08 11:49:43 -0700972
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700973 // Manage the dispatch cycle for a single connection.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700974 // These methods are deliberately not Interruptible because doing all of the work
975 // with the mutex held makes it easier to ensure that connection invariants are maintained.
976 // If needed, the methods post commands to run later once the critical bits are done.
977 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700978 EventEntry* eventEntry, const InputTarget* inputTarget,
979 bool resumeWithAppendedMotionSample);
Jeff Brown519e0242010-09-15 15:18:56 -0700980 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown3915bb82010-11-05 15:02:16 -0700981 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
982 bool handled);
Jeff Brownb88102f2010-09-08 11:49:43 -0700983 void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brownb6997262010-10-08 22:31:17 -0700984 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -0700985 void drainOutboundQueueLocked(Connection* connection);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700986 static int handleReceiveCallback(int receiveFd, int events, void* data);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700987
Jeff Brownb6997262010-10-08 22:31:17 -0700988 void synthesizeCancelationEventsForAllConnectionsLocked(
Jeff Brown524ee642011-03-29 15:11:34 -0700989 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -0700990 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Jeff Brown524ee642011-03-29 15:11:34 -0700991 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -0700992 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Jeff Brown524ee642011-03-29 15:11:34 -0700993 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -0700994
Jeff Brown01ce2e92010-09-26 22:20:12 -0700995 // Splitting motion events across windows.
996 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
997
Jeff Brown120a4592010-10-27 18:43:51 -0700998 // Reset and drop everything the dispatcher is doing.
999 void resetAndDropEverythingLocked(const char* reason);
1000
Jeff Brownb88102f2010-09-08 11:49:43 -07001001 // Dump state.
1002 void dumpDispatchStateLocked(String8& dump);
1003 void logDispatchStateLocked();
1004
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001005 // Add or remove a connection to the mActiveConnections vector.
1006 void activateConnectionLocked(Connection* connection);
1007 void deactivateConnectionLocked(Connection* connection);
1008
1009 // Interesting events that we might like to log or tell the framework about.
Jeff Brown9c3cda02010-06-15 01:31:58 -07001010 void onDispatchCycleStartedLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001011 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001012 void onDispatchCycleFinishedLocked(
Jeff Brown3915bb82010-11-05 15:02:16 -07001013 nsecs_t currentTime, const sp<Connection>& connection, bool handled);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001014 void onDispatchCycleBrokenLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001015 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001016 void onANRLocked(
1017 nsecs_t currentTime, const InputApplication* application, const InputWindow* window,
1018 nsecs_t eventTime, nsecs_t waitStartTime);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001019
Jeff Brown7fbdc842010-06-17 20:52:56 -07001020 // Outbound policy interactions.
Jeff Brownb88102f2010-09-08 11:49:43 -07001021 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001022 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown519e0242010-09-15 15:18:56 -07001023 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07001024 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001025 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07001026 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001027 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -07001028
1029 // Statistics gathering.
1030 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1031 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001032};
1033
1034/* Enqueues and dispatches input events, endlessly. */
1035class InputDispatcherThread : public Thread {
1036public:
1037 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1038 ~InputDispatcherThread();
1039
1040private:
1041 virtual bool threadLoop();
1042
1043 sp<InputDispatcherInterface> mDispatcher;
1044};
1045
1046} // namespace android
1047
Jeff Brownb88102f2010-09-08 11:49:43 -07001048#endif // _UI_INPUT_DISPATCHER_H