blob: 7e1a80cecdd177d42eceee9b216e109c6d37710e [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
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
Jeff Brown46b9ac02010-04-22 18:58:52 -070017#define LOG_TAG "InputDispatcher"
Jeff Brown481c1572012-03-09 14:41:15 -080018#define ATRACE_TAG ATRACE_TAG_INPUT
Jeff Brown46b9ac02010-04-22 18:58:52 -070019
20//#define LOG_NDEBUG 0
21
22// Log detailed debug messages about each inbound event notification to the dispatcher.
Jeff Brown349703e2010-06-22 01:27:15 -070023#define DEBUG_INBOUND_EVENT_DETAILS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070024
25// Log detailed debug messages about each outbound event processed by the dispatcher.
Jeff Brown349703e2010-06-22 01:27:15 -070026#define DEBUG_OUTBOUND_EVENT_DETAILS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070027
Jeff Brown46b9ac02010-04-22 18:58:52 -070028// Log debug messages about the dispatch cycle.
Jeff Brown349703e2010-06-22 01:27:15 -070029#define DEBUG_DISPATCH_CYCLE 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070030
Jeff Brown9c3cda02010-06-15 01:31:58 -070031// Log debug messages about registrations.
Jeff Brown349703e2010-06-22 01:27:15 -070032#define DEBUG_REGISTRATION 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070033
Jeff Brown7fbdc842010-06-17 20:52:56 -070034// Log debug messages about input event injection.
Jeff Brown349703e2010-06-22 01:27:15 -070035#define DEBUG_INJECTION 0
Jeff Brown7fbdc842010-06-17 20:52:56 -070036
Jeff Brownb88102f2010-09-08 11:49:43 -070037// Log debug messages about input focus tracking.
38#define DEBUG_FOCUS 0
39
40// Log debug messages about the app switch latency optimization.
41#define DEBUG_APP_SWITCH 0
42
Jeff Browna032cc02011-03-07 16:56:21 -080043// Log debug messages about hover events.
44#define DEBUG_HOVER 0
45
Jeff Brownb4ff35d2011-01-02 16:37:43 -080046#include "InputDispatcher.h"
47
Jeff Brown481c1572012-03-09 14:41:15 -080048#include <utils/Trace.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070049#include <cutils/log.h>
Mathias Agopianb93a03f82012-02-17 15:34:57 -080050#include <androidfw/PowerManager.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070051
52#include <stddef.h>
53#include <unistd.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070054#include <errno.h>
55#include <limits.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070056
Jeff Brownf2f48712010-10-01 17:46:21 -070057#define INDENT " "
58#define INDENT2 " "
Jeff Brown265f1cc2012-06-11 18:01:06 -070059#define INDENT3 " "
60#define INDENT4 " "
Jeff Brownf2f48712010-10-01 17:46:21 -070061
Jeff Brown46b9ac02010-04-22 18:58:52 -070062namespace android {
63
Jeff Brownb88102f2010-09-08 11:49:43 -070064// Default input dispatching timeout if there is no focused application or paused window
65// from which to determine an appropriate dispatching timeout.
66const nsecs_t DEFAULT_INPUT_DISPATCHING_TIMEOUT = 5000 * 1000000LL; // 5 sec
67
68// Amount of time to allow for all pending events to be processed when an app switch
69// key is on the way. This is used to preempt input dispatch and drop input events
70// when an application takes too long to respond and the user has pressed an app switch key.
71const nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec
72
Jeff Brown928e0542011-01-10 11:17:36 -080073// Amount of time to allow for an event to be dispatched (measured since its eventTime)
74// before considering it stale and dropping it.
75const nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec
76
Jeff Brownd1c48a02012-02-06 19:12:47 -080077// Amount of time to allow touch events to be streamed out to a connection before requiring
78// that the first event be finished. This value extends the ANR timeout by the specified
79// amount. For example, if streaming is allowed to get ahead by one second relative to the
80// queue of waiting unfinished events, then ANRs will similarly be delayed by one second.
81const nsecs_t STREAM_AHEAD_EVENT_TIMEOUT = 500 * 1000000LL; // 0.5sec
82
Jeff Brown265f1cc2012-06-11 18:01:06 -070083// Log a warning when an event takes longer than this to process, even if an ANR does not occur.
84const nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec
85
Jeff Brown46b9ac02010-04-22 18:58:52 -070086
Jeff Brown7fbdc842010-06-17 20:52:56 -070087static inline nsecs_t now() {
88 return systemTime(SYSTEM_TIME_MONOTONIC);
89}
90
Jeff Brownb88102f2010-09-08 11:49:43 -070091static inline const char* toString(bool value) {
92 return value ? "true" : "false";
93}
94
Jeff Brown01ce2e92010-09-26 22:20:12 -070095static inline int32_t getMotionEventActionPointerIndex(int32_t action) {
96 return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
97 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
98}
99
100static bool isValidKeyAction(int32_t action) {
101 switch (action) {
102 case AKEY_EVENT_ACTION_DOWN:
103 case AKEY_EVENT_ACTION_UP:
104 return true;
105 default:
106 return false;
107 }
108}
109
110static bool validateKeyEvent(int32_t action) {
111 if (! isValidKeyAction(action)) {
Steve Block3762c312012-01-06 19:20:56 +0000112 ALOGE("Key event has invalid action code 0x%x", action);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700113 return false;
114 }
115 return true;
116}
117
Jeff Brownb6997262010-10-08 22:31:17 -0700118static bool isValidMotionAction(int32_t action, size_t pointerCount) {
Jeff Brown01ce2e92010-09-26 22:20:12 -0700119 switch (action & AMOTION_EVENT_ACTION_MASK) {
120 case AMOTION_EVENT_ACTION_DOWN:
121 case AMOTION_EVENT_ACTION_UP:
122 case AMOTION_EVENT_ACTION_CANCEL:
123 case AMOTION_EVENT_ACTION_MOVE:
Jeff Brown01ce2e92010-09-26 22:20:12 -0700124 case AMOTION_EVENT_ACTION_OUTSIDE:
Jeff Browna032cc02011-03-07 16:56:21 -0800125 case AMOTION_EVENT_ACTION_HOVER_ENTER:
Jeff Browncc0c1592011-02-19 05:07:28 -0800126 case AMOTION_EVENT_ACTION_HOVER_MOVE:
Jeff Browna032cc02011-03-07 16:56:21 -0800127 case AMOTION_EVENT_ACTION_HOVER_EXIT:
Jeff Brown33bbfd22011-02-24 20:55:35 -0800128 case AMOTION_EVENT_ACTION_SCROLL:
Jeff Brown01ce2e92010-09-26 22:20:12 -0700129 return true;
Jeff Brownb6997262010-10-08 22:31:17 -0700130 case AMOTION_EVENT_ACTION_POINTER_DOWN:
131 case AMOTION_EVENT_ACTION_POINTER_UP: {
132 int32_t index = getMotionEventActionPointerIndex(action);
133 return index >= 0 && size_t(index) < pointerCount;
134 }
Jeff Brown01ce2e92010-09-26 22:20:12 -0700135 default:
136 return false;
137 }
138}
139
140static bool validateMotionEvent(int32_t action, size_t pointerCount,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700141 const PointerProperties* pointerProperties) {
Jeff Brownb6997262010-10-08 22:31:17 -0700142 if (! isValidMotionAction(action, pointerCount)) {
Steve Block3762c312012-01-06 19:20:56 +0000143 ALOGE("Motion event has invalid action code 0x%x", action);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700144 return false;
145 }
146 if (pointerCount < 1 || pointerCount > MAX_POINTERS) {
Steve Block3762c312012-01-06 19:20:56 +0000147 ALOGE("Motion event has invalid pointer count %d; value must be between 1 and %d.",
Jeff Brown01ce2e92010-09-26 22:20:12 -0700148 pointerCount, MAX_POINTERS);
149 return false;
150 }
Jeff Brownc3db8582010-10-20 15:33:38 -0700151 BitSet32 pointerIdBits;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700152 for (size_t i = 0; i < pointerCount; i++) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700153 int32_t id = pointerProperties[i].id;
Jeff Brownc3db8582010-10-20 15:33:38 -0700154 if (id < 0 || id > MAX_POINTER_ID) {
Steve Block3762c312012-01-06 19:20:56 +0000155 ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d",
Jeff Brownc3db8582010-10-20 15:33:38 -0700156 id, MAX_POINTER_ID);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700157 return false;
158 }
Jeff Brownc3db8582010-10-20 15:33:38 -0700159 if (pointerIdBits.hasBit(id)) {
Steve Block3762c312012-01-06 19:20:56 +0000160 ALOGE("Motion event has duplicate pointer id %d", id);
Jeff Brownc3db8582010-10-20 15:33:38 -0700161 return false;
162 }
163 pointerIdBits.markBit(id);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700164 }
165 return true;
166}
167
Jeff Brownfbf09772011-01-16 14:06:57 -0800168static void dumpRegion(String8& dump, const SkRegion& region) {
169 if (region.isEmpty()) {
170 dump.append("<empty>");
171 return;
172 }
173
174 bool first = true;
175 for (SkRegion::Iterator it(region); !it.done(); it.next()) {
176 if (first) {
177 first = false;
178 } else {
179 dump.append("|");
180 }
181 const SkIRect& rect = it.rect();
182 dump.appendFormat("[%d,%d][%d,%d]", rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
183 }
184}
185
Jeff Brownb88102f2010-09-08 11:49:43 -0700186
Jeff Brown46b9ac02010-04-22 18:58:52 -0700187// --- InputDispatcher ---
188
Jeff Brown9c3cda02010-06-15 01:31:58 -0700189InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) :
Jeff Brownb88102f2010-09-08 11:49:43 -0700190 mPolicy(policy),
Jeff Brown928e0542011-01-10 11:17:36 -0800191 mPendingEvent(NULL), mAppSwitchSawKeyDown(false), mAppSwitchDueTime(LONG_LONG_MAX),
192 mNextUnblockedEvent(NULL),
Jeff Brownc042ee22012-05-08 13:03:42 -0700193 mDispatchEnabled(false), mDispatchFrozen(false), mInputFilterEnabled(false),
Jeff Brown9302c872011-07-13 22:51:29 -0700194 mInputTargetWaitCause(INPUT_TARGET_WAIT_CAUSE_NONE) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700195 mLooper = new Looper(false);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700196
Jeff Brown46b9ac02010-04-22 18:58:52 -0700197 mKeyRepeatState.lastKeyEntry = NULL;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700198
Jeff Brown214eaf42011-05-26 19:17:02 -0700199 policy->getDispatcherConfiguration(&mConfig);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700200}
201
202InputDispatcher::~InputDispatcher() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700203 { // acquire lock
204 AutoMutex _l(mLock);
205
206 resetKeyRepeatLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700207 releasePendingEventLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700208 drainInboundQueueLocked();
209 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700210
Jeff Browncbee6d62012-02-03 20:11:27 -0800211 while (mConnectionsByFd.size() != 0) {
212 unregisterInputChannel(mConnectionsByFd.valueAt(0)->inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700213 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700214}
215
216void InputDispatcher::dispatchOnce() {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700217 nsecs_t nextWakeupTime = LONG_LONG_MAX;
218 { // acquire lock
219 AutoMutex _l(mLock);
Jeff Brown112b5f52012-01-27 17:32:06 -0800220 mDispatcherIsAliveCondition.broadcast();
221
Jeff Brown214eaf42011-05-26 19:17:02 -0700222 dispatchOnceInnerLocked(&nextWakeupTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700223
Jeff Brownb88102f2010-09-08 11:49:43 -0700224 if (runCommandsLockedInterruptible()) {
225 nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately
Jeff Brown46b9ac02010-04-22 18:58:52 -0700226 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700227 } // release lock
228
Jeff Brownb88102f2010-09-08 11:49:43 -0700229 // Wait for callback or timeout or wake. (make sure we round up, not down)
230 nsecs_t currentTime = now();
Jeff Brownaa3855d2011-03-17 01:34:19 -0700231 int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700232 mLooper->pollOnce(timeoutMillis);
Jeff Brownb88102f2010-09-08 11:49:43 -0700233}
234
Jeff Brown214eaf42011-05-26 19:17:02 -0700235void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700236 nsecs_t currentTime = now();
237
238 // Reset the key repeat timer whenever we disallow key events, even if the next event
239 // is not a key. This is to ensure that we abort a key repeat if the device is just coming
240 // out of sleep.
Jeff Brown214eaf42011-05-26 19:17:02 -0700241 if (!mPolicy->isKeyRepeatEnabled()) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700242 resetKeyRepeatLocked();
243 }
244
Jeff Brownb88102f2010-09-08 11:49:43 -0700245 // If dispatching is frozen, do not process timeouts or try to deliver any new events.
246 if (mDispatchFrozen) {
247#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +0000248 ALOGD("Dispatch frozen. Waiting some more.");
Jeff Brownb88102f2010-09-08 11:49:43 -0700249#endif
250 return;
251 }
252
253 // Optimize latency of app switches.
254 // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has
255 // been pressed. When it expires, we preempt dispatch and drop all other pending events.
256 bool isAppSwitchDue = mAppSwitchDueTime <= currentTime;
257 if (mAppSwitchDueTime < *nextWakeupTime) {
258 *nextWakeupTime = mAppSwitchDueTime;
259 }
260
Jeff Brownb88102f2010-09-08 11:49:43 -0700261 // Ready to start a new event.
262 // If we don't already have a pending event, go grab one.
263 if (! mPendingEvent) {
264 if (mInboundQueue.isEmpty()) {
265 if (isAppSwitchDue) {
266 // The inbound queue is empty so the app switch key we were waiting
267 // for will never arrive. Stop waiting for it.
268 resetPendingAppSwitchLocked(false);
269 isAppSwitchDue = false;
270 }
271
272 // Synthesize a key repeat if appropriate.
273 if (mKeyRepeatState.lastKeyEntry) {
274 if (currentTime >= mKeyRepeatState.nextRepeatTime) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700275 mPendingEvent = synthesizeKeyRepeatLocked(currentTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700276 } else {
277 if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) {
278 *nextWakeupTime = mKeyRepeatState.nextRepeatTime;
279 }
280 }
281 }
Jeff Browncc4f7db2011-08-30 20:34:48 -0700282
283 // Nothing to do if there is no pending event.
Jeff Browne9bb9be2012-02-06 15:47:55 -0800284 if (!mPendingEvent) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700285 return;
286 }
287 } else {
288 // Inbound queue has at least one entry.
Jeff Browne9bb9be2012-02-06 15:47:55 -0800289 mPendingEvent = mInboundQueue.dequeueAtHead();
Jeff Brown481c1572012-03-09 14:41:15 -0800290 traceInboundQueueLengthLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700291 }
Jeff Browne2fe69e2010-10-18 13:21:23 -0700292
293 // Poke user activity for this event.
294 if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) {
295 pokeUserActivityLocked(mPendingEvent);
296 }
Jeff Browne9bb9be2012-02-06 15:47:55 -0800297
298 // Get ready to dispatch the event.
299 resetANRTimeoutsLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700300 }
301
302 // Now we have an event to dispatch.
Jeff Brown928e0542011-01-10 11:17:36 -0800303 // All events are eventually dequeued and processed this way, even if we intend to drop them.
Steve Blockec193de2012-01-09 18:35:44 +0000304 ALOG_ASSERT(mPendingEvent != NULL);
Jeff Brown54a18252010-09-16 14:07:33 -0700305 bool done = false;
Jeff Brownb6997262010-10-08 22:31:17 -0700306 DropReason dropReason = DROP_REASON_NOT_DROPPED;
307 if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) {
308 dropReason = DROP_REASON_POLICY;
309 } else if (!mDispatchEnabled) {
310 dropReason = DROP_REASON_DISABLED;
311 }
Jeff Brown928e0542011-01-10 11:17:36 -0800312
313 if (mNextUnblockedEvent == mPendingEvent) {
314 mNextUnblockedEvent = NULL;
315 }
316
Jeff Brownb88102f2010-09-08 11:49:43 -0700317 switch (mPendingEvent->type) {
318 case EventEntry::TYPE_CONFIGURATION_CHANGED: {
319 ConfigurationChangedEntry* typedEntry =
320 static_cast<ConfigurationChangedEntry*>(mPendingEvent);
Jeff Brown54a18252010-09-16 14:07:33 -0700321 done = dispatchConfigurationChangedLocked(currentTime, typedEntry);
Jeff Brownb6997262010-10-08 22:31:17 -0700322 dropReason = DROP_REASON_NOT_DROPPED; // configuration changes are never dropped
Jeff Brownb88102f2010-09-08 11:49:43 -0700323 break;
324 }
325
Jeff Brown65fd2512011-08-18 11:20:58 -0700326 case EventEntry::TYPE_DEVICE_RESET: {
327 DeviceResetEntry* typedEntry =
328 static_cast<DeviceResetEntry*>(mPendingEvent);
329 done = dispatchDeviceResetLocked(currentTime, typedEntry);
330 dropReason = DROP_REASON_NOT_DROPPED; // device resets are never dropped
331 break;
332 }
333
Jeff Brownb88102f2010-09-08 11:49:43 -0700334 case EventEntry::TYPE_KEY: {
335 KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent);
Jeff Brownb6997262010-10-08 22:31:17 -0700336 if (isAppSwitchDue) {
337 if (isAppSwitchKeyEventLocked(typedEntry)) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700338 resetPendingAppSwitchLocked(true);
Jeff Brownb6997262010-10-08 22:31:17 -0700339 isAppSwitchDue = false;
340 } else if (dropReason == DROP_REASON_NOT_DROPPED) {
341 dropReason = DROP_REASON_APP_SWITCH;
Jeff Brownb88102f2010-09-08 11:49:43 -0700342 }
343 }
Jeff Brown928e0542011-01-10 11:17:36 -0800344 if (dropReason == DROP_REASON_NOT_DROPPED
345 && isStaleEventLocked(currentTime, typedEntry)) {
346 dropReason = DROP_REASON_STALE;
347 }
348 if (dropReason == DROP_REASON_NOT_DROPPED && mNextUnblockedEvent) {
349 dropReason = DROP_REASON_BLOCKED;
350 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700351 done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700352 break;
353 }
354
355 case EventEntry::TYPE_MOTION: {
356 MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent);
Jeff Brownb6997262010-10-08 22:31:17 -0700357 if (dropReason == DROP_REASON_NOT_DROPPED && isAppSwitchDue) {
358 dropReason = DROP_REASON_APP_SWITCH;
Jeff Brownb88102f2010-09-08 11:49:43 -0700359 }
Jeff Brown928e0542011-01-10 11:17:36 -0800360 if (dropReason == DROP_REASON_NOT_DROPPED
361 && isStaleEventLocked(currentTime, typedEntry)) {
362 dropReason = DROP_REASON_STALE;
363 }
364 if (dropReason == DROP_REASON_NOT_DROPPED && mNextUnblockedEvent) {
365 dropReason = DROP_REASON_BLOCKED;
366 }
Jeff Brownb6997262010-10-08 22:31:17 -0700367 done = dispatchMotionLocked(currentTime, typedEntry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700368 &dropReason, nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700369 break;
370 }
371
372 default:
Steve Blockec193de2012-01-09 18:35:44 +0000373 ALOG_ASSERT(false);
Jeff Brownb88102f2010-09-08 11:49:43 -0700374 break;
375 }
376
Jeff Brown54a18252010-09-16 14:07:33 -0700377 if (done) {
Jeff Brownb6997262010-10-08 22:31:17 -0700378 if (dropReason != DROP_REASON_NOT_DROPPED) {
379 dropInboundEventLocked(mPendingEvent, dropReason);
380 }
381
Jeff Brown54a18252010-09-16 14:07:33 -0700382 releasePendingEventLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700383 *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately
384 }
385}
386
387bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) {
388 bool needWake = mInboundQueue.isEmpty();
389 mInboundQueue.enqueueAtTail(entry);
Jeff Brown481c1572012-03-09 14:41:15 -0800390 traceInboundQueueLengthLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700391
392 switch (entry->type) {
Jeff Brownb6997262010-10-08 22:31:17 -0700393 case EventEntry::TYPE_KEY: {
Jeff Brown928e0542011-01-10 11:17:36 -0800394 // Optimize app switch latency.
395 // If the application takes too long to catch up then we drop all events preceding
396 // the app switch key.
Jeff Brownb6997262010-10-08 22:31:17 -0700397 KeyEntry* keyEntry = static_cast<KeyEntry*>(entry);
398 if (isAppSwitchKeyEventLocked(keyEntry)) {
399 if (keyEntry->action == AKEY_EVENT_ACTION_DOWN) {
400 mAppSwitchSawKeyDown = true;
401 } else if (keyEntry->action == AKEY_EVENT_ACTION_UP) {
402 if (mAppSwitchSawKeyDown) {
403#if DEBUG_APP_SWITCH
Steve Block5baa3a62011-12-20 16:23:08 +0000404 ALOGD("App switch is pending!");
Jeff Brownb6997262010-10-08 22:31:17 -0700405#endif
406 mAppSwitchDueTime = keyEntry->eventTime + APP_SWITCH_TIMEOUT;
407 mAppSwitchSawKeyDown = false;
408 needWake = true;
409 }
410 }
411 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700412 break;
413 }
Jeff Brown928e0542011-01-10 11:17:36 -0800414
415 case EventEntry::TYPE_MOTION: {
416 // Optimize case where the current application is unresponsive and the user
417 // decides to touch a window in a different application.
418 // If the application takes too long to catch up then we drop all events preceding
419 // the touch into the other window.
420 MotionEntry* motionEntry = static_cast<MotionEntry*>(entry);
Jeff Brown33bbfd22011-02-24 20:55:35 -0800421 if (motionEntry->action == AMOTION_EVENT_ACTION_DOWN
Jeff Brown928e0542011-01-10 11:17:36 -0800422 && (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER)
423 && mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY
Jeff Brown9302c872011-07-13 22:51:29 -0700424 && mInputTargetWaitApplicationHandle != NULL) {
Jeff Brown3241b6b2012-02-03 15:08:02 -0800425 int32_t x = int32_t(motionEntry->pointerCoords[0].
Jeff Brownebbd5d12011-02-17 13:01:34 -0800426 getAxisValue(AMOTION_EVENT_AXIS_X));
Jeff Brown3241b6b2012-02-03 15:08:02 -0800427 int32_t y = int32_t(motionEntry->pointerCoords[0].
Jeff Brownebbd5d12011-02-17 13:01:34 -0800428 getAxisValue(AMOTION_EVENT_AXIS_Y));
Jeff Brown9302c872011-07-13 22:51:29 -0700429 sp<InputWindowHandle> touchedWindowHandle = findTouchedWindowAtLocked(x, y);
430 if (touchedWindowHandle != NULL
431 && touchedWindowHandle->inputApplicationHandle
432 != mInputTargetWaitApplicationHandle) {
Jeff Brown928e0542011-01-10 11:17:36 -0800433 // User touched a different application than the one we are waiting on.
434 // Flag the event, and start pruning the input queue.
435 mNextUnblockedEvent = motionEntry;
436 needWake = true;
437 }
438 }
439 break;
440 }
Jeff Brownb6997262010-10-08 22:31:17 -0700441 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700442
443 return needWake;
444}
445
Jeff Brown9302c872011-07-13 22:51:29 -0700446sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t x, int32_t y) {
Jeff Brown928e0542011-01-10 11:17:36 -0800447 // Traverse windows from front to back to find touched window.
Jeff Brown9302c872011-07-13 22:51:29 -0700448 size_t numWindows = mWindowHandles.size();
Jeff Brown928e0542011-01-10 11:17:36 -0800449 for (size_t i = 0; i < numWindows; i++) {
Jeff Brown9302c872011-07-13 22:51:29 -0700450 sp<InputWindowHandle> windowHandle = mWindowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700451 const InputWindowInfo* windowInfo = windowHandle->getInfo();
452 int32_t flags = windowInfo->layoutParamsFlags;
Jeff Brown928e0542011-01-10 11:17:36 -0800453
Jeff Browncc4f7db2011-08-30 20:34:48 -0700454 if (windowInfo->visible) {
455 if (!(flags & InputWindowInfo::FLAG_NOT_TOUCHABLE)) {
456 bool isTouchModal = (flags & (InputWindowInfo::FLAG_NOT_FOCUSABLE
457 | InputWindowInfo::FLAG_NOT_TOUCH_MODAL)) == 0;
458 if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) {
Jeff Brown928e0542011-01-10 11:17:36 -0800459 // Found window.
Jeff Brown9302c872011-07-13 22:51:29 -0700460 return windowHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800461 }
462 }
463 }
464
Jeff Browncc4f7db2011-08-30 20:34:48 -0700465 if (flags & InputWindowInfo::FLAG_SYSTEM_ERROR) {
Jeff Brown928e0542011-01-10 11:17:36 -0800466 // Error window is on top but not visible, so touch is dropped.
467 return NULL;
468 }
469 }
470 return NULL;
471}
472
Jeff Brownb6997262010-10-08 22:31:17 -0700473void InputDispatcher::dropInboundEventLocked(EventEntry* entry, DropReason dropReason) {
474 const char* reason;
475 switch (dropReason) {
476 case DROP_REASON_POLICY:
Jeff Browne20c9e02010-10-11 14:20:19 -0700477#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +0000478 ALOGD("Dropped event because policy consumed it.");
Jeff Browne20c9e02010-10-11 14:20:19 -0700479#endif
Jeff Brown3122e442010-10-11 23:32:49 -0700480 reason = "inbound event was dropped because the policy consumed it";
Jeff Brownb6997262010-10-08 22:31:17 -0700481 break;
482 case DROP_REASON_DISABLED:
Steve Block6215d3f2012-01-04 20:05:49 +0000483 ALOGI("Dropped event because input dispatch is disabled.");
Jeff Brownb6997262010-10-08 22:31:17 -0700484 reason = "inbound event was dropped because input dispatch is disabled";
485 break;
486 case DROP_REASON_APP_SWITCH:
Steve Block6215d3f2012-01-04 20:05:49 +0000487 ALOGI("Dropped event because of pending overdue app switch.");
Jeff Brownb6997262010-10-08 22:31:17 -0700488 reason = "inbound event was dropped because of pending overdue app switch";
489 break;
Jeff Brown928e0542011-01-10 11:17:36 -0800490 case DROP_REASON_BLOCKED:
Steve Block6215d3f2012-01-04 20:05:49 +0000491 ALOGI("Dropped event because the current application is not responding and the user "
Jeff Brown81346812011-06-28 20:08:48 -0700492 "has started interacting with a different application.");
Jeff Brown928e0542011-01-10 11:17:36 -0800493 reason = "inbound event was dropped because the current application is not responding "
Jeff Brown81346812011-06-28 20:08:48 -0700494 "and the user has started interacting with a different application";
Jeff Brown928e0542011-01-10 11:17:36 -0800495 break;
496 case DROP_REASON_STALE:
Steve Block6215d3f2012-01-04 20:05:49 +0000497 ALOGI("Dropped event because it is stale.");
Jeff Brown928e0542011-01-10 11:17:36 -0800498 reason = "inbound event was dropped because it is stale";
499 break;
Jeff Brownb6997262010-10-08 22:31:17 -0700500 default:
Steve Blockec193de2012-01-09 18:35:44 +0000501 ALOG_ASSERT(false);
Jeff Brownb6997262010-10-08 22:31:17 -0700502 return;
503 }
504
505 switch (entry->type) {
Jeff Brownda3d5a92011-03-29 15:11:34 -0700506 case EventEntry::TYPE_KEY: {
507 CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason);
508 synthesizeCancelationEventsForAllConnectionsLocked(options);
Jeff Brownb6997262010-10-08 22:31:17 -0700509 break;
Jeff Brownda3d5a92011-03-29 15:11:34 -0700510 }
Jeff Brownb6997262010-10-08 22:31:17 -0700511 case EventEntry::TYPE_MOTION: {
512 MotionEntry* motionEntry = static_cast<MotionEntry*>(entry);
513 if (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) {
Jeff Brownda3d5a92011-03-29 15:11:34 -0700514 CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason);
515 synthesizeCancelationEventsForAllConnectionsLocked(options);
Jeff Brownb6997262010-10-08 22:31:17 -0700516 } else {
Jeff Brownda3d5a92011-03-29 15:11:34 -0700517 CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason);
518 synthesizeCancelationEventsForAllConnectionsLocked(options);
Jeff Brownb6997262010-10-08 22:31:17 -0700519 }
520 break;
521 }
522 }
523}
524
525bool InputDispatcher::isAppSwitchKeyCode(int32_t keyCode) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700526 return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL;
527}
528
Jeff Brownb6997262010-10-08 22:31:17 -0700529bool InputDispatcher::isAppSwitchKeyEventLocked(KeyEntry* keyEntry) {
530 return ! (keyEntry->flags & AKEY_EVENT_FLAG_CANCELED)
531 && isAppSwitchKeyCode(keyEntry->keyCode)
Jeff Browne20c9e02010-10-11 14:20:19 -0700532 && (keyEntry->policyFlags & POLICY_FLAG_TRUSTED)
Jeff Brownb6997262010-10-08 22:31:17 -0700533 && (keyEntry->policyFlags & POLICY_FLAG_PASS_TO_USER);
534}
535
Jeff Brownb88102f2010-09-08 11:49:43 -0700536bool InputDispatcher::isAppSwitchPendingLocked() {
537 return mAppSwitchDueTime != LONG_LONG_MAX;
538}
539
Jeff Brownb88102f2010-09-08 11:49:43 -0700540void InputDispatcher::resetPendingAppSwitchLocked(bool handled) {
541 mAppSwitchDueTime = LONG_LONG_MAX;
542
543#if DEBUG_APP_SWITCH
544 if (handled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000545 ALOGD("App switch has arrived.");
Jeff Brownb88102f2010-09-08 11:49:43 -0700546 } else {
Steve Block5baa3a62011-12-20 16:23:08 +0000547 ALOGD("App switch was abandoned.");
Jeff Brownb88102f2010-09-08 11:49:43 -0700548 }
549#endif
Jeff Brown46b9ac02010-04-22 18:58:52 -0700550}
551
Jeff Brown928e0542011-01-10 11:17:36 -0800552bool InputDispatcher::isStaleEventLocked(nsecs_t currentTime, EventEntry* entry) {
553 return currentTime - entry->eventTime >= STALE_EVENT_TIMEOUT;
554}
555
Jeff Brown9c3cda02010-06-15 01:31:58 -0700556bool InputDispatcher::runCommandsLockedInterruptible() {
557 if (mCommandQueue.isEmpty()) {
558 return false;
559 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700560
Jeff Brown9c3cda02010-06-15 01:31:58 -0700561 do {
562 CommandEntry* commandEntry = mCommandQueue.dequeueAtHead();
563
564 Command command = commandEntry->command;
565 (this->*command)(commandEntry); // commands are implicitly 'LockedInterruptible'
566
Jeff Brown7fbdc842010-06-17 20:52:56 -0700567 commandEntry->connection.clear();
Jeff Brownac386072011-07-20 15:19:50 -0700568 delete commandEntry;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700569 } while (! mCommandQueue.isEmpty());
570 return true;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700571}
572
Jeff Brown9c3cda02010-06-15 01:31:58 -0700573InputDispatcher::CommandEntry* InputDispatcher::postCommandLocked(Command command) {
Jeff Brownac386072011-07-20 15:19:50 -0700574 CommandEntry* commandEntry = new CommandEntry(command);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700575 mCommandQueue.enqueueAtTail(commandEntry);
576 return commandEntry;
577}
578
Jeff Brownb88102f2010-09-08 11:49:43 -0700579void InputDispatcher::drainInboundQueueLocked() {
580 while (! mInboundQueue.isEmpty()) {
581 EventEntry* entry = mInboundQueue.dequeueAtHead();
Jeff Brown54a18252010-09-16 14:07:33 -0700582 releaseInboundEventLocked(entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700583 }
Jeff Brown481c1572012-03-09 14:41:15 -0800584 traceInboundQueueLengthLocked();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700585}
586
Jeff Brown54a18252010-09-16 14:07:33 -0700587void InputDispatcher::releasePendingEventLocked() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700588 if (mPendingEvent) {
Jeff Browne9bb9be2012-02-06 15:47:55 -0800589 resetANRTimeoutsLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700590 releaseInboundEventLocked(mPendingEvent);
Jeff Brownb88102f2010-09-08 11:49:43 -0700591 mPendingEvent = NULL;
592 }
593}
594
Jeff Brown54a18252010-09-16 14:07:33 -0700595void InputDispatcher::releaseInboundEventLocked(EventEntry* entry) {
Jeff Brown01ce2e92010-09-26 22:20:12 -0700596 InjectionState* injectionState = entry->injectionState;
597 if (injectionState && injectionState->injectionResult == INPUT_EVENT_INJECTION_PENDING) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700598#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +0000599 ALOGD("Injected inbound event was dropped.");
Jeff Brownb88102f2010-09-08 11:49:43 -0700600#endif
601 setInjectionResultLocked(entry, INPUT_EVENT_INJECTION_FAILED);
602 }
Jeff Brownabb4d442011-08-15 12:55:32 -0700603 if (entry == mNextUnblockedEvent) {
604 mNextUnblockedEvent = NULL;
605 }
Jeff Brownac386072011-07-20 15:19:50 -0700606 entry->release();
Jeff Brownb88102f2010-09-08 11:49:43 -0700607}
608
Jeff Brownb88102f2010-09-08 11:49:43 -0700609void InputDispatcher::resetKeyRepeatLocked() {
610 if (mKeyRepeatState.lastKeyEntry) {
Jeff Brownac386072011-07-20 15:19:50 -0700611 mKeyRepeatState.lastKeyEntry->release();
Jeff Brownb88102f2010-09-08 11:49:43 -0700612 mKeyRepeatState.lastKeyEntry = NULL;
613 }
614}
615
Jeff Brown214eaf42011-05-26 19:17:02 -0700616InputDispatcher::KeyEntry* InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) {
Jeff Brown349703e2010-06-22 01:27:15 -0700617 KeyEntry* entry = mKeyRepeatState.lastKeyEntry;
618
Jeff Brown349703e2010-06-22 01:27:15 -0700619 // Reuse the repeated key entry if it is otherwise unreferenced.
Jeff Browne20c9e02010-10-11 14:20:19 -0700620 uint32_t policyFlags = (entry->policyFlags & POLICY_FLAG_RAW_MASK)
621 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700622 if (entry->refCount == 1) {
Jeff Brownac386072011-07-20 15:19:50 -0700623 entry->recycle();
Jeff Brown7fbdc842010-06-17 20:52:56 -0700624 entry->eventTime = currentTime;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700625 entry->policyFlags = policyFlags;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700626 entry->repeatCount += 1;
627 } else {
Jeff Brownac386072011-07-20 15:19:50 -0700628 KeyEntry* newEntry = new KeyEntry(currentTime,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700629 entry->deviceId, entry->source, policyFlags,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700630 entry->action, entry->flags, entry->keyCode, entry->scanCode,
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700631 entry->metaState, entry->repeatCount + 1, entry->downTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700632
633 mKeyRepeatState.lastKeyEntry = newEntry;
Jeff Brownac386072011-07-20 15:19:50 -0700634 entry->release();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700635
636 entry = newEntry;
637 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700638 entry->syntheticRepeat = true;
639
640 // Increment reference count since we keep a reference to the event in
641 // mKeyRepeatState.lastKeyEntry in addition to the one we return.
642 entry->refCount += 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700643
Jeff Brown214eaf42011-05-26 19:17:02 -0700644 mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay;
Jeff Brownb88102f2010-09-08 11:49:43 -0700645 return entry;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700646}
647
Jeff Brownb88102f2010-09-08 11:49:43 -0700648bool InputDispatcher::dispatchConfigurationChangedLocked(
649 nsecs_t currentTime, ConfigurationChangedEntry* entry) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700650#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +0000651 ALOGD("dispatchConfigurationChanged - eventTime=%lld", entry->eventTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700652#endif
653
654 // Reset key repeating in case a keyboard device was added or removed or something.
655 resetKeyRepeatLocked();
656
657 // Enqueue a command to run outside the lock to tell the policy that the configuration changed.
658 CommandEntry* commandEntry = postCommandLocked(
659 & InputDispatcher::doNotifyConfigurationChangedInterruptible);
660 commandEntry->eventTime = entry->eventTime;
661 return true;
662}
663
Jeff Brown65fd2512011-08-18 11:20:58 -0700664bool InputDispatcher::dispatchDeviceResetLocked(
665 nsecs_t currentTime, DeviceResetEntry* entry) {
666#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +0000667 ALOGD("dispatchDeviceReset - eventTime=%lld, deviceId=%d", entry->eventTime, entry->deviceId);
Jeff Brown65fd2512011-08-18 11:20:58 -0700668#endif
669
670 CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS,
671 "device was reset");
672 options.deviceId = entry->deviceId;
673 synthesizeCancelationEventsForAllConnectionsLocked(options);
674 return true;
675}
676
Jeff Brown214eaf42011-05-26 19:17:02 -0700677bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700678 DropReason* dropReason, nsecs_t* nextWakeupTime) {
Jeff Browne46a0a42010-11-02 17:58:22 -0700679 // Preprocessing.
680 if (! entry->dispatchInProgress) {
681 if (entry->repeatCount == 0
682 && entry->action == AKEY_EVENT_ACTION_DOWN
683 && (entry->policyFlags & POLICY_FLAG_TRUSTED)
Jeff Brown0029c662011-03-30 02:25:18 -0700684 && (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) {
Jeff Browne46a0a42010-11-02 17:58:22 -0700685 if (mKeyRepeatState.lastKeyEntry
686 && mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode) {
687 // We have seen two identical key downs in a row which indicates that the device
688 // driver is automatically generating key repeats itself. We take note of the
689 // repeat here, but we disable our own next key repeat timer since it is clear that
690 // we will not need to synthesize key repeats ourselves.
691 entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1;
692 resetKeyRepeatLocked();
693 mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves
694 } else {
695 // Not a repeat. Save key down state in case we do see a repeat later.
696 resetKeyRepeatLocked();
Jeff Brown214eaf42011-05-26 19:17:02 -0700697 mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout;
Jeff Browne46a0a42010-11-02 17:58:22 -0700698 }
699 mKeyRepeatState.lastKeyEntry = entry;
700 entry->refCount += 1;
701 } else if (! entry->syntheticRepeat) {
702 resetKeyRepeatLocked();
703 }
704
Jeff Browne2e01262011-03-02 20:34:30 -0800705 if (entry->repeatCount == 1) {
706 entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS;
707 } else {
708 entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS;
709 }
710
Jeff Browne46a0a42010-11-02 17:58:22 -0700711 entry->dispatchInProgress = true;
Jeff Browne46a0a42010-11-02 17:58:22 -0700712
713 logOutboundKeyDetailsLocked("dispatchKey - ", entry);
714 }
715
Jeff Brown905805a2011-10-12 13:57:59 -0700716 // Handle case where the policy asked us to try again later last time.
717 if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) {
718 if (currentTime < entry->interceptKeyWakeupTime) {
719 if (entry->interceptKeyWakeupTime < *nextWakeupTime) {
720 *nextWakeupTime = entry->interceptKeyWakeupTime;
721 }
722 return false; // wait until next wakeup
723 }
724 entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN;
725 entry->interceptKeyWakeupTime = 0;
726 }
727
Jeff Brown54a18252010-09-16 14:07:33 -0700728 // Give the policy a chance to intercept the key.
729 if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700730 if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) {
Jeff Brown54a18252010-09-16 14:07:33 -0700731 CommandEntry* commandEntry = postCommandLocked(
732 & InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible);
Jeff Brown9302c872011-07-13 22:51:29 -0700733 if (mFocusedWindowHandle != NULL) {
734 commandEntry->inputWindowHandle = mFocusedWindowHandle;
Jeff Brown54a18252010-09-16 14:07:33 -0700735 }
736 commandEntry->keyEntry = entry;
737 entry->refCount += 1;
738 return false; // wait for the command to run
739 } else {
740 entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE;
741 }
742 } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700743 if (*dropReason == DROP_REASON_NOT_DROPPED) {
744 *dropReason = DROP_REASON_POLICY;
745 }
Jeff Brown54a18252010-09-16 14:07:33 -0700746 }
747
748 // Clean up if dropping the event.
Jeff Browne20c9e02010-10-11 14:20:19 -0700749 if (*dropReason != DROP_REASON_NOT_DROPPED) {
Jeff Brown3122e442010-10-11 23:32:49 -0700750 setInjectionResultLocked(entry, *dropReason == DROP_REASON_POLICY
751 ? INPUT_EVENT_INJECTION_SUCCEEDED : INPUT_EVENT_INJECTION_FAILED);
Jeff Brown54a18252010-09-16 14:07:33 -0700752 return true;
753 }
754
Jeff Brownb88102f2010-09-08 11:49:43 -0700755 // Identify targets.
Jeff Browne9bb9be2012-02-06 15:47:55 -0800756 Vector<InputTarget> inputTargets;
757 int32_t injectionResult = findFocusedWindowTargetsLocked(currentTime,
758 entry, inputTargets, nextWakeupTime);
759 if (injectionResult == INPUT_EVENT_INJECTION_PENDING) {
760 return false;
Jeff Brownb88102f2010-09-08 11:49:43 -0700761 }
762
Jeff Browne9bb9be2012-02-06 15:47:55 -0800763 setInjectionResultLocked(entry, injectionResult);
764 if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) {
765 return true;
766 }
767
768 addMonitoringTargetsLocked(inputTargets);
769
Jeff Brownb88102f2010-09-08 11:49:43 -0700770 // Dispatch the key.
Jeff Browne9bb9be2012-02-06 15:47:55 -0800771 dispatchEventLocked(currentTime, entry, inputTargets);
Jeff Brownb88102f2010-09-08 11:49:43 -0700772 return true;
773}
774
775void InputDispatcher::logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry) {
776#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +0000777 ALOGD("%seventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, "
Jeff Brownb88102f2010-09-08 11:49:43 -0700778 "action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, "
Jeff Browne46a0a42010-11-02 17:58:22 -0700779 "repeatCount=%d, downTime=%lld",
Jeff Brownb88102f2010-09-08 11:49:43 -0700780 prefix,
781 entry->eventTime, entry->deviceId, entry->source, entry->policyFlags,
782 entry->action, entry->flags, entry->keyCode, entry->scanCode, entry->metaState,
Jeff Browne46a0a42010-11-02 17:58:22 -0700783 entry->repeatCount, entry->downTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700784#endif
785}
786
787bool InputDispatcher::dispatchMotionLocked(
Jeff Browne20c9e02010-10-11 14:20:19 -0700788 nsecs_t currentTime, MotionEntry* entry, DropReason* dropReason, nsecs_t* nextWakeupTime) {
Jeff Browne46a0a42010-11-02 17:58:22 -0700789 // Preprocessing.
790 if (! entry->dispatchInProgress) {
791 entry->dispatchInProgress = true;
Jeff Browne46a0a42010-11-02 17:58:22 -0700792
793 logOutboundMotionDetailsLocked("dispatchMotion - ", entry);
794 }
795
Jeff Brown54a18252010-09-16 14:07:33 -0700796 // Clean up if dropping the event.
Jeff Browne20c9e02010-10-11 14:20:19 -0700797 if (*dropReason != DROP_REASON_NOT_DROPPED) {
Jeff Brown3122e442010-10-11 23:32:49 -0700798 setInjectionResultLocked(entry, *dropReason == DROP_REASON_POLICY
799 ? INPUT_EVENT_INJECTION_SUCCEEDED : INPUT_EVENT_INJECTION_FAILED);
Jeff Brown54a18252010-09-16 14:07:33 -0700800 return true;
801 }
802
Jeff Brownb88102f2010-09-08 11:49:43 -0700803 bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER;
804
805 // Identify targets.
Jeff Browne9bb9be2012-02-06 15:47:55 -0800806 Vector<InputTarget> inputTargets;
807
Jeff Browncc0c1592011-02-19 05:07:28 -0800808 bool conflictingPointerActions = false;
Jeff Browne9bb9be2012-02-06 15:47:55 -0800809 int32_t injectionResult;
810 if (isPointerEvent) {
811 // Pointer event. (eg. touchscreen)
812 injectionResult = findTouchedWindowTargetsLocked(currentTime,
813 entry, inputTargets, nextWakeupTime, &conflictingPointerActions);
814 } else {
815 // Non touch event. (eg. trackball)
816 injectionResult = findFocusedWindowTargetsLocked(currentTime,
817 entry, inputTargets, nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700818 }
Jeff Browne9bb9be2012-02-06 15:47:55 -0800819 if (injectionResult == INPUT_EVENT_INJECTION_PENDING) {
820 return false;
821 }
822
823 setInjectionResultLocked(entry, injectionResult);
824 if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) {
825 return true;
826 }
827
828 addMonitoringTargetsLocked(inputTargets);
Jeff Brownb88102f2010-09-08 11:49:43 -0700829
830 // Dispatch the motion.
Jeff Browncc0c1592011-02-19 05:07:28 -0800831 if (conflictingPointerActions) {
Jeff Brownda3d5a92011-03-29 15:11:34 -0700832 CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS,
833 "conflicting pointer actions");
834 synthesizeCancelationEventsForAllConnectionsLocked(options);
Jeff Browncc0c1592011-02-19 05:07:28 -0800835 }
Jeff Browne9bb9be2012-02-06 15:47:55 -0800836 dispatchEventLocked(currentTime, entry, inputTargets);
Jeff Brownb88102f2010-09-08 11:49:43 -0700837 return true;
838}
839
840
841void InputDispatcher::logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry) {
842#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +0000843 ALOGD("%seventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, "
Jeff Brown85a31762010-09-01 17:01:00 -0700844 "action=0x%x, flags=0x%x, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700845 "metaState=0x%x, buttonState=0x%x, "
846 "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%lld",
Jeff Brownb88102f2010-09-08 11:49:43 -0700847 prefix,
Jeff Brown85a31762010-09-01 17:01:00 -0700848 entry->eventTime, entry->deviceId, entry->source, entry->policyFlags,
849 entry->action, entry->flags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700850 entry->metaState, entry->buttonState,
851 entry->edgeFlags, entry->xPrecision, entry->yPrecision,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700852 entry->downTime);
853
Jeff Brown46b9ac02010-04-22 18:58:52 -0700854 for (uint32_t i = 0; i < entry->pointerCount; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +0000855 ALOGD(" Pointer %d: id=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700856 "x=%f, y=%f, pressure=%f, size=%f, "
Jeff Brown85a31762010-09-01 17:01:00 -0700857 "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, "
Jeff Brown8d608662010-08-30 03:02:23 -0700858 "orientation=%f",
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700859 i, entry->pointerProperties[i].id,
860 entry->pointerProperties[i].toolType,
Jeff Brown3241b6b2012-02-03 15:08:02 -0800861 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
862 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
863 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
864 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
865 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
866 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
867 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
868 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
869 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
Jeff Brown46b9ac02010-04-22 18:58:52 -0700870 }
871#endif
Jeff Brown46b9ac02010-04-22 18:58:52 -0700872}
873
Jeff Browne9bb9be2012-02-06 15:47:55 -0800874void InputDispatcher::dispatchEventLocked(nsecs_t currentTime,
875 EventEntry* eventEntry, const Vector<InputTarget>& inputTargets) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700876#if DEBUG_DISPATCH_CYCLE
Jeff Brown3241b6b2012-02-03 15:08:02 -0800877 ALOGD("dispatchEventToCurrentInputTargets");
Jeff Brown46b9ac02010-04-22 18:58:52 -0700878#endif
879
Steve Blockec193de2012-01-09 18:35:44 +0000880 ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true
Jeff Brown9c3cda02010-06-15 01:31:58 -0700881
Jeff Browne2fe69e2010-10-18 13:21:23 -0700882 pokeUserActivityLocked(eventEntry);
883
Jeff Browne9bb9be2012-02-06 15:47:55 -0800884 for (size_t i = 0; i < inputTargets.size(); i++) {
885 const InputTarget& inputTarget = inputTargets.itemAt(i);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700886
Jeff Brown519e0242010-09-15 15:18:56 -0700887 ssize_t connectionIndex = getConnectionIndexLocked(inputTarget.inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700888 if (connectionIndex >= 0) {
Jeff Browncbee6d62012-02-03 20:11:27 -0800889 sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex);
Jeff Brown3241b6b2012-02-03 15:08:02 -0800890 prepareDispatchCycleLocked(currentTime, connection, eventEntry, &inputTarget);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700891 } else {
Jeff Brownb6997262010-10-08 22:31:17 -0700892#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +0000893 ALOGD("Dropping event delivery to target with channel '%s' because it "
Jeff Brownb6997262010-10-08 22:31:17 -0700894 "is no longer registered with the input dispatcher.",
Jeff Brown46b9ac02010-04-22 18:58:52 -0700895 inputTarget.inputChannel->getName().string());
Jeff Brownb6997262010-10-08 22:31:17 -0700896#endif
Jeff Brown46b9ac02010-04-22 18:58:52 -0700897 }
898 }
899}
900
Jeff Brownb88102f2010-09-08 11:49:43 -0700901int32_t InputDispatcher::handleTargetsNotReadyLocked(nsecs_t currentTime,
Jeff Brown9302c872011-07-13 22:51:29 -0700902 const EventEntry* entry,
903 const sp<InputApplicationHandle>& applicationHandle,
904 const sp<InputWindowHandle>& windowHandle,
Jeff Brown265f1cc2012-06-11 18:01:06 -0700905 nsecs_t* nextWakeupTime, const char* reason) {
Jeff Brown9302c872011-07-13 22:51:29 -0700906 if (applicationHandle == NULL && windowHandle == NULL) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700907 if (mInputTargetWaitCause != INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY) {
908#if DEBUG_FOCUS
Jeff Brown265f1cc2012-06-11 18:01:06 -0700909 ALOGD("Waiting for system to become ready for input. Reason: %s", reason);
Jeff Brownb88102f2010-09-08 11:49:43 -0700910#endif
911 mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY;
912 mInputTargetWaitStartTime = currentTime;
913 mInputTargetWaitTimeoutTime = LONG_LONG_MAX;
914 mInputTargetWaitTimeoutExpired = false;
Jeff Brown9302c872011-07-13 22:51:29 -0700915 mInputTargetWaitApplicationHandle.clear();
Jeff Brownb88102f2010-09-08 11:49:43 -0700916 }
917 } else {
918 if (mInputTargetWaitCause != INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) {
919#if DEBUG_FOCUS
Jeff Brown265f1cc2012-06-11 18:01:06 -0700920 ALOGD("Waiting for application to become ready for input: %s. Reason: %s",
921 getApplicationWindowLabelLocked(applicationHandle, windowHandle).string(),
922 reason);
Jeff Brownb88102f2010-09-08 11:49:43 -0700923#endif
Jeff Browncc4f7db2011-08-30 20:34:48 -0700924 nsecs_t timeout;
925 if (windowHandle != NULL) {
926 timeout = windowHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT);
927 } else if (applicationHandle != NULL) {
928 timeout = applicationHandle->getDispatchingTimeout(
929 DEFAULT_INPUT_DISPATCHING_TIMEOUT);
930 } else {
931 timeout = DEFAULT_INPUT_DISPATCHING_TIMEOUT;
932 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700933
934 mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY;
935 mInputTargetWaitStartTime = currentTime;
936 mInputTargetWaitTimeoutTime = currentTime + timeout;
937 mInputTargetWaitTimeoutExpired = false;
Jeff Brown9302c872011-07-13 22:51:29 -0700938 mInputTargetWaitApplicationHandle.clear();
Jeff Brown928e0542011-01-10 11:17:36 -0800939
Jeff Brown9302c872011-07-13 22:51:29 -0700940 if (windowHandle != NULL) {
941 mInputTargetWaitApplicationHandle = windowHandle->inputApplicationHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800942 }
Jeff Brown9302c872011-07-13 22:51:29 -0700943 if (mInputTargetWaitApplicationHandle == NULL && applicationHandle != NULL) {
944 mInputTargetWaitApplicationHandle = applicationHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800945 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700946 }
947 }
948
949 if (mInputTargetWaitTimeoutExpired) {
950 return INPUT_EVENT_INJECTION_TIMED_OUT;
951 }
952
953 if (currentTime >= mInputTargetWaitTimeoutTime) {
Jeff Brown9302c872011-07-13 22:51:29 -0700954 onANRLocked(currentTime, applicationHandle, windowHandle,
Jeff Brown265f1cc2012-06-11 18:01:06 -0700955 entry->eventTime, mInputTargetWaitStartTime, reason);
Jeff Brownb88102f2010-09-08 11:49:43 -0700956
957 // Force poll loop to wake up immediately on next iteration once we get the
958 // ANR response back from the policy.
959 *nextWakeupTime = LONG_LONG_MIN;
960 return INPUT_EVENT_INJECTION_PENDING;
961 } else {
962 // Force poll loop to wake up when timeout is due.
963 if (mInputTargetWaitTimeoutTime < *nextWakeupTime) {
964 *nextWakeupTime = mInputTargetWaitTimeoutTime;
965 }
966 return INPUT_EVENT_INJECTION_PENDING;
967 }
968}
969
Jeff Brown519e0242010-09-15 15:18:56 -0700970void InputDispatcher::resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
971 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700972 if (newTimeout > 0) {
973 // Extend the timeout.
974 mInputTargetWaitTimeoutTime = now() + newTimeout;
975 } else {
976 // Give up.
977 mInputTargetWaitTimeoutExpired = true;
Jeff Brown519e0242010-09-15 15:18:56 -0700978
979 // Input state will not be realistic. Mark it out of sync.
Jeff Browndc3e0052010-09-16 11:02:16 -0700980 if (inputChannel.get()) {
981 ssize_t connectionIndex = getConnectionIndexLocked(inputChannel);
982 if (connectionIndex >= 0) {
Jeff Browncbee6d62012-02-03 20:11:27 -0800983 sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex);
Jeff Brownf44e3942012-04-20 11:33:27 -0700984 sp<InputWindowHandle> windowHandle = connection->inputWindowHandle;
985
986 if (windowHandle != NULL) {
987 mTouchState.removeWindow(windowHandle);
988 }
989
Jeff Brown00045a72010-12-09 18:10:30 -0800990 if (connection->status == Connection::STATUS_NORMAL) {
Jeff Brownda3d5a92011-03-29 15:11:34 -0700991 CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS,
Jeff Brown00045a72010-12-09 18:10:30 -0800992 "application not responding");
Jeff Brownda3d5a92011-03-29 15:11:34 -0700993 synthesizeCancelationEventsForConnectionLocked(connection, options);
Jeff Brown00045a72010-12-09 18:10:30 -0800994 }
Jeff Browndc3e0052010-09-16 11:02:16 -0700995 }
Jeff Brown519e0242010-09-15 15:18:56 -0700996 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700997 }
998}
999
Jeff Brown519e0242010-09-15 15:18:56 -07001000nsecs_t InputDispatcher::getTimeSpentWaitingForApplicationLocked(
Jeff Brownb88102f2010-09-08 11:49:43 -07001001 nsecs_t currentTime) {
1002 if (mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) {
1003 return currentTime - mInputTargetWaitStartTime;
1004 }
1005 return 0;
1006}
1007
1008void InputDispatcher::resetANRTimeoutsLocked() {
1009#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001010 ALOGD("Resetting ANR timeouts.");
Jeff Brownb88102f2010-09-08 11:49:43 -07001011#endif
1012
Jeff Brownb88102f2010-09-08 11:49:43 -07001013 // Reset input target wait timeout.
1014 mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_NONE;
Jeff Brown5ea29ab2011-07-27 11:50:51 -07001015 mInputTargetWaitApplicationHandle.clear();
Jeff Brownb88102f2010-09-08 11:49:43 -07001016}
1017
Jeff Brown01ce2e92010-09-26 22:20:12 -07001018int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime,
Jeff Browne9bb9be2012-02-06 15:47:55 -08001019 const EventEntry* entry, Vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001020 int32_t injectionResult;
1021
1022 // If there is no currently focused window and no focused application
1023 // then drop the event.
Jeff Brown9302c872011-07-13 22:51:29 -07001024 if (mFocusedWindowHandle == NULL) {
1025 if (mFocusedApplicationHandle != NULL) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001026 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown265f1cc2012-06-11 18:01:06 -07001027 mFocusedApplicationHandle, NULL, nextWakeupTime,
1028 "Waiting because no window has focus but there is a "
1029 "focused application that may eventually add a window "
1030 "when it finishes starting up.");
Jeff Brownb88102f2010-09-08 11:49:43 -07001031 goto Unresponsive;
1032 }
1033
Steve Block6215d3f2012-01-04 20:05:49 +00001034 ALOGI("Dropping event because there is no focused window or focused application.");
Jeff Brownb88102f2010-09-08 11:49:43 -07001035 injectionResult = INPUT_EVENT_INJECTION_FAILED;
1036 goto Failed;
1037 }
1038
1039 // Check permissions.
Jeff Brown9302c872011-07-13 22:51:29 -07001040 if (! checkInjectionPermission(mFocusedWindowHandle, entry->injectionState)) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001041 injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED;
1042 goto Failed;
1043 }
1044
1045 // If the currently focused window is paused then keep waiting.
Jeff Browncc4f7db2011-08-30 20:34:48 -07001046 if (mFocusedWindowHandle->getInfo()->paused) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001047 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown265f1cc2012-06-11 18:01:06 -07001048 mFocusedApplicationHandle, mFocusedWindowHandle, nextWakeupTime,
1049 "Waiting because the focused window is paused.");
Jeff Brownb88102f2010-09-08 11:49:43 -07001050 goto Unresponsive;
1051 }
1052
Jeff Brown519e0242010-09-15 15:18:56 -07001053 // If the currently focused window is still working on previous events then keep waiting.
Jeff Brown0952c302012-02-13 13:48:59 -08001054 if (!isWindowReadyForMoreInputLocked(currentTime, mFocusedWindowHandle, entry)) {
Jeff Brown519e0242010-09-15 15:18:56 -07001055 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown265f1cc2012-06-11 18:01:06 -07001056 mFocusedApplicationHandle, mFocusedWindowHandle, nextWakeupTime,
1057 "Waiting because the focused window has not finished "
1058 "processing the input events that were previously delivered to it.");
Jeff Brown519e0242010-09-15 15:18:56 -07001059 goto Unresponsive;
1060 }
1061
Jeff Brownb88102f2010-09-08 11:49:43 -07001062 // Success! Output targets.
1063 injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED;
Jeff Brown9302c872011-07-13 22:51:29 -07001064 addWindowTargetLocked(mFocusedWindowHandle,
Jeff Browne9bb9be2012-02-06 15:47:55 -08001065 InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, BitSet32(0),
1066 inputTargets);
Jeff Brownb88102f2010-09-08 11:49:43 -07001067
1068 // Done.
1069Failed:
1070Unresponsive:
Jeff Brown519e0242010-09-15 15:18:56 -07001071 nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime);
1072 updateDispatchStatisticsLocked(currentTime, entry,
1073 injectionResult, timeSpentWaitingForApplication);
Jeff Brownb88102f2010-09-08 11:49:43 -07001074#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001075 ALOGD("findFocusedWindow finished: injectionResult=%d, "
Jeff Brown519e0242010-09-15 15:18:56 -07001076 "timeSpendWaitingForApplication=%0.1fms",
1077 injectionResult, timeSpentWaitingForApplication / 1000000.0);
Jeff Brownb88102f2010-09-08 11:49:43 -07001078#endif
1079 return injectionResult;
1080}
1081
Jeff Brown01ce2e92010-09-26 22:20:12 -07001082int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime,
Jeff Browne9bb9be2012-02-06 15:47:55 -08001083 const MotionEntry* entry, Vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime,
1084 bool* outConflictingPointerActions) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001085 enum InjectionPermission {
1086 INJECTION_PERMISSION_UNKNOWN,
1087 INJECTION_PERMISSION_GRANTED,
1088 INJECTION_PERMISSION_DENIED
1089 };
1090
Jeff Brownb88102f2010-09-08 11:49:43 -07001091 nsecs_t startTime = now();
1092
1093 // For security reasons, we defer updating the touch state until we are sure that
1094 // event injection will be allowed.
1095 //
1096 // FIXME In the original code, screenWasOff could never be set to true.
1097 // The reason is that the POLICY_FLAG_WOKE_HERE
1098 // and POLICY_FLAG_BRIGHT_HERE flags were set only when preprocessing raw
1099 // EV_KEY, EV_REL and EV_ABS events. As it happens, the touch event was
1100 // actually enqueued using the policyFlags that appeared in the final EV_SYN
1101 // events upon which no preprocessing took place. So policyFlags was always 0.
1102 // In the new native input dispatcher we're a bit more careful about event
1103 // preprocessing so the touches we receive can actually have non-zero policyFlags.
1104 // Unfortunately we obtain undesirable behavior.
1105 //
1106 // Here's what happens:
1107 //
1108 // When the device dims in anticipation of going to sleep, touches
1109 // in windows which have FLAG_TOUCHABLE_WHEN_WAKING cause
1110 // the device to brighten and reset the user activity timer.
1111 // Touches on other windows (such as the launcher window)
1112 // are dropped. Then after a moment, the device goes to sleep. Oops.
1113 //
1114 // Also notice how screenWasOff was being initialized using POLICY_FLAG_BRIGHT_HERE
1115 // instead of POLICY_FLAG_WOKE_HERE...
1116 //
1117 bool screenWasOff = false; // original policy: policyFlags & POLICY_FLAG_BRIGHT_HERE;
1118
1119 int32_t action = entry->action;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001120 int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK;
Jeff Brownb88102f2010-09-08 11:49:43 -07001121
1122 // Update the touch state as needed based on the properties of the touch event.
Jeff Brown01ce2e92010-09-26 22:20:12 -07001123 int32_t injectionResult = INPUT_EVENT_INJECTION_PENDING;
1124 InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN;
Jeff Brown9302c872011-07-13 22:51:29 -07001125 sp<InputWindowHandle> newHoverWindowHandle;
Jeff Browncc0c1592011-02-19 05:07:28 -08001126
1127 bool isSplit = mTouchState.split;
Jeff Brown2717eff2011-06-30 23:53:07 -07001128 bool switchedDevice = mTouchState.deviceId >= 0
1129 && (mTouchState.deviceId != entry->deviceId
1130 || mTouchState.source != entry->source);
Jeff Browna032cc02011-03-07 16:56:21 -08001131 bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE
1132 || maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER
1133 || maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT);
1134 bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN
1135 || maskedAction == AMOTION_EVENT_ACTION_SCROLL
1136 || isHoverAction);
Jeff Brown81346812011-06-28 20:08:48 -07001137 bool wrongDevice = false;
Jeff Browna032cc02011-03-07 16:56:21 -08001138 if (newGesture) {
Jeff Browncc0c1592011-02-19 05:07:28 -08001139 bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN;
Jeff Brown81346812011-06-28 20:08:48 -07001140 if (switchedDevice && mTouchState.down && !down) {
1141#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001142 ALOGD("Dropping event because a pointer for a different device is already down.");
Jeff Brown81346812011-06-28 20:08:48 -07001143#endif
Jeff Browncc0c1592011-02-19 05:07:28 -08001144 mTempTouchState.copyFrom(mTouchState);
Jeff Brown81346812011-06-28 20:08:48 -07001145 injectionResult = INPUT_EVENT_INJECTION_FAILED;
1146 switchedDevice = false;
1147 wrongDevice = true;
1148 goto Failed;
Jeff Browncc0c1592011-02-19 05:07:28 -08001149 }
Jeff Brown81346812011-06-28 20:08:48 -07001150 mTempTouchState.reset();
1151 mTempTouchState.down = down;
1152 mTempTouchState.deviceId = entry->deviceId;
1153 mTempTouchState.source = entry->source;
1154 isSplit = false;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001155 } else {
1156 mTempTouchState.copyFrom(mTouchState);
Jeff Browncc0c1592011-02-19 05:07:28 -08001157 }
Jeff Brownb88102f2010-09-08 11:49:43 -07001158
Jeff Browna032cc02011-03-07 16:56:21 -08001159 if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08001160 /* Case 1: New splittable pointer going down, or need target for hover or scroll. */
Jeff Brownb88102f2010-09-08 11:49:43 -07001161
Jeff Brown01ce2e92010-09-26 22:20:12 -07001162 int32_t pointerIndex = getMotionEventActionPointerIndex(action);
Jeff Brown3241b6b2012-02-03 15:08:02 -08001163 int32_t x = int32_t(entry->pointerCoords[pointerIndex].
Jeff Brownebbd5d12011-02-17 13:01:34 -08001164 getAxisValue(AMOTION_EVENT_AXIS_X));
Jeff Brown3241b6b2012-02-03 15:08:02 -08001165 int32_t y = int32_t(entry->pointerCoords[pointerIndex].
Jeff Brownebbd5d12011-02-17 13:01:34 -08001166 getAxisValue(AMOTION_EVENT_AXIS_Y));
Jeff Brown9302c872011-07-13 22:51:29 -07001167 sp<InputWindowHandle> newTouchedWindowHandle;
1168 sp<InputWindowHandle> topErrorWindowHandle;
Jeff Browna032cc02011-03-07 16:56:21 -08001169 bool isTouchModal = false;
Jeff Brownb88102f2010-09-08 11:49:43 -07001170
1171 // Traverse windows from front to back to find touched window and outside targets.
Jeff Brown9302c872011-07-13 22:51:29 -07001172 size_t numWindows = mWindowHandles.size();
Jeff Brownb88102f2010-09-08 11:49:43 -07001173 for (size_t i = 0; i < numWindows; i++) {
Jeff Brown9302c872011-07-13 22:51:29 -07001174 sp<InputWindowHandle> windowHandle = mWindowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001175 const InputWindowInfo* windowInfo = windowHandle->getInfo();
1176 int32_t flags = windowInfo->layoutParamsFlags;
Jeff Brownb88102f2010-09-08 11:49:43 -07001177
Jeff Browncc4f7db2011-08-30 20:34:48 -07001178 if (flags & InputWindowInfo::FLAG_SYSTEM_ERROR) {
Jeff Brown9302c872011-07-13 22:51:29 -07001179 if (topErrorWindowHandle == NULL) {
1180 topErrorWindowHandle = windowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -07001181 }
1182 }
1183
Jeff Browncc4f7db2011-08-30 20:34:48 -07001184 if (windowInfo->visible) {
1185 if (! (flags & InputWindowInfo::FLAG_NOT_TOUCHABLE)) {
1186 isTouchModal = (flags & (InputWindowInfo::FLAG_NOT_FOCUSABLE
1187 | InputWindowInfo::FLAG_NOT_TOUCH_MODAL)) == 0;
1188 if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) {
Jeff Brown9302c872011-07-13 22:51:29 -07001189 if (! screenWasOff
Jeff Browncc4f7db2011-08-30 20:34:48 -07001190 || (flags & InputWindowInfo::FLAG_TOUCHABLE_WHEN_WAKING)) {
Jeff Brown9302c872011-07-13 22:51:29 -07001191 newTouchedWindowHandle = windowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -07001192 }
1193 break; // found touched window, exit window loop
1194 }
1195 }
1196
Jeff Brown01ce2e92010-09-26 22:20:12 -07001197 if (maskedAction == AMOTION_EVENT_ACTION_DOWN
Jeff Browncc4f7db2011-08-30 20:34:48 -07001198 && (flags & InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH)) {
Jeff Browna032cc02011-03-07 16:56:21 -08001199 int32_t outsideTargetFlags = InputTarget::FLAG_DISPATCH_AS_OUTSIDE;
Jeff Brown9302c872011-07-13 22:51:29 -07001200 if (isWindowObscuredAtPointLocked(windowHandle, x, y)) {
Jeff Brown19dfc832010-10-05 12:26:23 -07001201 outsideTargetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED;
1202 }
1203
Jeff Brown9302c872011-07-13 22:51:29 -07001204 mTempTouchState.addOrUpdateWindow(
1205 windowHandle, outsideTargetFlags, BitSet32(0));
Jeff Brownb88102f2010-09-08 11:49:43 -07001206 }
1207 }
1208 }
1209
1210 // If there is an error window but it is not taking focus (typically because
1211 // it is invisible) then wait for it. Any other focused window may in
1212 // fact be in ANR state.
Jeff Brown9302c872011-07-13 22:51:29 -07001213 if (topErrorWindowHandle != NULL && newTouchedWindowHandle != topErrorWindowHandle) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001214 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown265f1cc2012-06-11 18:01:06 -07001215 NULL, NULL, nextWakeupTime,
1216 "Waiting because a system error window is about to be displayed.");
Jeff Brownb88102f2010-09-08 11:49:43 -07001217 injectionPermission = INJECTION_PERMISSION_UNKNOWN;
1218 goto Unresponsive;
1219 }
1220
Jeff Brown01ce2e92010-09-26 22:20:12 -07001221 // Figure out whether splitting will be allowed for this window.
Jeff Browncc4f7db2011-08-30 20:34:48 -07001222 if (newTouchedWindowHandle != NULL
1223 && newTouchedWindowHandle->getInfo()->supportsSplitTouch()) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001224 // New window supports splitting.
1225 isSplit = true;
1226 } else if (isSplit) {
1227 // New window does not support splitting but we have already split events.
Jeff Brown8249fc62012-05-24 18:57:32 -07001228 // Ignore the new window.
1229 newTouchedWindowHandle = NULL;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001230 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001231
Jeff Brown8249fc62012-05-24 18:57:32 -07001232 // Handle the case where we did not find a window.
Jeff Brown9302c872011-07-13 22:51:29 -07001233 if (newTouchedWindowHandle == NULL) {
Jeff Brown8249fc62012-05-24 18:57:32 -07001234 // Try to assign the pointer to the first foreground window we find, if there is one.
1235 newTouchedWindowHandle = mTempTouchState.getFirstForegroundWindowHandle();
1236 if (newTouchedWindowHandle == NULL) {
1237 // There is no touched window. If this is an initial down event
1238 // then wait for a window to appear that will handle the touch. This is
1239 // to ensure that we report an ANR in the case where an application has started
1240 // but not yet put up a window and the user is starting to get impatient.
1241 if (maskedAction == AMOTION_EVENT_ACTION_DOWN
1242 && mFocusedApplicationHandle != NULL) {
Jeff Brown8249fc62012-05-24 18:57:32 -07001243 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown265f1cc2012-06-11 18:01:06 -07001244 mFocusedApplicationHandle, NULL, nextWakeupTime,
1245 "Waiting because there is no touchable window that can "
1246 "handle the event but there is focused application that may "
1247 "eventually add a new window when it finishes starting up.");
Jeff Brown8249fc62012-05-24 18:57:32 -07001248 goto Unresponsive;
1249 }
Jeff Brownb88102f2010-09-08 11:49:43 -07001250
Jeff Brown8249fc62012-05-24 18:57:32 -07001251 ALOGI("Dropping event because there is no touched window.");
1252 injectionResult = INPUT_EVENT_INJECTION_FAILED;
1253 goto Failed;
1254 }
Jeff Brownb88102f2010-09-08 11:49:43 -07001255 }
1256
Jeff Brown19dfc832010-10-05 12:26:23 -07001257 // Set target flags.
Jeff Browna032cc02011-03-07 16:56:21 -08001258 int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS;
Jeff Brown19dfc832010-10-05 12:26:23 -07001259 if (isSplit) {
1260 targetFlags |= InputTarget::FLAG_SPLIT;
1261 }
Jeff Brown9302c872011-07-13 22:51:29 -07001262 if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) {
Jeff Brown19dfc832010-10-05 12:26:23 -07001263 targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED;
1264 }
1265
Jeff Browna032cc02011-03-07 16:56:21 -08001266 // Update hover state.
1267 if (isHoverAction) {
Jeff Brown9302c872011-07-13 22:51:29 -07001268 newHoverWindowHandle = newTouchedWindowHandle;
Jeff Browna032cc02011-03-07 16:56:21 -08001269 } else if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) {
Jeff Brown9302c872011-07-13 22:51:29 -07001270 newHoverWindowHandle = mLastHoverWindowHandle;
Jeff Browna032cc02011-03-07 16:56:21 -08001271 }
1272
Jeff Brown01ce2e92010-09-26 22:20:12 -07001273 // Update the temporary touch state.
1274 BitSet32 pointerIds;
1275 if (isSplit) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001276 uint32_t pointerId = entry->pointerProperties[pointerIndex].id;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001277 pointerIds.markBit(pointerId);
Jeff Brownb88102f2010-09-08 11:49:43 -07001278 }
Jeff Brown9302c872011-07-13 22:51:29 -07001279 mTempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds);
Jeff Brownb88102f2010-09-08 11:49:43 -07001280 } else {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001281 /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */
Jeff Brownb88102f2010-09-08 11:49:43 -07001282
1283 // If the pointer is not currently down, then ignore the event.
Jeff Brown01ce2e92010-09-26 22:20:12 -07001284 if (! mTempTouchState.down) {
Jeff Browna2cc28d2011-03-25 11:58:46 -07001285#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001286 ALOGD("Dropping event because the pointer is not down or we previously "
Jeff Brown76860e32010-10-25 17:37:46 -07001287 "dropped the pointer down event.");
1288#endif
Jeff Brownb88102f2010-09-08 11:49:43 -07001289 injectionResult = INPUT_EVENT_INJECTION_FAILED;
Jeff Brownb88102f2010-09-08 11:49:43 -07001290 goto Failed;
1291 }
Jeff Brown98db5fa2011-06-08 15:37:10 -07001292
1293 // Check whether touches should slip outside of the current foreground window.
1294 if (maskedAction == AMOTION_EVENT_ACTION_MOVE
1295 && entry->pointerCount == 1
1296 && mTempTouchState.isSlippery()) {
Jeff Brown3241b6b2012-02-03 15:08:02 -08001297 int32_t x = int32_t(entry->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X));
1298 int32_t y = int32_t(entry->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y));
Jeff Brown98db5fa2011-06-08 15:37:10 -07001299
Jeff Brown9302c872011-07-13 22:51:29 -07001300 sp<InputWindowHandle> oldTouchedWindowHandle =
1301 mTempTouchState.getFirstForegroundWindowHandle();
1302 sp<InputWindowHandle> newTouchedWindowHandle = findTouchedWindowAtLocked(x, y);
1303 if (oldTouchedWindowHandle != newTouchedWindowHandle
1304 && newTouchedWindowHandle != NULL) {
Jeff Brown98db5fa2011-06-08 15:37:10 -07001305#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001306 ALOGD("Touch is slipping out of window %s into window %s.",
Jeff Browncc4f7db2011-08-30 20:34:48 -07001307 oldTouchedWindowHandle->getName().string(),
1308 newTouchedWindowHandle->getName().string());
Jeff Brown98db5fa2011-06-08 15:37:10 -07001309#endif
1310 // Make a slippery exit from the old window.
Jeff Brown9302c872011-07-13 22:51:29 -07001311 mTempTouchState.addOrUpdateWindow(oldTouchedWindowHandle,
Jeff Brown98db5fa2011-06-08 15:37:10 -07001312 InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, BitSet32(0));
1313
1314 // Make a slippery entrance into the new window.
Jeff Browncc4f7db2011-08-30 20:34:48 -07001315 if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) {
Jeff Brown98db5fa2011-06-08 15:37:10 -07001316 isSplit = true;
1317 }
1318
1319 int32_t targetFlags = InputTarget::FLAG_FOREGROUND
1320 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER;
1321 if (isSplit) {
1322 targetFlags |= InputTarget::FLAG_SPLIT;
1323 }
Jeff Brown9302c872011-07-13 22:51:29 -07001324 if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) {
Jeff Brown98db5fa2011-06-08 15:37:10 -07001325 targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED;
1326 }
1327
1328 BitSet32 pointerIds;
1329 if (isSplit) {
1330 pointerIds.markBit(entry->pointerProperties[0].id);
1331 }
Jeff Brown9302c872011-07-13 22:51:29 -07001332 mTempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds);
Jeff Brown98db5fa2011-06-08 15:37:10 -07001333 }
1334 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001335 }
Jeff Brownb88102f2010-09-08 11:49:43 -07001336
Jeff Brown9302c872011-07-13 22:51:29 -07001337 if (newHoverWindowHandle != mLastHoverWindowHandle) {
Jeff Browna032cc02011-03-07 16:56:21 -08001338 // Let the previous window know that the hover sequence is over.
Jeff Brown9302c872011-07-13 22:51:29 -07001339 if (mLastHoverWindowHandle != NULL) {
Jeff Browna032cc02011-03-07 16:56:21 -08001340#if DEBUG_HOVER
Steve Block5baa3a62011-12-20 16:23:08 +00001341 ALOGD("Sending hover exit event to window %s.",
Jeff Browncc4f7db2011-08-30 20:34:48 -07001342 mLastHoverWindowHandle->getName().string());
Jeff Browna032cc02011-03-07 16:56:21 -08001343#endif
Jeff Brown9302c872011-07-13 22:51:29 -07001344 mTempTouchState.addOrUpdateWindow(mLastHoverWindowHandle,
Jeff Browna032cc02011-03-07 16:56:21 -08001345 InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0));
1346 }
1347
1348 // Let the new window know that the hover sequence is starting.
Jeff Brown9302c872011-07-13 22:51:29 -07001349 if (newHoverWindowHandle != NULL) {
Jeff Browna032cc02011-03-07 16:56:21 -08001350#if DEBUG_HOVER
Steve Block5baa3a62011-12-20 16:23:08 +00001351 ALOGD("Sending hover enter event to window %s.",
Jeff Browncc4f7db2011-08-30 20:34:48 -07001352 newHoverWindowHandle->getName().string());
Jeff Browna032cc02011-03-07 16:56:21 -08001353#endif
Jeff Brown9302c872011-07-13 22:51:29 -07001354 mTempTouchState.addOrUpdateWindow(newHoverWindowHandle,
Jeff Browna032cc02011-03-07 16:56:21 -08001355 InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, BitSet32(0));
1356 }
1357 }
1358
Jeff Brown01ce2e92010-09-26 22:20:12 -07001359 // Check permission to inject into all touched foreground windows and ensure there
1360 // is at least one touched foreground window.
1361 {
1362 bool haveForegroundWindow = false;
1363 for (size_t i = 0; i < mTempTouchState.windows.size(); i++) {
1364 const TouchedWindow& touchedWindow = mTempTouchState.windows[i];
1365 if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) {
1366 haveForegroundWindow = true;
Jeff Brown9302c872011-07-13 22:51:29 -07001367 if (! checkInjectionPermission(touchedWindow.windowHandle,
1368 entry->injectionState)) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001369 injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED;
1370 injectionPermission = INJECTION_PERMISSION_DENIED;
1371 goto Failed;
1372 }
1373 }
1374 }
1375 if (! haveForegroundWindow) {
Jeff Browna2cc28d2011-03-25 11:58:46 -07001376#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001377 ALOGD("Dropping event because there is no touched foreground window to receive it.");
Jeff Brownb88102f2010-09-08 11:49:43 -07001378#endif
1379 injectionResult = INPUT_EVENT_INJECTION_FAILED;
Jeff Brownb88102f2010-09-08 11:49:43 -07001380 goto Failed;
1381 }
1382
Jeff Brown01ce2e92010-09-26 22:20:12 -07001383 // Permission granted to injection into all touched foreground windows.
1384 injectionPermission = INJECTION_PERMISSION_GRANTED;
1385 }
Jeff Brown519e0242010-09-15 15:18:56 -07001386
Kenny Root7a9db182011-06-02 15:16:05 -07001387 // Check whether windows listening for outside touches are owned by the same UID. If it is
1388 // set the policy flag that we will not reveal coordinate information to this window.
1389 if (maskedAction == AMOTION_EVENT_ACTION_DOWN) {
Jeff Brown9302c872011-07-13 22:51:29 -07001390 sp<InputWindowHandle> foregroundWindowHandle =
1391 mTempTouchState.getFirstForegroundWindowHandle();
Jeff Browncc4f7db2011-08-30 20:34:48 -07001392 const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid;
Kenny Root7a9db182011-06-02 15:16:05 -07001393 for (size_t i = 0; i < mTempTouchState.windows.size(); i++) {
1394 const TouchedWindow& touchedWindow = mTempTouchState.windows[i];
1395 if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) {
Jeff Brown9302c872011-07-13 22:51:29 -07001396 sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle;
Jeff Browncc4f7db2011-08-30 20:34:48 -07001397 if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) {
Jeff Brown9302c872011-07-13 22:51:29 -07001398 mTempTouchState.addOrUpdateWindow(inputWindowHandle,
Kenny Root7a9db182011-06-02 15:16:05 -07001399 InputTarget::FLAG_ZERO_COORDS, BitSet32(0));
1400 }
1401 }
1402 }
1403 }
1404
Jeff Brown01ce2e92010-09-26 22:20:12 -07001405 // Ensure all touched foreground windows are ready for new input.
1406 for (size_t i = 0; i < mTempTouchState.windows.size(); i++) {
1407 const TouchedWindow& touchedWindow = mTempTouchState.windows[i];
1408 if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) {
1409 // If the touched window is paused then keep waiting.
Jeff Browncc4f7db2011-08-30 20:34:48 -07001410 if (touchedWindow.windowHandle->getInfo()->paused) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001411 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown265f1cc2012-06-11 18:01:06 -07001412 NULL, touchedWindow.windowHandle, nextWakeupTime,
1413 "Waiting because the touched window is paused.");
Jeff Brown01ce2e92010-09-26 22:20:12 -07001414 goto Unresponsive;
1415 }
1416
1417 // If the touched window is still working on previous events then keep waiting.
Jeff Brown0952c302012-02-13 13:48:59 -08001418 if (!isWindowReadyForMoreInputLocked(currentTime, touchedWindow.windowHandle, entry)) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001419 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown265f1cc2012-06-11 18:01:06 -07001420 NULL, touchedWindow.windowHandle, nextWakeupTime,
1421 "Waiting because the touched window has not finished "
1422 "processing the input events that were previously delivered to it.");
Jeff Brown01ce2e92010-09-26 22:20:12 -07001423 goto Unresponsive;
1424 }
1425 }
1426 }
1427
1428 // If this is the first pointer going down and the touched window has a wallpaper
1429 // then also add the touched wallpaper windows so they are locked in for the duration
1430 // of the touch gesture.
Jeff Brown33bbfd22011-02-24 20:55:35 -08001431 // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper
1432 // engine only supports touch events. We would need to add a mechanism similar
1433 // to View.onGenericMotionEvent to enable wallpapers to handle these events.
1434 if (maskedAction == AMOTION_EVENT_ACTION_DOWN) {
Jeff Brown9302c872011-07-13 22:51:29 -07001435 sp<InputWindowHandle> foregroundWindowHandle =
1436 mTempTouchState.getFirstForegroundWindowHandle();
Jeff Browncc4f7db2011-08-30 20:34:48 -07001437 if (foregroundWindowHandle->getInfo()->hasWallpaper) {
Jeff Brown9302c872011-07-13 22:51:29 -07001438 for (size_t i = 0; i < mWindowHandles.size(); i++) {
1439 sp<InputWindowHandle> windowHandle = mWindowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001440 if (windowHandle->getInfo()->layoutParamsType
1441 == InputWindowInfo::TYPE_WALLPAPER) {
Jeff Brown9302c872011-07-13 22:51:29 -07001442 mTempTouchState.addOrUpdateWindow(windowHandle,
Jeff Browna032cc02011-03-07 16:56:21 -08001443 InputTarget::FLAG_WINDOW_IS_OBSCURED
1444 | InputTarget::FLAG_DISPATCH_AS_IS,
1445 BitSet32(0));
Jeff Brown01ce2e92010-09-26 22:20:12 -07001446 }
1447 }
1448 }
1449 }
1450
Jeff Brownb88102f2010-09-08 11:49:43 -07001451 // Success! Output targets.
1452 injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED;
Jeff Brownb88102f2010-09-08 11:49:43 -07001453
Jeff Brown01ce2e92010-09-26 22:20:12 -07001454 for (size_t i = 0; i < mTempTouchState.windows.size(); i++) {
1455 const TouchedWindow& touchedWindow = mTempTouchState.windows.itemAt(i);
Jeff Brown9302c872011-07-13 22:51:29 -07001456 addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags,
Jeff Browne9bb9be2012-02-06 15:47:55 -08001457 touchedWindow.pointerIds, inputTargets);
Jeff Brownb88102f2010-09-08 11:49:43 -07001458 }
1459
Jeff Browna032cc02011-03-07 16:56:21 -08001460 // Drop the outside or hover touch windows since we will not care about them
1461 // in the next iteration.
1462 mTempTouchState.filterNonAsIsTouchWindows();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001463
Jeff Brownb88102f2010-09-08 11:49:43 -07001464Failed:
1465 // Check injection permission once and for all.
1466 if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001467 if (checkInjectionPermission(NULL, entry->injectionState)) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001468 injectionPermission = INJECTION_PERMISSION_GRANTED;
1469 } else {
1470 injectionPermission = INJECTION_PERMISSION_DENIED;
1471 }
1472 }
1473
1474 // Update final pieces of touch state if the injector had permission.
1475 if (injectionPermission == INJECTION_PERMISSION_GRANTED) {
Jeff Brown95712852011-01-04 19:41:59 -08001476 if (!wrongDevice) {
Jeff Brown81346812011-06-28 20:08:48 -07001477 if (switchedDevice) {
1478#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001479 ALOGD("Conflicting pointer actions: Switched to a different device.");
Jeff Brown81346812011-06-28 20:08:48 -07001480#endif
1481 *outConflictingPointerActions = true;
1482 }
1483
1484 if (isHoverAction) {
1485 // Started hovering, therefore no longer down.
1486 if (mTouchState.down) {
1487#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001488 ALOGD("Conflicting pointer actions: Hover received while pointer was down.");
Jeff Brown81346812011-06-28 20:08:48 -07001489#endif
1490 *outConflictingPointerActions = true;
1491 }
1492 mTouchState.reset();
1493 if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER
1494 || maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) {
1495 mTouchState.deviceId = entry->deviceId;
1496 mTouchState.source = entry->source;
1497 }
1498 } else if (maskedAction == AMOTION_EVENT_ACTION_UP
1499 || maskedAction == AMOTION_EVENT_ACTION_CANCEL) {
Jeff Brown95712852011-01-04 19:41:59 -08001500 // All pointers up or canceled.
Jeff Brown33bbfd22011-02-24 20:55:35 -08001501 mTouchState.reset();
Jeff Brown95712852011-01-04 19:41:59 -08001502 } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) {
1503 // First pointer went down.
1504 if (mTouchState.down) {
Jeff Brownb6997262010-10-08 22:31:17 -07001505#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001506 ALOGD("Conflicting pointer actions: Down received while already down.");
Jeff Brownb6997262010-10-08 22:31:17 -07001507#endif
Jeff Brown81346812011-06-28 20:08:48 -07001508 *outConflictingPointerActions = true;
Jeff Brown95712852011-01-04 19:41:59 -08001509 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001510 mTouchState.copyFrom(mTempTouchState);
Jeff Brown95712852011-01-04 19:41:59 -08001511 } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) {
1512 // One pointer went up.
1513 if (isSplit) {
1514 int32_t pointerIndex = getMotionEventActionPointerIndex(action);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001515 uint32_t pointerId = entry->pointerProperties[pointerIndex].id;
Jeff Brownb88102f2010-09-08 11:49:43 -07001516
Jeff Brown95712852011-01-04 19:41:59 -08001517 for (size_t i = 0; i < mTempTouchState.windows.size(); ) {
1518 TouchedWindow& touchedWindow = mTempTouchState.windows.editItemAt(i);
1519 if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) {
1520 touchedWindow.pointerIds.clearBit(pointerId);
1521 if (touchedWindow.pointerIds.isEmpty()) {
1522 mTempTouchState.windows.removeAt(i);
1523 continue;
1524 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001525 }
Jeff Brown95712852011-01-04 19:41:59 -08001526 i += 1;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001527 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001528 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001529 mTouchState.copyFrom(mTempTouchState);
1530 } else if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) {
1531 // Discard temporary touch state since it was only valid for this action.
1532 } else {
1533 // Save changes to touch state as-is for all other actions.
1534 mTouchState.copyFrom(mTempTouchState);
Jeff Brownb88102f2010-09-08 11:49:43 -07001535 }
Jeff Browna032cc02011-03-07 16:56:21 -08001536
1537 // Update hover state.
Jeff Brown9302c872011-07-13 22:51:29 -07001538 mLastHoverWindowHandle = newHoverWindowHandle;
Jeff Brown95712852011-01-04 19:41:59 -08001539 }
Jeff Brownb88102f2010-09-08 11:49:43 -07001540 } else {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001541#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001542 ALOGD("Not updating touch focus because injection was denied.");
Jeff Brown01ce2e92010-09-26 22:20:12 -07001543#endif
Jeff Brownb88102f2010-09-08 11:49:43 -07001544 }
1545
1546Unresponsive:
Jeff Brown120a4592010-10-27 18:43:51 -07001547 // Reset temporary touch state to ensure we release unnecessary references to input channels.
1548 mTempTouchState.reset();
1549
Jeff Brown519e0242010-09-15 15:18:56 -07001550 nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime);
1551 updateDispatchStatisticsLocked(currentTime, entry,
1552 injectionResult, timeSpentWaitingForApplication);
Jeff Brownb88102f2010-09-08 11:49:43 -07001553#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001554 ALOGD("findTouchedWindow finished: injectionResult=%d, injectionPermission=%d, "
Jeff Brown01ce2e92010-09-26 22:20:12 -07001555 "timeSpentWaitingForApplication=%0.1fms",
Jeff Brown519e0242010-09-15 15:18:56 -07001556 injectionResult, injectionPermission, timeSpentWaitingForApplication / 1000000.0);
Jeff Brownb88102f2010-09-08 11:49:43 -07001557#endif
1558 return injectionResult;
1559}
1560
Jeff Brown9302c872011-07-13 22:51:29 -07001561void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle,
Jeff Browne9bb9be2012-02-06 15:47:55 -08001562 int32_t targetFlags, BitSet32 pointerIds, Vector<InputTarget>& inputTargets) {
1563 inputTargets.push();
Jeff Brownb88102f2010-09-08 11:49:43 -07001564
Jeff Browncc4f7db2011-08-30 20:34:48 -07001565 const InputWindowInfo* windowInfo = windowHandle->getInfo();
Jeff Browne9bb9be2012-02-06 15:47:55 -08001566 InputTarget& target = inputTargets.editTop();
Jeff Browncc4f7db2011-08-30 20:34:48 -07001567 target.inputChannel = windowInfo->inputChannel;
Jeff Brownb88102f2010-09-08 11:49:43 -07001568 target.flags = targetFlags;
Jeff Browncc4f7db2011-08-30 20:34:48 -07001569 target.xOffset = - windowInfo->frameLeft;
1570 target.yOffset = - windowInfo->frameTop;
1571 target.scaleFactor = windowInfo->scaleFactor;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001572 target.pointerIds = pointerIds;
Jeff Brownb88102f2010-09-08 11:49:43 -07001573}
1574
Jeff Browne9bb9be2012-02-06 15:47:55 -08001575void InputDispatcher::addMonitoringTargetsLocked(Vector<InputTarget>& inputTargets) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001576 for (size_t i = 0; i < mMonitoringChannels.size(); i++) {
Jeff Browne9bb9be2012-02-06 15:47:55 -08001577 inputTargets.push();
Jeff Brownb88102f2010-09-08 11:49:43 -07001578
Jeff Browne9bb9be2012-02-06 15:47:55 -08001579 InputTarget& target = inputTargets.editTop();
Jeff Brownb88102f2010-09-08 11:49:43 -07001580 target.inputChannel = mMonitoringChannels[i];
Jeff Brownb6110c22011-04-01 16:15:13 -07001581 target.flags = InputTarget::FLAG_DISPATCH_AS_IS;
Jeff Brownb88102f2010-09-08 11:49:43 -07001582 target.xOffset = 0;
1583 target.yOffset = 0;
Jeff Brownb6110c22011-04-01 16:15:13 -07001584 target.pointerIds.clear();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001585 target.scaleFactor = 1.0f;
Jeff Brownb88102f2010-09-08 11:49:43 -07001586 }
1587}
1588
Jeff Brown9302c872011-07-13 22:51:29 -07001589bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
Jeff Brown01ce2e92010-09-26 22:20:12 -07001590 const InjectionState* injectionState) {
1591 if (injectionState
Jeff Browncc4f7db2011-08-30 20:34:48 -07001592 && (windowHandle == NULL
1593 || windowHandle->getInfo()->ownerUid != injectionState->injectorUid)
Jeff Brownb6997262010-10-08 22:31:17 -07001594 && !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) {
Jeff Brown9302c872011-07-13 22:51:29 -07001595 if (windowHandle != NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00001596 ALOGW("Permission denied: injecting event from pid %d uid %d to window %s "
Jeff Brown9302c872011-07-13 22:51:29 -07001597 "owned by uid %d",
Jeff Brownb6997262010-10-08 22:31:17 -07001598 injectionState->injectorPid, injectionState->injectorUid,
Jeff Browncc4f7db2011-08-30 20:34:48 -07001599 windowHandle->getName().string(),
1600 windowHandle->getInfo()->ownerUid);
Jeff Brownb6997262010-10-08 22:31:17 -07001601 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00001602 ALOGW("Permission denied: injecting event from pid %d uid %d",
Jeff Brownb6997262010-10-08 22:31:17 -07001603 injectionState->injectorPid, injectionState->injectorUid);
Jeff Brownb88102f2010-09-08 11:49:43 -07001604 }
Jeff Brownb6997262010-10-08 22:31:17 -07001605 return false;
Jeff Brownb88102f2010-09-08 11:49:43 -07001606 }
1607 return true;
1608}
1609
Jeff Brown19dfc832010-10-05 12:26:23 -07001610bool InputDispatcher::isWindowObscuredAtPointLocked(
Jeff Brown9302c872011-07-13 22:51:29 -07001611 const sp<InputWindowHandle>& windowHandle, int32_t x, int32_t y) const {
1612 size_t numWindows = mWindowHandles.size();
Jeff Brownb88102f2010-09-08 11:49:43 -07001613 for (size_t i = 0; i < numWindows; i++) {
Jeff Brown9302c872011-07-13 22:51:29 -07001614 sp<InputWindowHandle> otherHandle = mWindowHandles.itemAt(i);
1615 if (otherHandle == windowHandle) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001616 break;
1617 }
Jeff Browncc4f7db2011-08-30 20:34:48 -07001618
1619 const InputWindowInfo* otherInfo = otherHandle->getInfo();
1620 if (otherInfo->visible && ! otherInfo->isTrustedOverlay()
1621 && otherInfo->frameContainsPoint(x, y)) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001622 return true;
1623 }
1624 }
1625 return false;
1626}
1627
Jeff Brownd1c48a02012-02-06 19:12:47 -08001628bool InputDispatcher::isWindowReadyForMoreInputLocked(nsecs_t currentTime,
Jeff Brown0952c302012-02-13 13:48:59 -08001629 const sp<InputWindowHandle>& windowHandle, const EventEntry* eventEntry) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001630 ssize_t connectionIndex = getConnectionIndexLocked(windowHandle->getInputChannel());
Jeff Brown519e0242010-09-15 15:18:56 -07001631 if (connectionIndex >= 0) {
Jeff Browncbee6d62012-02-03 20:11:27 -08001632 sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex);
Jeff Brownd1c48a02012-02-06 19:12:47 -08001633 if (connection->inputPublisherBlocked) {
1634 return false;
1635 }
Jeff Brown0952c302012-02-13 13:48:59 -08001636 if (eventEntry->type == EventEntry::TYPE_KEY) {
1637 // If the event is a key event, then we must wait for all previous events to
1638 // complete before delivering it because previous events may have the
1639 // side-effect of transferring focus to a different window and we want to
1640 // ensure that the following keys are sent to the new window.
1641 //
1642 // Suppose the user touches a button in a window then immediately presses "A".
1643 // If the button causes a pop-up window to appear then we want to ensure that
1644 // the "A" key is delivered to the new pop-up window. This is because users
1645 // often anticipate pending UI changes when typing on a keyboard.
1646 // To obtain this behavior, we must serialize key events with respect to all
1647 // prior input events.
Jeff Brownd1c48a02012-02-06 19:12:47 -08001648 return connection->outboundQueue.isEmpty()
1649 && connection->waitQueue.isEmpty();
1650 }
Jeff Brown0952c302012-02-13 13:48:59 -08001651 // Touch events can always be sent to a window immediately because the user intended
1652 // to touch whatever was visible at the time. Even if focus changes or a new
1653 // window appears moments later, the touch event was meant to be delivered to
1654 // whatever window happened to be on screen at the time.
1655 //
1656 // Generic motion events, such as trackball or joystick events are a little trickier.
1657 // Like key events, generic motion events are delivered to the focused window.
1658 // Unlike key events, generic motion events don't tend to transfer focus to other
1659 // windows and it is not important for them to be serialized. So we prefer to deliver
1660 // generic motion events as soon as possible to improve efficiency and reduce lag
1661 // through batching.
1662 //
1663 // The one case where we pause input event delivery is when the wait queue is piling
1664 // up with lots of events because the application is not responding.
1665 // This condition ensures that ANRs are detected reliably.
Jeff Brownd1c48a02012-02-06 19:12:47 -08001666 if (!connection->waitQueue.isEmpty()
1667 && currentTime >= connection->waitQueue.head->eventEntry->eventTime
1668 + STREAM_AHEAD_EVENT_TIMEOUT) {
1669 return false;
1670 }
Jeff Brown519e0242010-09-15 15:18:56 -07001671 }
Jeff Brownd1c48a02012-02-06 19:12:47 -08001672 return true;
Jeff Brown519e0242010-09-15 15:18:56 -07001673}
1674
Jeff Brown9302c872011-07-13 22:51:29 -07001675String8 InputDispatcher::getApplicationWindowLabelLocked(
1676 const sp<InputApplicationHandle>& applicationHandle,
1677 const sp<InputWindowHandle>& windowHandle) {
1678 if (applicationHandle != NULL) {
1679 if (windowHandle != NULL) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001680 String8 label(applicationHandle->getName());
Jeff Brown519e0242010-09-15 15:18:56 -07001681 label.append(" - ");
Jeff Browncc4f7db2011-08-30 20:34:48 -07001682 label.append(windowHandle->getName());
Jeff Brown519e0242010-09-15 15:18:56 -07001683 return label;
1684 } else {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001685 return applicationHandle->getName();
Jeff Brown519e0242010-09-15 15:18:56 -07001686 }
Jeff Brown9302c872011-07-13 22:51:29 -07001687 } else if (windowHandle != NULL) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001688 return windowHandle->getName();
Jeff Brown519e0242010-09-15 15:18:56 -07001689 } else {
1690 return String8("<unknown application or window>");
1691 }
1692}
1693
Jeff Browne2fe69e2010-10-18 13:21:23 -07001694void InputDispatcher::pokeUserActivityLocked(const EventEntry* eventEntry) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001695 int32_t eventType = POWER_MANAGER_OTHER_EVENT;
Jeff Brown4d396052010-10-29 21:50:21 -07001696 switch (eventEntry->type) {
1697 case EventEntry::TYPE_MOTION: {
Jeff Browne2fe69e2010-10-18 13:21:23 -07001698 const MotionEntry* motionEntry = static_cast<const MotionEntry*>(eventEntry);
Jeff Brown4d396052010-10-29 21:50:21 -07001699 if (motionEntry->action == AMOTION_EVENT_ACTION_CANCEL) {
1700 return;
1701 }
1702
Jeff Brown56194eb2011-03-02 19:23:13 -08001703 if (MotionEvent::isTouchEvent(motionEntry->source, motionEntry->action)) {
Joe Onorato1a542c72010-11-08 09:48:20 -08001704 eventType = POWER_MANAGER_TOUCH_EVENT;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001705 }
Jeff Brown4d396052010-10-29 21:50:21 -07001706 break;
1707 }
1708 case EventEntry::TYPE_KEY: {
1709 const KeyEntry* keyEntry = static_cast<const KeyEntry*>(eventEntry);
1710 if (keyEntry->flags & AKEY_EVENT_FLAG_CANCELED) {
1711 return;
1712 }
Jeff Brown56194eb2011-03-02 19:23:13 -08001713 eventType = POWER_MANAGER_BUTTON_EVENT;
Jeff Brown4d396052010-10-29 21:50:21 -07001714 break;
1715 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001716 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001717
Jeff Brownb88102f2010-09-08 11:49:43 -07001718 CommandEntry* commandEntry = postCommandLocked(
1719 & InputDispatcher::doPokeUserActivityLockedInterruptible);
Jeff Browne2fe69e2010-10-18 13:21:23 -07001720 commandEntry->eventTime = eventEntry->eventTime;
Jeff Brownb88102f2010-09-08 11:49:43 -07001721 commandEntry->userActivityEventType = eventType;
1722}
1723
Jeff Brown7fbdc842010-06-17 20:52:56 -07001724void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001725 const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001726#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001727 ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, "
Jeff Brown9cc695c2011-08-23 18:35:04 -07001728 "xOffset=%f, yOffset=%f, scaleFactor=%f, "
Jeff Brown3241b6b2012-02-03 15:08:02 -08001729 "pointerIds=0x%x",
Jeff Brown519e0242010-09-15 15:18:56 -07001730 connection->getInputChannelName(), inputTarget->flags,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001731 inputTarget->xOffset, inputTarget->yOffset,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001732 inputTarget->scaleFactor, inputTarget->pointerIds.value);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001733#endif
1734
1735 // Skip this event if the connection status is not normal.
Jeff Brown519e0242010-09-15 15:18:56 -07001736 // We don't want to enqueue additional outbound events if the connection is broken.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001737 if (connection->status != Connection::STATUS_NORMAL) {
Jeff Brownb6997262010-10-08 22:31:17 -07001738#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001739 ALOGD("channel '%s' ~ Dropping event because the channel status is %s",
Jeff Brownb88102f2010-09-08 11:49:43 -07001740 connection->getInputChannelName(), connection->getStatusLabel());
Jeff Brownb6997262010-10-08 22:31:17 -07001741#endif
Jeff Brown46b9ac02010-04-22 18:58:52 -07001742 return;
1743 }
1744
Jeff Brown01ce2e92010-09-26 22:20:12 -07001745 // Split a motion event if needed.
Jeff Brown3241b6b2012-02-03 15:08:02 -08001746 if (inputTarget->flags & InputTarget::FLAG_SPLIT) {
Steve Blockec193de2012-01-09 18:35:44 +00001747 ALOG_ASSERT(eventEntry->type == EventEntry::TYPE_MOTION);
Jeff Brown01ce2e92010-09-26 22:20:12 -07001748
1749 MotionEntry* originalMotionEntry = static_cast<MotionEntry*>(eventEntry);
1750 if (inputTarget->pointerIds.count() != originalMotionEntry->pointerCount) {
1751 MotionEntry* splitMotionEntry = splitMotionEvent(
1752 originalMotionEntry, inputTarget->pointerIds);
Jeff Brown58a2da82011-01-25 16:02:22 -08001753 if (!splitMotionEntry) {
1754 return; // split event was dropped
1755 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001756#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001757 ALOGD("channel '%s' ~ Split motion event.",
Jeff Brown01ce2e92010-09-26 22:20:12 -07001758 connection->getInputChannelName());
1759 logOutboundMotionDetailsLocked(" ", splitMotionEntry);
1760#endif
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001761 enqueueDispatchEntriesLocked(currentTime, connection,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001762 splitMotionEntry, inputTarget);
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001763 splitMotionEntry->release();
1764 return;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001765 }
1766 }
1767
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001768 // Not splitting. Enqueue dispatch entries for the event as is.
Jeff Brown3241b6b2012-02-03 15:08:02 -08001769 enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget);
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001770}
1771
1772void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001773 const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001774 bool wasEmpty = connection->outboundQueue.isEmpty();
1775
Jeff Browna032cc02011-03-07 16:56:21 -08001776 // Enqueue dispatch entries for the requested modes.
1777 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001778 InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT);
Jeff Browna032cc02011-03-07 16:56:21 -08001779 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001780 InputTarget::FLAG_DISPATCH_AS_OUTSIDE);
Jeff Browna032cc02011-03-07 16:56:21 -08001781 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001782 InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER);
Jeff Browna032cc02011-03-07 16:56:21 -08001783 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001784 InputTarget::FLAG_DISPATCH_AS_IS);
Jeff Brown98db5fa2011-06-08 15:37:10 -07001785 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001786 InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT);
Jeff Brown98db5fa2011-06-08 15:37:10 -07001787 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001788 InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER);
Jeff Browna032cc02011-03-07 16:56:21 -08001789
1790 // If the outbound queue was previously empty, start the dispatch cycle going.
Jeff Brownb6110c22011-04-01 16:15:13 -07001791 if (wasEmpty && !connection->outboundQueue.isEmpty()) {
Jeff Browna032cc02011-03-07 16:56:21 -08001792 startDispatchCycleLocked(currentTime, connection);
1793 }
1794}
1795
1796void InputDispatcher::enqueueDispatchEntryLocked(
1797 const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001798 int32_t dispatchMode) {
Jeff Browna032cc02011-03-07 16:56:21 -08001799 int32_t inputTargetFlags = inputTarget->flags;
1800 if (!(inputTargetFlags & dispatchMode)) {
1801 return;
1802 }
1803 inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode;
1804
Jeff Brown46b9ac02010-04-22 18:58:52 -07001805 // This is a new event.
1806 // Enqueue a new dispatch entry onto the outbound queue for this connection.
Jeff Brownac386072011-07-20 15:19:50 -07001807 DispatchEntry* dispatchEntry = new DispatchEntry(eventEntry, // increments ref
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -07001808 inputTargetFlags, inputTarget->xOffset, inputTarget->yOffset,
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001809 inputTarget->scaleFactor);
Jeff Brown6ec402b2010-07-28 15:48:59 -07001810
Jeff Brown81346812011-06-28 20:08:48 -07001811 // Apply target flags and update the connection's input state.
1812 switch (eventEntry->type) {
1813 case EventEntry::TYPE_KEY: {
1814 KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry);
1815 dispatchEntry->resolvedAction = keyEntry->action;
1816 dispatchEntry->resolvedFlags = keyEntry->flags;
1817
1818 if (!connection->inputState.trackKey(keyEntry,
1819 dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags)) {
1820#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001821 ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event",
Jeff Brown81346812011-06-28 20:08:48 -07001822 connection->getInputChannelName());
1823#endif
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001824 delete dispatchEntry;
Jeff Brown81346812011-06-28 20:08:48 -07001825 return; // skip the inconsistent event
1826 }
1827 break;
1828 }
1829
1830 case EventEntry::TYPE_MOTION: {
1831 MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry);
1832 if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) {
1833 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE;
1834 } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) {
1835 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT;
1836 } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) {
1837 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER;
1838 } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) {
1839 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL;
1840 } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) {
1841 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN;
1842 } else {
1843 dispatchEntry->resolvedAction = motionEntry->action;
1844 }
1845 if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE
1846 && !connection->inputState.isHovering(
1847 motionEntry->deviceId, motionEntry->source)) {
1848#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001849 ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter event",
Jeff Brown81346812011-06-28 20:08:48 -07001850 connection->getInputChannelName());
1851#endif
1852 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER;
1853 }
1854
1855 dispatchEntry->resolvedFlags = motionEntry->flags;
1856 if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) {
1857 dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
1858 }
1859
1860 if (!connection->inputState.trackMotion(motionEntry,
1861 dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags)) {
1862#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001863 ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion event",
Jeff Brown81346812011-06-28 20:08:48 -07001864 connection->getInputChannelName());
1865#endif
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001866 delete dispatchEntry;
Jeff Brown81346812011-06-28 20:08:48 -07001867 return; // skip the inconsistent event
1868 }
1869 break;
1870 }
1871 }
1872
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001873 // Remember that we are waiting for this dispatch to complete.
1874 if (dispatchEntry->hasForegroundTarget()) {
1875 incrementPendingForegroundDispatchesLocked(eventEntry);
1876 }
1877
Jeff Brown46b9ac02010-04-22 18:58:52 -07001878 // Enqueue the dispatch entry.
1879 connection->outboundQueue.enqueueAtTail(dispatchEntry);
Jeff Brown481c1572012-03-09 14:41:15 -08001880 traceOutboundQueueLengthLocked(connection);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001881}
1882
Jeff Brown7fbdc842010-06-17 20:52:56 -07001883void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime,
Jeff Brown519e0242010-09-15 15:18:56 -07001884 const sp<Connection>& connection) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001885#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001886 ALOGD("channel '%s' ~ startDispatchCycle",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001887 connection->getInputChannelName());
1888#endif
1889
Jeff Brownd1c48a02012-02-06 19:12:47 -08001890 while (connection->status == Connection::STATUS_NORMAL
1891 && !connection->outboundQueue.isEmpty()) {
1892 DispatchEntry* dispatchEntry = connection->outboundQueue.head;
Jeff Brown265f1cc2012-06-11 18:01:06 -07001893 dispatchEntry->deliveryTime = currentTime;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001894
Jeff Brownd1c48a02012-02-06 19:12:47 -08001895 // Publish the event.
1896 status_t status;
1897 EventEntry* eventEntry = dispatchEntry->eventEntry;
1898 switch (eventEntry->type) {
1899 case EventEntry::TYPE_KEY: {
1900 KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001901
Jeff Brownd1c48a02012-02-06 19:12:47 -08001902 // Publish the key event.
Jeff Brown072ec962012-02-07 14:46:57 -08001903 status = connection->inputPublisher.publishKeyEvent(dispatchEntry->seq,
Jeff Brownd1c48a02012-02-06 19:12:47 -08001904 keyEntry->deviceId, keyEntry->source,
1905 dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags,
1906 keyEntry->keyCode, keyEntry->scanCode,
1907 keyEntry->metaState, keyEntry->repeatCount, keyEntry->downTime,
1908 keyEntry->eventTime);
1909 break;
1910 }
Jeff Brownb88102f2010-09-08 11:49:43 -07001911
Jeff Brownd1c48a02012-02-06 19:12:47 -08001912 case EventEntry::TYPE_MOTION: {
1913 MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001914
Jeff Brownd1c48a02012-02-06 19:12:47 -08001915 PointerCoords scaledCoords[MAX_POINTERS];
1916 const PointerCoords* usingCoords = motionEntry->pointerCoords;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001917
Jeff Brownd1c48a02012-02-06 19:12:47 -08001918 // Set the X and Y offset depending on the input source.
1919 float xOffset, yOffset, scaleFactor;
1920 if ((motionEntry->source & AINPUT_SOURCE_CLASS_POINTER)
1921 && !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) {
1922 scaleFactor = dispatchEntry->scaleFactor;
1923 xOffset = dispatchEntry->xOffset * scaleFactor;
1924 yOffset = dispatchEntry->yOffset * scaleFactor;
1925 if (scaleFactor != 1.0f) {
1926 for (size_t i = 0; i < motionEntry->pointerCount; i++) {
1927 scaledCoords[i] = motionEntry->pointerCoords[i];
1928 scaledCoords[i].scale(scaleFactor);
1929 }
1930 usingCoords = scaledCoords;
1931 }
1932 } else {
1933 xOffset = 0.0f;
1934 yOffset = 0.0f;
1935 scaleFactor = 1.0f;
1936
1937 // We don't want the dispatch target to know.
1938 if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) {
1939 for (size_t i = 0; i < motionEntry->pointerCount; i++) {
1940 scaledCoords[i].clear();
1941 }
1942 usingCoords = scaledCoords;
1943 }
1944 }
1945
1946 // Publish the motion event.
Jeff Brown072ec962012-02-07 14:46:57 -08001947 status = connection->inputPublisher.publishMotionEvent(dispatchEntry->seq,
Jeff Brownd1c48a02012-02-06 19:12:47 -08001948 motionEntry->deviceId, motionEntry->source,
1949 dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags,
1950 motionEntry->edgeFlags, motionEntry->metaState, motionEntry->buttonState,
1951 xOffset, yOffset,
1952 motionEntry->xPrecision, motionEntry->yPrecision,
1953 motionEntry->downTime, motionEntry->eventTime,
1954 motionEntry->pointerCount, motionEntry->pointerProperties,
1955 usingCoords);
1956 break;
1957 }
1958
1959 default:
1960 ALOG_ASSERT(false);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001961 return;
1962 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001963
Jeff Brownd1c48a02012-02-06 19:12:47 -08001964 // Check the result.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001965 if (status) {
Jeff Brownd1c48a02012-02-06 19:12:47 -08001966 if (status == WOULD_BLOCK) {
1967 if (connection->waitQueue.isEmpty()) {
1968 ALOGE("channel '%s' ~ Could not publish event because the pipe is full. "
1969 "This is unexpected because the wait queue is empty, so the pipe "
1970 "should be empty and we shouldn't have any problems writing an "
1971 "event to it, status=%d", connection->getInputChannelName(), status);
1972 abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/);
1973 } else {
1974 // Pipe is full and we are waiting for the app to finish process some events
1975 // before sending more events to it.
1976#if DEBUG_DISPATCH_CYCLE
1977 ALOGD("channel '%s' ~ Could not publish event because the pipe is full, "
1978 "waiting for the application to catch up",
1979 connection->getInputChannelName());
1980#endif
1981 connection->inputPublisherBlocked = true;
1982 }
1983 } else {
1984 ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, "
1985 "status=%d", connection->getInputChannelName(), status);
1986 abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/);
1987 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001988 return;
1989 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001990
Jeff Brownd1c48a02012-02-06 19:12:47 -08001991 // Re-enqueue the event on the wait queue.
1992 connection->outboundQueue.dequeue(dispatchEntry);
Jeff Brown481c1572012-03-09 14:41:15 -08001993 traceOutboundQueueLengthLocked(connection);
Jeff Brownd1c48a02012-02-06 19:12:47 -08001994 connection->waitQueue.enqueueAtTail(dispatchEntry);
Jeff Brown481c1572012-03-09 14:41:15 -08001995 traceWaitQueueLengthLocked(connection);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001996 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001997}
1998
Jeff Brown7fbdc842010-06-17 20:52:56 -07001999void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime,
Jeff Brown072ec962012-02-07 14:46:57 -08002000 const sp<Connection>& connection, uint32_t seq, bool handled) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002001#if DEBUG_DISPATCH_CYCLE
Jeff Brown072ec962012-02-07 14:46:57 -08002002 ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s",
2003 connection->getInputChannelName(), seq, toString(handled));
Jeff Brown46b9ac02010-04-22 18:58:52 -07002004#endif
2005
Jeff Brownd1c48a02012-02-06 19:12:47 -08002006 connection->inputPublisherBlocked = false;
2007
Jeff Brown9c3cda02010-06-15 01:31:58 -07002008 if (connection->status == Connection::STATUS_BROKEN
2009 || connection->status == Connection::STATUS_ZOMBIE) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002010 return;
2011 }
2012
Jeff Brown3915bb82010-11-05 15:02:16 -07002013 // Notify other system components and prepare to start the next dispatch cycle.
Jeff Brown072ec962012-02-07 14:46:57 -08002014 onDispatchCycleFinishedLocked(currentTime, connection, seq, handled);
Jeff Brownb88102f2010-09-08 11:49:43 -07002015}
2016
Jeff Brownb6997262010-10-08 22:31:17 -07002017void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime,
Jeff Browncc4f7db2011-08-30 20:34:48 -07002018 const sp<Connection>& connection, bool notify) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002019#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00002020 ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s",
Jeff Browncc4f7db2011-08-30 20:34:48 -07002021 connection->getInputChannelName(), toString(notify));
Jeff Brown46b9ac02010-04-22 18:58:52 -07002022#endif
2023
Jeff Brownd1c48a02012-02-06 19:12:47 -08002024 // Clear the dispatch queues.
2025 drainDispatchQueueLocked(&connection->outboundQueue);
Jeff Brown481c1572012-03-09 14:41:15 -08002026 traceOutboundQueueLengthLocked(connection);
Jeff Brownd1c48a02012-02-06 19:12:47 -08002027 drainDispatchQueueLocked(&connection->waitQueue);
Jeff Brown481c1572012-03-09 14:41:15 -08002028 traceWaitQueueLengthLocked(connection);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002029
Jeff Brownb6997262010-10-08 22:31:17 -07002030 // The connection appears to be unrecoverably broken.
Jeff Brown9c3cda02010-06-15 01:31:58 -07002031 // Ignore already broken or zombie connections.
Jeff Brownb6997262010-10-08 22:31:17 -07002032 if (connection->status == Connection::STATUS_NORMAL) {
2033 connection->status = Connection::STATUS_BROKEN;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002034
Jeff Browncc4f7db2011-08-30 20:34:48 -07002035 if (notify) {
2036 // Notify other system components.
2037 onDispatchCycleBrokenLocked(currentTime, connection);
2038 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002039 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002040}
2041
Jeff Brownd1c48a02012-02-06 19:12:47 -08002042void InputDispatcher::drainDispatchQueueLocked(Queue<DispatchEntry>* queue) {
2043 while (!queue->isEmpty()) {
2044 DispatchEntry* dispatchEntry = queue->dequeueAtHead();
2045 releaseDispatchEntryLocked(dispatchEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07002046 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002047}
2048
Jeff Brownd1c48a02012-02-06 19:12:47 -08002049void InputDispatcher::releaseDispatchEntryLocked(DispatchEntry* dispatchEntry) {
2050 if (dispatchEntry->hasForegroundTarget()) {
2051 decrementPendingForegroundDispatchesLocked(dispatchEntry->eventEntry);
2052 }
2053 delete dispatchEntry;
2054}
2055
Jeff Browncbee6d62012-02-03 20:11:27 -08002056int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002057 InputDispatcher* d = static_cast<InputDispatcher*>(data);
2058
2059 { // acquire lock
2060 AutoMutex _l(d->mLock);
2061
Jeff Browncbee6d62012-02-03 20:11:27 -08002062 ssize_t connectionIndex = d->mConnectionsByFd.indexOfKey(fd);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002063 if (connectionIndex < 0) {
Steve Block3762c312012-01-06 19:20:56 +00002064 ALOGE("Received spurious receive callback for unknown input channel. "
Jeff Browncbee6d62012-02-03 20:11:27 -08002065 "fd=%d, events=0x%x", fd, events);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002066 return 0; // remove the callback
Jeff Brown46b9ac02010-04-22 18:58:52 -07002067 }
2068
Jeff Browncc4f7db2011-08-30 20:34:48 -07002069 bool notify;
Jeff Browncbee6d62012-02-03 20:11:27 -08002070 sp<Connection> connection = d->mConnectionsByFd.valueAt(connectionIndex);
Jeff Browncc4f7db2011-08-30 20:34:48 -07002071 if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) {
2072 if (!(events & ALOOPER_EVENT_INPUT)) {
Steve Block8564c8d2012-01-05 23:22:43 +00002073 ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. "
Jeff Browncc4f7db2011-08-30 20:34:48 -07002074 "events=0x%x", connection->getInputChannelName(), events);
2075 return 1;
2076 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002077
Jeff Brown1adee112012-02-07 10:25:41 -08002078 nsecs_t currentTime = now();
2079 bool gotOne = false;
2080 status_t status;
2081 for (;;) {
Jeff Brown072ec962012-02-07 14:46:57 -08002082 uint32_t seq;
2083 bool handled;
2084 status = connection->inputPublisher.receiveFinishedSignal(&seq, &handled);
Jeff Brown1adee112012-02-07 10:25:41 -08002085 if (status) {
2086 break;
2087 }
Jeff Brown072ec962012-02-07 14:46:57 -08002088 d->finishDispatchCycleLocked(currentTime, connection, seq, handled);
Jeff Brown1adee112012-02-07 10:25:41 -08002089 gotOne = true;
2090 }
2091 if (gotOne) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07002092 d->runCommandsLockedInterruptible();
Jeff Brown1adee112012-02-07 10:25:41 -08002093 if (status == WOULD_BLOCK) {
2094 return 1;
2095 }
Jeff Browncc4f7db2011-08-30 20:34:48 -07002096 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002097
Jeff Brown1adee112012-02-07 10:25:41 -08002098 notify = status != DEAD_OBJECT || !connection->monitor;
2099 if (notify) {
2100 ALOGE("channel '%s' ~ Failed to receive finished signal. status=%d",
2101 connection->getInputChannelName(), status);
2102 }
Jeff Browncc4f7db2011-08-30 20:34:48 -07002103 } else {
2104 // Monitor channels are never explicitly unregistered.
2105 // We do it automatically when the remote endpoint is closed so don't warn
2106 // about them.
2107 notify = !connection->monitor;
2108 if (notify) {
Steve Block8564c8d2012-01-05 23:22:43 +00002109 ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. "
Jeff Browncc4f7db2011-08-30 20:34:48 -07002110 "events=0x%x", connection->getInputChannelName(), events);
2111 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002112 }
2113
Jeff Browncc4f7db2011-08-30 20:34:48 -07002114 // Unregister the channel.
2115 d->unregisterInputChannelLocked(connection->inputChannel, notify);
2116 return 0; // remove the callback
Jeff Brown46b9ac02010-04-22 18:58:52 -07002117 } // release lock
2118}
2119
Jeff Brownb6997262010-10-08 22:31:17 -07002120void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked(
Jeff Brownda3d5a92011-03-29 15:11:34 -07002121 const CancelationOptions& options) {
Jeff Browncbee6d62012-02-03 20:11:27 -08002122 for (size_t i = 0; i < mConnectionsByFd.size(); i++) {
Jeff Brownb6997262010-10-08 22:31:17 -07002123 synthesizeCancelationEventsForConnectionLocked(
Jeff Browncbee6d62012-02-03 20:11:27 -08002124 mConnectionsByFd.valueAt(i), options);
Jeff Brownb6997262010-10-08 22:31:17 -07002125 }
2126}
2127
2128void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked(
Jeff Brownda3d5a92011-03-29 15:11:34 -07002129 const sp<InputChannel>& channel, const CancelationOptions& options) {
Jeff Brownb6997262010-10-08 22:31:17 -07002130 ssize_t index = getConnectionIndexLocked(channel);
2131 if (index >= 0) {
2132 synthesizeCancelationEventsForConnectionLocked(
Jeff Browncbee6d62012-02-03 20:11:27 -08002133 mConnectionsByFd.valueAt(index), options);
Jeff Brownb6997262010-10-08 22:31:17 -07002134 }
2135}
2136
2137void InputDispatcher::synthesizeCancelationEventsForConnectionLocked(
Jeff Brownda3d5a92011-03-29 15:11:34 -07002138 const sp<Connection>& connection, const CancelationOptions& options) {
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08002139 if (connection->status == Connection::STATUS_BROKEN) {
2140 return;
2141 }
2142
Jeff Brownb6997262010-10-08 22:31:17 -07002143 nsecs_t currentTime = now();
2144
Jeff Brown8b4be5602012-02-06 16:31:05 -08002145 Vector<EventEntry*> cancelationEvents;
Jeff Brownac386072011-07-20 15:19:50 -07002146 connection->inputState.synthesizeCancelationEvents(currentTime,
Jeff Brown8b4be5602012-02-06 16:31:05 -08002147 cancelationEvents, options);
Jeff Brownb6997262010-10-08 22:31:17 -07002148
Jeff Brown8b4be5602012-02-06 16:31:05 -08002149 if (!cancelationEvents.isEmpty()) {
Jeff Brownb6997262010-10-08 22:31:17 -07002150#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002151 ALOGD("channel '%s' ~ Synthesized %d cancelation events to bring channel back in sync "
Jeff Brownda3d5a92011-03-29 15:11:34 -07002152 "with reality: %s, mode=%d.",
Jeff Brown8b4be5602012-02-06 16:31:05 -08002153 connection->getInputChannelName(), cancelationEvents.size(),
Jeff Brownda3d5a92011-03-29 15:11:34 -07002154 options.reason, options.mode);
Jeff Brownb6997262010-10-08 22:31:17 -07002155#endif
Jeff Brown8b4be5602012-02-06 16:31:05 -08002156 for (size_t i = 0; i < cancelationEvents.size(); i++) {
2157 EventEntry* cancelationEventEntry = cancelationEvents.itemAt(i);
Jeff Brownb6997262010-10-08 22:31:17 -07002158 switch (cancelationEventEntry->type) {
2159 case EventEntry::TYPE_KEY:
2160 logOutboundKeyDetailsLocked("cancel - ",
2161 static_cast<KeyEntry*>(cancelationEventEntry));
2162 break;
2163 case EventEntry::TYPE_MOTION:
2164 logOutboundMotionDetailsLocked("cancel - ",
2165 static_cast<MotionEntry*>(cancelationEventEntry));
2166 break;
2167 }
2168
Jeff Brown81346812011-06-28 20:08:48 -07002169 InputTarget target;
Jeff Brown9302c872011-07-13 22:51:29 -07002170 sp<InputWindowHandle> windowHandle = getWindowHandleLocked(connection->inputChannel);
2171 if (windowHandle != NULL) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07002172 const InputWindowInfo* windowInfo = windowHandle->getInfo();
2173 target.xOffset = -windowInfo->frameLeft;
2174 target.yOffset = -windowInfo->frameTop;
2175 target.scaleFactor = windowInfo->scaleFactor;
Jeff Brownb6997262010-10-08 22:31:17 -07002176 } else {
Jeff Brown81346812011-06-28 20:08:48 -07002177 target.xOffset = 0;
2178 target.yOffset = 0;
2179 target.scaleFactor = 1.0f;
Jeff Brownb6997262010-10-08 22:31:17 -07002180 }
Jeff Brown81346812011-06-28 20:08:48 -07002181 target.inputChannel = connection->inputChannel;
2182 target.flags = InputTarget::FLAG_DISPATCH_AS_IS;
Jeff Brownb6997262010-10-08 22:31:17 -07002183
Jeff Brown81346812011-06-28 20:08:48 -07002184 enqueueDispatchEntryLocked(connection, cancelationEventEntry, // increments ref
Jeff Brown3241b6b2012-02-03 15:08:02 -08002185 &target, InputTarget::FLAG_DISPATCH_AS_IS);
Jeff Brownb6997262010-10-08 22:31:17 -07002186
Jeff Brownac386072011-07-20 15:19:50 -07002187 cancelationEventEntry->release();
Jeff Brownb6997262010-10-08 22:31:17 -07002188 }
2189
Jeff Brownd1c48a02012-02-06 19:12:47 -08002190 startDispatchCycleLocked(currentTime, connection);
Jeff Brownb6997262010-10-08 22:31:17 -07002191 }
2192}
2193
Jeff Brown01ce2e92010-09-26 22:20:12 -07002194InputDispatcher::MotionEntry*
2195InputDispatcher::splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds) {
Steve Blockec193de2012-01-09 18:35:44 +00002196 ALOG_ASSERT(pointerIds.value != 0);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002197
2198 uint32_t splitPointerIndexMap[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002199 PointerProperties splitPointerProperties[MAX_POINTERS];
Jeff Brown01ce2e92010-09-26 22:20:12 -07002200 PointerCoords splitPointerCoords[MAX_POINTERS];
2201
2202 uint32_t originalPointerCount = originalMotionEntry->pointerCount;
2203 uint32_t splitPointerCount = 0;
2204
2205 for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount;
2206 originalPointerIndex++) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002207 const PointerProperties& pointerProperties =
2208 originalMotionEntry->pointerProperties[originalPointerIndex];
2209 uint32_t pointerId = uint32_t(pointerProperties.id);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002210 if (pointerIds.hasBit(pointerId)) {
2211 splitPointerIndexMap[splitPointerCount] = originalPointerIndex;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002212 splitPointerProperties[splitPointerCount].copyFrom(pointerProperties);
Jeff Brownace13b12011-03-09 17:39:48 -08002213 splitPointerCoords[splitPointerCount].copyFrom(
Jeff Brown3241b6b2012-02-03 15:08:02 -08002214 originalMotionEntry->pointerCoords[originalPointerIndex]);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002215 splitPointerCount += 1;
2216 }
2217 }
Jeff Brown58a2da82011-01-25 16:02:22 -08002218
2219 if (splitPointerCount != pointerIds.count()) {
2220 // This is bad. We are missing some of the pointers that we expected to deliver.
2221 // Most likely this indicates that we received an ACTION_MOVE events that has
2222 // different pointer ids than we expected based on the previous ACTION_DOWN
2223 // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers
2224 // in this way.
Steve Block8564c8d2012-01-05 23:22:43 +00002225 ALOGW("Dropping split motion event because the pointer count is %d but "
Jeff Brown58a2da82011-01-25 16:02:22 -08002226 "we expected there to be %d pointers. This probably means we received "
2227 "a broken sequence of pointer ids from the input device.",
2228 splitPointerCount, pointerIds.count());
2229 return NULL;
2230 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07002231
2232 int32_t action = originalMotionEntry->action;
2233 int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK;
2234 if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN
2235 || maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) {
2236 int32_t originalPointerIndex = getMotionEventActionPointerIndex(action);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002237 const PointerProperties& pointerProperties =
2238 originalMotionEntry->pointerProperties[originalPointerIndex];
2239 uint32_t pointerId = uint32_t(pointerProperties.id);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002240 if (pointerIds.hasBit(pointerId)) {
2241 if (pointerIds.count() == 1) {
2242 // The first/last pointer went down/up.
2243 action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN
2244 ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
Jeff Brown9a01d052010-09-27 16:35:11 -07002245 } else {
2246 // A secondary pointer went down/up.
2247 uint32_t splitPointerIndex = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002248 while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) {
Jeff Brown9a01d052010-09-27 16:35:11 -07002249 splitPointerIndex += 1;
2250 }
2251 action = maskedAction | (splitPointerIndex
2252 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002253 }
2254 } else {
2255 // An unrelated pointer changed.
2256 action = AMOTION_EVENT_ACTION_MOVE;
2257 }
2258 }
2259
Jeff Brownac386072011-07-20 15:19:50 -07002260 MotionEntry* splitMotionEntry = new MotionEntry(
Jeff Brown01ce2e92010-09-26 22:20:12 -07002261 originalMotionEntry->eventTime,
2262 originalMotionEntry->deviceId,
2263 originalMotionEntry->source,
2264 originalMotionEntry->policyFlags,
2265 action,
2266 originalMotionEntry->flags,
2267 originalMotionEntry->metaState,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002268 originalMotionEntry->buttonState,
Jeff Brown01ce2e92010-09-26 22:20:12 -07002269 originalMotionEntry->edgeFlags,
2270 originalMotionEntry->xPrecision,
2271 originalMotionEntry->yPrecision,
2272 originalMotionEntry->downTime,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002273 splitPointerCount, splitPointerProperties, splitPointerCoords);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002274
Jeff Browna032cc02011-03-07 16:56:21 -08002275 if (originalMotionEntry->injectionState) {
2276 splitMotionEntry->injectionState = originalMotionEntry->injectionState;
2277 splitMotionEntry->injectionState->refCount += 1;
2278 }
2279
Jeff Brown01ce2e92010-09-26 22:20:12 -07002280 return splitMotionEntry;
2281}
2282
Jeff Brownbe1aa822011-07-27 16:04:54 -07002283void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002284#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002285 ALOGD("notifyConfigurationChanged - eventTime=%lld", args->eventTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002286#endif
2287
Jeff Brownb88102f2010-09-08 11:49:43 -07002288 bool needWake;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002289 { // acquire lock
2290 AutoMutex _l(mLock);
2291
Jeff Brownbe1aa822011-07-27 16:04:54 -07002292 ConfigurationChangedEntry* newEntry = new ConfigurationChangedEntry(args->eventTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07002293 needWake = enqueueInboundEventLocked(newEntry);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002294 } // release lock
2295
Jeff Brownb88102f2010-09-08 11:49:43 -07002296 if (needWake) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002297 mLooper->wake();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002298 }
2299}
2300
Jeff Brownbe1aa822011-07-27 16:04:54 -07002301void InputDispatcher::notifyKey(const NotifyKeyArgs* args) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002302#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002303 ALOGD("notifyKey - eventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, action=0x%x, "
Jeff Brown46b9ac02010-04-22 18:58:52 -07002304 "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07002305 args->eventTime, args->deviceId, args->source, args->policyFlags,
2306 args->action, args->flags, args->keyCode, args->scanCode,
2307 args->metaState, args->downTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002308#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07002309 if (!validateKeyEvent(args->action)) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002310 return;
2311 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002312
Jeff Brownbe1aa822011-07-27 16:04:54 -07002313 uint32_t policyFlags = args->policyFlags;
2314 int32_t flags = args->flags;
2315 int32_t metaState = args->metaState;
Jeff Brown1f245102010-11-18 20:53:46 -08002316 if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) {
2317 policyFlags |= POLICY_FLAG_VIRTUAL;
2318 flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2319 }
Jeff Brown924c4d42011-03-07 16:40:47 -08002320 if (policyFlags & POLICY_FLAG_ALT) {
2321 metaState |= AMETA_ALT_ON | AMETA_ALT_LEFT_ON;
2322 }
2323 if (policyFlags & POLICY_FLAG_ALT_GR) {
2324 metaState |= AMETA_ALT_ON | AMETA_ALT_RIGHT_ON;
2325 }
2326 if (policyFlags & POLICY_FLAG_SHIFT) {
2327 metaState |= AMETA_SHIFT_ON | AMETA_SHIFT_LEFT_ON;
2328 }
2329 if (policyFlags & POLICY_FLAG_CAPS_LOCK) {
2330 metaState |= AMETA_CAPS_LOCK_ON;
2331 }
2332 if (policyFlags & POLICY_FLAG_FUNCTION) {
2333 metaState |= AMETA_FUNCTION_ON;
2334 }
Jeff Brown1f245102010-11-18 20:53:46 -08002335
Jeff Browne20c9e02010-10-11 14:20:19 -07002336 policyFlags |= POLICY_FLAG_TRUSTED;
Jeff Brown1f245102010-11-18 20:53:46 -08002337
2338 KeyEvent event;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002339 event.initialize(args->deviceId, args->source, args->action,
2340 flags, args->keyCode, args->scanCode, metaState, 0,
2341 args->downTime, args->eventTime);
Jeff Brown1f245102010-11-18 20:53:46 -08002342
2343 mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags);
2344
2345 if (policyFlags & POLICY_FLAG_WOKE_HERE) {
2346 flags |= AKEY_EVENT_FLAG_WOKE_HERE;
2347 }
Jeff Brownb6997262010-10-08 22:31:17 -07002348
Jeff Brownb88102f2010-09-08 11:49:43 -07002349 bool needWake;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002350 { // acquire lock
Jeff Brown0029c662011-03-30 02:25:18 -07002351 mLock.lock();
2352
2353 if (mInputFilterEnabled) {
2354 mLock.unlock();
2355
2356 policyFlags |= POLICY_FLAG_FILTERED;
2357 if (!mPolicy->filterInputEvent(&event, policyFlags)) {
2358 return; // event was consumed by the filter
2359 }
2360
2361 mLock.lock();
2362 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002363
Jeff Brown7fbdc842010-06-17 20:52:56 -07002364 int32_t repeatCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002365 KeyEntry* newEntry = new KeyEntry(args->eventTime,
2366 args->deviceId, args->source, policyFlags,
2367 args->action, flags, args->keyCode, args->scanCode,
2368 metaState, repeatCount, args->downTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002369
Jeff Brownb88102f2010-09-08 11:49:43 -07002370 needWake = enqueueInboundEventLocked(newEntry);
Jeff Brown0029c662011-03-30 02:25:18 -07002371 mLock.unlock();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002372 } // release lock
2373
Jeff Brownb88102f2010-09-08 11:49:43 -07002374 if (needWake) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002375 mLooper->wake();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002376 }
2377}
2378
Jeff Brownbe1aa822011-07-27 16:04:54 -07002379void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002380#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002381 ALOGD("notifyMotion - eventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002382 "action=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, edgeFlags=0x%x, "
Jeff Brown85a31762010-09-01 17:01:00 -07002383 "xPrecision=%f, yPrecision=%f, downTime=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07002384 args->eventTime, args->deviceId, args->source, args->policyFlags,
2385 args->action, args->flags, args->metaState, args->buttonState,
2386 args->edgeFlags, args->xPrecision, args->yPrecision, args->downTime);
2387 for (uint32_t i = 0; i < args->pointerCount; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00002388 ALOGD(" Pointer %d: id=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002389 "x=%f, y=%f, pressure=%f, size=%f, "
Jeff Brown85a31762010-09-01 17:01:00 -07002390 "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, "
Jeff Brown8d608662010-08-30 03:02:23 -07002391 "orientation=%f",
Jeff Brownbe1aa822011-07-27 16:04:54 -07002392 i, args->pointerProperties[i].id,
2393 args->pointerProperties[i].toolType,
2394 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
2395 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
2396 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2397 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
2398 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2399 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2400 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2401 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2402 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
Jeff Brown46b9ac02010-04-22 18:58:52 -07002403 }
2404#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07002405 if (!validateMotionEvent(args->action, args->pointerCount, args->pointerProperties)) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002406 return;
2407 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002408
Jeff Brownbe1aa822011-07-27 16:04:54 -07002409 uint32_t policyFlags = args->policyFlags;
Jeff Browne20c9e02010-10-11 14:20:19 -07002410 policyFlags |= POLICY_FLAG_TRUSTED;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002411 mPolicy->interceptMotionBeforeQueueing(args->eventTime, /*byref*/ policyFlags);
Jeff Brownb6997262010-10-08 22:31:17 -07002412
Jeff Brownb88102f2010-09-08 11:49:43 -07002413 bool needWake;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002414 { // acquire lock
Jeff Brown0029c662011-03-30 02:25:18 -07002415 mLock.lock();
2416
2417 if (mInputFilterEnabled) {
2418 mLock.unlock();
2419
2420 MotionEvent event;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002421 event.initialize(args->deviceId, args->source, args->action, args->flags,
2422 args->edgeFlags, args->metaState, args->buttonState, 0, 0,
2423 args->xPrecision, args->yPrecision,
2424 args->downTime, args->eventTime,
2425 args->pointerCount, args->pointerProperties, args->pointerCoords);
Jeff Brown0029c662011-03-30 02:25:18 -07002426
2427 policyFlags |= POLICY_FLAG_FILTERED;
2428 if (!mPolicy->filterInputEvent(&event, policyFlags)) {
2429 return; // event was consumed by the filter
2430 }
2431
2432 mLock.lock();
2433 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002434
Jeff Brown46b9ac02010-04-22 18:58:52 -07002435 // Just enqueue a new motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002436 MotionEntry* newEntry = new MotionEntry(args->eventTime,
2437 args->deviceId, args->source, policyFlags,
2438 args->action, args->flags, args->metaState, args->buttonState,
2439 args->edgeFlags, args->xPrecision, args->yPrecision, args->downTime,
2440 args->pointerCount, args->pointerProperties, args->pointerCoords);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002441
Jeff Brownb88102f2010-09-08 11:49:43 -07002442 needWake = enqueueInboundEventLocked(newEntry);
Jeff Brown0029c662011-03-30 02:25:18 -07002443 mLock.unlock();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002444 } // release lock
2445
Jeff Brownb88102f2010-09-08 11:49:43 -07002446 if (needWake) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002447 mLooper->wake();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002448 }
2449}
2450
Jeff Brownbe1aa822011-07-27 16:04:54 -07002451void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) {
Jeff Brownb6997262010-10-08 22:31:17 -07002452#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002453 ALOGD("notifySwitch - eventTime=%lld, policyFlags=0x%x, switchCode=%d, switchValue=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07002454 args->eventTime, args->policyFlags,
2455 args->switchCode, args->switchValue);
Jeff Brownb6997262010-10-08 22:31:17 -07002456#endif
2457
Jeff Brownbe1aa822011-07-27 16:04:54 -07002458 uint32_t policyFlags = args->policyFlags;
Jeff Browne20c9e02010-10-11 14:20:19 -07002459 policyFlags |= POLICY_FLAG_TRUSTED;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002460 mPolicy->notifySwitch(args->eventTime,
2461 args->switchCode, args->switchValue, policyFlags);
Jeff Brownb6997262010-10-08 22:31:17 -07002462}
2463
Jeff Brown65fd2512011-08-18 11:20:58 -07002464void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) {
2465#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002466 ALOGD("notifyDeviceReset - eventTime=%lld, deviceId=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07002467 args->eventTime, args->deviceId);
2468#endif
2469
2470 bool needWake;
2471 { // acquire lock
2472 AutoMutex _l(mLock);
2473
2474 DeviceResetEntry* newEntry = new DeviceResetEntry(args->eventTime, args->deviceId);
2475 needWake = enqueueInboundEventLocked(newEntry);
2476 } // release lock
2477
2478 if (needWake) {
2479 mLooper->wake();
2480 }
2481}
2482
Jeff Brown7fbdc842010-06-17 20:52:56 -07002483int32_t InputDispatcher::injectInputEvent(const InputEvent* event,
Jeff Brown0029c662011-03-30 02:25:18 -07002484 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
2485 uint32_t policyFlags) {
Jeff Brown7fbdc842010-06-17 20:52:56 -07002486#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002487 ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, "
Jeff Brown0029c662011-03-30 02:25:18 -07002488 "syncMode=%d, timeoutMillis=%d, policyFlags=0x%08x",
2489 event->getType(), injectorPid, injectorUid, syncMode, timeoutMillis, policyFlags);
Jeff Brown7fbdc842010-06-17 20:52:56 -07002490#endif
2491
2492 nsecs_t endTime = now() + milliseconds_to_nanoseconds(timeoutMillis);
Jeff Browne20c9e02010-10-11 14:20:19 -07002493
Jeff Brown0029c662011-03-30 02:25:18 -07002494 policyFlags |= POLICY_FLAG_INJECTED;
Jeff Browne20c9e02010-10-11 14:20:19 -07002495 if (hasInjectionPermission(injectorPid, injectorUid)) {
2496 policyFlags |= POLICY_FLAG_TRUSTED;
2497 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07002498
Jeff Brown3241b6b2012-02-03 15:08:02 -08002499 EventEntry* firstInjectedEntry;
2500 EventEntry* lastInjectedEntry;
Jeff Brownb6997262010-10-08 22:31:17 -07002501 switch (event->getType()) {
2502 case AINPUT_EVENT_TYPE_KEY: {
2503 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(event);
2504 int32_t action = keyEvent->getAction();
2505 if (! validateKeyEvent(action)) {
Jeff Brownb88102f2010-09-08 11:49:43 -07002506 return INPUT_EVENT_INJECTION_FAILED;
2507 }
2508
Jeff Brownb6997262010-10-08 22:31:17 -07002509 int32_t flags = keyEvent->getFlags();
Jeff Brown1f245102010-11-18 20:53:46 -08002510 if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) {
2511 policyFlags |= POLICY_FLAG_VIRTUAL;
2512 }
2513
Jeff Brown0029c662011-03-30 02:25:18 -07002514 if (!(policyFlags & POLICY_FLAG_FILTERED)) {
2515 mPolicy->interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags);
2516 }
Jeff Brown1f245102010-11-18 20:53:46 -08002517
2518 if (policyFlags & POLICY_FLAG_WOKE_HERE) {
2519 flags |= AKEY_EVENT_FLAG_WOKE_HERE;
2520 }
Jeff Brown6ec402b2010-07-28 15:48:59 -07002521
Jeff Brownb6997262010-10-08 22:31:17 -07002522 mLock.lock();
Jeff Brown3241b6b2012-02-03 15:08:02 -08002523 firstInjectedEntry = new KeyEntry(keyEvent->getEventTime(),
Jeff Brown1f245102010-11-18 20:53:46 -08002524 keyEvent->getDeviceId(), keyEvent->getSource(),
2525 policyFlags, action, flags,
2526 keyEvent->getKeyCode(), keyEvent->getScanCode(), keyEvent->getMetaState(),
Jeff Brownb6997262010-10-08 22:31:17 -07002527 keyEvent->getRepeatCount(), keyEvent->getDownTime());
Jeff Brown3241b6b2012-02-03 15:08:02 -08002528 lastInjectedEntry = firstInjectedEntry;
Jeff Brownb6997262010-10-08 22:31:17 -07002529 break;
2530 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07002531
Jeff Brownb6997262010-10-08 22:31:17 -07002532 case AINPUT_EVENT_TYPE_MOTION: {
2533 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event);
2534 int32_t action = motionEvent->getAction();
2535 size_t pointerCount = motionEvent->getPointerCount();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002536 const PointerProperties* pointerProperties = motionEvent->getPointerProperties();
2537 if (! validateMotionEvent(action, pointerCount, pointerProperties)) {
Jeff Brownb6997262010-10-08 22:31:17 -07002538 return INPUT_EVENT_INJECTION_FAILED;
2539 }
2540
Jeff Brown0029c662011-03-30 02:25:18 -07002541 if (!(policyFlags & POLICY_FLAG_FILTERED)) {
2542 nsecs_t eventTime = motionEvent->getEventTime();
2543 mPolicy->interceptMotionBeforeQueueing(eventTime, /*byref*/ policyFlags);
2544 }
Jeff Brownb6997262010-10-08 22:31:17 -07002545
2546 mLock.lock();
2547 const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes();
2548 const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords();
Jeff Brown3241b6b2012-02-03 15:08:02 -08002549 firstInjectedEntry = new MotionEntry(*sampleEventTimes,
Jeff Brownb6997262010-10-08 22:31:17 -07002550 motionEvent->getDeviceId(), motionEvent->getSource(), policyFlags,
2551 action, motionEvent->getFlags(),
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002552 motionEvent->getMetaState(), motionEvent->getButtonState(),
2553 motionEvent->getEdgeFlags(),
Jeff Brownb6997262010-10-08 22:31:17 -07002554 motionEvent->getXPrecision(), motionEvent->getYPrecision(),
2555 motionEvent->getDownTime(), uint32_t(pointerCount),
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002556 pointerProperties, samplePointerCoords);
Jeff Brown3241b6b2012-02-03 15:08:02 -08002557 lastInjectedEntry = firstInjectedEntry;
Jeff Brownb6997262010-10-08 22:31:17 -07002558 for (size_t i = motionEvent->getHistorySize(); i > 0; i--) {
2559 sampleEventTimes += 1;
2560 samplePointerCoords += pointerCount;
Jeff Brown3241b6b2012-02-03 15:08:02 -08002561 MotionEntry* nextInjectedEntry = new MotionEntry(*sampleEventTimes,
2562 motionEvent->getDeviceId(), motionEvent->getSource(), policyFlags,
2563 action, motionEvent->getFlags(),
2564 motionEvent->getMetaState(), motionEvent->getButtonState(),
2565 motionEvent->getEdgeFlags(),
2566 motionEvent->getXPrecision(), motionEvent->getYPrecision(),
2567 motionEvent->getDownTime(), uint32_t(pointerCount),
2568 pointerProperties, samplePointerCoords);
2569 lastInjectedEntry->next = nextInjectedEntry;
2570 lastInjectedEntry = nextInjectedEntry;
Jeff Brownb6997262010-10-08 22:31:17 -07002571 }
Jeff Brownb6997262010-10-08 22:31:17 -07002572 break;
2573 }
2574
2575 default:
Steve Block8564c8d2012-01-05 23:22:43 +00002576 ALOGW("Cannot inject event of type %d", event->getType());
Jeff Brownb6997262010-10-08 22:31:17 -07002577 return INPUT_EVENT_INJECTION_FAILED;
2578 }
2579
Jeff Brownac386072011-07-20 15:19:50 -07002580 InjectionState* injectionState = new InjectionState(injectorPid, injectorUid);
Jeff Brownb6997262010-10-08 22:31:17 -07002581 if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) {
2582 injectionState->injectionIsAsync = true;
2583 }
2584
2585 injectionState->refCount += 1;
Jeff Brown3241b6b2012-02-03 15:08:02 -08002586 lastInjectedEntry->injectionState = injectionState;
Jeff Brownb6997262010-10-08 22:31:17 -07002587
Jeff Brown3241b6b2012-02-03 15:08:02 -08002588 bool needWake = false;
2589 for (EventEntry* entry = firstInjectedEntry; entry != NULL; ) {
2590 EventEntry* nextEntry = entry->next;
2591 needWake |= enqueueInboundEventLocked(entry);
2592 entry = nextEntry;
2593 }
2594
Jeff Brownb6997262010-10-08 22:31:17 -07002595 mLock.unlock();
Jeff Brown7fbdc842010-06-17 20:52:56 -07002596
Jeff Brownb88102f2010-09-08 11:49:43 -07002597 if (needWake) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002598 mLooper->wake();
Jeff Brown7fbdc842010-06-17 20:52:56 -07002599 }
2600
2601 int32_t injectionResult;
2602 { // acquire lock
2603 AutoMutex _l(mLock);
2604
Jeff Brown6ec402b2010-07-28 15:48:59 -07002605 if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) {
2606 injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED;
2607 } else {
2608 for (;;) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002609 injectionResult = injectionState->injectionResult;
Jeff Brown6ec402b2010-07-28 15:48:59 -07002610 if (injectionResult != INPUT_EVENT_INJECTION_PENDING) {
2611 break;
2612 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07002613
Jeff Brown7fbdc842010-06-17 20:52:56 -07002614 nsecs_t remainingTimeout = endTime - now();
2615 if (remainingTimeout <= 0) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002616#if DEBUG_INJECTION
Steve Block5baa3a62011-12-20 16:23:08 +00002617 ALOGD("injectInputEvent - Timed out waiting for injection result "
Jeff Brown6ec402b2010-07-28 15:48:59 -07002618 "to become available.");
2619#endif
Jeff Brown7fbdc842010-06-17 20:52:56 -07002620 injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT;
2621 break;
2622 }
2623
Jeff Brown6ec402b2010-07-28 15:48:59 -07002624 mInjectionResultAvailableCondition.waitRelative(mLock, remainingTimeout);
2625 }
2626
2627 if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED
2628 && syncMode == INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002629 while (injectionState->pendingForegroundDispatches != 0) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002630#if DEBUG_INJECTION
Steve Block5baa3a62011-12-20 16:23:08 +00002631 ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.",
Jeff Brown01ce2e92010-09-26 22:20:12 -07002632 injectionState->pendingForegroundDispatches);
Jeff Brown6ec402b2010-07-28 15:48:59 -07002633#endif
2634 nsecs_t remainingTimeout = endTime - now();
2635 if (remainingTimeout <= 0) {
2636#if DEBUG_INJECTION
Steve Block5baa3a62011-12-20 16:23:08 +00002637 ALOGD("injectInputEvent - Timed out waiting for pending foreground "
Jeff Brown6ec402b2010-07-28 15:48:59 -07002638 "dispatches to finish.");
2639#endif
2640 injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT;
2641 break;
2642 }
2643
2644 mInjectionSyncFinishedCondition.waitRelative(mLock, remainingTimeout);
2645 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07002646 }
2647 }
2648
Jeff Brownac386072011-07-20 15:19:50 -07002649 injectionState->release();
Jeff Brown7fbdc842010-06-17 20:52:56 -07002650 } // release lock
2651
Jeff Brown6ec402b2010-07-28 15:48:59 -07002652#if DEBUG_INJECTION
Steve Block5baa3a62011-12-20 16:23:08 +00002653 ALOGD("injectInputEvent - Finished with result %d. "
Jeff Brown6ec402b2010-07-28 15:48:59 -07002654 "injectorPid=%d, injectorUid=%d",
2655 injectionResult, injectorPid, injectorUid);
2656#endif
2657
Jeff Brown7fbdc842010-06-17 20:52:56 -07002658 return injectionResult;
2659}
2660
Jeff Brownb6997262010-10-08 22:31:17 -07002661bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) {
2662 return injectorUid == 0
2663 || mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid);
2664}
2665
Jeff Brown7fbdc842010-06-17 20:52:56 -07002666void InputDispatcher::setInjectionResultLocked(EventEntry* entry, int32_t injectionResult) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002667 InjectionState* injectionState = entry->injectionState;
2668 if (injectionState) {
Jeff Brown7fbdc842010-06-17 20:52:56 -07002669#if DEBUG_INJECTION
Steve Block5baa3a62011-12-20 16:23:08 +00002670 ALOGD("Setting input event injection result to %d. "
Jeff Brown7fbdc842010-06-17 20:52:56 -07002671 "injectorPid=%d, injectorUid=%d",
Jeff Brown01ce2e92010-09-26 22:20:12 -07002672 injectionResult, injectionState->injectorPid, injectionState->injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -07002673#endif
2674
Jeff Brown0029c662011-03-30 02:25:18 -07002675 if (injectionState->injectionIsAsync
2676 && !(entry->policyFlags & POLICY_FLAG_FILTERED)) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002677 // Log the outcome since the injector did not wait for the injection result.
2678 switch (injectionResult) {
2679 case INPUT_EVENT_INJECTION_SUCCEEDED:
Steve Block71f2cf12011-10-20 11:56:00 +01002680 ALOGV("Asynchronous input event injection succeeded.");
Jeff Brown6ec402b2010-07-28 15:48:59 -07002681 break;
2682 case INPUT_EVENT_INJECTION_FAILED:
Steve Block8564c8d2012-01-05 23:22:43 +00002683 ALOGW("Asynchronous input event injection failed.");
Jeff Brown6ec402b2010-07-28 15:48:59 -07002684 break;
2685 case INPUT_EVENT_INJECTION_PERMISSION_DENIED:
Steve Block8564c8d2012-01-05 23:22:43 +00002686 ALOGW("Asynchronous input event injection permission denied.");
Jeff Brown6ec402b2010-07-28 15:48:59 -07002687 break;
2688 case INPUT_EVENT_INJECTION_TIMED_OUT:
Steve Block8564c8d2012-01-05 23:22:43 +00002689 ALOGW("Asynchronous input event injection timed out.");
Jeff Brown6ec402b2010-07-28 15:48:59 -07002690 break;
2691 }
2692 }
2693
Jeff Brown01ce2e92010-09-26 22:20:12 -07002694 injectionState->injectionResult = injectionResult;
Jeff Brown7fbdc842010-06-17 20:52:56 -07002695 mInjectionResultAvailableCondition.broadcast();
2696 }
2697}
2698
Jeff Brown01ce2e92010-09-26 22:20:12 -07002699void InputDispatcher::incrementPendingForegroundDispatchesLocked(EventEntry* entry) {
2700 InjectionState* injectionState = entry->injectionState;
2701 if (injectionState) {
2702 injectionState->pendingForegroundDispatches += 1;
2703 }
2704}
2705
Jeff Brown519e0242010-09-15 15:18:56 -07002706void InputDispatcher::decrementPendingForegroundDispatchesLocked(EventEntry* entry) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002707 InjectionState* injectionState = entry->injectionState;
2708 if (injectionState) {
2709 injectionState->pendingForegroundDispatches -= 1;
Jeff Brown6ec402b2010-07-28 15:48:59 -07002710
Jeff Brown01ce2e92010-09-26 22:20:12 -07002711 if (injectionState->pendingForegroundDispatches == 0) {
2712 mInjectionSyncFinishedCondition.broadcast();
2713 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002714 }
2715}
2716
Jeff Brown9302c872011-07-13 22:51:29 -07002717sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked(
2718 const sp<InputChannel>& inputChannel) const {
2719 size_t numWindows = mWindowHandles.size();
2720 for (size_t i = 0; i < numWindows; i++) {
2721 const sp<InputWindowHandle>& windowHandle = mWindowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -07002722 if (windowHandle->getInputChannel() == inputChannel) {
Jeff Brown9302c872011-07-13 22:51:29 -07002723 return windowHandle;
Jeff Brown01ce2e92010-09-26 22:20:12 -07002724 }
2725 }
2726 return NULL;
2727}
2728
Jeff Brown9302c872011-07-13 22:51:29 -07002729bool InputDispatcher::hasWindowHandleLocked(
2730 const sp<InputWindowHandle>& windowHandle) const {
2731 size_t numWindows = mWindowHandles.size();
2732 for (size_t i = 0; i < numWindows; i++) {
2733 if (mWindowHandles.itemAt(i) == windowHandle) {
2734 return true;
2735 }
2736 }
2737 return false;
2738}
2739
2740void InputDispatcher::setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles) {
Jeff Brownb88102f2010-09-08 11:49:43 -07002741#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002742 ALOGD("setInputWindows");
Jeff Brownb88102f2010-09-08 11:49:43 -07002743#endif
2744 { // acquire lock
2745 AutoMutex _l(mLock);
2746
Jeff Browncc4f7db2011-08-30 20:34:48 -07002747 Vector<sp<InputWindowHandle> > oldWindowHandles = mWindowHandles;
Jeff Brown9302c872011-07-13 22:51:29 -07002748 mWindowHandles = inputWindowHandles;
Jeff Brownb6997262010-10-08 22:31:17 -07002749
Jeff Brown9302c872011-07-13 22:51:29 -07002750 sp<InputWindowHandle> newFocusedWindowHandle;
2751 bool foundHoveredWindow = false;
2752 for (size_t i = 0; i < mWindowHandles.size(); i++) {
2753 const sp<InputWindowHandle>& windowHandle = mWindowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -07002754 if (!windowHandle->updateInfo() || windowHandle->getInputChannel() == NULL) {
Jeff Brown9302c872011-07-13 22:51:29 -07002755 mWindowHandles.removeAt(i--);
2756 continue;
2757 }
Jeff Browncc4f7db2011-08-30 20:34:48 -07002758 if (windowHandle->getInfo()->hasFocus) {
Jeff Brown9302c872011-07-13 22:51:29 -07002759 newFocusedWindowHandle = windowHandle;
2760 }
2761 if (windowHandle == mLastHoverWindowHandle) {
2762 foundHoveredWindow = true;
Jeff Brownb88102f2010-09-08 11:49:43 -07002763 }
2764 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07002765
Jeff Brown9302c872011-07-13 22:51:29 -07002766 if (!foundHoveredWindow) {
2767 mLastHoverWindowHandle = NULL;
2768 }
2769
2770 if (mFocusedWindowHandle != newFocusedWindowHandle) {
2771 if (mFocusedWindowHandle != NULL) {
Jeff Brownb6997262010-10-08 22:31:17 -07002772#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002773 ALOGD("Focus left window: %s",
Jeff Browncc4f7db2011-08-30 20:34:48 -07002774 mFocusedWindowHandle->getName().string());
Jeff Brownb6997262010-10-08 22:31:17 -07002775#endif
Jeff Browncc4f7db2011-08-30 20:34:48 -07002776 sp<InputChannel> focusedInputChannel = mFocusedWindowHandle->getInputChannel();
2777 if (focusedInputChannel != NULL) {
Christopher Tated9be36c2011-08-16 16:09:33 -07002778 CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS,
2779 "focus left window");
2780 synthesizeCancelationEventsForInputChannelLocked(
Jeff Browncc4f7db2011-08-30 20:34:48 -07002781 focusedInputChannel, options);
Christopher Tated9be36c2011-08-16 16:09:33 -07002782 }
Jeff Brownb6997262010-10-08 22:31:17 -07002783 }
Jeff Brown9302c872011-07-13 22:51:29 -07002784 if (newFocusedWindowHandle != NULL) {
Jeff Brownb6997262010-10-08 22:31:17 -07002785#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002786 ALOGD("Focus entered window: %s",
Jeff Browncc4f7db2011-08-30 20:34:48 -07002787 newFocusedWindowHandle->getName().string());
Jeff Brownb6997262010-10-08 22:31:17 -07002788#endif
Jeff Brown9302c872011-07-13 22:51:29 -07002789 }
2790 mFocusedWindowHandle = newFocusedWindowHandle;
Jeff Brownb6997262010-10-08 22:31:17 -07002791 }
2792
Jeff Brown9302c872011-07-13 22:51:29 -07002793 for (size_t i = 0; i < mTouchState.windows.size(); i++) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002794 TouchedWindow& touchedWindow = mTouchState.windows.editItemAt(i);
Jeff Brown9302c872011-07-13 22:51:29 -07002795 if (!hasWindowHandleLocked(touchedWindow.windowHandle)) {
Jeff Brownb6997262010-10-08 22:31:17 -07002796#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002797 ALOGD("Touched window was removed: %s",
Jeff Browncc4f7db2011-08-30 20:34:48 -07002798 touchedWindow.windowHandle->getName().string());
Jeff Brownb6997262010-10-08 22:31:17 -07002799#endif
Jeff Browncc4f7db2011-08-30 20:34:48 -07002800 sp<InputChannel> touchedInputChannel =
2801 touchedWindow.windowHandle->getInputChannel();
2802 if (touchedInputChannel != NULL) {
Christopher Tated9be36c2011-08-16 16:09:33 -07002803 CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS,
2804 "touched window was removed");
2805 synthesizeCancelationEventsForInputChannelLocked(
Jeff Browncc4f7db2011-08-30 20:34:48 -07002806 touchedInputChannel, options);
Christopher Tated9be36c2011-08-16 16:09:33 -07002807 }
Jeff Brown9302c872011-07-13 22:51:29 -07002808 mTouchState.windows.removeAt(i--);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002809 }
2810 }
Jeff Browncc4f7db2011-08-30 20:34:48 -07002811
2812 // Release information for windows that are no longer present.
2813 // This ensures that unused input channels are released promptly.
2814 // Otherwise, they might stick around until the window handle is destroyed
2815 // which might not happen until the next GC.
2816 for (size_t i = 0; i < oldWindowHandles.size(); i++) {
2817 const sp<InputWindowHandle>& oldWindowHandle = oldWindowHandles.itemAt(i);
2818 if (!hasWindowHandleLocked(oldWindowHandle)) {
2819#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002820 ALOGD("Window went away: %s", oldWindowHandle->getName().string());
Jeff Browncc4f7db2011-08-30 20:34:48 -07002821#endif
2822 oldWindowHandle->releaseInfo();
2823 }
2824 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002825 } // release lock
2826
2827 // Wake up poll loop since it may need to make new input dispatching choices.
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002828 mLooper->wake();
Jeff Brownb88102f2010-09-08 11:49:43 -07002829}
2830
Jeff Brown9302c872011-07-13 22:51:29 -07002831void InputDispatcher::setFocusedApplication(
2832 const sp<InputApplicationHandle>& inputApplicationHandle) {
Jeff Brownb88102f2010-09-08 11:49:43 -07002833#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002834 ALOGD("setFocusedApplication");
Jeff Brownb88102f2010-09-08 11:49:43 -07002835#endif
2836 { // acquire lock
2837 AutoMutex _l(mLock);
2838
Jeff Browncc4f7db2011-08-30 20:34:48 -07002839 if (inputApplicationHandle != NULL && inputApplicationHandle->updateInfo()) {
Jeff Brown5ea29ab2011-07-27 11:50:51 -07002840 if (mFocusedApplicationHandle != inputApplicationHandle) {
2841 if (mFocusedApplicationHandle != NULL) {
Jeff Browne9bb9be2012-02-06 15:47:55 -08002842 resetANRTimeoutsLocked();
Jeff Browncc4f7db2011-08-30 20:34:48 -07002843 mFocusedApplicationHandle->releaseInfo();
Jeff Brown5ea29ab2011-07-27 11:50:51 -07002844 }
2845 mFocusedApplicationHandle = inputApplicationHandle;
2846 }
2847 } else if (mFocusedApplicationHandle != NULL) {
Jeff Browne9bb9be2012-02-06 15:47:55 -08002848 resetANRTimeoutsLocked();
Jeff Browncc4f7db2011-08-30 20:34:48 -07002849 mFocusedApplicationHandle->releaseInfo();
Jeff Brown9302c872011-07-13 22:51:29 -07002850 mFocusedApplicationHandle.clear();
Jeff Brownb88102f2010-09-08 11:49:43 -07002851 }
2852
2853#if DEBUG_FOCUS
Jeff Brownb6997262010-10-08 22:31:17 -07002854 //logDispatchStateLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -07002855#endif
2856 } // release lock
2857
2858 // Wake up poll loop since it may need to make new input dispatching choices.
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002859 mLooper->wake();
Jeff Brownb88102f2010-09-08 11:49:43 -07002860}
2861
Jeff Brownb88102f2010-09-08 11:49:43 -07002862void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) {
2863#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002864 ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen);
Jeff Brownb88102f2010-09-08 11:49:43 -07002865#endif
2866
2867 bool changed;
2868 { // acquire lock
2869 AutoMutex _l(mLock);
2870
2871 if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) {
Jeff Brown120a4592010-10-27 18:43:51 -07002872 if (mDispatchFrozen && !frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -07002873 resetANRTimeoutsLocked();
2874 }
2875
Jeff Brown120a4592010-10-27 18:43:51 -07002876 if (mDispatchEnabled && !enabled) {
2877 resetAndDropEverythingLocked("dispatcher is being disabled");
2878 }
2879
Jeff Brownb88102f2010-09-08 11:49:43 -07002880 mDispatchEnabled = enabled;
2881 mDispatchFrozen = frozen;
2882 changed = true;
2883 } else {
2884 changed = false;
2885 }
2886
2887#if DEBUG_FOCUS
Jeff Brownb6997262010-10-08 22:31:17 -07002888 //logDispatchStateLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -07002889#endif
2890 } // release lock
2891
2892 if (changed) {
2893 // Wake up poll loop since it may need to make new input dispatching choices.
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002894 mLooper->wake();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002895 }
2896}
2897
Jeff Brown0029c662011-03-30 02:25:18 -07002898void InputDispatcher::setInputFilterEnabled(bool enabled) {
2899#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002900 ALOGD("setInputFilterEnabled: enabled=%d", enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07002901#endif
2902
2903 { // acquire lock
2904 AutoMutex _l(mLock);
2905
2906 if (mInputFilterEnabled == enabled) {
2907 return;
2908 }
2909
2910 mInputFilterEnabled = enabled;
2911 resetAndDropEverythingLocked("input filter is being enabled or disabled");
2912 } // release lock
2913
2914 // Wake up poll loop since there might be work to do to drop everything.
2915 mLooper->wake();
2916}
2917
Jeff Browne6504122010-09-27 14:52:15 -07002918bool InputDispatcher::transferTouchFocus(const sp<InputChannel>& fromChannel,
2919 const sp<InputChannel>& toChannel) {
2920#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002921 ALOGD("transferTouchFocus: fromChannel=%s, toChannel=%s",
Jeff Browne6504122010-09-27 14:52:15 -07002922 fromChannel->getName().string(), toChannel->getName().string());
2923#endif
2924 { // acquire lock
2925 AutoMutex _l(mLock);
2926
Jeff Brown9302c872011-07-13 22:51:29 -07002927 sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromChannel);
2928 sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toChannel);
2929 if (fromWindowHandle == NULL || toWindowHandle == NULL) {
Jeff Browne6504122010-09-27 14:52:15 -07002930#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002931 ALOGD("Cannot transfer focus because from or to window not found.");
Jeff Browne6504122010-09-27 14:52:15 -07002932#endif
2933 return false;
2934 }
Jeff Brown9302c872011-07-13 22:51:29 -07002935 if (fromWindowHandle == toWindowHandle) {
Jeff Browne6504122010-09-27 14:52:15 -07002936#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002937 ALOGD("Trivial transfer to same window.");
Jeff Browne6504122010-09-27 14:52:15 -07002938#endif
2939 return true;
2940 }
2941
2942 bool found = false;
2943 for (size_t i = 0; i < mTouchState.windows.size(); i++) {
2944 const TouchedWindow& touchedWindow = mTouchState.windows[i];
Jeff Brown9302c872011-07-13 22:51:29 -07002945 if (touchedWindow.windowHandle == fromWindowHandle) {
Jeff Browne6504122010-09-27 14:52:15 -07002946 int32_t oldTargetFlags = touchedWindow.targetFlags;
2947 BitSet32 pointerIds = touchedWindow.pointerIds;
2948
2949 mTouchState.windows.removeAt(i);
2950
Jeff Brown46e75292010-11-10 16:53:45 -08002951 int32_t newTargetFlags = oldTargetFlags
Jeff Browna032cc02011-03-07 16:56:21 -08002952 & (InputTarget::FLAG_FOREGROUND
2953 | InputTarget::FLAG_SPLIT | InputTarget::FLAG_DISPATCH_AS_IS);
Jeff Brown9302c872011-07-13 22:51:29 -07002954 mTouchState.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds);
Jeff Browne6504122010-09-27 14:52:15 -07002955
2956 found = true;
2957 break;
2958 }
2959 }
2960
2961 if (! found) {
2962#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002963 ALOGD("Focus transfer failed because from window did not have focus.");
Jeff Browne6504122010-09-27 14:52:15 -07002964#endif
2965 return false;
2966 }
2967
Jeff Brown9c9f1a32010-10-11 18:32:20 -07002968 ssize_t fromConnectionIndex = getConnectionIndexLocked(fromChannel);
2969 ssize_t toConnectionIndex = getConnectionIndexLocked(toChannel);
2970 if (fromConnectionIndex >= 0 && toConnectionIndex >= 0) {
Jeff Browncbee6d62012-02-03 20:11:27 -08002971 sp<Connection> fromConnection = mConnectionsByFd.valueAt(fromConnectionIndex);
2972 sp<Connection> toConnection = mConnectionsByFd.valueAt(toConnectionIndex);
Jeff Brown9c9f1a32010-10-11 18:32:20 -07002973
2974 fromConnection->inputState.copyPointerStateTo(toConnection->inputState);
Jeff Brownda3d5a92011-03-29 15:11:34 -07002975 CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS,
Jeff Brown9c9f1a32010-10-11 18:32:20 -07002976 "transferring touch focus from this window to another window");
Jeff Brownda3d5a92011-03-29 15:11:34 -07002977 synthesizeCancelationEventsForConnectionLocked(fromConnection, options);
Jeff Brown9c9f1a32010-10-11 18:32:20 -07002978 }
2979
Jeff Browne6504122010-09-27 14:52:15 -07002980#if DEBUG_FOCUS
2981 logDispatchStateLocked();
2982#endif
2983 } // release lock
2984
2985 // Wake up poll loop since it may need to make new input dispatching choices.
2986 mLooper->wake();
2987 return true;
2988}
2989
Jeff Brown120a4592010-10-27 18:43:51 -07002990void InputDispatcher::resetAndDropEverythingLocked(const char* reason) {
2991#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002992 ALOGD("Resetting and dropping all events (%s).", reason);
Jeff Brown120a4592010-10-27 18:43:51 -07002993#endif
2994
Jeff Brownda3d5a92011-03-29 15:11:34 -07002995 CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason);
2996 synthesizeCancelationEventsForAllConnectionsLocked(options);
Jeff Brown120a4592010-10-27 18:43:51 -07002997
2998 resetKeyRepeatLocked();
2999 releasePendingEventLocked();
3000 drainInboundQueueLocked();
Jeff Browne9bb9be2012-02-06 15:47:55 -08003001 resetANRTimeoutsLocked();
Jeff Brown120a4592010-10-27 18:43:51 -07003002
3003 mTouchState.reset();
Jeff Brown9302c872011-07-13 22:51:29 -07003004 mLastHoverWindowHandle.clear();
Jeff Brown120a4592010-10-27 18:43:51 -07003005}
3006
Jeff Brownb88102f2010-09-08 11:49:43 -07003007void InputDispatcher::logDispatchStateLocked() {
3008 String8 dump;
3009 dumpDispatchStateLocked(dump);
Jeff Brown2a95c2a2010-09-16 12:31:46 -07003010
3011 char* text = dump.lockBuffer(dump.size());
3012 char* start = text;
3013 while (*start != '\0') {
3014 char* end = strchr(start, '\n');
3015 if (*end == '\n') {
3016 *(end++) = '\0';
3017 }
Steve Block5baa3a62011-12-20 16:23:08 +00003018 ALOGD("%s", start);
Jeff Brown2a95c2a2010-09-16 12:31:46 -07003019 start = end;
3020 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003021}
3022
3023void InputDispatcher::dumpDispatchStateLocked(String8& dump) {
Jeff Brownf2f48712010-10-01 17:46:21 -07003024 dump.appendFormat(INDENT "DispatchEnabled: %d\n", mDispatchEnabled);
3025 dump.appendFormat(INDENT "DispatchFrozen: %d\n", mDispatchFrozen);
Jeff Brownb88102f2010-09-08 11:49:43 -07003026
Jeff Brown9302c872011-07-13 22:51:29 -07003027 if (mFocusedApplicationHandle != NULL) {
Jeff Brownf2f48712010-10-01 17:46:21 -07003028 dump.appendFormat(INDENT "FocusedApplication: name='%s', dispatchingTimeout=%0.3fms\n",
Jeff Browncc4f7db2011-08-30 20:34:48 -07003029 mFocusedApplicationHandle->getName().string(),
3030 mFocusedApplicationHandle->getDispatchingTimeout(
3031 DEFAULT_INPUT_DISPATCHING_TIMEOUT) / 1000000.0);
Jeff Brownb88102f2010-09-08 11:49:43 -07003032 } else {
Jeff Brownf2f48712010-10-01 17:46:21 -07003033 dump.append(INDENT "FocusedApplication: <null>\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003034 }
Jeff Brownf2f48712010-10-01 17:46:21 -07003035 dump.appendFormat(INDENT "FocusedWindow: name='%s'\n",
Jeff Browncc4f7db2011-08-30 20:34:48 -07003036 mFocusedWindowHandle != NULL ? mFocusedWindowHandle->getName().string() : "<null>");
Jeff Brownf2f48712010-10-01 17:46:21 -07003037
3038 dump.appendFormat(INDENT "TouchDown: %s\n", toString(mTouchState.down));
3039 dump.appendFormat(INDENT "TouchSplit: %s\n", toString(mTouchState.split));
Jeff Brown95712852011-01-04 19:41:59 -08003040 dump.appendFormat(INDENT "TouchDeviceId: %d\n", mTouchState.deviceId);
Jeff Brown58a2da82011-01-25 16:02:22 -08003041 dump.appendFormat(INDENT "TouchSource: 0x%08x\n", mTouchState.source);
Jeff Brownf2f48712010-10-01 17:46:21 -07003042 if (!mTouchState.windows.isEmpty()) {
3043 dump.append(INDENT "TouchedWindows:\n");
3044 for (size_t i = 0; i < mTouchState.windows.size(); i++) {
3045 const TouchedWindow& touchedWindow = mTouchState.windows[i];
3046 dump.appendFormat(INDENT2 "%d: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n",
Jeff Browncc4f7db2011-08-30 20:34:48 -07003047 i, touchedWindow.windowHandle->getName().string(),
3048 touchedWindow.pointerIds.value,
Jeff Brownf2f48712010-10-01 17:46:21 -07003049 touchedWindow.targetFlags);
3050 }
3051 } else {
3052 dump.append(INDENT "TouchedWindows: <none>\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003053 }
3054
Jeff Brown9302c872011-07-13 22:51:29 -07003055 if (!mWindowHandles.isEmpty()) {
Jeff Brownf2f48712010-10-01 17:46:21 -07003056 dump.append(INDENT "Windows:\n");
Jeff Brown9302c872011-07-13 22:51:29 -07003057 for (size_t i = 0; i < mWindowHandles.size(); i++) {
3058 const sp<InputWindowHandle>& windowHandle = mWindowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -07003059 const InputWindowInfo* windowInfo = windowHandle->getInfo();
3060
Jeff Brownf2f48712010-10-01 17:46:21 -07003061 dump.appendFormat(INDENT2 "%d: name='%s', paused=%s, hasFocus=%s, hasWallpaper=%s, "
3062 "visible=%s, canReceiveKeys=%s, flags=0x%08x, type=0x%08x, layer=%d, "
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003063 "frame=[%d,%d][%d,%d], scale=%f, "
Jeff Brownfbf09772011-01-16 14:06:57 -08003064 "touchableRegion=",
Jeff Browncc4f7db2011-08-30 20:34:48 -07003065 i, windowInfo->name.string(),
3066 toString(windowInfo->paused),
3067 toString(windowInfo->hasFocus),
3068 toString(windowInfo->hasWallpaper),
3069 toString(windowInfo->visible),
3070 toString(windowInfo->canReceiveKeys),
3071 windowInfo->layoutParamsFlags, windowInfo->layoutParamsType,
3072 windowInfo->layer,
3073 windowInfo->frameLeft, windowInfo->frameTop,
3074 windowInfo->frameRight, windowInfo->frameBottom,
3075 windowInfo->scaleFactor);
3076 dumpRegion(dump, windowInfo->touchableRegion);
3077 dump.appendFormat(", inputFeatures=0x%08x", windowInfo->inputFeatures);
Jeff Brownfbf09772011-01-16 14:06:57 -08003078 dump.appendFormat(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%0.3fms\n",
Jeff Browncc4f7db2011-08-30 20:34:48 -07003079 windowInfo->ownerPid, windowInfo->ownerUid,
3080 windowInfo->dispatchingTimeout / 1000000.0);
Jeff Brownf2f48712010-10-01 17:46:21 -07003081 }
3082 } else {
3083 dump.append(INDENT "Windows: <none>\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003084 }
3085
Jeff Brownf2f48712010-10-01 17:46:21 -07003086 if (!mMonitoringChannels.isEmpty()) {
3087 dump.append(INDENT "MonitoringChannels:\n");
3088 for (size_t i = 0; i < mMonitoringChannels.size(); i++) {
3089 const sp<InputChannel>& channel = mMonitoringChannels[i];
3090 dump.appendFormat(INDENT2 "%d: '%s'\n", i, channel->getName().string());
3091 }
3092 } else {
3093 dump.append(INDENT "MonitoringChannels: <none>\n");
3094 }
Jeff Brown519e0242010-09-15 15:18:56 -07003095
Jeff Brown265f1cc2012-06-11 18:01:06 -07003096 nsecs_t currentTime = now();
3097
3098 if (!mInboundQueue.isEmpty()) {
3099 dump.appendFormat(INDENT "InboundQueue: length=%u\n", mInboundQueue.count());
3100 for (EventEntry* entry = mInboundQueue.head; entry; entry = entry->next) {
3101 dump.append(INDENT2);
3102 entry->appendDescription(dump);
3103 dump.appendFormat(", age=%01.1fms\n",
3104 (currentTime - entry->eventTime) * 0.000001f);
3105 }
3106 } else {
3107 dump.append(INDENT "InboundQueue: <empty>\n");
3108 }
3109
3110 if (!mConnectionsByFd.isEmpty()) {
3111 dump.append(INDENT "Connections:\n");
3112 for (size_t i = 0; i < mConnectionsByFd.size(); i++) {
3113 const sp<Connection>& connection = mConnectionsByFd.valueAt(i);
3114 dump.appendFormat(INDENT2 "%d: channelName='%s', windowName='%s', "
3115 "status=%s, monitor=%s, inputPublisherBlocked=%s\n",
3116 i, connection->getInputChannelName(), connection->getWindowName(),
3117 connection->getStatusLabel(), toString(connection->monitor),
3118 toString(connection->inputPublisherBlocked));
3119
3120 if (!connection->outboundQueue.isEmpty()) {
3121 dump.appendFormat(INDENT3 "OutboundQueue: length=%u\n",
3122 connection->outboundQueue.count());
3123 for (DispatchEntry* entry = connection->outboundQueue.head; entry;
3124 entry = entry->next) {
3125 dump.append(INDENT4);
3126 entry->eventEntry->appendDescription(dump);
3127 dump.appendFormat(", targetFlags=0x%08x, resolvedAction=%d, age=%01.1fms\n",
3128 entry->targetFlags, entry->resolvedAction,
3129 (currentTime - entry->eventEntry->eventTime) * 0.000001f);
3130 }
3131 } else {
3132 dump.append(INDENT3 "OutboundQueue: <empty>\n");
3133 }
3134
3135 if (!connection->waitQueue.isEmpty()) {
3136 dump.appendFormat(INDENT3 "WaitQueue: length=%u\n",
3137 connection->waitQueue.count());
3138 for (DispatchEntry* entry = connection->waitQueue.head; entry;
3139 entry = entry->next) {
3140 dump.append(INDENT4);
3141 entry->eventEntry->appendDescription(dump);
3142 dump.appendFormat(", targetFlags=0x%08x, resolvedAction=%d, "
3143 "age=%01.1fms, wait=%01.1fms\n",
3144 entry->targetFlags, entry->resolvedAction,
3145 (currentTime - entry->eventEntry->eventTime) * 0.000001f,
3146 (currentTime - entry->deliveryTime) * 0.000001f);
3147 }
3148 } else {
3149 dump.append(INDENT3 "WaitQueue: <empty>\n");
3150 }
3151 }
3152 } else {
3153 dump.append(INDENT "Connections: <none>\n");
3154 }
Jeff Brownf2f48712010-10-01 17:46:21 -07003155
Jeff Brownb88102f2010-09-08 11:49:43 -07003156 if (isAppSwitchPendingLocked()) {
Jeff Brownf2f48712010-10-01 17:46:21 -07003157 dump.appendFormat(INDENT "AppSwitch: pending, due in %01.1fms\n",
Jeff Brownb88102f2010-09-08 11:49:43 -07003158 (mAppSwitchDueTime - now()) / 1000000.0);
3159 } else {
Jeff Brownf2f48712010-10-01 17:46:21 -07003160 dump.append(INDENT "AppSwitch: not pending\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003161 }
3162}
3163
Jeff Brown928e0542011-01-10 11:17:36 -08003164status_t InputDispatcher::registerInputChannel(const sp<InputChannel>& inputChannel,
3165 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
Jeff Brown9c3cda02010-06-15 01:31:58 -07003166#if DEBUG_REGISTRATION
Steve Block5baa3a62011-12-20 16:23:08 +00003167 ALOGD("channel '%s' ~ registerInputChannel - monitor=%s", inputChannel->getName().string(),
Jeff Brownb88102f2010-09-08 11:49:43 -07003168 toString(monitor));
Jeff Brown9c3cda02010-06-15 01:31:58 -07003169#endif
3170
Jeff Brown46b9ac02010-04-22 18:58:52 -07003171 { // acquire lock
3172 AutoMutex _l(mLock);
3173
Jeff Brown519e0242010-09-15 15:18:56 -07003174 if (getConnectionIndexLocked(inputChannel) >= 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003175 ALOGW("Attempted to register already registered input channel '%s'",
Jeff Brown46b9ac02010-04-22 18:58:52 -07003176 inputChannel->getName().string());
3177 return BAD_VALUE;
3178 }
3179
Jeff Browncc4f7db2011-08-30 20:34:48 -07003180 sp<Connection> connection = new Connection(inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003181
Jeff Brown91e32892012-02-14 15:56:29 -08003182 int fd = inputChannel->getFd();
Jeff Browncbee6d62012-02-03 20:11:27 -08003183 mConnectionsByFd.add(fd, connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -07003184
Jeff Brownb88102f2010-09-08 11:49:43 -07003185 if (monitor) {
3186 mMonitoringChannels.push(inputChannel);
3187 }
3188
Jeff Browncbee6d62012-02-03 20:11:27 -08003189 mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this);
Jeff Brown2cbecea2010-08-17 15:59:26 -07003190
Jeff Brown9c3cda02010-06-15 01:31:58 -07003191 runCommandsLockedInterruptible();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003192 } // release lock
Jeff Brown46b9ac02010-04-22 18:58:52 -07003193 return OK;
3194}
3195
3196status_t InputDispatcher::unregisterInputChannel(const sp<InputChannel>& inputChannel) {
Jeff Brown9c3cda02010-06-15 01:31:58 -07003197#if DEBUG_REGISTRATION
Steve Block5baa3a62011-12-20 16:23:08 +00003198 ALOGD("channel '%s' ~ unregisterInputChannel", inputChannel->getName().string());
Jeff Brown9c3cda02010-06-15 01:31:58 -07003199#endif
3200
Jeff Brown46b9ac02010-04-22 18:58:52 -07003201 { // acquire lock
3202 AutoMutex _l(mLock);
3203
Jeff Browncc4f7db2011-08-30 20:34:48 -07003204 status_t status = unregisterInputChannelLocked(inputChannel, false /*notify*/);
3205 if (status) {
3206 return status;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003207 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003208 } // release lock
3209
Jeff Brown46b9ac02010-04-22 18:58:52 -07003210 // Wake the poll loop because removing the connection may have changed the current
3211 // synchronization state.
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07003212 mLooper->wake();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003213 return OK;
3214}
3215
Jeff Browncc4f7db2011-08-30 20:34:48 -07003216status_t InputDispatcher::unregisterInputChannelLocked(const sp<InputChannel>& inputChannel,
3217 bool notify) {
3218 ssize_t connectionIndex = getConnectionIndexLocked(inputChannel);
3219 if (connectionIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003220 ALOGW("Attempted to unregister already unregistered input channel '%s'",
Jeff Browncc4f7db2011-08-30 20:34:48 -07003221 inputChannel->getName().string());
3222 return BAD_VALUE;
3223 }
3224
Jeff Browncbee6d62012-02-03 20:11:27 -08003225 sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex);
3226 mConnectionsByFd.removeItemsAt(connectionIndex);
Jeff Browncc4f7db2011-08-30 20:34:48 -07003227
3228 if (connection->monitor) {
3229 removeMonitorChannelLocked(inputChannel);
3230 }
3231
Jeff Browncbee6d62012-02-03 20:11:27 -08003232 mLooper->removeFd(inputChannel->getFd());
Jeff Browncc4f7db2011-08-30 20:34:48 -07003233
3234 nsecs_t currentTime = now();
3235 abortBrokenDispatchCycleLocked(currentTime, connection, notify);
3236
3237 runCommandsLockedInterruptible();
3238
3239 connection->status = Connection::STATUS_ZOMBIE;
3240 return OK;
3241}
3242
3243void InputDispatcher::removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) {
3244 for (size_t i = 0; i < mMonitoringChannels.size(); i++) {
3245 if (mMonitoringChannels[i] == inputChannel) {
3246 mMonitoringChannels.removeAt(i);
3247 break;
3248 }
3249 }
3250}
3251
Jeff Brown519e0242010-09-15 15:18:56 -07003252ssize_t InputDispatcher::getConnectionIndexLocked(const sp<InputChannel>& inputChannel) {
Jeff Browncbee6d62012-02-03 20:11:27 -08003253 ssize_t connectionIndex = mConnectionsByFd.indexOfKey(inputChannel->getFd());
Jeff Brown2cbecea2010-08-17 15:59:26 -07003254 if (connectionIndex >= 0) {
Jeff Browncbee6d62012-02-03 20:11:27 -08003255 sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex);
Jeff Brown2cbecea2010-08-17 15:59:26 -07003256 if (connection->inputChannel.get() == inputChannel.get()) {
3257 return connectionIndex;
3258 }
3259 }
3260
3261 return -1;
3262}
3263
Jeff Brown9c3cda02010-06-15 01:31:58 -07003264void InputDispatcher::onDispatchCycleFinishedLocked(
Jeff Brown072ec962012-02-07 14:46:57 -08003265 nsecs_t currentTime, const sp<Connection>& connection, uint32_t seq, bool handled) {
Jeff Brown3915bb82010-11-05 15:02:16 -07003266 CommandEntry* commandEntry = postCommandLocked(
3267 & InputDispatcher::doDispatchCycleFinishedLockedInterruptible);
3268 commandEntry->connection = connection;
Jeff Brown265f1cc2012-06-11 18:01:06 -07003269 commandEntry->eventTime = currentTime;
Jeff Brown072ec962012-02-07 14:46:57 -08003270 commandEntry->seq = seq;
Jeff Brown3915bb82010-11-05 15:02:16 -07003271 commandEntry->handled = handled;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003272}
3273
Jeff Brown9c3cda02010-06-15 01:31:58 -07003274void InputDispatcher::onDispatchCycleBrokenLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07003275 nsecs_t currentTime, const sp<Connection>& connection) {
Steve Block3762c312012-01-06 19:20:56 +00003276 ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!",
Jeff Brown46b9ac02010-04-22 18:58:52 -07003277 connection->getInputChannelName());
3278
Jeff Brown9c3cda02010-06-15 01:31:58 -07003279 CommandEntry* commandEntry = postCommandLocked(
3280 & InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible);
Jeff Brown7fbdc842010-06-17 20:52:56 -07003281 commandEntry->connection = connection;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003282}
3283
Jeff Brown519e0242010-09-15 15:18:56 -07003284void InputDispatcher::onANRLocked(
Jeff Brown9302c872011-07-13 22:51:29 -07003285 nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
3286 const sp<InputWindowHandle>& windowHandle,
Jeff Brown265f1cc2012-06-11 18:01:06 -07003287 nsecs_t eventTime, nsecs_t waitStartTime, const char* reason) {
Steve Block6215d3f2012-01-04 20:05:49 +00003288 ALOGI("Application is not responding: %s. "
Jeff Brown265f1cc2012-06-11 18:01:06 -07003289 "It has been %01.1fms since event, %01.1fms since wait started. Reason: %s",
Jeff Brown9302c872011-07-13 22:51:29 -07003290 getApplicationWindowLabelLocked(applicationHandle, windowHandle).string(),
Jeff Brown519e0242010-09-15 15:18:56 -07003291 (currentTime - eventTime) / 1000000.0,
Jeff Brown265f1cc2012-06-11 18:01:06 -07003292 (currentTime - waitStartTime) / 1000000.0, reason);
Jeff Brown519e0242010-09-15 15:18:56 -07003293
3294 CommandEntry* commandEntry = postCommandLocked(
3295 & InputDispatcher::doNotifyANRLockedInterruptible);
Jeff Brown9302c872011-07-13 22:51:29 -07003296 commandEntry->inputApplicationHandle = applicationHandle;
3297 commandEntry->inputWindowHandle = windowHandle;
Jeff Brown519e0242010-09-15 15:18:56 -07003298}
3299
Jeff Brownb88102f2010-09-08 11:49:43 -07003300void InputDispatcher::doNotifyConfigurationChangedInterruptible(
3301 CommandEntry* commandEntry) {
3302 mLock.unlock();
3303
3304 mPolicy->notifyConfigurationChanged(commandEntry->eventTime);
3305
3306 mLock.lock();
3307}
3308
Jeff Brown9c3cda02010-06-15 01:31:58 -07003309void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(
3310 CommandEntry* commandEntry) {
Jeff Brown7fbdc842010-06-17 20:52:56 -07003311 sp<Connection> connection = commandEntry->connection;
Jeff Brown9c3cda02010-06-15 01:31:58 -07003312
Jeff Brown7fbdc842010-06-17 20:52:56 -07003313 if (connection->status != Connection::STATUS_ZOMBIE) {
3314 mLock.unlock();
Jeff Brown9c3cda02010-06-15 01:31:58 -07003315
Jeff Brown928e0542011-01-10 11:17:36 -08003316 mPolicy->notifyInputChannelBroken(connection->inputWindowHandle);
Jeff Brown7fbdc842010-06-17 20:52:56 -07003317
3318 mLock.lock();
3319 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07003320}
3321
Jeff Brown519e0242010-09-15 15:18:56 -07003322void InputDispatcher::doNotifyANRLockedInterruptible(
Jeff Brown9c3cda02010-06-15 01:31:58 -07003323 CommandEntry* commandEntry) {
Jeff Brown519e0242010-09-15 15:18:56 -07003324 mLock.unlock();
Jeff Brown9c3cda02010-06-15 01:31:58 -07003325
Jeff Brown519e0242010-09-15 15:18:56 -07003326 nsecs_t newTimeout = mPolicy->notifyANR(
Jeff Brown928e0542011-01-10 11:17:36 -08003327 commandEntry->inputApplicationHandle, commandEntry->inputWindowHandle);
Jeff Brown9c3cda02010-06-15 01:31:58 -07003328
Jeff Brown519e0242010-09-15 15:18:56 -07003329 mLock.lock();
Jeff Brown7fbdc842010-06-17 20:52:56 -07003330
Jeff Brown9302c872011-07-13 22:51:29 -07003331 resumeAfterTargetsNotReadyTimeoutLocked(newTimeout,
3332 commandEntry->inputWindowHandle != NULL
Jeff Browncc4f7db2011-08-30 20:34:48 -07003333 ? commandEntry->inputWindowHandle->getInputChannel() : NULL);
Jeff Brown9c3cda02010-06-15 01:31:58 -07003334}
3335
Jeff Brownb88102f2010-09-08 11:49:43 -07003336void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible(
3337 CommandEntry* commandEntry) {
3338 KeyEntry* entry = commandEntry->keyEntry;
Jeff Brown1f245102010-11-18 20:53:46 -08003339
3340 KeyEvent event;
3341 initializeKeyEvent(&event, entry);
Jeff Brownb88102f2010-09-08 11:49:43 -07003342
3343 mLock.unlock();
3344
Jeff Brown905805a2011-10-12 13:57:59 -07003345 nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(commandEntry->inputWindowHandle,
Jeff Brown1f245102010-11-18 20:53:46 -08003346 &event, entry->policyFlags);
Jeff Brownb88102f2010-09-08 11:49:43 -07003347
3348 mLock.lock();
3349
Jeff Brown905805a2011-10-12 13:57:59 -07003350 if (delay < 0) {
3351 entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP;
3352 } else if (!delay) {
3353 entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE;
3354 } else {
3355 entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER;
3356 entry->interceptKeyWakeupTime = now() + delay;
3357 }
Jeff Brownac386072011-07-20 15:19:50 -07003358 entry->release();
Jeff Brownb88102f2010-09-08 11:49:43 -07003359}
3360
Jeff Brown3915bb82010-11-05 15:02:16 -07003361void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(
3362 CommandEntry* commandEntry) {
3363 sp<Connection> connection = commandEntry->connection;
Jeff Brown265f1cc2012-06-11 18:01:06 -07003364 nsecs_t finishTime = commandEntry->eventTime;
Jeff Brown072ec962012-02-07 14:46:57 -08003365 uint32_t seq = commandEntry->seq;
Jeff Brown3915bb82010-11-05 15:02:16 -07003366 bool handled = commandEntry->handled;
3367
Jeff Brown072ec962012-02-07 14:46:57 -08003368 // Handle post-event policy actions.
3369 DispatchEntry* dispatchEntry = connection->findWaitQueueEntry(seq);
3370 if (dispatchEntry) {
Jeff Brown265f1cc2012-06-11 18:01:06 -07003371 nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime;
3372 if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) {
3373 String8 msg;
3374 msg.appendFormat("Window '%s' spent %01.1fms processing the last input event: ",
3375 connection->getWindowName(), eventDuration * 0.000001f);
3376 dispatchEntry->eventEntry->appendDescription(msg);
3377 ALOGI("%s", msg.string());
3378 }
3379
Jeff Brownd1c48a02012-02-06 19:12:47 -08003380 bool restartEvent;
Jeff Brownd1c48a02012-02-06 19:12:47 -08003381 if (dispatchEntry->eventEntry->type == EventEntry::TYPE_KEY) {
3382 KeyEntry* keyEntry = static_cast<KeyEntry*>(dispatchEntry->eventEntry);
3383 restartEvent = afterKeyEventLockedInterruptible(connection,
3384 dispatchEntry, keyEntry, handled);
3385 } else if (dispatchEntry->eventEntry->type == EventEntry::TYPE_MOTION) {
3386 MotionEntry* motionEntry = static_cast<MotionEntry*>(dispatchEntry->eventEntry);
3387 restartEvent = afterMotionEventLockedInterruptible(connection,
3388 dispatchEntry, motionEntry, handled);
3389 } else {
3390 restartEvent = false;
3391 }
3392
3393 // Dequeue the event and start the next cycle.
3394 // Note that because the lock might have been released, it is possible that the
3395 // contents of the wait queue to have been drained, so we need to double-check
3396 // a few things.
Jeff Brown072ec962012-02-07 14:46:57 -08003397 if (dispatchEntry == connection->findWaitQueueEntry(seq)) {
3398 connection->waitQueue.dequeue(dispatchEntry);
Jeff Brown481c1572012-03-09 14:41:15 -08003399 traceWaitQueueLengthLocked(connection);
Jeff Brownd1c48a02012-02-06 19:12:47 -08003400 if (restartEvent && connection->status == Connection::STATUS_NORMAL) {
3401 connection->outboundQueue.enqueueAtHead(dispatchEntry);
Jeff Brown481c1572012-03-09 14:41:15 -08003402 traceOutboundQueueLengthLocked(connection);
Jeff Brownd1c48a02012-02-06 19:12:47 -08003403 } else {
3404 releaseDispatchEntryLocked(dispatchEntry);
Jeff Brown49ed71d2010-12-06 17:13:33 -08003405 }
Jeff Brown3915bb82010-11-05 15:02:16 -07003406 }
Jeff Brown3915bb82010-11-05 15:02:16 -07003407
Jeff Brownd1c48a02012-02-06 19:12:47 -08003408 // Start the next dispatch cycle for this connection.
3409 startDispatchCycleLocked(now(), connection);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003410 }
3411}
3412
3413bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection,
3414 DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled) {
3415 if (!(keyEntry->flags & AKEY_EVENT_FLAG_FALLBACK)) {
3416 // Get the fallback key state.
3417 // Clear it out after dispatching the UP.
3418 int32_t originalKeyCode = keyEntry->keyCode;
3419 int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode);
3420 if (keyEntry->action == AKEY_EVENT_ACTION_UP) {
3421 connection->inputState.removeFallbackKey(originalKeyCode);
3422 }
3423
3424 if (handled || !dispatchEntry->hasForegroundTarget()) {
3425 // If the application handles the original key for which we previously
3426 // generated a fallback or if the window is not a foreground window,
3427 // then cancel the associated fallback key, if any.
3428 if (fallbackKeyCode != -1) {
Jeff Brownfd23e3e2012-05-09 13:34:28 -07003429 // Dispatch the unhandled key to the policy with the cancel flag.
3430#if DEBUG_OUTBOUND_EVENT_DETAILS
3431 ALOGD("Unhandled key event: Asking policy to cancel fallback action. "
3432 "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x",
3433 keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount,
3434 keyEntry->policyFlags);
3435#endif
3436 KeyEvent event;
3437 initializeKeyEvent(&event, keyEntry);
3438 event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED);
3439
3440 mLock.unlock();
3441
3442 mPolicy->dispatchUnhandledKey(connection->inputWindowHandle,
3443 &event, keyEntry->policyFlags, &event);
3444
3445 mLock.lock();
3446
3447 // Cancel the fallback key.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003448 if (fallbackKeyCode != AKEYCODE_UNKNOWN) {
3449 CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS,
3450 "application handled the original non-fallback key "
3451 "or is no longer a foreground target, "
3452 "canceling previously dispatched fallback key");
3453 options.keyCode = fallbackKeyCode;
3454 synthesizeCancelationEventsForConnectionLocked(connection, options);
3455 }
3456 connection->inputState.removeFallbackKey(originalKeyCode);
3457 }
3458 } else {
3459 // If the application did not handle a non-fallback key, first check
3460 // that we are in a good state to perform unhandled key event processing
3461 // Then ask the policy what to do with it.
3462 bool initialDown = keyEntry->action == AKEY_EVENT_ACTION_DOWN
3463 && keyEntry->repeatCount == 0;
3464 if (fallbackKeyCode == -1 && !initialDown) {
3465#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003466 ALOGD("Unhandled key event: Skipping unhandled key event processing "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003467 "since this is not an initial down. "
Jeff Brownfd23e3e2012-05-09 13:34:28 -07003468 "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x",
3469 originalKeyCode, keyEntry->action, keyEntry->repeatCount,
3470 keyEntry->policyFlags);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003471#endif
3472 return false;
3473 }
3474
3475 // Dispatch the unhandled key to the policy.
3476#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003477 ALOGD("Unhandled key event: Asking policy to perform fallback action. "
Jeff Brownfd23e3e2012-05-09 13:34:28 -07003478 "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x",
3479 keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount,
3480 keyEntry->policyFlags);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003481#endif
3482 KeyEvent event;
3483 initializeKeyEvent(&event, keyEntry);
3484
3485 mLock.unlock();
3486
3487 bool fallback = mPolicy->dispatchUnhandledKey(connection->inputWindowHandle,
3488 &event, keyEntry->policyFlags, &event);
3489
3490 mLock.lock();
3491
3492 if (connection->status != Connection::STATUS_NORMAL) {
3493 connection->inputState.removeFallbackKey(originalKeyCode);
Jeff Brownd1c48a02012-02-06 19:12:47 -08003494 return false;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003495 }
3496
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003497 // Latch the fallback keycode for this key on an initial down.
3498 // The fallback keycode cannot change at any other point in the lifecycle.
3499 if (initialDown) {
3500 if (fallback) {
3501 fallbackKeyCode = event.getKeyCode();
3502 } else {
3503 fallbackKeyCode = AKEYCODE_UNKNOWN;
3504 }
3505 connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode);
3506 }
3507
Steve Blockec193de2012-01-09 18:35:44 +00003508 ALOG_ASSERT(fallbackKeyCode != -1);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003509
3510 // Cancel the fallback key if the policy decides not to send it anymore.
3511 // We will continue to dispatch the key to the policy but we will no
3512 // longer dispatch a fallback key to the application.
3513 if (fallbackKeyCode != AKEYCODE_UNKNOWN
3514 && (!fallback || fallbackKeyCode != event.getKeyCode())) {
3515#if DEBUG_OUTBOUND_EVENT_DETAILS
3516 if (fallback) {
Steve Block5baa3a62011-12-20 16:23:08 +00003517 ALOGD("Unhandled key event: Policy requested to send key %d"
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003518 "as a fallback for %d, but on the DOWN it had requested "
3519 "to send %d instead. Fallback canceled.",
3520 event.getKeyCode(), originalKeyCode, fallbackKeyCode);
3521 } else {
Jeff Brownfd23e3e2012-05-09 13:34:28 -07003522 ALOGD("Unhandled key event: Policy did not request fallback for %d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003523 "but on the DOWN it had requested to send %d. "
3524 "Fallback canceled.",
3525 originalKeyCode, fallbackKeyCode);
3526 }
3527#endif
3528
3529 CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS,
3530 "canceling fallback, policy no longer desires it");
3531 options.keyCode = fallbackKeyCode;
3532 synthesizeCancelationEventsForConnectionLocked(connection, options);
3533
3534 fallback = false;
3535 fallbackKeyCode = AKEYCODE_UNKNOWN;
3536 if (keyEntry->action != AKEY_EVENT_ACTION_UP) {
3537 connection->inputState.setFallbackKey(originalKeyCode,
3538 fallbackKeyCode);
3539 }
3540 }
3541
3542#if DEBUG_OUTBOUND_EVENT_DETAILS
3543 {
3544 String8 msg;
3545 const KeyedVector<int32_t, int32_t>& fallbackKeys =
3546 connection->inputState.getFallbackKeys();
3547 for (size_t i = 0; i < fallbackKeys.size(); i++) {
3548 msg.appendFormat(", %d->%d", fallbackKeys.keyAt(i),
3549 fallbackKeys.valueAt(i));
3550 }
Steve Block5baa3a62011-12-20 16:23:08 +00003551 ALOGD("Unhandled key event: %d currently tracked fallback keys%s.",
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003552 fallbackKeys.size(), msg.string());
3553 }
3554#endif
3555
3556 if (fallback) {
3557 // Restart the dispatch cycle using the fallback key.
3558 keyEntry->eventTime = event.getEventTime();
3559 keyEntry->deviceId = event.getDeviceId();
3560 keyEntry->source = event.getSource();
3561 keyEntry->flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK;
3562 keyEntry->keyCode = fallbackKeyCode;
3563 keyEntry->scanCode = event.getScanCode();
3564 keyEntry->metaState = event.getMetaState();
3565 keyEntry->repeatCount = event.getRepeatCount();
3566 keyEntry->downTime = event.getDownTime();
3567 keyEntry->syntheticRepeat = false;
3568
3569#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003570 ALOGD("Unhandled key event: Dispatching fallback key. "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003571 "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x",
3572 originalKeyCode, fallbackKeyCode, keyEntry->metaState);
3573#endif
Jeff Brownd1c48a02012-02-06 19:12:47 -08003574 return true; // restart the event
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003575 } else {
3576#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003577 ALOGD("Unhandled key event: No fallback key.");
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003578#endif
3579 }
3580 }
3581 }
3582 return false;
3583}
3584
3585bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection,
3586 DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled) {
3587 return false;
Jeff Brown3915bb82010-11-05 15:02:16 -07003588}
3589
Jeff Brownb88102f2010-09-08 11:49:43 -07003590void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) {
3591 mLock.unlock();
3592
Jeff Brown01ce2e92010-09-26 22:20:12 -07003593 mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType);
Jeff Brownb88102f2010-09-08 11:49:43 -07003594
3595 mLock.lock();
3596}
3597
Jeff Brown3915bb82010-11-05 15:02:16 -07003598void InputDispatcher::initializeKeyEvent(KeyEvent* event, const KeyEntry* entry) {
3599 event->initialize(entry->deviceId, entry->source, entry->action, entry->flags,
3600 entry->keyCode, entry->scanCode, entry->metaState, entry->repeatCount,
3601 entry->downTime, entry->eventTime);
3602}
3603
Jeff Brown519e0242010-09-15 15:18:56 -07003604void InputDispatcher::updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
3605 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication) {
3606 // TODO Write some statistics about how long we spend waiting.
Jeff Brownb88102f2010-09-08 11:49:43 -07003607}
3608
Jeff Brown481c1572012-03-09 14:41:15 -08003609void InputDispatcher::traceInboundQueueLengthLocked() {
3610 if (ATRACE_ENABLED()) {
3611 ATRACE_INT("iq", mInboundQueue.count());
3612 }
3613}
3614
3615void InputDispatcher::traceOutboundQueueLengthLocked(const sp<Connection>& connection) {
3616 if (ATRACE_ENABLED()) {
3617 char counterName[40];
3618 snprintf(counterName, sizeof(counterName), "oq:%s", connection->getWindowName());
3619 ATRACE_INT(counterName, connection->outboundQueue.count());
3620 }
3621}
3622
3623void InputDispatcher::traceWaitQueueLengthLocked(const sp<Connection>& connection) {
3624 if (ATRACE_ENABLED()) {
3625 char counterName[40];
3626 snprintf(counterName, sizeof(counterName), "wq:%s", connection->getWindowName());
3627 ATRACE_INT(counterName, connection->waitQueue.count());
3628 }
3629}
3630
Jeff Brownb88102f2010-09-08 11:49:43 -07003631void InputDispatcher::dump(String8& dump) {
Jeff Brown89ef0722011-08-10 16:25:21 -07003632 AutoMutex _l(mLock);
3633
Jeff Brownf2f48712010-10-01 17:46:21 -07003634 dump.append("Input Dispatcher State:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003635 dumpDispatchStateLocked(dump);
Jeff Brown214eaf42011-05-26 19:17:02 -07003636
3637 dump.append(INDENT "Configuration:\n");
Jeff Brown214eaf42011-05-26 19:17:02 -07003638 dump.appendFormat(INDENT2 "KeyRepeatDelay: %0.1fms\n", mConfig.keyRepeatDelay * 0.000001f);
3639 dump.appendFormat(INDENT2 "KeyRepeatTimeout: %0.1fms\n", mConfig.keyRepeatTimeout * 0.000001f);
Jeff Brownb88102f2010-09-08 11:49:43 -07003640}
3641
Jeff Brown89ef0722011-08-10 16:25:21 -07003642void InputDispatcher::monitor() {
3643 // Acquire and release the lock to ensure that the dispatcher has not deadlocked.
3644 mLock.lock();
Jeff Brown112b5f52012-01-27 17:32:06 -08003645 mLooper->wake();
3646 mDispatcherIsAliveCondition.wait(mLock);
Jeff Brown89ef0722011-08-10 16:25:21 -07003647 mLock.unlock();
3648}
3649
Jeff Brown9c3cda02010-06-15 01:31:58 -07003650
Jeff Brown519e0242010-09-15 15:18:56 -07003651// --- InputDispatcher::Queue ---
3652
3653template <typename T>
3654uint32_t InputDispatcher::Queue<T>::count() const {
3655 uint32_t result = 0;
Jeff Brownac386072011-07-20 15:19:50 -07003656 for (const T* entry = head; entry; entry = entry->next) {
Jeff Brown519e0242010-09-15 15:18:56 -07003657 result += 1;
3658 }
3659 return result;
3660}
3661
3662
Jeff Brownac386072011-07-20 15:19:50 -07003663// --- InputDispatcher::InjectionState ---
Jeff Brown46b9ac02010-04-22 18:58:52 -07003664
Jeff Brownac386072011-07-20 15:19:50 -07003665InputDispatcher::InjectionState::InjectionState(int32_t injectorPid, int32_t injectorUid) :
3666 refCount(1),
3667 injectorPid(injectorPid), injectorUid(injectorUid),
3668 injectionResult(INPUT_EVENT_INJECTION_PENDING), injectionIsAsync(false),
3669 pendingForegroundDispatches(0) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003670}
3671
Jeff Brownac386072011-07-20 15:19:50 -07003672InputDispatcher::InjectionState::~InjectionState() {
Jeff Brown01ce2e92010-09-26 22:20:12 -07003673}
3674
Jeff Brownac386072011-07-20 15:19:50 -07003675void InputDispatcher::InjectionState::release() {
3676 refCount -= 1;
3677 if (refCount == 0) {
3678 delete this;
3679 } else {
Steve Blockec193de2012-01-09 18:35:44 +00003680 ALOG_ASSERT(refCount > 0);
Jeff Brown01ce2e92010-09-26 22:20:12 -07003681 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07003682}
3683
Jeff Brownac386072011-07-20 15:19:50 -07003684
3685// --- InputDispatcher::EventEntry ---
3686
3687InputDispatcher::EventEntry::EventEntry(int32_t type, nsecs_t eventTime, uint32_t policyFlags) :
3688 refCount(1), type(type), eventTime(eventTime), policyFlags(policyFlags),
3689 injectionState(NULL), dispatchInProgress(false) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003690}
3691
Jeff Brownac386072011-07-20 15:19:50 -07003692InputDispatcher::EventEntry::~EventEntry() {
3693 releaseInjectionState();
3694}
3695
3696void InputDispatcher::EventEntry::release() {
3697 refCount -= 1;
3698 if (refCount == 0) {
3699 delete this;
3700 } else {
Steve Blockec193de2012-01-09 18:35:44 +00003701 ALOG_ASSERT(refCount > 0);
Jeff Brownac386072011-07-20 15:19:50 -07003702 }
3703}
3704
3705void InputDispatcher::EventEntry::releaseInjectionState() {
3706 if (injectionState) {
3707 injectionState->release();
3708 injectionState = NULL;
3709 }
3710}
3711
3712
3713// --- InputDispatcher::ConfigurationChangedEntry ---
3714
3715InputDispatcher::ConfigurationChangedEntry::ConfigurationChangedEntry(nsecs_t eventTime) :
3716 EventEntry(TYPE_CONFIGURATION_CHANGED, eventTime, 0) {
3717}
3718
3719InputDispatcher::ConfigurationChangedEntry::~ConfigurationChangedEntry() {
3720}
3721
Jeff Brown265f1cc2012-06-11 18:01:06 -07003722void InputDispatcher::ConfigurationChangedEntry::appendDescription(String8& msg) const {
3723 msg.append("ConfigurationChangedEvent()");
3724}
3725
Jeff Brownac386072011-07-20 15:19:50 -07003726
Jeff Brown65fd2512011-08-18 11:20:58 -07003727// --- InputDispatcher::DeviceResetEntry ---
3728
3729InputDispatcher::DeviceResetEntry::DeviceResetEntry(nsecs_t eventTime, int32_t deviceId) :
3730 EventEntry(TYPE_DEVICE_RESET, eventTime, 0),
3731 deviceId(deviceId) {
3732}
3733
3734InputDispatcher::DeviceResetEntry::~DeviceResetEntry() {
3735}
3736
Jeff Brown265f1cc2012-06-11 18:01:06 -07003737void InputDispatcher::DeviceResetEntry::appendDescription(String8& msg) const {
3738 msg.appendFormat("DeviceResetEvent(deviceId=%d)", deviceId);
3739}
3740
Jeff Brown65fd2512011-08-18 11:20:58 -07003741
Jeff Brownac386072011-07-20 15:19:50 -07003742// --- InputDispatcher::KeyEntry ---
3743
3744InputDispatcher::KeyEntry::KeyEntry(nsecs_t eventTime,
Jeff Brown58a2da82011-01-25 16:02:22 -08003745 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown7fbdc842010-06-17 20:52:56 -07003746 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
Jeff Brownac386072011-07-20 15:19:50 -07003747 int32_t repeatCount, nsecs_t downTime) :
3748 EventEntry(TYPE_KEY, eventTime, policyFlags),
3749 deviceId(deviceId), source(source), action(action), flags(flags),
3750 keyCode(keyCode), scanCode(scanCode), metaState(metaState),
3751 repeatCount(repeatCount), downTime(downTime),
Jeff Brown905805a2011-10-12 13:57:59 -07003752 syntheticRepeat(false), interceptKeyResult(KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN),
3753 interceptKeyWakeupTime(0) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003754}
3755
Jeff Brownac386072011-07-20 15:19:50 -07003756InputDispatcher::KeyEntry::~KeyEntry() {
3757}
Jeff Brown7fbdc842010-06-17 20:52:56 -07003758
Jeff Brown265f1cc2012-06-11 18:01:06 -07003759void InputDispatcher::KeyEntry::appendDescription(String8& msg) const {
3760 msg.appendFormat("KeyEvent(action=%d, deviceId=%d, source=0x%08x)",
3761 action, deviceId, source);
3762}
3763
Jeff Brownac386072011-07-20 15:19:50 -07003764void InputDispatcher::KeyEntry::recycle() {
3765 releaseInjectionState();
3766
3767 dispatchInProgress = false;
3768 syntheticRepeat = false;
3769 interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN;
Jeff Brown905805a2011-10-12 13:57:59 -07003770 interceptKeyWakeupTime = 0;
Jeff Brownac386072011-07-20 15:19:50 -07003771}
3772
3773
Jeff Brownae9fc032010-08-18 15:51:08 -07003774// --- InputDispatcher::MotionEntry ---
3775
Jeff Brownac386072011-07-20 15:19:50 -07003776InputDispatcher::MotionEntry::MotionEntry(nsecs_t eventTime,
3777 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action, int32_t flags,
3778 int32_t metaState, int32_t buttonState,
3779 int32_t edgeFlags, float xPrecision, float yPrecision,
3780 nsecs_t downTime, uint32_t pointerCount,
3781 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords) :
3782 EventEntry(TYPE_MOTION, eventTime, policyFlags),
Jeff Brown3241b6b2012-02-03 15:08:02 -08003783 eventTime(eventTime),
Jeff Brownac386072011-07-20 15:19:50 -07003784 deviceId(deviceId), source(source), action(action), flags(flags),
3785 metaState(metaState), buttonState(buttonState), edgeFlags(edgeFlags),
3786 xPrecision(xPrecision), yPrecision(yPrecision),
Jeff Brown3241b6b2012-02-03 15:08:02 -08003787 downTime(downTime), pointerCount(pointerCount) {
Jeff Brownac386072011-07-20 15:19:50 -07003788 for (uint32_t i = 0; i < pointerCount; i++) {
3789 this->pointerProperties[i].copyFrom(pointerProperties[i]);
Jeff Brown3241b6b2012-02-03 15:08:02 -08003790 this->pointerCoords[i].copyFrom(pointerCoords[i]);
Jeff Brownac386072011-07-20 15:19:50 -07003791 }
3792}
3793
3794InputDispatcher::MotionEntry::~MotionEntry() {
Jeff Brownac386072011-07-20 15:19:50 -07003795}
3796
Jeff Brown265f1cc2012-06-11 18:01:06 -07003797void InputDispatcher::MotionEntry::appendDescription(String8& msg) const {
3798 msg.appendFormat("MotionEvent(action=%d, deviceId=%d, source=0x%08x)",
3799 action, deviceId, source);
3800}
3801
Jeff Brownac386072011-07-20 15:19:50 -07003802
3803// --- InputDispatcher::DispatchEntry ---
3804
Jeff Brown072ec962012-02-07 14:46:57 -08003805volatile int32_t InputDispatcher::DispatchEntry::sNextSeqAtomic;
3806
Jeff Brownac386072011-07-20 15:19:50 -07003807InputDispatcher::DispatchEntry::DispatchEntry(EventEntry* eventEntry,
3808 int32_t targetFlags, float xOffset, float yOffset, float scaleFactor) :
Jeff Brown072ec962012-02-07 14:46:57 -08003809 seq(nextSeq()),
Jeff Brownac386072011-07-20 15:19:50 -07003810 eventEntry(eventEntry), targetFlags(targetFlags),
3811 xOffset(xOffset), yOffset(yOffset), scaleFactor(scaleFactor),
Jeff Brown265f1cc2012-06-11 18:01:06 -07003812 deliveryTime(0), resolvedAction(0), resolvedFlags(0) {
Jeff Brownac386072011-07-20 15:19:50 -07003813 eventEntry->refCount += 1;
3814}
3815
3816InputDispatcher::DispatchEntry::~DispatchEntry() {
3817 eventEntry->release();
3818}
3819
Jeff Brown072ec962012-02-07 14:46:57 -08003820uint32_t InputDispatcher::DispatchEntry::nextSeq() {
3821 // Sequence number 0 is reserved and will never be returned.
3822 uint32_t seq;
3823 do {
3824 seq = android_atomic_inc(&sNextSeqAtomic);
3825 } while (!seq);
3826 return seq;
3827}
3828
Jeff Brownb88102f2010-09-08 11:49:43 -07003829
3830// --- InputDispatcher::InputState ---
3831
Jeff Brownb6997262010-10-08 22:31:17 -07003832InputDispatcher::InputState::InputState() {
Jeff Brownb88102f2010-09-08 11:49:43 -07003833}
3834
3835InputDispatcher::InputState::~InputState() {
3836}
3837
3838bool InputDispatcher::InputState::isNeutral() const {
3839 return mKeyMementos.isEmpty() && mMotionMementos.isEmpty();
3840}
3841
Jeff Brown81346812011-06-28 20:08:48 -07003842bool InputDispatcher::InputState::isHovering(int32_t deviceId, uint32_t source) const {
3843 for (size_t i = 0; i < mMotionMementos.size(); i++) {
3844 const MotionMemento& memento = mMotionMementos.itemAt(i);
3845 if (memento.deviceId == deviceId
3846 && memento.source == source
3847 && memento.hovering) {
3848 return true;
3849 }
3850 }
3851 return false;
3852}
Jeff Brownb88102f2010-09-08 11:49:43 -07003853
Jeff Brown81346812011-06-28 20:08:48 -07003854bool InputDispatcher::InputState::trackKey(const KeyEntry* entry,
3855 int32_t action, int32_t flags) {
3856 switch (action) {
3857 case AKEY_EVENT_ACTION_UP: {
3858 if (entry->flags & AKEY_EVENT_FLAG_FALLBACK) {
3859 for (size_t i = 0; i < mFallbackKeys.size(); ) {
3860 if (mFallbackKeys.valueAt(i) == entry->keyCode) {
3861 mFallbackKeys.removeItemsAt(i);
3862 } else {
3863 i += 1;
3864 }
3865 }
3866 }
3867 ssize_t index = findKeyMemento(entry);
3868 if (index >= 0) {
3869 mKeyMementos.removeAt(index);
3870 return true;
3871 }
Jeff Brown68b909d2011-12-07 16:36:01 -08003872 /* FIXME: We can't just drop the key up event because that prevents creating
3873 * popup windows that are automatically shown when a key is held and then
3874 * dismissed when the key is released. The problem is that the popup will
3875 * not have received the original key down, so the key up will be considered
3876 * to be inconsistent with its observed state. We could perhaps handle this
3877 * by synthesizing a key down but that will cause other problems.
3878 *
3879 * So for now, allow inconsistent key up events to be dispatched.
3880 *
Jeff Brown81346812011-06-28 20:08:48 -07003881#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003882 ALOGD("Dropping inconsistent key up event: deviceId=%d, source=%08x, "
Jeff Brown81346812011-06-28 20:08:48 -07003883 "keyCode=%d, scanCode=%d",
3884 entry->deviceId, entry->source, entry->keyCode, entry->scanCode);
3885#endif
3886 return false;
Jeff Brown68b909d2011-12-07 16:36:01 -08003887 */
3888 return true;
Jeff Brown81346812011-06-28 20:08:48 -07003889 }
3890
3891 case AKEY_EVENT_ACTION_DOWN: {
3892 ssize_t index = findKeyMemento(entry);
3893 if (index >= 0) {
3894 mKeyMementos.removeAt(index);
3895 }
3896 addKeyMemento(entry, flags);
3897 return true;
3898 }
3899
3900 default:
3901 return true;
Jeff Brownb88102f2010-09-08 11:49:43 -07003902 }
3903}
3904
Jeff Brown81346812011-06-28 20:08:48 -07003905bool InputDispatcher::InputState::trackMotion(const MotionEntry* entry,
3906 int32_t action, int32_t flags) {
3907 int32_t actionMasked = action & AMOTION_EVENT_ACTION_MASK;
3908 switch (actionMasked) {
3909 case AMOTION_EVENT_ACTION_UP:
3910 case AMOTION_EVENT_ACTION_CANCEL: {
3911 ssize_t index = findMotionMemento(entry, false /*hovering*/);
3912 if (index >= 0) {
3913 mMotionMementos.removeAt(index);
3914 return true;
Jeff Brownda3d5a92011-03-29 15:11:34 -07003915 }
Jeff Brown81346812011-06-28 20:08:48 -07003916#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003917 ALOGD("Dropping inconsistent motion up or cancel event: deviceId=%d, source=%08x, "
Jeff Brown81346812011-06-28 20:08:48 -07003918 "actionMasked=%d",
3919 entry->deviceId, entry->source, actionMasked);
3920#endif
3921 return false;
Jeff Brownda3d5a92011-03-29 15:11:34 -07003922 }
3923
Jeff Brown81346812011-06-28 20:08:48 -07003924 case AMOTION_EVENT_ACTION_DOWN: {
3925 ssize_t index = findMotionMemento(entry, false /*hovering*/);
3926 if (index >= 0) {
3927 mMotionMementos.removeAt(index);
3928 }
3929 addMotionMemento(entry, flags, false /*hovering*/);
3930 return true;
3931 }
3932
3933 case AMOTION_EVENT_ACTION_POINTER_UP:
3934 case AMOTION_EVENT_ACTION_POINTER_DOWN:
3935 case AMOTION_EVENT_ACTION_MOVE: {
3936 ssize_t index = findMotionMemento(entry, false /*hovering*/);
3937 if (index >= 0) {
3938 MotionMemento& memento = mMotionMementos.editItemAt(index);
3939 memento.setPointers(entry);
3940 return true;
3941 }
Jeff Brown2e45fb62011-06-29 21:19:05 -07003942 if (actionMasked == AMOTION_EVENT_ACTION_MOVE
3943 && (entry->source & (AINPUT_SOURCE_CLASS_JOYSTICK
3944 | AINPUT_SOURCE_CLASS_NAVIGATION))) {
3945 // Joysticks and trackballs can send MOVE events without corresponding DOWN or UP.
3946 return true;
3947 }
Jeff Brown81346812011-06-28 20:08:48 -07003948#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003949 ALOGD("Dropping inconsistent motion pointer up/down or move event: "
Jeff Brown81346812011-06-28 20:08:48 -07003950 "deviceId=%d, source=%08x, actionMasked=%d",
3951 entry->deviceId, entry->source, actionMasked);
3952#endif
3953 return false;
3954 }
3955
3956 case AMOTION_EVENT_ACTION_HOVER_EXIT: {
3957 ssize_t index = findMotionMemento(entry, true /*hovering*/);
3958 if (index >= 0) {
3959 mMotionMementos.removeAt(index);
3960 return true;
3961 }
3962#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003963 ALOGD("Dropping inconsistent motion hover exit event: deviceId=%d, source=%08x",
Jeff Brown81346812011-06-28 20:08:48 -07003964 entry->deviceId, entry->source);
3965#endif
3966 return false;
3967 }
3968
3969 case AMOTION_EVENT_ACTION_HOVER_ENTER:
3970 case AMOTION_EVENT_ACTION_HOVER_MOVE: {
3971 ssize_t index = findMotionMemento(entry, true /*hovering*/);
3972 if (index >= 0) {
3973 mMotionMementos.removeAt(index);
3974 }
3975 addMotionMemento(entry, flags, true /*hovering*/);
3976 return true;
3977 }
3978
3979 default:
3980 return true;
3981 }
3982}
3983
3984ssize_t InputDispatcher::InputState::findKeyMemento(const KeyEntry* entry) const {
Jeff Brownb88102f2010-09-08 11:49:43 -07003985 for (size_t i = 0; i < mKeyMementos.size(); i++) {
Jeff Brown81346812011-06-28 20:08:48 -07003986 const KeyMemento& memento = mKeyMementos.itemAt(i);
Jeff Brownb88102f2010-09-08 11:49:43 -07003987 if (memento.deviceId == entry->deviceId
3988 && memento.source == entry->source
3989 && memento.keyCode == entry->keyCode
3990 && memento.scanCode == entry->scanCode) {
Jeff Brown81346812011-06-28 20:08:48 -07003991 return i;
Jeff Brownb88102f2010-09-08 11:49:43 -07003992 }
3993 }
Jeff Brown81346812011-06-28 20:08:48 -07003994 return -1;
Jeff Brownb88102f2010-09-08 11:49:43 -07003995}
3996
Jeff Brown81346812011-06-28 20:08:48 -07003997ssize_t InputDispatcher::InputState::findMotionMemento(const MotionEntry* entry,
3998 bool hovering) const {
Jeff Brownb88102f2010-09-08 11:49:43 -07003999 for (size_t i = 0; i < mMotionMementos.size(); i++) {
Jeff Brown81346812011-06-28 20:08:48 -07004000 const MotionMemento& memento = mMotionMementos.itemAt(i);
Jeff Brownb88102f2010-09-08 11:49:43 -07004001 if (memento.deviceId == entry->deviceId
Jeff Brown81346812011-06-28 20:08:48 -07004002 && memento.source == entry->source
4003 && memento.hovering == hovering) {
4004 return i;
Jeff Brownb88102f2010-09-08 11:49:43 -07004005 }
4006 }
Jeff Brown81346812011-06-28 20:08:48 -07004007 return -1;
4008}
Jeff Brownb88102f2010-09-08 11:49:43 -07004009
Jeff Brown81346812011-06-28 20:08:48 -07004010void InputDispatcher::InputState::addKeyMemento(const KeyEntry* entry, int32_t flags) {
4011 mKeyMementos.push();
4012 KeyMemento& memento = mKeyMementos.editTop();
4013 memento.deviceId = entry->deviceId;
4014 memento.source = entry->source;
4015 memento.keyCode = entry->keyCode;
4016 memento.scanCode = entry->scanCode;
Jeff Brownfd23e3e2012-05-09 13:34:28 -07004017 memento.metaState = entry->metaState;
Jeff Brown81346812011-06-28 20:08:48 -07004018 memento.flags = flags;
4019 memento.downTime = entry->downTime;
Jeff Brownfd23e3e2012-05-09 13:34:28 -07004020 memento.policyFlags = entry->policyFlags;
Jeff Brown81346812011-06-28 20:08:48 -07004021}
4022
4023void InputDispatcher::InputState::addMotionMemento(const MotionEntry* entry,
4024 int32_t flags, bool hovering) {
4025 mMotionMementos.push();
4026 MotionMemento& memento = mMotionMementos.editTop();
4027 memento.deviceId = entry->deviceId;
4028 memento.source = entry->source;
4029 memento.flags = flags;
4030 memento.xPrecision = entry->xPrecision;
4031 memento.yPrecision = entry->yPrecision;
4032 memento.downTime = entry->downTime;
4033 memento.setPointers(entry);
4034 memento.hovering = hovering;
Jeff Brownfd23e3e2012-05-09 13:34:28 -07004035 memento.policyFlags = entry->policyFlags;
Jeff Brownb88102f2010-09-08 11:49:43 -07004036}
4037
4038void InputDispatcher::InputState::MotionMemento::setPointers(const MotionEntry* entry) {
4039 pointerCount = entry->pointerCount;
4040 for (uint32_t i = 0; i < entry->pointerCount; i++) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004041 pointerProperties[i].copyFrom(entry->pointerProperties[i]);
Jeff Brown3241b6b2012-02-03 15:08:02 -08004042 pointerCoords[i].copyFrom(entry->pointerCoords[i]);
Jeff Brownb88102f2010-09-08 11:49:43 -07004043 }
4044}
4045
Jeff Brownb6997262010-10-08 22:31:17 -07004046void InputDispatcher::InputState::synthesizeCancelationEvents(nsecs_t currentTime,
Jeff Brownac386072011-07-20 15:19:50 -07004047 Vector<EventEntry*>& outEvents, const CancelationOptions& options) {
Jeff Brown81346812011-06-28 20:08:48 -07004048 for (size_t i = 0; i < mKeyMementos.size(); i++) {
Jeff Brownb88102f2010-09-08 11:49:43 -07004049 const KeyMemento& memento = mKeyMementos.itemAt(i);
Jeff Brown49ed71d2010-12-06 17:13:33 -08004050 if (shouldCancelKey(memento, options)) {
Jeff Brownac386072011-07-20 15:19:50 -07004051 outEvents.push(new KeyEntry(currentTime,
Jeff Brownfd23e3e2012-05-09 13:34:28 -07004052 memento.deviceId, memento.source, memento.policyFlags,
Jeff Brown49ed71d2010-12-06 17:13:33 -08004053 AKEY_EVENT_ACTION_UP, memento.flags | AKEY_EVENT_FLAG_CANCELED,
Jeff Brownfd23e3e2012-05-09 13:34:28 -07004054 memento.keyCode, memento.scanCode, memento.metaState, 0, memento.downTime));
Jeff Brownb6997262010-10-08 22:31:17 -07004055 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004056 }
4057
Jeff Brown81346812011-06-28 20:08:48 -07004058 for (size_t i = 0; i < mMotionMementos.size(); i++) {
Jeff Brownb88102f2010-09-08 11:49:43 -07004059 const MotionMemento& memento = mMotionMementos.itemAt(i);
Jeff Brown49ed71d2010-12-06 17:13:33 -08004060 if (shouldCancelMotion(memento, options)) {
Jeff Brownac386072011-07-20 15:19:50 -07004061 outEvents.push(new MotionEntry(currentTime,
Jeff Brownfd23e3e2012-05-09 13:34:28 -07004062 memento.deviceId, memento.source, memento.policyFlags,
Jeff Browna032cc02011-03-07 16:56:21 -08004063 memento.hovering
4064 ? AMOTION_EVENT_ACTION_HOVER_EXIT
4065 : AMOTION_EVENT_ACTION_CANCEL,
Jeff Brown81346812011-06-28 20:08:48 -07004066 memento.flags, 0, 0, 0,
Jeff Brownb6997262010-10-08 22:31:17 -07004067 memento.xPrecision, memento.yPrecision, memento.downTime,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004068 memento.pointerCount, memento.pointerProperties, memento.pointerCoords));
Jeff Brownb6997262010-10-08 22:31:17 -07004069 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004070 }
4071}
4072
4073void InputDispatcher::InputState::clear() {
4074 mKeyMementos.clear();
4075 mMotionMementos.clear();
Jeff Brownda3d5a92011-03-29 15:11:34 -07004076 mFallbackKeys.clear();
Jeff Brownb6997262010-10-08 22:31:17 -07004077}
4078
Jeff Brown9c9f1a32010-10-11 18:32:20 -07004079void InputDispatcher::InputState::copyPointerStateTo(InputState& other) const {
4080 for (size_t i = 0; i < mMotionMementos.size(); i++) {
4081 const MotionMemento& memento = mMotionMementos.itemAt(i);
4082 if (memento.source & AINPUT_SOURCE_CLASS_POINTER) {
4083 for (size_t j = 0; j < other.mMotionMementos.size(); ) {
4084 const MotionMemento& otherMemento = other.mMotionMementos.itemAt(j);
4085 if (memento.deviceId == otherMemento.deviceId
4086 && memento.source == otherMemento.source) {
4087 other.mMotionMementos.removeAt(j);
4088 } else {
4089 j += 1;
4090 }
4091 }
4092 other.mMotionMementos.push(memento);
4093 }
4094 }
4095}
4096
Jeff Brownda3d5a92011-03-29 15:11:34 -07004097int32_t InputDispatcher::InputState::getFallbackKey(int32_t originalKeyCode) {
4098 ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode);
4099 return index >= 0 ? mFallbackKeys.valueAt(index) : -1;
4100}
4101
4102void InputDispatcher::InputState::setFallbackKey(int32_t originalKeyCode,
4103 int32_t fallbackKeyCode) {
4104 ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode);
4105 if (index >= 0) {
4106 mFallbackKeys.replaceValueAt(index, fallbackKeyCode);
4107 } else {
4108 mFallbackKeys.add(originalKeyCode, fallbackKeyCode);
4109 }
4110}
4111
4112void InputDispatcher::InputState::removeFallbackKey(int32_t originalKeyCode) {
4113 mFallbackKeys.removeItem(originalKeyCode);
4114}
4115
Jeff Brown49ed71d2010-12-06 17:13:33 -08004116bool InputDispatcher::InputState::shouldCancelKey(const KeyMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -07004117 const CancelationOptions& options) {
4118 if (options.keyCode != -1 && memento.keyCode != options.keyCode) {
4119 return false;
4120 }
4121
Jeff Brown65fd2512011-08-18 11:20:58 -07004122 if (options.deviceId != -1 && memento.deviceId != options.deviceId) {
4123 return false;
4124 }
4125
Jeff Brownda3d5a92011-03-29 15:11:34 -07004126 switch (options.mode) {
4127 case CancelationOptions::CANCEL_ALL_EVENTS:
4128 case CancelationOptions::CANCEL_NON_POINTER_EVENTS:
Jeff Brownb6997262010-10-08 22:31:17 -07004129 return true;
Jeff Brownda3d5a92011-03-29 15:11:34 -07004130 case CancelationOptions::CANCEL_FALLBACK_EVENTS:
Jeff Brown49ed71d2010-12-06 17:13:33 -08004131 return memento.flags & AKEY_EVENT_FLAG_FALLBACK;
4132 default:
4133 return false;
4134 }
4135}
4136
4137bool InputDispatcher::InputState::shouldCancelMotion(const MotionMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -07004138 const CancelationOptions& options) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004139 if (options.deviceId != -1 && memento.deviceId != options.deviceId) {
4140 return false;
4141 }
4142
Jeff Brownda3d5a92011-03-29 15:11:34 -07004143 switch (options.mode) {
4144 case CancelationOptions::CANCEL_ALL_EVENTS:
Jeff Brown49ed71d2010-12-06 17:13:33 -08004145 return true;
Jeff Brownda3d5a92011-03-29 15:11:34 -07004146 case CancelationOptions::CANCEL_POINTER_EVENTS:
Jeff Brown49ed71d2010-12-06 17:13:33 -08004147 return memento.source & AINPUT_SOURCE_CLASS_POINTER;
Jeff Brownda3d5a92011-03-29 15:11:34 -07004148 case CancelationOptions::CANCEL_NON_POINTER_EVENTS:
Jeff Brown49ed71d2010-12-06 17:13:33 -08004149 return !(memento.source & AINPUT_SOURCE_CLASS_POINTER);
4150 default:
4151 return false;
Jeff Brownb6997262010-10-08 22:31:17 -07004152 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004153}
4154
4155
Jeff Brown46b9ac02010-04-22 18:58:52 -07004156// --- InputDispatcher::Connection ---
4157
Jeff Brown928e0542011-01-10 11:17:36 -08004158InputDispatcher::Connection::Connection(const sp<InputChannel>& inputChannel,
Jeff Browncc4f7db2011-08-30 20:34:48 -07004159 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) :
Jeff Brown928e0542011-01-10 11:17:36 -08004160 status(STATUS_NORMAL), inputChannel(inputChannel), inputWindowHandle(inputWindowHandle),
Jeff Browncc4f7db2011-08-30 20:34:48 -07004161 monitor(monitor),
Jeff Brownd1c48a02012-02-06 19:12:47 -08004162 inputPublisher(inputChannel), inputPublisherBlocked(false) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07004163}
4164
4165InputDispatcher::Connection::~Connection() {
4166}
4167
Jeff Brown481c1572012-03-09 14:41:15 -08004168const char* InputDispatcher::Connection::getWindowName() const {
4169 if (inputWindowHandle != NULL) {
4170 return inputWindowHandle->getName().string();
4171 }
4172 if (monitor) {
4173 return "monitor";
4174 }
4175 return "?";
4176}
4177
Jeff Brown9c3cda02010-06-15 01:31:58 -07004178const char* InputDispatcher::Connection::getStatusLabel() const {
4179 switch (status) {
4180 case STATUS_NORMAL:
4181 return "NORMAL";
4182
4183 case STATUS_BROKEN:
4184 return "BROKEN";
4185
Jeff Brown9c3cda02010-06-15 01:31:58 -07004186 case STATUS_ZOMBIE:
4187 return "ZOMBIE";
4188
4189 default:
4190 return "UNKNOWN";
4191 }
4192}
4193
Jeff Brown072ec962012-02-07 14:46:57 -08004194InputDispatcher::DispatchEntry* InputDispatcher::Connection::findWaitQueueEntry(uint32_t seq) {
4195 for (DispatchEntry* entry = waitQueue.head; entry != NULL; entry = entry->next) {
4196 if (entry->seq == seq) {
4197 return entry;
4198 }
4199 }
4200 return NULL;
4201}
4202
Jeff Brownb88102f2010-09-08 11:49:43 -07004203
Jeff Brown9c3cda02010-06-15 01:31:58 -07004204// --- InputDispatcher::CommandEntry ---
4205
Jeff Brownac386072011-07-20 15:19:50 -07004206InputDispatcher::CommandEntry::CommandEntry(Command command) :
Jeff Brown072ec962012-02-07 14:46:57 -08004207 command(command), eventTime(0), keyEntry(NULL), userActivityEventType(0),
4208 seq(0), handled(false) {
Jeff Brown9c3cda02010-06-15 01:31:58 -07004209}
4210
4211InputDispatcher::CommandEntry::~CommandEntry() {
4212}
4213
Jeff Brown46b9ac02010-04-22 18:58:52 -07004214
Jeff Brown01ce2e92010-09-26 22:20:12 -07004215// --- InputDispatcher::TouchState ---
4216
4217InputDispatcher::TouchState::TouchState() :
Jeff Brown58a2da82011-01-25 16:02:22 -08004218 down(false), split(false), deviceId(-1), source(0) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07004219}
4220
4221InputDispatcher::TouchState::~TouchState() {
4222}
4223
4224void InputDispatcher::TouchState::reset() {
4225 down = false;
4226 split = false;
Jeff Brown95712852011-01-04 19:41:59 -08004227 deviceId = -1;
Jeff Brown58a2da82011-01-25 16:02:22 -08004228 source = 0;
Jeff Brown01ce2e92010-09-26 22:20:12 -07004229 windows.clear();
4230}
4231
4232void InputDispatcher::TouchState::copyFrom(const TouchState& other) {
4233 down = other.down;
4234 split = other.split;
Jeff Brown95712852011-01-04 19:41:59 -08004235 deviceId = other.deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -08004236 source = other.source;
Jeff Brown9302c872011-07-13 22:51:29 -07004237 windows = other.windows;
Jeff Brown01ce2e92010-09-26 22:20:12 -07004238}
4239
Jeff Brown9302c872011-07-13 22:51:29 -07004240void InputDispatcher::TouchState::addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle,
Jeff Brown01ce2e92010-09-26 22:20:12 -07004241 int32_t targetFlags, BitSet32 pointerIds) {
4242 if (targetFlags & InputTarget::FLAG_SPLIT) {
4243 split = true;
4244 }
4245
4246 for (size_t i = 0; i < windows.size(); i++) {
4247 TouchedWindow& touchedWindow = windows.editItemAt(i);
Jeff Brown9302c872011-07-13 22:51:29 -07004248 if (touchedWindow.windowHandle == windowHandle) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07004249 touchedWindow.targetFlags |= targetFlags;
Jeff Brown98db5fa2011-06-08 15:37:10 -07004250 if (targetFlags & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) {
4251 touchedWindow.targetFlags &= ~InputTarget::FLAG_DISPATCH_AS_IS;
4252 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07004253 touchedWindow.pointerIds.value |= pointerIds.value;
4254 return;
4255 }
4256 }
4257
4258 windows.push();
4259
4260 TouchedWindow& touchedWindow = windows.editTop();
Jeff Brown9302c872011-07-13 22:51:29 -07004261 touchedWindow.windowHandle = windowHandle;
Jeff Brown01ce2e92010-09-26 22:20:12 -07004262 touchedWindow.targetFlags = targetFlags;
4263 touchedWindow.pointerIds = pointerIds;
Jeff Brown01ce2e92010-09-26 22:20:12 -07004264}
4265
Jeff Brownf44e3942012-04-20 11:33:27 -07004266void InputDispatcher::TouchState::removeWindow(const sp<InputWindowHandle>& windowHandle) {
4267 for (size_t i = 0; i < windows.size(); i++) {
4268 if (windows.itemAt(i).windowHandle == windowHandle) {
4269 windows.removeAt(i);
4270 return;
4271 }
4272 }
4273}
4274
Jeff Browna032cc02011-03-07 16:56:21 -08004275void InputDispatcher::TouchState::filterNonAsIsTouchWindows() {
Jeff Brown01ce2e92010-09-26 22:20:12 -07004276 for (size_t i = 0 ; i < windows.size(); ) {
Jeff Browna032cc02011-03-07 16:56:21 -08004277 TouchedWindow& window = windows.editItemAt(i);
Jeff Brown98db5fa2011-06-08 15:37:10 -07004278 if (window.targetFlags & (InputTarget::FLAG_DISPATCH_AS_IS
4279 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER)) {
Jeff Browna032cc02011-03-07 16:56:21 -08004280 window.targetFlags &= ~InputTarget::FLAG_DISPATCH_MASK;
4281 window.targetFlags |= InputTarget::FLAG_DISPATCH_AS_IS;
Jeff Brown01ce2e92010-09-26 22:20:12 -07004282 i += 1;
Jeff Browna032cc02011-03-07 16:56:21 -08004283 } else {
4284 windows.removeAt(i);
Jeff Brown01ce2e92010-09-26 22:20:12 -07004285 }
4286 }
4287}
4288
Jeff Brown9302c872011-07-13 22:51:29 -07004289sp<InputWindowHandle> InputDispatcher::TouchState::getFirstForegroundWindowHandle() const {
Jeff Brown01ce2e92010-09-26 22:20:12 -07004290 for (size_t i = 0; i < windows.size(); i++) {
Jeff Brown98db5fa2011-06-08 15:37:10 -07004291 const TouchedWindow& window = windows.itemAt(i);
4292 if (window.targetFlags & InputTarget::FLAG_FOREGROUND) {
Jeff Brown9302c872011-07-13 22:51:29 -07004293 return window.windowHandle;
Jeff Brown01ce2e92010-09-26 22:20:12 -07004294 }
4295 }
4296 return NULL;
4297}
4298
Jeff Brown98db5fa2011-06-08 15:37:10 -07004299bool InputDispatcher::TouchState::isSlippery() const {
4300 // Must have exactly one foreground window.
4301 bool haveSlipperyForegroundWindow = false;
4302 for (size_t i = 0; i < windows.size(); i++) {
4303 const TouchedWindow& window = windows.itemAt(i);
4304 if (window.targetFlags & InputTarget::FLAG_FOREGROUND) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07004305 if (haveSlipperyForegroundWindow
4306 || !(window.windowHandle->getInfo()->layoutParamsFlags
4307 & InputWindowInfo::FLAG_SLIPPERY)) {
Jeff Brown98db5fa2011-06-08 15:37:10 -07004308 return false;
4309 }
4310 haveSlipperyForegroundWindow = true;
4311 }
4312 }
4313 return haveSlipperyForegroundWindow;
4314}
4315
Jeff Brown01ce2e92010-09-26 22:20:12 -07004316
Jeff Brown46b9ac02010-04-22 18:58:52 -07004317// --- InputDispatcherThread ---
4318
4319InputDispatcherThread::InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher) :
4320 Thread(/*canCallJava*/ true), mDispatcher(dispatcher) {
4321}
4322
4323InputDispatcherThread::~InputDispatcherThread() {
4324}
4325
4326bool InputDispatcherThread::threadLoop() {
4327 mDispatcher->dispatchOnce();
4328 return true;
4329}
4330
4331} // namespace android