blob: d0824fcda0d74e6c7aee1881d960368e1ed61f06 [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _UI_INPUT_DISPATCHER_H
18#define _UI_INPUT_DISPATCHER_H
19
Mathias Agopianb93a03f82012-02-17 15:34:57 -080020#include <androidfw/Input.h>
21#include <androidfw/InputTransport.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070022#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 Brown072ec962012-02-07 14:46:57 -080030#include <cutils/atomic.h>
Jeff Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -070035
Jeff Brown928e0542011-01-10 11:17:36 -080036#include "InputWindow.h"
37#include "InputApplication.h"
Jeff Brownbe1aa822011-07-27 16:04:54 -070038#include "InputListener.h"
Jeff Brown928e0542011-01-10 11:17:36 -080039
Jeff Brown46b9ac02010-04-22 18:58:52 -070040
41namespace android {
42
Jeff Brown9c3cda02010-06-15 01:31:58 -070043/*
Jeff Brown7fbdc842010-06-17 20:52:56 -070044 * Constants used to report the outcome of input event injection.
45 */
46enum {
47 /* (INTERNAL USE ONLY) Specifies that injection is pending and its outcome is unknown. */
48 INPUT_EVENT_INJECTION_PENDING = -1,
49
50 /* Injection succeeded. */
51 INPUT_EVENT_INJECTION_SUCCEEDED = 0,
52
53 /* Injection failed because the injector did not have permission to inject
54 * into the application with input focus. */
55 INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1,
56
57 /* Injection failed because there were no available input targets. */
58 INPUT_EVENT_INJECTION_FAILED = 2,
59
60 /* Injection failed due to a timeout. */
61 INPUT_EVENT_INJECTION_TIMED_OUT = 3
62};
63
Jeff Brown6ec402b2010-07-28 15:48:59 -070064/*
65 * Constants used to determine the input event injection synchronization mode.
66 */
67enum {
68 /* Injection is asynchronous and is assumed always to be successful. */
69 INPUT_EVENT_INJECTION_SYNC_NONE = 0,
70
71 /* Waits for previous events to be dispatched so that the input dispatcher can determine
72 * whether input event injection willbe permitted based on the current input focus.
73 * Does not wait for the input event to finish processing. */
74 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1,
75
76 /* Waits for the input event to be completely processed. */
77 INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED = 2,
78};
79
Jeff Brown7fbdc842010-06-17 20:52:56 -070080
81/*
Jeff Brown9c3cda02010-06-15 01:31:58 -070082 * An input target specifies how an input event is to be dispatched to a particular window
83 * including the window's input channel, control flags, a timeout, and an X / Y offset to
84 * be added to input event coordinates to compensate for the absolute position of the
85 * window area.
86 */
87struct InputTarget {
88 enum {
Jeff Brown519e0242010-09-15 15:18:56 -070089 /* This flag indicates that the event is being delivered to a foreground application. */
Jeff Browna032cc02011-03-07 16:56:21 -080090 FLAG_FOREGROUND = 1 << 0,
Jeff Brown9c3cda02010-06-15 01:31:58 -070091
Jeff Brown85a31762010-09-01 17:01:00 -070092 /* This flag indicates that the target of a MotionEvent is partly or wholly
93 * obscured by another visible window above it. The motion event should be
94 * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */
Jeff Browna032cc02011-03-07 16:56:21 -080095 FLAG_WINDOW_IS_OBSCURED = 1 << 1,
Jeff Brown01ce2e92010-09-26 22:20:12 -070096
97 /* This flag indicates that a motion event is being split across multiple windows. */
Jeff Browna032cc02011-03-07 16:56:21 -080098 FLAG_SPLIT = 1 << 2,
99
Kenny Root7a9db182011-06-02 15:16:05 -0700100 /* This flag indicates that the pointer coordinates dispatched to the application
101 * will be zeroed out to avoid revealing information to an application. This is
102 * used in conjunction with FLAG_DISPATCH_AS_OUTSIDE to prevent apps not sharing
103 * the same UID from watching all touches. */
104 FLAG_ZERO_COORDS = 1 << 3,
105
Jeff Browna032cc02011-03-07 16:56:21 -0800106 /* This flag indicates that the event should be sent as is.
107 * Should always be set unless the event is to be transmuted. */
108 FLAG_DISPATCH_AS_IS = 1 << 8,
109
110 /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside
111 * of the area of this target and so should instead be delivered as an
112 * AMOTION_EVENT_ACTION_OUTSIDE to this target. */
113 FLAG_DISPATCH_AS_OUTSIDE = 1 << 9,
114
115 /* This flag indicates that a hover sequence is starting in the given window.
116 * The event is transmuted into ACTION_HOVER_ENTER. */
117 FLAG_DISPATCH_AS_HOVER_ENTER = 1 << 10,
118
119 /* This flag indicates that a hover event happened outside of a window which handled
120 * previous hover events, signifying the end of the current hover sequence for that
121 * window.
122 * The event is transmuted into ACTION_HOVER_ENTER. */
123 FLAG_DISPATCH_AS_HOVER_EXIT = 1 << 11,
124
Jeff Brown98db5fa2011-06-08 15:37:10 -0700125 /* This flag indicates that the event should be canceled.
126 * It is used to transmute ACTION_MOVE into ACTION_CANCEL when a touch slips
127 * outside of a window. */
128 FLAG_DISPATCH_AS_SLIPPERY_EXIT = 1 << 12,
129
130 /* This flag indicates that the event should be dispatched as an initial down.
131 * It is used to transmute ACTION_MOVE into ACTION_DOWN when a touch slips
132 * into a new window. */
133 FLAG_DISPATCH_AS_SLIPPERY_ENTER = 1 << 13,
134
Jeff Browna032cc02011-03-07 16:56:21 -0800135 /* Mask for all dispatch modes. */
136 FLAG_DISPATCH_MASK = FLAG_DISPATCH_AS_IS
137 | FLAG_DISPATCH_AS_OUTSIDE
138 | FLAG_DISPATCH_AS_HOVER_ENTER
Jeff Brown98db5fa2011-06-08 15:37:10 -0700139 | FLAG_DISPATCH_AS_HOVER_EXIT
140 | FLAG_DISPATCH_AS_SLIPPERY_EXIT
141 | FLAG_DISPATCH_AS_SLIPPERY_ENTER,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700142 };
143
144 // The input channel to be targeted.
145 sp<InputChannel> inputChannel;
146
147 // Flags for the input target.
148 int32_t flags;
149
Jeff Brown9c3cda02010-06-15 01:31:58 -0700150 // The x and y offset to add to a MotionEvent as it is delivered.
151 // (ignored for KeyEvents)
152 float xOffset, yOffset;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700153
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400154 // Scaling factor to apply to MotionEvent as it is delivered.
155 // (ignored for KeyEvents)
156 float scaleFactor;
157
Jeff Brown01ce2e92010-09-26 22:20:12 -0700158 // The subset of pointer ids to include in motion events dispatched to this input target
159 // if FLAG_SPLIT is set.
160 BitSet32 pointerIds;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700161};
162
Jeff Brown7fbdc842010-06-17 20:52:56 -0700163
Jeff Brown9c3cda02010-06-15 01:31:58 -0700164/*
Jeff Brown214eaf42011-05-26 19:17:02 -0700165 * Input dispatcher configuration.
166 *
167 * Specifies various options that modify the behavior of the input dispatcher.
168 */
169struct InputDispatcherConfiguration {
170 // The key repeat initial timeout.
171 nsecs_t keyRepeatTimeout;
172
173 // The key repeat inter-key delay.
174 nsecs_t keyRepeatDelay;
175
Jeff Brown214eaf42011-05-26 19:17:02 -0700176 InputDispatcherConfiguration() :
177 keyRepeatTimeout(500 * 1000000LL),
Jeff Brown30802802012-02-03 13:35:13 -0800178 keyRepeatDelay(50 * 1000000LL) { }
Jeff Brown214eaf42011-05-26 19:17:02 -0700179};
180
181
182/*
Jeff Brown9c3cda02010-06-15 01:31:58 -0700183 * Input dispatcher policy interface.
184 *
185 * The input reader policy is used by the input reader to interact with the Window Manager
186 * and other system components.
187 *
188 * The actual implementation is partially supported by callbacks into the DVM
189 * via JNI. This interface is also mocked in the unit tests.
190 */
191class InputDispatcherPolicyInterface : public virtual RefBase {
192protected:
193 InputDispatcherPolicyInterface() { }
194 virtual ~InputDispatcherPolicyInterface() { }
195
196public:
197 /* Notifies the system that a configuration change has occurred. */
198 virtual void notifyConfigurationChanged(nsecs_t when) = 0;
199
Jeff Brownb88102f2010-09-08 11:49:43 -0700200 /* Notifies the system that an application is not responding.
201 * Returns a new timeout to continue waiting, or 0 to abort dispatch. */
Jeff Brown519e0242010-09-15 15:18:56 -0700202 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -0800203 const sp<InputWindowHandle>& inputWindowHandle) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700204
Jeff Brown9c3cda02010-06-15 01:31:58 -0700205 /* Notifies the system that an input channel is unrecoverably broken. */
Jeff Brown928e0542011-01-10 11:17:36 -0800206 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700207
Jeff Brown214eaf42011-05-26 19:17:02 -0700208 /* Gets the input dispatcher configuration. */
209 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700210
Jeff Brown214eaf42011-05-26 19:17:02 -0700211 /* Returns true if automatic key repeating is enabled. */
212 virtual bool isKeyRepeatEnabled() = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700213
Jeff Brown0029c662011-03-30 02:25:18 -0700214 /* Filters an input event.
215 * Return true to dispatch the event unmodified, false to consume the event.
216 * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED
217 * to injectInputEvent.
218 */
219 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) = 0;
220
Jeff Brownb6997262010-10-08 22:31:17 -0700221 /* Intercepts a key event immediately before queueing it.
222 * The policy can use this method as an opportunity to perform power management functions
223 * and early event preprocessing such as updating policy flags.
224 *
225 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
226 * should be dispatched to applications.
227 */
Jeff Brown1f245102010-11-18 20:53:46 -0800228 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700229
Jeff Brown56194eb2011-03-02 19:23:13 -0800230 /* Intercepts a touch, trackball or other motion event before queueing it.
Jeff Brownb6997262010-10-08 22:31:17 -0700231 * The policy can use this method as an opportunity to perform power management functions
232 * and early event preprocessing such as updating policy flags.
233 *
234 * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event
235 * should be dispatched to applications.
236 */
Jeff Brown56194eb2011-03-02 19:23:13 -0800237 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) = 0;
Jeff Brownb6997262010-10-08 22:31:17 -0700238
Jeff Brownb88102f2010-09-08 11:49:43 -0700239 /* Allows the policy a chance to intercept a key before dispatching. */
Jeff Brown905805a2011-10-12 13:57:59 -0700240 virtual nsecs_t interceptKeyBeforeDispatching(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700241 const KeyEvent* keyEvent, uint32_t policyFlags) = 0;
242
Jeff Brown49ed71d2010-12-06 17:13:33 -0800243 /* Allows the policy a chance to perform default processing for an unhandled key.
244 * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */
Jeff Brown928e0542011-01-10 11:17:36 -0800245 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800246 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0;
Jeff Brown3915bb82010-11-05 15:02:16 -0700247
Jeff Brownb6997262010-10-08 22:31:17 -0700248 /* Notifies the policy about switch events.
249 */
250 virtual void notifySwitch(nsecs_t when,
251 int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0;
252
Jeff Brownb88102f2010-09-08 11:49:43 -0700253 /* Poke user activity for an event dispatched to a window. */
Jeff Brown01ce2e92010-09-26 22:20:12 -0700254 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700255
256 /* Checks whether a given application pid/uid has permission to inject input events
257 * into other applications.
258 *
259 * This method is special in that its implementation promises to be non-reentrant and
260 * is safe to call while holding other locks. (Most other methods make no such guarantees!)
261 */
262 virtual bool checkInjectEventsPermissionNonReentrant(
263 int32_t injectorPid, int32_t injectorUid) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700264};
265
266
Jeff Brown46b9ac02010-04-22 18:58:52 -0700267/* Notifies the system about input events generated by the input reader.
268 * The dispatcher is expected to be mostly asynchronous. */
Jeff Brownbe1aa822011-07-27 16:04:54 -0700269class InputDispatcherInterface : public virtual RefBase, public InputListenerInterface {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700270protected:
271 InputDispatcherInterface() { }
272 virtual ~InputDispatcherInterface() { }
273
274public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700275 /* Dumps the state of the input dispatcher.
276 *
277 * This method may be called on any thread (usually by the input manager). */
278 virtual void dump(String8& dump) = 0;
279
Jeff Brown89ef0722011-08-10 16:25:21 -0700280 /* Called by the heatbeat to ensures that the dispatcher has not deadlocked. */
281 virtual void monitor() = 0;
282
Jeff Brown46b9ac02010-04-22 18:58:52 -0700283 /* Runs a single iteration of the dispatch loop.
284 * Nominally processes one queued event, a timeout, or a response from an input consumer.
285 *
286 * This method should only be called on the input dispatcher thread.
287 */
288 virtual void dispatchOnce() = 0;
289
Jeff Brown7fbdc842010-06-17 20:52:56 -0700290 /* Injects an input event and optionally waits for sync.
Jeff Brown6ec402b2010-07-28 15:48:59 -0700291 * The synchronization mode determines whether the method blocks while waiting for
292 * input injection to proceed.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700293 * Returns one of the INPUT_EVENT_INJECTION_XXX constants.
294 *
295 * This method may be called on any thread (usually by the input manager).
296 */
297 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown0029c662011-03-30 02:25:18 -0700298 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
299 uint32_t policyFlags) = 0;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700300
Jeff Brownb88102f2010-09-08 11:49:43 -0700301 /* Sets the list of input windows.
302 *
303 * This method may be called on any thread (usually by the input manager).
304 */
Jeff Brown9302c872011-07-13 22:51:29 -0700305 virtual void setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700306
307 /* Sets the focused application.
308 *
309 * This method may be called on any thread (usually by the input manager).
310 */
Jeff Brown9302c872011-07-13 22:51:29 -0700311 virtual void setFocusedApplication(
312 const sp<InputApplicationHandle>& inputApplicationHandle) = 0;
Jeff Brownb88102f2010-09-08 11:49:43 -0700313
314 /* Sets the input dispatching mode.
315 *
316 * This method may be called on any thread (usually by the input manager).
317 */
318 virtual void setInputDispatchMode(bool enabled, bool frozen) = 0;
319
Jeff Brown0029c662011-03-30 02:25:18 -0700320 /* Sets whether input event filtering is enabled.
321 * When enabled, incoming input events are sent to the policy's filterInputEvent
322 * method instead of being dispatched. The filter is expected to use
323 * injectInputEvent to inject the events it would like to have dispatched.
324 * It should include POLICY_FLAG_FILTERED in the policy flags during injection.
325 */
326 virtual void setInputFilterEnabled(bool enabled) = 0;
327
Jeff Browne6504122010-09-27 14:52:15 -0700328 /* Transfers touch focus from the window associated with one channel to the
329 * window associated with the other channel.
330 *
331 * Returns true on success. False if the window did not actually have touch focus.
332 */
333 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
334 const sp<InputChannel>& toChannel) = 0;
335
Jeff Brown46b9ac02010-04-22 18:58:52 -0700336 /* Registers or unregister input channels that may be used as targets for input events.
Jeff Brownb88102f2010-09-08 11:49:43 -0700337 * If monitor is true, the channel will receive a copy of all input events.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700338 *
339 * These methods may be called on any thread (usually by the input manager).
340 */
Jeff Brown928e0542011-01-10 11:17:36 -0800341 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
342 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700343 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0;
344};
345
Jeff Brown9c3cda02010-06-15 01:31:58 -0700346/* Dispatches events to input targets. Some functions of the input dispatcher, such as
347 * identifying input targets, are controlled by a separate policy object.
348 *
349 * IMPORTANT INVARIANT:
350 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
351 * the input dispatcher never calls into the policy while holding its internal locks.
352 * The implementation is also carefully designed to recover from scenarios such as an
353 * input channel becoming unregistered while identifying input targets or processing timeouts.
354 *
355 * Methods marked 'Locked' must be called with the lock acquired.
356 *
357 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
358 * may during the course of their execution release the lock, call into the policy, and
359 * then reacquire the lock. The caller is responsible for recovering gracefully.
360 *
361 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
362 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700363class InputDispatcher : public InputDispatcherInterface {
364protected:
365 virtual ~InputDispatcher();
366
367public:
Jeff Brown9c3cda02010-06-15 01:31:58 -0700368 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700369
Jeff Brownb88102f2010-09-08 11:49:43 -0700370 virtual void dump(String8& dump);
Jeff Brown89ef0722011-08-10 16:25:21 -0700371 virtual void monitor();
Jeff Brownb88102f2010-09-08 11:49:43 -0700372
Jeff Brown46b9ac02010-04-22 18:58:52 -0700373 virtual void dispatchOnce();
374
Jeff Brownbe1aa822011-07-27 16:04:54 -0700375 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args);
376 virtual void notifyKey(const NotifyKeyArgs* args);
377 virtual void notifyMotion(const NotifyMotionArgs* args);
378 virtual void notifySwitch(const NotifySwitchArgs* args);
Jeff Brown65fd2512011-08-18 11:20:58 -0700379 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700380
Jeff Brown7fbdc842010-06-17 20:52:56 -0700381 virtual int32_t injectInputEvent(const InputEvent* event,
Jeff Brown0029c662011-03-30 02:25:18 -0700382 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
383 uint32_t policyFlags);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700384
Jeff Brown9302c872011-07-13 22:51:29 -0700385 virtual void setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles);
386 virtual void setFocusedApplication(const sp<InputApplicationHandle>& inputApplicationHandle);
Jeff Brownb88102f2010-09-08 11:49:43 -0700387 virtual void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown0029c662011-03-30 02:25:18 -0700388 virtual void setInputFilterEnabled(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700389
Jeff Browne6504122010-09-27 14:52:15 -0700390 virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel,
391 const sp<InputChannel>& toChannel);
392
Jeff Brown928e0542011-01-10 11:17:36 -0800393 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel,
394 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700395 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel);
396
397private:
398 template <typename T>
399 struct Link {
400 T* next;
401 T* prev;
Jeff Brown3241b6b2012-02-03 15:08:02 -0800402
403 protected:
404 inline Link() : next(NULL), prev(NULL) { }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700405 };
406
Jeff Brown01ce2e92010-09-26 22:20:12 -0700407 struct InjectionState {
408 mutable int32_t refCount;
409
410 int32_t injectorPid;
411 int32_t injectorUid;
412 int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING
413 bool injectionIsAsync; // set to true if injection is not waiting for the result
414 int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress
Jeff Brownac386072011-07-20 15:19:50 -0700415
416 InjectionState(int32_t injectorPid, int32_t injectorUid);
417 void release();
418
419 private:
420 ~InjectionState();
Jeff Brown01ce2e92010-09-26 22:20:12 -0700421 };
422
Jeff Brown46b9ac02010-04-22 18:58:52 -0700423 struct EventEntry : Link<EventEntry> {
424 enum {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700425 TYPE_CONFIGURATION_CHANGED,
Jeff Brown65fd2512011-08-18 11:20:58 -0700426 TYPE_DEVICE_RESET,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700427 TYPE_KEY,
428 TYPE_MOTION
429 };
430
Jeff Brown01ce2e92010-09-26 22:20:12 -0700431 mutable int32_t refCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700432 int32_t type;
433 nsecs_t eventTime;
Jeff Brownb6997262010-10-08 22:31:17 -0700434 uint32_t policyFlags;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700435 InjectionState* injectionState;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700436
Jeff Brown9c3cda02010-06-15 01:31:58 -0700437 bool dispatchInProgress; // initially false, set to true while dispatching
Jeff Brown7fbdc842010-06-17 20:52:56 -0700438
Jeff Brown4e91a182011-04-07 11:38:09 -0700439 inline bool isInjected() const { return injectionState != NULL; }
Jeff Brownac386072011-07-20 15:19:50 -0700440
441 void release();
442
Jeff Brown265f1cc2012-06-11 18:01:06 -0700443 virtual void appendDescription(String8& msg) const = 0;
444
Jeff Brownac386072011-07-20 15:19:50 -0700445 protected:
446 EventEntry(int32_t type, nsecs_t eventTime, uint32_t policyFlags);
447 virtual ~EventEntry();
448 void releaseInjectionState();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700449 };
450
451 struct ConfigurationChangedEntry : EventEntry {
Jeff Brownac386072011-07-20 15:19:50 -0700452 ConfigurationChangedEntry(nsecs_t eventTime);
Jeff Brown265f1cc2012-06-11 18:01:06 -0700453 virtual void appendDescription(String8& msg) const;
Jeff Brownac386072011-07-20 15:19:50 -0700454
455 protected:
456 virtual ~ConfigurationChangedEntry();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700457 };
458
Jeff Brown65fd2512011-08-18 11:20:58 -0700459 struct DeviceResetEntry : EventEntry {
460 int32_t deviceId;
461
462 DeviceResetEntry(nsecs_t eventTime, int32_t deviceId);
Jeff Brown265f1cc2012-06-11 18:01:06 -0700463 virtual void appendDescription(String8& msg) const;
Jeff Brown65fd2512011-08-18 11:20:58 -0700464
465 protected:
466 virtual ~DeviceResetEntry();
467 };
468
Jeff Brown46b9ac02010-04-22 18:58:52 -0700469 struct KeyEntry : EventEntry {
470 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800471 uint32_t source;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700472 int32_t action;
473 int32_t flags;
474 int32_t keyCode;
475 int32_t scanCode;
476 int32_t metaState;
477 int32_t repeatCount;
478 nsecs_t downTime;
Jeff Brownb88102f2010-09-08 11:49:43 -0700479
480 bool syntheticRepeat; // set to true for synthetic key repeats
481
482 enum InterceptKeyResult {
483 INTERCEPT_KEY_RESULT_UNKNOWN,
484 INTERCEPT_KEY_RESULT_SKIP,
485 INTERCEPT_KEY_RESULT_CONTINUE,
Jeff Brown905805a2011-10-12 13:57:59 -0700486 INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER,
Jeff Brownb88102f2010-09-08 11:49:43 -0700487 };
488 InterceptKeyResult interceptKeyResult; // set based on the interception result
Jeff Brown905805a2011-10-12 13:57:59 -0700489 nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER
Jeff Brownac386072011-07-20 15:19:50 -0700490
491 KeyEntry(nsecs_t eventTime,
492 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
493 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
494 int32_t repeatCount, nsecs_t downTime);
Jeff Brown265f1cc2012-06-11 18:01:06 -0700495 virtual void appendDescription(String8& msg) const;
Jeff Brownac386072011-07-20 15:19:50 -0700496 void recycle();
497
498 protected:
499 virtual ~KeyEntry();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700500 };
501
Jeff Brown46b9ac02010-04-22 18:58:52 -0700502 struct MotionEntry : EventEntry {
Jeff Brown3241b6b2012-02-03 15:08:02 -0800503 nsecs_t eventTime;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700504 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800505 uint32_t source;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700506 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -0700507 int32_t flags;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700508 int32_t metaState;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700509 int32_t buttonState;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700510 int32_t edgeFlags;
511 float xPrecision;
512 float yPrecision;
513 nsecs_t downTime;
514 uint32_t pointerCount;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700515 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brown3241b6b2012-02-03 15:08:02 -0800516 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownae9fc032010-08-18 15:51:08 -0700517
Jeff Brownac386072011-07-20 15:19:50 -0700518 MotionEntry(nsecs_t eventTime,
519 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
520 int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
521 float xPrecision, float yPrecision,
522 nsecs_t downTime, uint32_t pointerCount,
523 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords);
Jeff Brown265f1cc2012-06-11 18:01:06 -0700524 virtual void appendDescription(String8& msg) const;
Jeff Brownac386072011-07-20 15:19:50 -0700525
Jeff Brownac386072011-07-20 15:19:50 -0700526 protected:
527 virtual ~MotionEntry();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700528 };
529
Jeff Brown9c3cda02010-06-15 01:31:58 -0700530 // Tracks the progress of dispatching a particular event to a particular connection.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700531 struct DispatchEntry : Link<DispatchEntry> {
Jeff Brown072ec962012-02-07 14:46:57 -0800532 const uint32_t seq; // unique sequence number, never 0
533
Jeff Brown46b9ac02010-04-22 18:58:52 -0700534 EventEntry* eventEntry; // the event to dispatch
535 int32_t targetFlags;
536 float xOffset;
537 float yOffset;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400538 float scaleFactor;
Jeff Brown265f1cc2012-06-11 18:01:06 -0700539 nsecs_t deliveryTime; // time when the event was actually delivered
Jeff Brown46b9ac02010-04-22 18:58:52 -0700540
Jeff Brown81346812011-06-28 20:08:48 -0700541 // Set to the resolved action and flags when the event is enqueued.
542 int32_t resolvedAction;
543 int32_t resolvedFlags;
544
Jeff Brownac386072011-07-20 15:19:50 -0700545 DispatchEntry(EventEntry* eventEntry,
546 int32_t targetFlags, float xOffset, float yOffset, float scaleFactor);
547 ~DispatchEntry();
548
Jeff Brown519e0242010-09-15 15:18:56 -0700549 inline bool hasForegroundTarget() const {
550 return targetFlags & InputTarget::FLAG_FOREGROUND;
Jeff Brownb88102f2010-09-08 11:49:43 -0700551 }
Jeff Brown01ce2e92010-09-26 22:20:12 -0700552
553 inline bool isSplit() const {
554 return targetFlags & InputTarget::FLAG_SPLIT;
555 }
Jeff Brown072ec962012-02-07 14:46:57 -0800556
557 private:
558 static volatile int32_t sNextSeqAtomic;
559
560 static uint32_t nextSeq();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700561 };
562
Jeff Brown9c3cda02010-06-15 01:31:58 -0700563 // A command entry captures state and behavior for an action to be performed in the
564 // dispatch loop after the initial processing has taken place. It is essentially
565 // a kind of continuation used to postpone sensitive policy interactions to a point
566 // in the dispatch loop where it is safe to release the lock (generally after finishing
567 // the critical parts of the dispatch cycle).
568 //
569 // The special thing about commands is that they can voluntarily release and reacquire
570 // the dispatcher lock at will. Initially when the command starts running, the
571 // dispatcher lock is held. However, if the command needs to call into the policy to
572 // do some work, it can release the lock, do the work, then reacquire the lock again
573 // before returning.
574 //
575 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch
576 // never calls into the policy while holding its lock.
577 //
578 // Commands are implicitly 'LockedInterruptible'.
579 struct CommandEntry;
580 typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry);
581
Jeff Brown7fbdc842010-06-17 20:52:56 -0700582 class Connection;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700583 struct CommandEntry : Link<CommandEntry> {
Jeff Brownac386072011-07-20 15:19:50 -0700584 CommandEntry(Command command);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700585 ~CommandEntry();
586
587 Command command;
588
589 // parameters for the command (usage varies by command)
Jeff Brown7fbdc842010-06-17 20:52:56 -0700590 sp<Connection> connection;
Jeff Brownb88102f2010-09-08 11:49:43 -0700591 nsecs_t eventTime;
592 KeyEntry* keyEntry;
Jeff Brownb88102f2010-09-08 11:49:43 -0700593 sp<InputApplicationHandle> inputApplicationHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800594 sp<InputWindowHandle> inputWindowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700595 int32_t userActivityEventType;
Jeff Brown072ec962012-02-07 14:46:57 -0800596 uint32_t seq;
Jeff Brown3915bb82010-11-05 15:02:16 -0700597 bool handled;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700598 };
599
600 // Generic queue implementation.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700601 template <typename T>
602 struct Queue {
Jeff Brownac386072011-07-20 15:19:50 -0700603 T* head;
604 T* tail;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700605
Jeff Brownac386072011-07-20 15:19:50 -0700606 inline Queue() : head(NULL), tail(NULL) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700607 }
608
Jeff Brownb88102f2010-09-08 11:49:43 -0700609 inline bool isEmpty() const {
Jeff Brownac386072011-07-20 15:19:50 -0700610 return !head;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700611 }
612
613 inline void enqueueAtTail(T* entry) {
Jeff Brownac386072011-07-20 15:19:50 -0700614 entry->prev = tail;
615 if (tail) {
616 tail->next = entry;
617 } else {
618 head = entry;
619 }
620 entry->next = NULL;
621 tail = entry;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700622 }
623
624 inline void enqueueAtHead(T* entry) {
Jeff Brownac386072011-07-20 15:19:50 -0700625 entry->next = head;
626 if (head) {
627 head->prev = entry;
628 } else {
629 tail = entry;
630 }
631 entry->prev = NULL;
632 head = entry;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700633 }
634
635 inline void dequeue(T* entry) {
Jeff Brownac386072011-07-20 15:19:50 -0700636 if (entry->prev) {
637 entry->prev->next = entry->next;
638 } else {
639 head = entry->next;
640 }
641 if (entry->next) {
642 entry->next->prev = entry->prev;
643 } else {
644 tail = entry->prev;
645 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700646 }
647
648 inline T* dequeueAtHead() {
Jeff Brownac386072011-07-20 15:19:50 -0700649 T* entry = head;
650 head = entry->next;
651 if (head) {
652 head->prev = NULL;
653 } else {
654 tail = NULL;
655 }
656 return entry;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700657 }
Jeff Brown519e0242010-09-15 15:18:56 -0700658
659 uint32_t count() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700660 };
661
Jeff Brownda3d5a92011-03-29 15:11:34 -0700662 /* Specifies which events are to be canceled and why. */
663 struct CancelationOptions {
664 enum Mode {
Jeff Brownb6997262010-10-08 22:31:17 -0700665 CANCEL_ALL_EVENTS = 0,
666 CANCEL_POINTER_EVENTS = 1,
667 CANCEL_NON_POINTER_EVENTS = 2,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800668 CANCEL_FALLBACK_EVENTS = 3,
Jeff Brownb6997262010-10-08 22:31:17 -0700669 };
670
Jeff Brownda3d5a92011-03-29 15:11:34 -0700671 // The criterion to use to determine which events should be canceled.
672 Mode mode;
673
674 // Descriptive reason for the cancelation.
675 const char* reason;
676
677 // The specific keycode of the key event to cancel, or -1 to cancel any key event.
678 int32_t keyCode;
679
Jeff Brown65fd2512011-08-18 11:20:58 -0700680 // The specific device id of events to cancel, or -1 to cancel events from any device.
681 int32_t deviceId;
682
Jeff Brownda3d5a92011-03-29 15:11:34 -0700683 CancelationOptions(Mode mode, const char* reason) :
Jeff Brown65fd2512011-08-18 11:20:58 -0700684 mode(mode), reason(reason), keyCode(-1), deviceId(-1) { }
Jeff Brownda3d5a92011-03-29 15:11:34 -0700685 };
686
687 /* Tracks dispatched key and motion event state so that cancelation events can be
688 * synthesized when events are dropped. */
689 class InputState {
690 public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700691 InputState();
692 ~InputState();
693
694 // Returns true if there is no state to be canceled.
695 bool isNeutral() const;
696
Jeff Brown81346812011-06-28 20:08:48 -0700697 // Returns true if the specified source is known to have received a hover enter
698 // motion event.
699 bool isHovering(int32_t deviceId, uint32_t source) const;
Jeff Brownb88102f2010-09-08 11:49:43 -0700700
701 // Records tracking information for a key event that has just been published.
Jeff Brown81346812011-06-28 20:08:48 -0700702 // Returns true if the event should be delivered, false if it is inconsistent
703 // and should be skipped.
704 bool trackKey(const KeyEntry* entry, int32_t action, int32_t flags);
Jeff Brownb88102f2010-09-08 11:49:43 -0700705
706 // Records tracking information for a motion event that has just been published.
Jeff Brown81346812011-06-28 20:08:48 -0700707 // Returns true if the event should be delivered, false if it is inconsistent
708 // and should be skipped.
709 bool trackMotion(const MotionEntry* entry, int32_t action, int32_t flags);
Jeff Brownb88102f2010-09-08 11:49:43 -0700710
Jeff Brownb6997262010-10-08 22:31:17 -0700711 // Synthesizes cancelation events for the current state and resets the tracked state.
Jeff Brownac386072011-07-20 15:19:50 -0700712 void synthesizeCancelationEvents(nsecs_t currentTime,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700713 Vector<EventEntry*>& outEvents, const CancelationOptions& options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700714
715 // Clears the current state.
716 void clear();
717
Jeff Brown9c9f1a32010-10-11 18:32:20 -0700718 // Copies pointer-related parts of the input state to another instance.
719 void copyPointerStateTo(InputState& other) const;
720
Jeff Brownda3d5a92011-03-29 15:11:34 -0700721 // Gets the fallback key associated with a keycode.
722 // Returns -1 if none.
723 // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy.
724 int32_t getFallbackKey(int32_t originalKeyCode);
725
726 // Sets the fallback key for a particular keycode.
727 void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode);
728
729 // Removes the fallback key for a particular keycode.
730 void removeFallbackKey(int32_t originalKeyCode);
731
732 inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const {
733 return mFallbackKeys;
734 }
735
Jeff Brownb88102f2010-09-08 11:49:43 -0700736 private:
Jeff Brownb88102f2010-09-08 11:49:43 -0700737 struct KeyMemento {
738 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800739 uint32_t source;
Jeff Brownb88102f2010-09-08 11:49:43 -0700740 int32_t keyCode;
741 int32_t scanCode;
Jeff Brownfd23e3e2012-05-09 13:34:28 -0700742 int32_t metaState;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800743 int32_t flags;
Jeff Brownb88102f2010-09-08 11:49:43 -0700744 nsecs_t downTime;
Jeff Brownfd23e3e2012-05-09 13:34:28 -0700745 uint32_t policyFlags;
Jeff Brownb88102f2010-09-08 11:49:43 -0700746 };
747
748 struct MotionMemento {
749 int32_t deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -0800750 uint32_t source;
Jeff Brown81346812011-06-28 20:08:48 -0700751 int32_t flags;
Jeff Brownb88102f2010-09-08 11:49:43 -0700752 float xPrecision;
753 float yPrecision;
754 nsecs_t downTime;
755 uint32_t pointerCount;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700756 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownb88102f2010-09-08 11:49:43 -0700757 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Browna032cc02011-03-07 16:56:21 -0800758 bool hovering;
Jeff Brownfd23e3e2012-05-09 13:34:28 -0700759 uint32_t policyFlags;
Jeff Brownb88102f2010-09-08 11:49:43 -0700760
761 void setPointers(const MotionEntry* entry);
762 };
763
764 Vector<KeyMemento> mKeyMementos;
765 Vector<MotionMemento> mMotionMementos;
Jeff Brownda3d5a92011-03-29 15:11:34 -0700766 KeyedVector<int32_t, int32_t> mFallbackKeys;
Jeff Brownb6997262010-10-08 22:31:17 -0700767
Jeff Brown81346812011-06-28 20:08:48 -0700768 ssize_t findKeyMemento(const KeyEntry* entry) const;
769 ssize_t findMotionMemento(const MotionEntry* entry, bool hovering) const;
770
771 void addKeyMemento(const KeyEntry* entry, int32_t flags);
772 void addMotionMemento(const MotionEntry* entry, int32_t flags, bool hovering);
773
Jeff Brown49ed71d2010-12-06 17:13:33 -0800774 static bool shouldCancelKey(const KeyMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700775 const CancelationOptions& options);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800776 static bool shouldCancelMotion(const MotionMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -0700777 const CancelationOptions& options);
Jeff Brownb88102f2010-09-08 11:49:43 -0700778 };
779
Jeff Brown46b9ac02010-04-22 18:58:52 -0700780 /* Manages the dispatch state associated with a single input channel. */
781 class Connection : public RefBase {
782 protected:
783 virtual ~Connection();
784
785 public:
786 enum Status {
787 // Everything is peachy.
788 STATUS_NORMAL,
789 // An unrecoverable communication error has occurred.
790 STATUS_BROKEN,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700791 // The input channel has been unregistered.
792 STATUS_ZOMBIE
793 };
794
795 Status status;
Jeff Brown928e0542011-01-10 11:17:36 -0800796 sp<InputChannel> inputChannel; // never null
797 sp<InputWindowHandle> inputWindowHandle; // may be null
Jeff Browncc4f7db2011-08-30 20:34:48 -0700798 bool monitor;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700799 InputPublisher inputPublisher;
Jeff Brownb88102f2010-09-08 11:49:43 -0700800 InputState inputState;
Jeff Brownd1c48a02012-02-06 19:12:47 -0800801
802 // True if the socket is full and no further events can be published until
803 // the application consumes some of the input.
804 bool inputPublisherBlocked;
805
806 // Queue of events that need to be published to the connection.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700807 Queue<DispatchEntry> outboundQueue;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700808
Jeff Brownd1c48a02012-02-06 19:12:47 -0800809 // Queue of events that have been published to the connection but that have not
810 // yet received a "finished" response from the application.
811 Queue<DispatchEntry> waitQueue;
812
Jeff Brown928e0542011-01-10 11:17:36 -0800813 explicit Connection(const sp<InputChannel>& inputChannel,
Jeff Browncc4f7db2011-08-30 20:34:48 -0700814 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700815
Jeff Brown9c3cda02010-06-15 01:31:58 -0700816 inline const char* getInputChannelName() const { return inputChannel->getName().string(); }
817
Jeff Brown481c1572012-03-09 14:41:15 -0800818 const char* getWindowName() const;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700819 const char* getStatusLabel() const;
Jeff Brown072ec962012-02-07 14:46:57 -0800820
821 DispatchEntry* findWaitQueueEntry(uint32_t seq);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700822 };
823
Jeff Brownb6997262010-10-08 22:31:17 -0700824 enum DropReason {
825 DROP_REASON_NOT_DROPPED = 0,
826 DROP_REASON_POLICY = 1,
827 DROP_REASON_APP_SWITCH = 2,
828 DROP_REASON_DISABLED = 3,
Jeff Brown928e0542011-01-10 11:17:36 -0800829 DROP_REASON_BLOCKED = 4,
830 DROP_REASON_STALE = 5,
Jeff Brownb6997262010-10-08 22:31:17 -0700831 };
832
Jeff Brown9c3cda02010-06-15 01:31:58 -0700833 sp<InputDispatcherPolicyInterface> mPolicy;
Jeff Brown214eaf42011-05-26 19:17:02 -0700834 InputDispatcherConfiguration mConfig;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700835
836 Mutex mLock;
837
Jeff Brown112b5f52012-01-27 17:32:06 -0800838 Condition mDispatcherIsAliveCondition;
839
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700840 sp<Looper> mLooper;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700841
Jeff Brownb88102f2010-09-08 11:49:43 -0700842 EventEntry* mPendingEvent;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700843 Queue<EventEntry> mInboundQueue;
844 Queue<CommandEntry> mCommandQueue;
845
Jeff Brown214eaf42011-05-26 19:17:02 -0700846 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700847
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700848 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Jeff Brownb88102f2010-09-08 11:49:43 -0700849 bool enqueueInboundEventLocked(EventEntry* entry);
850
Jeff Brownb6997262010-10-08 22:31:17 -0700851 // Cleans up input state when dropping an inbound event.
852 void dropInboundEventLocked(EventEntry* entry, DropReason dropReason);
853
Jeff Brownb88102f2010-09-08 11:49:43 -0700854 // App switch latency optimization.
Jeff Brownb6997262010-10-08 22:31:17 -0700855 bool mAppSwitchSawKeyDown;
Jeff Brownb88102f2010-09-08 11:49:43 -0700856 nsecs_t mAppSwitchDueTime;
857
Jeff Brownb6997262010-10-08 22:31:17 -0700858 static bool isAppSwitchKeyCode(int32_t keyCode);
859 bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700860 bool isAppSwitchPendingLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700861 void resetPendingAppSwitchLocked(bool handled);
862
Jeff Brown928e0542011-01-10 11:17:36 -0800863 // Stale event latency optimization.
864 static bool isStaleEventLocked(nsecs_t currentTime, EventEntry* entry);
865
866 // Blocked event latency optimization. Drops old events when the user intends
867 // to transfer focus to a new application.
868 EventEntry* mNextUnblockedEvent;
869
Jeff Brown9302c872011-07-13 22:51:29 -0700870 sp<InputWindowHandle> findTouchedWindowAtLocked(int32_t x, int32_t y);
Jeff Brown928e0542011-01-10 11:17:36 -0800871
Jeff Browncbee6d62012-02-03 20:11:27 -0800872 // All registered connections mapped by channel file descriptor.
873 KeyedVector<int, sp<Connection> > mConnectionsByFd;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700874
Jeff Brown519e0242010-09-15 15:18:56 -0700875 ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel);
Jeff Brown2cbecea2010-08-17 15:59:26 -0700876
Jeff Brownb88102f2010-09-08 11:49:43 -0700877 // Input channels that will receive a copy of all input events.
878 Vector<sp<InputChannel> > mMonitoringChannels;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700879
Jeff Brown7fbdc842010-06-17 20:52:56 -0700880 // Event injection and synchronization.
881 Condition mInjectionResultAvailableCondition;
Jeff Brownb6997262010-10-08 22:31:17 -0700882 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700883 void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
884
Jeff Brown6ec402b2010-07-28 15:48:59 -0700885 Condition mInjectionSyncFinishedCondition;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700886 void incrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -0700887 void decrementPendingForegroundDispatchesLocked(EventEntry* entry);
Jeff Brown6ec402b2010-07-28 15:48:59 -0700888
Jeff Brown46b9ac02010-04-22 18:58:52 -0700889 // Key repeat tracking.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700890 struct KeyRepeatState {
891 KeyEntry* lastKeyEntry; // or null if no repeat
892 nsecs_t nextRepeatTime;
893 } mKeyRepeatState;
894
895 void resetKeyRepeatLocked();
Jeff Brown214eaf42011-05-26 19:17:02 -0700896 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700897
Jeff Brown9c3cda02010-06-15 01:31:58 -0700898 // Deferred command processing.
899 bool runCommandsLockedInterruptible();
900 CommandEntry* postCommandLocked(Command command);
901
Jeff Brownb88102f2010-09-08 11:49:43 -0700902 // Inbound event processing.
903 void drainInboundQueueLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700904 void releasePendingEventLocked();
905 void releaseInboundEventLocked(EventEntry* entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700906
Jeff Brownb88102f2010-09-08 11:49:43 -0700907 // Dispatch state.
908 bool mDispatchEnabled;
909 bool mDispatchFrozen;
Jeff Brown0029c662011-03-30 02:25:18 -0700910 bool mInputFilterEnabled;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700911
Jeff Brown9302c872011-07-13 22:51:29 -0700912 Vector<sp<InputWindowHandle> > mWindowHandles;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700913
Jeff Brown9302c872011-07-13 22:51:29 -0700914 sp<InputWindowHandle> getWindowHandleLocked(const sp<InputChannel>& inputChannel) const;
915 bool hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const;
Jeff Brownb88102f2010-09-08 11:49:43 -0700916
917 // Focus tracking for keys, trackball, etc.
Jeff Brown9302c872011-07-13 22:51:29 -0700918 sp<InputWindowHandle> mFocusedWindowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700919
920 // Focus tracking for touch.
Jeff Brown01ce2e92010-09-26 22:20:12 -0700921 struct TouchedWindow {
Jeff Brown9302c872011-07-13 22:51:29 -0700922 sp<InputWindowHandle> windowHandle;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700923 int32_t targetFlags;
Jeff Brown46e75292010-11-10 16:53:45 -0800924 BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set
Jeff Brownb88102f2010-09-08 11:49:43 -0700925 };
Jeff Brown01ce2e92010-09-26 22:20:12 -0700926 struct TouchState {
927 bool down;
928 bool split;
Jeff Brown95712852011-01-04 19:41:59 -0800929 int32_t deviceId; // id of the device that is currently down, others are rejected
Jeff Brown58a2da82011-01-25 16:02:22 -0800930 uint32_t source; // source of the device that is current down, others are rejected
Jeff Brown01ce2e92010-09-26 22:20:12 -0700931 Vector<TouchedWindow> windows;
932
933 TouchState();
934 ~TouchState();
935 void reset();
936 void copyFrom(const TouchState& other);
Jeff Brown9302c872011-07-13 22:51:29 -0700937 void addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle,
938 int32_t targetFlags, BitSet32 pointerIds);
Jeff Brownf44e3942012-04-20 11:33:27 -0700939 void removeWindow(const sp<InputWindowHandle>& windowHandle);
Jeff Browna032cc02011-03-07 16:56:21 -0800940 void filterNonAsIsTouchWindows();
Jeff Brown9302c872011-07-13 22:51:29 -0700941 sp<InputWindowHandle> getFirstForegroundWindowHandle() const;
Jeff Brown98db5fa2011-06-08 15:37:10 -0700942 bool isSlippery() const;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700943 };
944
945 TouchState mTouchState;
946 TouchState mTempTouchState;
Jeff Brownb88102f2010-09-08 11:49:43 -0700947
948 // Focused application.
Jeff Brown9302c872011-07-13 22:51:29 -0700949 sp<InputApplicationHandle> mFocusedApplicationHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700950
Jeff Brown22aa5122012-06-17 12:01:06 -0700951 // Dispatcher state at time of last ANR.
952 String8 mLastANRState;
953
Jeff Brownb88102f2010-09-08 11:49:43 -0700954 // Dispatch inbound events.
955 bool dispatchConfigurationChangedLocked(
956 nsecs_t currentTime, ConfigurationChangedEntry* entry);
Jeff Brown65fd2512011-08-18 11:20:58 -0700957 bool dispatchDeviceResetLocked(
958 nsecs_t currentTime, DeviceResetEntry* entry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700959 bool dispatchKeyLocked(
Jeff Brown214eaf42011-05-26 19:17:02 -0700960 nsecs_t currentTime, KeyEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700961 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700962 bool dispatchMotionLocked(
963 nsecs_t currentTime, MotionEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700964 DropReason* dropReason, nsecs_t* nextWakeupTime);
Jeff Browne9bb9be2012-02-06 15:47:55 -0800965 void dispatchEventLocked(nsecs_t currentTime, EventEntry* entry,
966 const Vector<InputTarget>& inputTargets);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700967
Jeff Brownb88102f2010-09-08 11:49:43 -0700968 void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry);
969 void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry);
970
Jeff Browne9bb9be2012-02-06 15:47:55 -0800971 // Keeping track of ANR timeouts.
Jeff Brownb88102f2010-09-08 11:49:43 -0700972 enum InputTargetWaitCause {
973 INPUT_TARGET_WAIT_CAUSE_NONE,
974 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
975 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
976 };
977
978 InputTargetWaitCause mInputTargetWaitCause;
979 nsecs_t mInputTargetWaitStartTime;
980 nsecs_t mInputTargetWaitTimeoutTime;
981 bool mInputTargetWaitTimeoutExpired;
Jeff Brown9302c872011-07-13 22:51:29 -0700982 sp<InputApplicationHandle> mInputTargetWaitApplicationHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -0700983
Jeff Browna032cc02011-03-07 16:56:21 -0800984 // Contains the last window which received a hover event.
Jeff Brown9302c872011-07-13 22:51:29 -0700985 sp<InputWindowHandle> mLastHoverWindowHandle;
Jeff Browna032cc02011-03-07 16:56:21 -0800986
Jeff Brownb88102f2010-09-08 11:49:43 -0700987 // Finding targets for input events.
Jeff Brownb88102f2010-09-08 11:49:43 -0700988 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry,
Jeff Brown9302c872011-07-13 22:51:29 -0700989 const sp<InputApplicationHandle>& applicationHandle,
990 const sp<InputWindowHandle>& windowHandle,
Jeff Brown265f1cc2012-06-11 18:01:06 -0700991 nsecs_t* nextWakeupTime, const char* reason);
Jeff Brown519e0242010-09-15 15:18:56 -0700992 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
993 const sp<InputChannel>& inputChannel);
994 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700995 void resetANRTimeoutsLocked();
996
Jeff Brown01ce2e92010-09-26 22:20:12 -0700997 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry,
Jeff Browne9bb9be2012-02-06 15:47:55 -0800998 Vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700999 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry,
Jeff Browne9bb9be2012-02-06 15:47:55 -08001000 Vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime,
1001 bool* outConflictingPointerActions);
Jeff Brownb88102f2010-09-08 11:49:43 -07001002
Jeff Brown9302c872011-07-13 22:51:29 -07001003 void addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle,
Jeff Browne9bb9be2012-02-06 15:47:55 -08001004 int32_t targetFlags, BitSet32 pointerIds, Vector<InputTarget>& inputTargets);
1005 void addMonitoringTargetsLocked(Vector<InputTarget>& inputTargets);
1006
Jeff Browne2fe69e2010-10-18 13:21:23 -07001007 void pokeUserActivityLocked(const EventEntry* eventEntry);
Jeff Brown9302c872011-07-13 22:51:29 -07001008 bool checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
1009 const InjectionState* injectionState);
1010 bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle,
1011 int32_t x, int32_t y) const;
Jeff Brownd1c48a02012-02-06 19:12:47 -08001012 bool isWindowReadyForMoreInputLocked(nsecs_t currentTime,
Jeff Brown0952c302012-02-13 13:48:59 -08001013 const sp<InputWindowHandle>& windowHandle, const EventEntry* eventEntry);
Jeff Brown9302c872011-07-13 22:51:29 -07001014 String8 getApplicationWindowLabelLocked(const sp<InputApplicationHandle>& applicationHandle,
1015 const sp<InputWindowHandle>& windowHandle);
Jeff Brownb88102f2010-09-08 11:49:43 -07001016
Jeff Brown46b9ac02010-04-22 18:58:52 -07001017 // Manage the dispatch cycle for a single connection.
Jeff Brown7fbdc842010-06-17 20:52:56 -07001018 // These methods are deliberately not Interruptible because doing all of the work
1019 // with the mutex held makes it easier to ensure that connection invariants are maintained.
1020 // If needed, the methods post commands to run later once the critical bits are done.
1021 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001022 EventEntry* eventEntry, const InputTarget* inputTarget);
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001023 void enqueueDispatchEntriesLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001024 EventEntry* eventEntry, const InputTarget* inputTarget);
Jeff Browna032cc02011-03-07 16:56:21 -08001025 void enqueueDispatchEntryLocked(const sp<Connection>& connection,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001026 EventEntry* eventEntry, const InputTarget* inputTarget, int32_t dispatchMode);
Jeff Brown519e0242010-09-15 15:18:56 -07001027 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown3915bb82010-11-05 15:02:16 -07001028 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Jeff Brown072ec962012-02-07 14:46:57 -08001029 uint32_t seq, bool handled);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001030 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
1031 bool notify);
Jeff Brownd1c48a02012-02-06 19:12:47 -08001032 void drainDispatchQueueLocked(Queue<DispatchEntry>* queue);
1033 void releaseDispatchEntryLocked(DispatchEntry* dispatchEntry);
Jeff Browncbee6d62012-02-03 20:11:27 -08001034 static int handleReceiveCallback(int fd, int events, void* data);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001035
Jeff Brownb6997262010-10-08 22:31:17 -07001036 void synthesizeCancelationEventsForAllConnectionsLocked(
Jeff Brownda3d5a92011-03-29 15:11:34 -07001037 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001038 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Jeff Brownda3d5a92011-03-29 15:11:34 -07001039 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001040 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Jeff Brownda3d5a92011-03-29 15:11:34 -07001041 const CancelationOptions& options);
Jeff Brownb6997262010-10-08 22:31:17 -07001042
Jeff Brown01ce2e92010-09-26 22:20:12 -07001043 // Splitting motion events across windows.
1044 MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds);
1045
Jeff Brown120a4592010-10-27 18:43:51 -07001046 // Reset and drop everything the dispatcher is doing.
1047 void resetAndDropEverythingLocked(const char* reason);
1048
Jeff Brownb88102f2010-09-08 11:49:43 -07001049 // Dump state.
1050 void dumpDispatchStateLocked(String8& dump);
1051 void logDispatchStateLocked();
1052
Jeff Browncc4f7db2011-08-30 20:34:48 -07001053 // Registration.
1054 void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel);
1055 status_t unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, bool notify);
1056
Jeff Brown46b9ac02010-04-22 18:58:52 -07001057 // Add or remove a connection to the mActiveConnections vector.
1058 void activateConnectionLocked(Connection* connection);
1059 void deactivateConnectionLocked(Connection* connection);
1060
1061 // Interesting events that we might like to log or tell the framework about.
Jeff Brown9c3cda02010-06-15 01:31:58 -07001062 void onDispatchCycleFinishedLocked(
Jeff Brown072ec962012-02-07 14:46:57 -08001063 nsecs_t currentTime, const sp<Connection>& connection, uint32_t seq, bool handled);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001064 void onDispatchCycleBrokenLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07001065 nsecs_t currentTime, const sp<Connection>& connection);
Jeff Brown519e0242010-09-15 15:18:56 -07001066 void onANRLocked(
Jeff Brown9302c872011-07-13 22:51:29 -07001067 nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
1068 const sp<InputWindowHandle>& windowHandle,
Jeff Brown265f1cc2012-06-11 18:01:06 -07001069 nsecs_t eventTime, nsecs_t waitStartTime, const char* reason);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001070
Jeff Brown7fbdc842010-06-17 20:52:56 -07001071 // Outbound policy interactions.
Jeff Brownb88102f2010-09-08 11:49:43 -07001072 void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001073 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown519e0242010-09-15 15:18:56 -07001074 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07001075 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001076 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001077 bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
1078 DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled);
1079 bool afterMotionEventLockedInterruptible(const sp<Connection>& connection,
1080 DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled);
Jeff Brownb88102f2010-09-08 11:49:43 -07001081 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry);
Jeff Brown3915bb82010-11-05 15:02:16 -07001082 void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry);
Jeff Brown519e0242010-09-15 15:18:56 -07001083
1084 // Statistics gathering.
1085 void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
1086 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Jeff Brown481c1572012-03-09 14:41:15 -08001087 void traceInboundQueueLengthLocked();
1088 void traceOutboundQueueLengthLocked(const sp<Connection>& connection);
1089 void traceWaitQueueLengthLocked(const sp<Connection>& connection);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001090};
1091
1092/* Enqueues and dispatches input events, endlessly. */
1093class InputDispatcherThread : public Thread {
1094public:
1095 explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher);
1096 ~InputDispatcherThread();
1097
1098private:
1099 virtual bool threadLoop();
1100
1101 sp<InputDispatcherInterface> mDispatcher;
1102};
1103
1104} // namespace android
1105
Jeff Brownb88102f2010-09-08 11:49:43 -07001106#endif // _UI_INPUT_DISPATCHER_H