blob: 5811f2e42b6dd68230c16e0237b364f43c452d6e [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"
18
19//#define LOG_NDEBUG 0
20
21// Log detailed debug messages about each inbound event notification to the dispatcher.
Jeff Brown349703e2010-06-22 01:27:15 -070022#define DEBUG_INBOUND_EVENT_DETAILS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070023
24// Log detailed debug messages about each outbound event processed by the dispatcher.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_OUTBOUND_EVENT_DETAILS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070026
Jeff Brown46b9ac02010-04-22 18:58:52 -070027// Log debug messages about the dispatch cycle.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_DISPATCH_CYCLE 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070029
Jeff Brown9c3cda02010-06-15 01:31:58 -070030// Log debug messages about registrations.
Jeff Brown349703e2010-06-22 01:27:15 -070031#define DEBUG_REGISTRATION 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070032
Jeff Brown7fbdc842010-06-17 20:52:56 -070033// Log debug messages about input event injection.
Jeff Brown349703e2010-06-22 01:27:15 -070034#define DEBUG_INJECTION 0
Jeff Brown7fbdc842010-06-17 20:52:56 -070035
Jeff Brownb88102f2010-09-08 11:49:43 -070036// Log debug messages about input focus tracking.
37#define DEBUG_FOCUS 0
38
39// Log debug messages about the app switch latency optimization.
40#define DEBUG_APP_SWITCH 0
41
Jeff Browna032cc02011-03-07 16:56:21 -080042// Log debug messages about hover events.
43#define DEBUG_HOVER 0
44
Jeff Brownb4ff35d2011-01-02 16:37:43 -080045#include "InputDispatcher.h"
46
Jeff Brown46b9ac02010-04-22 18:58:52 -070047#include <cutils/log.h>
Jeff Brownb88102f2010-09-08 11:49:43 -070048#include <ui/PowerManager.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070049
50#include <stddef.h>
51#include <unistd.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070052#include <errno.h>
53#include <limits.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070054
Jeff Brownf2f48712010-10-01 17:46:21 -070055#define INDENT " "
56#define INDENT2 " "
57
Jeff Brown46b9ac02010-04-22 18:58:52 -070058namespace android {
59
Jeff Brownb88102f2010-09-08 11:49:43 -070060// Default input dispatching timeout if there is no focused application or paused window
61// from which to determine an appropriate dispatching timeout.
62const nsecs_t DEFAULT_INPUT_DISPATCHING_TIMEOUT = 5000 * 1000000LL; // 5 sec
63
64// Amount of time to allow for all pending events to be processed when an app switch
65// key is on the way. This is used to preempt input dispatch and drop input events
66// when an application takes too long to respond and the user has pressed an app switch key.
67const nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec
68
Jeff Brown928e0542011-01-10 11:17:36 -080069// Amount of time to allow for an event to be dispatched (measured since its eventTime)
70// before considering it stale and dropping it.
71const nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec
72
Jeff Brown46b9ac02010-04-22 18:58:52 -070073
Jeff Brown7fbdc842010-06-17 20:52:56 -070074static inline nsecs_t now() {
75 return systemTime(SYSTEM_TIME_MONOTONIC);
76}
77
Jeff Brownb88102f2010-09-08 11:49:43 -070078static inline const char* toString(bool value) {
79 return value ? "true" : "false";
80}
81
Jeff Brown01ce2e92010-09-26 22:20:12 -070082static inline int32_t getMotionEventActionPointerIndex(int32_t action) {
83 return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
84 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
85}
86
87static bool isValidKeyAction(int32_t action) {
88 switch (action) {
89 case AKEY_EVENT_ACTION_DOWN:
90 case AKEY_EVENT_ACTION_UP:
91 return true;
92 default:
93 return false;
94 }
95}
96
97static bool validateKeyEvent(int32_t action) {
98 if (! isValidKeyAction(action)) {
Steve Block3762c312012-01-06 19:20:56 +000099 ALOGE("Key event has invalid action code 0x%x", action);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700100 return false;
101 }
102 return true;
103}
104
Jeff Brownb6997262010-10-08 22:31:17 -0700105static bool isValidMotionAction(int32_t action, size_t pointerCount) {
Jeff Brown01ce2e92010-09-26 22:20:12 -0700106 switch (action & AMOTION_EVENT_ACTION_MASK) {
107 case AMOTION_EVENT_ACTION_DOWN:
108 case AMOTION_EVENT_ACTION_UP:
109 case AMOTION_EVENT_ACTION_CANCEL:
110 case AMOTION_EVENT_ACTION_MOVE:
Jeff Brown01ce2e92010-09-26 22:20:12 -0700111 case AMOTION_EVENT_ACTION_OUTSIDE:
Jeff Browna032cc02011-03-07 16:56:21 -0800112 case AMOTION_EVENT_ACTION_HOVER_ENTER:
Jeff Browncc0c1592011-02-19 05:07:28 -0800113 case AMOTION_EVENT_ACTION_HOVER_MOVE:
Jeff Browna032cc02011-03-07 16:56:21 -0800114 case AMOTION_EVENT_ACTION_HOVER_EXIT:
Jeff Brown33bbfd22011-02-24 20:55:35 -0800115 case AMOTION_EVENT_ACTION_SCROLL:
Jeff Brown01ce2e92010-09-26 22:20:12 -0700116 return true;
Jeff Brownb6997262010-10-08 22:31:17 -0700117 case AMOTION_EVENT_ACTION_POINTER_DOWN:
118 case AMOTION_EVENT_ACTION_POINTER_UP: {
119 int32_t index = getMotionEventActionPointerIndex(action);
120 return index >= 0 && size_t(index) < pointerCount;
121 }
Jeff Brown01ce2e92010-09-26 22:20:12 -0700122 default:
123 return false;
124 }
125}
126
127static bool validateMotionEvent(int32_t action, size_t pointerCount,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700128 const PointerProperties* pointerProperties) {
Jeff Brownb6997262010-10-08 22:31:17 -0700129 if (! isValidMotionAction(action, pointerCount)) {
Steve Block3762c312012-01-06 19:20:56 +0000130 ALOGE("Motion event has invalid action code 0x%x", action);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700131 return false;
132 }
133 if (pointerCount < 1 || pointerCount > MAX_POINTERS) {
Steve Block3762c312012-01-06 19:20:56 +0000134 ALOGE("Motion event has invalid pointer count %d; value must be between 1 and %d.",
Jeff Brown01ce2e92010-09-26 22:20:12 -0700135 pointerCount, MAX_POINTERS);
136 return false;
137 }
Jeff Brownc3db8582010-10-20 15:33:38 -0700138 BitSet32 pointerIdBits;
Jeff Brown01ce2e92010-09-26 22:20:12 -0700139 for (size_t i = 0; i < pointerCount; i++) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700140 int32_t id = pointerProperties[i].id;
Jeff Brownc3db8582010-10-20 15:33:38 -0700141 if (id < 0 || id > MAX_POINTER_ID) {
Steve Block3762c312012-01-06 19:20:56 +0000142 ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d",
Jeff Brownc3db8582010-10-20 15:33:38 -0700143 id, MAX_POINTER_ID);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700144 return false;
145 }
Jeff Brownc3db8582010-10-20 15:33:38 -0700146 if (pointerIdBits.hasBit(id)) {
Steve Block3762c312012-01-06 19:20:56 +0000147 ALOGE("Motion event has duplicate pointer id %d", id);
Jeff Brownc3db8582010-10-20 15:33:38 -0700148 return false;
149 }
150 pointerIdBits.markBit(id);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700151 }
152 return true;
153}
154
Jeff Brownfbf09772011-01-16 14:06:57 -0800155static void dumpRegion(String8& dump, const SkRegion& region) {
156 if (region.isEmpty()) {
157 dump.append("<empty>");
158 return;
159 }
160
161 bool first = true;
162 for (SkRegion::Iterator it(region); !it.done(); it.next()) {
163 if (first) {
164 first = false;
165 } else {
166 dump.append("|");
167 }
168 const SkIRect& rect = it.rect();
169 dump.appendFormat("[%d,%d][%d,%d]", rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
170 }
171}
172
Jeff Brownb88102f2010-09-08 11:49:43 -0700173
Jeff Brown46b9ac02010-04-22 18:58:52 -0700174// --- InputDispatcher ---
175
Jeff Brown9c3cda02010-06-15 01:31:58 -0700176InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) :
Jeff Brownb88102f2010-09-08 11:49:43 -0700177 mPolicy(policy),
Jeff Brown928e0542011-01-10 11:17:36 -0800178 mPendingEvent(NULL), mAppSwitchSawKeyDown(false), mAppSwitchDueTime(LONG_LONG_MAX),
179 mNextUnblockedEvent(NULL),
Jeff Brown0029c662011-03-30 02:25:18 -0700180 mDispatchEnabled(true), mDispatchFrozen(false), mInputFilterEnabled(false),
Jeff Brown9302c872011-07-13 22:51:29 -0700181 mInputTargetWaitCause(INPUT_TARGET_WAIT_CAUSE_NONE) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700182 mLooper = new Looper(false);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700183
Jeff Brown46b9ac02010-04-22 18:58:52 -0700184 mKeyRepeatState.lastKeyEntry = NULL;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700185
Jeff Brown214eaf42011-05-26 19:17:02 -0700186 policy->getDispatcherConfiguration(&mConfig);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700187}
188
189InputDispatcher::~InputDispatcher() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700190 { // acquire lock
191 AutoMutex _l(mLock);
192
193 resetKeyRepeatLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700194 releasePendingEventLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700195 drainInboundQueueLocked();
196 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700197
Jeff Browncbee6d62012-02-03 20:11:27 -0800198 while (mConnectionsByFd.size() != 0) {
199 unregisterInputChannel(mConnectionsByFd.valueAt(0)->inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700200 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700201}
202
203void InputDispatcher::dispatchOnce() {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700204 nsecs_t nextWakeupTime = LONG_LONG_MAX;
205 { // acquire lock
206 AutoMutex _l(mLock);
Jeff Brown112b5f52012-01-27 17:32:06 -0800207 mDispatcherIsAliveCondition.broadcast();
208
Jeff Brown214eaf42011-05-26 19:17:02 -0700209 dispatchOnceInnerLocked(&nextWakeupTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700210
Jeff Brownb88102f2010-09-08 11:49:43 -0700211 if (runCommandsLockedInterruptible()) {
212 nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately
Jeff Brown46b9ac02010-04-22 18:58:52 -0700213 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700214 } // release lock
215
Jeff Brownb88102f2010-09-08 11:49:43 -0700216 // Wait for callback or timeout or wake. (make sure we round up, not down)
217 nsecs_t currentTime = now();
Jeff Brownaa3855d2011-03-17 01:34:19 -0700218 int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700219 mLooper->pollOnce(timeoutMillis);
Jeff Brownb88102f2010-09-08 11:49:43 -0700220}
221
Jeff Brown214eaf42011-05-26 19:17:02 -0700222void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700223 nsecs_t currentTime = now();
224
225 // Reset the key repeat timer whenever we disallow key events, even if the next event
226 // is not a key. This is to ensure that we abort a key repeat if the device is just coming
227 // out of sleep.
Jeff Brown214eaf42011-05-26 19:17:02 -0700228 if (!mPolicy->isKeyRepeatEnabled()) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700229 resetKeyRepeatLocked();
230 }
231
Jeff Brownb88102f2010-09-08 11:49:43 -0700232 // If dispatching is frozen, do not process timeouts or try to deliver any new events.
233 if (mDispatchFrozen) {
234#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +0000235 ALOGD("Dispatch frozen. Waiting some more.");
Jeff Brownb88102f2010-09-08 11:49:43 -0700236#endif
237 return;
238 }
239
240 // Optimize latency of app switches.
241 // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has
242 // been pressed. When it expires, we preempt dispatch and drop all other pending events.
243 bool isAppSwitchDue = mAppSwitchDueTime <= currentTime;
244 if (mAppSwitchDueTime < *nextWakeupTime) {
245 *nextWakeupTime = mAppSwitchDueTime;
246 }
247
Jeff Brownb88102f2010-09-08 11:49:43 -0700248 // Ready to start a new event.
249 // If we don't already have a pending event, go grab one.
250 if (! mPendingEvent) {
251 if (mInboundQueue.isEmpty()) {
252 if (isAppSwitchDue) {
253 // The inbound queue is empty so the app switch key we were waiting
254 // for will never arrive. Stop waiting for it.
255 resetPendingAppSwitchLocked(false);
256 isAppSwitchDue = false;
257 }
258
259 // Synthesize a key repeat if appropriate.
260 if (mKeyRepeatState.lastKeyEntry) {
261 if (currentTime >= mKeyRepeatState.nextRepeatTime) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700262 mPendingEvent = synthesizeKeyRepeatLocked(currentTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700263 } else {
264 if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) {
265 *nextWakeupTime = mKeyRepeatState.nextRepeatTime;
266 }
267 }
268 }
Jeff Browncc4f7db2011-08-30 20:34:48 -0700269
270 // Nothing to do if there is no pending event.
Jeff Browne9bb9be2012-02-06 15:47:55 -0800271 if (!mPendingEvent) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700272 return;
273 }
274 } else {
275 // Inbound queue has at least one entry.
Jeff Browne9bb9be2012-02-06 15:47:55 -0800276 mPendingEvent = mInboundQueue.dequeueAtHead();
Jeff Brownb88102f2010-09-08 11:49:43 -0700277 }
Jeff Browne2fe69e2010-10-18 13:21:23 -0700278
279 // Poke user activity for this event.
280 if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) {
281 pokeUserActivityLocked(mPendingEvent);
282 }
Jeff Browne9bb9be2012-02-06 15:47:55 -0800283
284 // Get ready to dispatch the event.
285 resetANRTimeoutsLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700286 }
287
288 // Now we have an event to dispatch.
Jeff Brown928e0542011-01-10 11:17:36 -0800289 // All events are eventually dequeued and processed this way, even if we intend to drop them.
Steve Blockec193de2012-01-09 18:35:44 +0000290 ALOG_ASSERT(mPendingEvent != NULL);
Jeff Brown54a18252010-09-16 14:07:33 -0700291 bool done = false;
Jeff Brownb6997262010-10-08 22:31:17 -0700292 DropReason dropReason = DROP_REASON_NOT_DROPPED;
293 if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) {
294 dropReason = DROP_REASON_POLICY;
295 } else if (!mDispatchEnabled) {
296 dropReason = DROP_REASON_DISABLED;
297 }
Jeff Brown928e0542011-01-10 11:17:36 -0800298
299 if (mNextUnblockedEvent == mPendingEvent) {
300 mNextUnblockedEvent = NULL;
301 }
302
Jeff Brownb88102f2010-09-08 11:49:43 -0700303 switch (mPendingEvent->type) {
304 case EventEntry::TYPE_CONFIGURATION_CHANGED: {
305 ConfigurationChangedEntry* typedEntry =
306 static_cast<ConfigurationChangedEntry*>(mPendingEvent);
Jeff Brown54a18252010-09-16 14:07:33 -0700307 done = dispatchConfigurationChangedLocked(currentTime, typedEntry);
Jeff Brownb6997262010-10-08 22:31:17 -0700308 dropReason = DROP_REASON_NOT_DROPPED; // configuration changes are never dropped
Jeff Brownb88102f2010-09-08 11:49:43 -0700309 break;
310 }
311
Jeff Brown65fd2512011-08-18 11:20:58 -0700312 case EventEntry::TYPE_DEVICE_RESET: {
313 DeviceResetEntry* typedEntry =
314 static_cast<DeviceResetEntry*>(mPendingEvent);
315 done = dispatchDeviceResetLocked(currentTime, typedEntry);
316 dropReason = DROP_REASON_NOT_DROPPED; // device resets are never dropped
317 break;
318 }
319
Jeff Brownb88102f2010-09-08 11:49:43 -0700320 case EventEntry::TYPE_KEY: {
321 KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent);
Jeff Brownb6997262010-10-08 22:31:17 -0700322 if (isAppSwitchDue) {
323 if (isAppSwitchKeyEventLocked(typedEntry)) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700324 resetPendingAppSwitchLocked(true);
Jeff Brownb6997262010-10-08 22:31:17 -0700325 isAppSwitchDue = false;
326 } else if (dropReason == DROP_REASON_NOT_DROPPED) {
327 dropReason = DROP_REASON_APP_SWITCH;
Jeff Brownb88102f2010-09-08 11:49:43 -0700328 }
329 }
Jeff Brown928e0542011-01-10 11:17:36 -0800330 if (dropReason == DROP_REASON_NOT_DROPPED
331 && isStaleEventLocked(currentTime, typedEntry)) {
332 dropReason = DROP_REASON_STALE;
333 }
334 if (dropReason == DROP_REASON_NOT_DROPPED && mNextUnblockedEvent) {
335 dropReason = DROP_REASON_BLOCKED;
336 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700337 done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700338 break;
339 }
340
341 case EventEntry::TYPE_MOTION: {
342 MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent);
Jeff Brownb6997262010-10-08 22:31:17 -0700343 if (dropReason == DROP_REASON_NOT_DROPPED && isAppSwitchDue) {
344 dropReason = DROP_REASON_APP_SWITCH;
Jeff Brownb88102f2010-09-08 11:49:43 -0700345 }
Jeff Brown928e0542011-01-10 11:17:36 -0800346 if (dropReason == DROP_REASON_NOT_DROPPED
347 && isStaleEventLocked(currentTime, typedEntry)) {
348 dropReason = DROP_REASON_STALE;
349 }
350 if (dropReason == DROP_REASON_NOT_DROPPED && mNextUnblockedEvent) {
351 dropReason = DROP_REASON_BLOCKED;
352 }
Jeff Brownb6997262010-10-08 22:31:17 -0700353 done = dispatchMotionLocked(currentTime, typedEntry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700354 &dropReason, nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700355 break;
356 }
357
358 default:
Steve Blockec193de2012-01-09 18:35:44 +0000359 ALOG_ASSERT(false);
Jeff Brownb88102f2010-09-08 11:49:43 -0700360 break;
361 }
362
Jeff Brown54a18252010-09-16 14:07:33 -0700363 if (done) {
Jeff Brownb6997262010-10-08 22:31:17 -0700364 if (dropReason != DROP_REASON_NOT_DROPPED) {
365 dropInboundEventLocked(mPendingEvent, dropReason);
366 }
367
Jeff Brown54a18252010-09-16 14:07:33 -0700368 releasePendingEventLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700369 *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately
370 }
371}
372
373bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) {
374 bool needWake = mInboundQueue.isEmpty();
375 mInboundQueue.enqueueAtTail(entry);
376
377 switch (entry->type) {
Jeff Brownb6997262010-10-08 22:31:17 -0700378 case EventEntry::TYPE_KEY: {
Jeff Brown928e0542011-01-10 11:17:36 -0800379 // Optimize app switch latency.
380 // If the application takes too long to catch up then we drop all events preceding
381 // the app switch key.
Jeff Brownb6997262010-10-08 22:31:17 -0700382 KeyEntry* keyEntry = static_cast<KeyEntry*>(entry);
383 if (isAppSwitchKeyEventLocked(keyEntry)) {
384 if (keyEntry->action == AKEY_EVENT_ACTION_DOWN) {
385 mAppSwitchSawKeyDown = true;
386 } else if (keyEntry->action == AKEY_EVENT_ACTION_UP) {
387 if (mAppSwitchSawKeyDown) {
388#if DEBUG_APP_SWITCH
Steve Block5baa3a62011-12-20 16:23:08 +0000389 ALOGD("App switch is pending!");
Jeff Brownb6997262010-10-08 22:31:17 -0700390#endif
391 mAppSwitchDueTime = keyEntry->eventTime + APP_SWITCH_TIMEOUT;
392 mAppSwitchSawKeyDown = false;
393 needWake = true;
394 }
395 }
396 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700397 break;
398 }
Jeff Brown928e0542011-01-10 11:17:36 -0800399
400 case EventEntry::TYPE_MOTION: {
401 // Optimize case where the current application is unresponsive and the user
402 // decides to touch a window in a different application.
403 // If the application takes too long to catch up then we drop all events preceding
404 // the touch into the other window.
405 MotionEntry* motionEntry = static_cast<MotionEntry*>(entry);
Jeff Brown33bbfd22011-02-24 20:55:35 -0800406 if (motionEntry->action == AMOTION_EVENT_ACTION_DOWN
Jeff Brown928e0542011-01-10 11:17:36 -0800407 && (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER)
408 && mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY
Jeff Brown9302c872011-07-13 22:51:29 -0700409 && mInputTargetWaitApplicationHandle != NULL) {
Jeff Brown3241b6b2012-02-03 15:08:02 -0800410 int32_t x = int32_t(motionEntry->pointerCoords[0].
Jeff Brownebbd5d12011-02-17 13:01:34 -0800411 getAxisValue(AMOTION_EVENT_AXIS_X));
Jeff Brown3241b6b2012-02-03 15:08:02 -0800412 int32_t y = int32_t(motionEntry->pointerCoords[0].
Jeff Brownebbd5d12011-02-17 13:01:34 -0800413 getAxisValue(AMOTION_EVENT_AXIS_Y));
Jeff Brown9302c872011-07-13 22:51:29 -0700414 sp<InputWindowHandle> touchedWindowHandle = findTouchedWindowAtLocked(x, y);
415 if (touchedWindowHandle != NULL
416 && touchedWindowHandle->inputApplicationHandle
417 != mInputTargetWaitApplicationHandle) {
Jeff Brown928e0542011-01-10 11:17:36 -0800418 // User touched a different application than the one we are waiting on.
419 // Flag the event, and start pruning the input queue.
420 mNextUnblockedEvent = motionEntry;
421 needWake = true;
422 }
423 }
424 break;
425 }
Jeff Brownb6997262010-10-08 22:31:17 -0700426 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700427
428 return needWake;
429}
430
Jeff Brown9302c872011-07-13 22:51:29 -0700431sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t x, int32_t y) {
Jeff Brown928e0542011-01-10 11:17:36 -0800432 // Traverse windows from front to back to find touched window.
Jeff Brown9302c872011-07-13 22:51:29 -0700433 size_t numWindows = mWindowHandles.size();
Jeff Brown928e0542011-01-10 11:17:36 -0800434 for (size_t i = 0; i < numWindows; i++) {
Jeff Brown9302c872011-07-13 22:51:29 -0700435 sp<InputWindowHandle> windowHandle = mWindowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700436 const InputWindowInfo* windowInfo = windowHandle->getInfo();
437 int32_t flags = windowInfo->layoutParamsFlags;
Jeff Brown928e0542011-01-10 11:17:36 -0800438
Jeff Browncc4f7db2011-08-30 20:34:48 -0700439 if (windowInfo->visible) {
440 if (!(flags & InputWindowInfo::FLAG_NOT_TOUCHABLE)) {
441 bool isTouchModal = (flags & (InputWindowInfo::FLAG_NOT_FOCUSABLE
442 | InputWindowInfo::FLAG_NOT_TOUCH_MODAL)) == 0;
443 if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) {
Jeff Brown928e0542011-01-10 11:17:36 -0800444 // Found window.
Jeff Brown9302c872011-07-13 22:51:29 -0700445 return windowHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800446 }
447 }
448 }
449
Jeff Browncc4f7db2011-08-30 20:34:48 -0700450 if (flags & InputWindowInfo::FLAG_SYSTEM_ERROR) {
Jeff Brown928e0542011-01-10 11:17:36 -0800451 // Error window is on top but not visible, so touch is dropped.
452 return NULL;
453 }
454 }
455 return NULL;
456}
457
Jeff Brownb6997262010-10-08 22:31:17 -0700458void InputDispatcher::dropInboundEventLocked(EventEntry* entry, DropReason dropReason) {
459 const char* reason;
460 switch (dropReason) {
461 case DROP_REASON_POLICY:
Jeff Browne20c9e02010-10-11 14:20:19 -0700462#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +0000463 ALOGD("Dropped event because policy consumed it.");
Jeff Browne20c9e02010-10-11 14:20:19 -0700464#endif
Jeff Brown3122e442010-10-11 23:32:49 -0700465 reason = "inbound event was dropped because the policy consumed it";
Jeff Brownb6997262010-10-08 22:31:17 -0700466 break;
467 case DROP_REASON_DISABLED:
Steve Block6215d3f2012-01-04 20:05:49 +0000468 ALOGI("Dropped event because input dispatch is disabled.");
Jeff Brownb6997262010-10-08 22:31:17 -0700469 reason = "inbound event was dropped because input dispatch is disabled";
470 break;
471 case DROP_REASON_APP_SWITCH:
Steve Block6215d3f2012-01-04 20:05:49 +0000472 ALOGI("Dropped event because of pending overdue app switch.");
Jeff Brownb6997262010-10-08 22:31:17 -0700473 reason = "inbound event was dropped because of pending overdue app switch";
474 break;
Jeff Brown928e0542011-01-10 11:17:36 -0800475 case DROP_REASON_BLOCKED:
Steve Block6215d3f2012-01-04 20:05:49 +0000476 ALOGI("Dropped event because the current application is not responding and the user "
Jeff Brown81346812011-06-28 20:08:48 -0700477 "has started interacting with a different application.");
Jeff Brown928e0542011-01-10 11:17:36 -0800478 reason = "inbound event was dropped because the current application is not responding "
Jeff Brown81346812011-06-28 20:08:48 -0700479 "and the user has started interacting with a different application";
Jeff Brown928e0542011-01-10 11:17:36 -0800480 break;
481 case DROP_REASON_STALE:
Steve Block6215d3f2012-01-04 20:05:49 +0000482 ALOGI("Dropped event because it is stale.");
Jeff Brown928e0542011-01-10 11:17:36 -0800483 reason = "inbound event was dropped because it is stale";
484 break;
Jeff Brownb6997262010-10-08 22:31:17 -0700485 default:
Steve Blockec193de2012-01-09 18:35:44 +0000486 ALOG_ASSERT(false);
Jeff Brownb6997262010-10-08 22:31:17 -0700487 return;
488 }
489
490 switch (entry->type) {
Jeff Brownda3d5a92011-03-29 15:11:34 -0700491 case EventEntry::TYPE_KEY: {
492 CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason);
493 synthesizeCancelationEventsForAllConnectionsLocked(options);
Jeff Brownb6997262010-10-08 22:31:17 -0700494 break;
Jeff Brownda3d5a92011-03-29 15:11:34 -0700495 }
Jeff Brownb6997262010-10-08 22:31:17 -0700496 case EventEntry::TYPE_MOTION: {
497 MotionEntry* motionEntry = static_cast<MotionEntry*>(entry);
498 if (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) {
Jeff Brownda3d5a92011-03-29 15:11:34 -0700499 CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason);
500 synthesizeCancelationEventsForAllConnectionsLocked(options);
Jeff Brownb6997262010-10-08 22:31:17 -0700501 } else {
Jeff Brownda3d5a92011-03-29 15:11:34 -0700502 CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason);
503 synthesizeCancelationEventsForAllConnectionsLocked(options);
Jeff Brownb6997262010-10-08 22:31:17 -0700504 }
505 break;
506 }
507 }
508}
509
510bool InputDispatcher::isAppSwitchKeyCode(int32_t keyCode) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700511 return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL;
512}
513
Jeff Brownb6997262010-10-08 22:31:17 -0700514bool InputDispatcher::isAppSwitchKeyEventLocked(KeyEntry* keyEntry) {
515 return ! (keyEntry->flags & AKEY_EVENT_FLAG_CANCELED)
516 && isAppSwitchKeyCode(keyEntry->keyCode)
Jeff Browne20c9e02010-10-11 14:20:19 -0700517 && (keyEntry->policyFlags & POLICY_FLAG_TRUSTED)
Jeff Brownb6997262010-10-08 22:31:17 -0700518 && (keyEntry->policyFlags & POLICY_FLAG_PASS_TO_USER);
519}
520
Jeff Brownb88102f2010-09-08 11:49:43 -0700521bool InputDispatcher::isAppSwitchPendingLocked() {
522 return mAppSwitchDueTime != LONG_LONG_MAX;
523}
524
Jeff Brownb88102f2010-09-08 11:49:43 -0700525void InputDispatcher::resetPendingAppSwitchLocked(bool handled) {
526 mAppSwitchDueTime = LONG_LONG_MAX;
527
528#if DEBUG_APP_SWITCH
529 if (handled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000530 ALOGD("App switch has arrived.");
Jeff Brownb88102f2010-09-08 11:49:43 -0700531 } else {
Steve Block5baa3a62011-12-20 16:23:08 +0000532 ALOGD("App switch was abandoned.");
Jeff Brownb88102f2010-09-08 11:49:43 -0700533 }
534#endif
Jeff Brown46b9ac02010-04-22 18:58:52 -0700535}
536
Jeff Brown928e0542011-01-10 11:17:36 -0800537bool InputDispatcher::isStaleEventLocked(nsecs_t currentTime, EventEntry* entry) {
538 return currentTime - entry->eventTime >= STALE_EVENT_TIMEOUT;
539}
540
Jeff Brown9c3cda02010-06-15 01:31:58 -0700541bool InputDispatcher::runCommandsLockedInterruptible() {
542 if (mCommandQueue.isEmpty()) {
543 return false;
544 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700545
Jeff Brown9c3cda02010-06-15 01:31:58 -0700546 do {
547 CommandEntry* commandEntry = mCommandQueue.dequeueAtHead();
548
549 Command command = commandEntry->command;
550 (this->*command)(commandEntry); // commands are implicitly 'LockedInterruptible'
551
Jeff Brown7fbdc842010-06-17 20:52:56 -0700552 commandEntry->connection.clear();
Jeff Brownac386072011-07-20 15:19:50 -0700553 delete commandEntry;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700554 } while (! mCommandQueue.isEmpty());
555 return true;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700556}
557
Jeff Brown9c3cda02010-06-15 01:31:58 -0700558InputDispatcher::CommandEntry* InputDispatcher::postCommandLocked(Command command) {
Jeff Brownac386072011-07-20 15:19:50 -0700559 CommandEntry* commandEntry = new CommandEntry(command);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700560 mCommandQueue.enqueueAtTail(commandEntry);
561 return commandEntry;
562}
563
Jeff Brownb88102f2010-09-08 11:49:43 -0700564void InputDispatcher::drainInboundQueueLocked() {
565 while (! mInboundQueue.isEmpty()) {
566 EventEntry* entry = mInboundQueue.dequeueAtHead();
Jeff Brown54a18252010-09-16 14:07:33 -0700567 releaseInboundEventLocked(entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700568 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700569}
570
Jeff Brown54a18252010-09-16 14:07:33 -0700571void InputDispatcher::releasePendingEventLocked() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700572 if (mPendingEvent) {
Jeff Browne9bb9be2012-02-06 15:47:55 -0800573 resetANRTimeoutsLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700574 releaseInboundEventLocked(mPendingEvent);
Jeff Brownb88102f2010-09-08 11:49:43 -0700575 mPendingEvent = NULL;
576 }
577}
578
Jeff Brown54a18252010-09-16 14:07:33 -0700579void InputDispatcher::releaseInboundEventLocked(EventEntry* entry) {
Jeff Brown01ce2e92010-09-26 22:20:12 -0700580 InjectionState* injectionState = entry->injectionState;
581 if (injectionState && injectionState->injectionResult == INPUT_EVENT_INJECTION_PENDING) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700582#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +0000583 ALOGD("Injected inbound event was dropped.");
Jeff Brownb88102f2010-09-08 11:49:43 -0700584#endif
585 setInjectionResultLocked(entry, INPUT_EVENT_INJECTION_FAILED);
586 }
Jeff Brownabb4d442011-08-15 12:55:32 -0700587 if (entry == mNextUnblockedEvent) {
588 mNextUnblockedEvent = NULL;
589 }
Jeff Brownac386072011-07-20 15:19:50 -0700590 entry->release();
Jeff Brownb88102f2010-09-08 11:49:43 -0700591}
592
Jeff Brownb88102f2010-09-08 11:49:43 -0700593void InputDispatcher::resetKeyRepeatLocked() {
594 if (mKeyRepeatState.lastKeyEntry) {
Jeff Brownac386072011-07-20 15:19:50 -0700595 mKeyRepeatState.lastKeyEntry->release();
Jeff Brownb88102f2010-09-08 11:49:43 -0700596 mKeyRepeatState.lastKeyEntry = NULL;
597 }
598}
599
Jeff Brown214eaf42011-05-26 19:17:02 -0700600InputDispatcher::KeyEntry* InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) {
Jeff Brown349703e2010-06-22 01:27:15 -0700601 KeyEntry* entry = mKeyRepeatState.lastKeyEntry;
602
Jeff Brown349703e2010-06-22 01:27:15 -0700603 // Reuse the repeated key entry if it is otherwise unreferenced.
Jeff Browne20c9e02010-10-11 14:20:19 -0700604 uint32_t policyFlags = (entry->policyFlags & POLICY_FLAG_RAW_MASK)
605 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700606 if (entry->refCount == 1) {
Jeff Brownac386072011-07-20 15:19:50 -0700607 entry->recycle();
Jeff Brown7fbdc842010-06-17 20:52:56 -0700608 entry->eventTime = currentTime;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700609 entry->policyFlags = policyFlags;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700610 entry->repeatCount += 1;
611 } else {
Jeff Brownac386072011-07-20 15:19:50 -0700612 KeyEntry* newEntry = new KeyEntry(currentTime,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700613 entry->deviceId, entry->source, policyFlags,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700614 entry->action, entry->flags, entry->keyCode, entry->scanCode,
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700615 entry->metaState, entry->repeatCount + 1, entry->downTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700616
617 mKeyRepeatState.lastKeyEntry = newEntry;
Jeff Brownac386072011-07-20 15:19:50 -0700618 entry->release();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700619
620 entry = newEntry;
621 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700622 entry->syntheticRepeat = true;
623
624 // Increment reference count since we keep a reference to the event in
625 // mKeyRepeatState.lastKeyEntry in addition to the one we return.
626 entry->refCount += 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700627
Jeff Brown214eaf42011-05-26 19:17:02 -0700628 mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay;
Jeff Brownb88102f2010-09-08 11:49:43 -0700629 return entry;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700630}
631
Jeff Brownb88102f2010-09-08 11:49:43 -0700632bool InputDispatcher::dispatchConfigurationChangedLocked(
633 nsecs_t currentTime, ConfigurationChangedEntry* entry) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700634#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +0000635 ALOGD("dispatchConfigurationChanged - eventTime=%lld", entry->eventTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700636#endif
637
638 // Reset key repeating in case a keyboard device was added or removed or something.
639 resetKeyRepeatLocked();
640
641 // Enqueue a command to run outside the lock to tell the policy that the configuration changed.
642 CommandEntry* commandEntry = postCommandLocked(
643 & InputDispatcher::doNotifyConfigurationChangedInterruptible);
644 commandEntry->eventTime = entry->eventTime;
645 return true;
646}
647
Jeff Brown65fd2512011-08-18 11:20:58 -0700648bool InputDispatcher::dispatchDeviceResetLocked(
649 nsecs_t currentTime, DeviceResetEntry* entry) {
650#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +0000651 ALOGD("dispatchDeviceReset - eventTime=%lld, deviceId=%d", entry->eventTime, entry->deviceId);
Jeff Brown65fd2512011-08-18 11:20:58 -0700652#endif
653
654 CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS,
655 "device was reset");
656 options.deviceId = entry->deviceId;
657 synthesizeCancelationEventsForAllConnectionsLocked(options);
658 return true;
659}
660
Jeff Brown214eaf42011-05-26 19:17:02 -0700661bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700662 DropReason* dropReason, nsecs_t* nextWakeupTime) {
Jeff Browne46a0a42010-11-02 17:58:22 -0700663 // Preprocessing.
664 if (! entry->dispatchInProgress) {
665 if (entry->repeatCount == 0
666 && entry->action == AKEY_EVENT_ACTION_DOWN
667 && (entry->policyFlags & POLICY_FLAG_TRUSTED)
Jeff Brown0029c662011-03-30 02:25:18 -0700668 && (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) {
Jeff Browne46a0a42010-11-02 17:58:22 -0700669 if (mKeyRepeatState.lastKeyEntry
670 && mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode) {
671 // We have seen two identical key downs in a row which indicates that the device
672 // driver is automatically generating key repeats itself. We take note of the
673 // repeat here, but we disable our own next key repeat timer since it is clear that
674 // we will not need to synthesize key repeats ourselves.
675 entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1;
676 resetKeyRepeatLocked();
677 mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves
678 } else {
679 // Not a repeat. Save key down state in case we do see a repeat later.
680 resetKeyRepeatLocked();
Jeff Brown214eaf42011-05-26 19:17:02 -0700681 mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout;
Jeff Browne46a0a42010-11-02 17:58:22 -0700682 }
683 mKeyRepeatState.lastKeyEntry = entry;
684 entry->refCount += 1;
685 } else if (! entry->syntheticRepeat) {
686 resetKeyRepeatLocked();
687 }
688
Jeff Browne2e01262011-03-02 20:34:30 -0800689 if (entry->repeatCount == 1) {
690 entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS;
691 } else {
692 entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS;
693 }
694
Jeff Browne46a0a42010-11-02 17:58:22 -0700695 entry->dispatchInProgress = true;
Jeff Browne46a0a42010-11-02 17:58:22 -0700696
697 logOutboundKeyDetailsLocked("dispatchKey - ", entry);
698 }
699
Jeff Brown905805a2011-10-12 13:57:59 -0700700 // Handle case where the policy asked us to try again later last time.
701 if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) {
702 if (currentTime < entry->interceptKeyWakeupTime) {
703 if (entry->interceptKeyWakeupTime < *nextWakeupTime) {
704 *nextWakeupTime = entry->interceptKeyWakeupTime;
705 }
706 return false; // wait until next wakeup
707 }
708 entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN;
709 entry->interceptKeyWakeupTime = 0;
710 }
711
Jeff Brown54a18252010-09-16 14:07:33 -0700712 // Give the policy a chance to intercept the key.
713 if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700714 if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) {
Jeff Brown54a18252010-09-16 14:07:33 -0700715 CommandEntry* commandEntry = postCommandLocked(
716 & InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible);
Jeff Brown9302c872011-07-13 22:51:29 -0700717 if (mFocusedWindowHandle != NULL) {
718 commandEntry->inputWindowHandle = mFocusedWindowHandle;
Jeff Brown54a18252010-09-16 14:07:33 -0700719 }
720 commandEntry->keyEntry = entry;
721 entry->refCount += 1;
722 return false; // wait for the command to run
723 } else {
724 entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE;
725 }
726 } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700727 if (*dropReason == DROP_REASON_NOT_DROPPED) {
728 *dropReason = DROP_REASON_POLICY;
729 }
Jeff Brown54a18252010-09-16 14:07:33 -0700730 }
731
732 // Clean up if dropping the event.
Jeff Browne20c9e02010-10-11 14:20:19 -0700733 if (*dropReason != DROP_REASON_NOT_DROPPED) {
Jeff Brown3122e442010-10-11 23:32:49 -0700734 setInjectionResultLocked(entry, *dropReason == DROP_REASON_POLICY
735 ? INPUT_EVENT_INJECTION_SUCCEEDED : INPUT_EVENT_INJECTION_FAILED);
Jeff Brown54a18252010-09-16 14:07:33 -0700736 return true;
737 }
738
Jeff Brownb88102f2010-09-08 11:49:43 -0700739 // Identify targets.
Jeff Browne9bb9be2012-02-06 15:47:55 -0800740 Vector<InputTarget> inputTargets;
741 int32_t injectionResult = findFocusedWindowTargetsLocked(currentTime,
742 entry, inputTargets, nextWakeupTime);
743 if (injectionResult == INPUT_EVENT_INJECTION_PENDING) {
744 return false;
Jeff Brownb88102f2010-09-08 11:49:43 -0700745 }
746
Jeff Browne9bb9be2012-02-06 15:47:55 -0800747 setInjectionResultLocked(entry, injectionResult);
748 if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) {
749 return true;
750 }
751
752 addMonitoringTargetsLocked(inputTargets);
753
Jeff Brownb88102f2010-09-08 11:49:43 -0700754 // Dispatch the key.
Jeff Browne9bb9be2012-02-06 15:47:55 -0800755 dispatchEventLocked(currentTime, entry, inputTargets);
Jeff Brownb88102f2010-09-08 11:49:43 -0700756 return true;
757}
758
759void InputDispatcher::logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry) {
760#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +0000761 ALOGD("%seventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, "
Jeff Brownb88102f2010-09-08 11:49:43 -0700762 "action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, "
Jeff Browne46a0a42010-11-02 17:58:22 -0700763 "repeatCount=%d, downTime=%lld",
Jeff Brownb88102f2010-09-08 11:49:43 -0700764 prefix,
765 entry->eventTime, entry->deviceId, entry->source, entry->policyFlags,
766 entry->action, entry->flags, entry->keyCode, entry->scanCode, entry->metaState,
Jeff Browne46a0a42010-11-02 17:58:22 -0700767 entry->repeatCount, entry->downTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700768#endif
769}
770
771bool InputDispatcher::dispatchMotionLocked(
Jeff Browne20c9e02010-10-11 14:20:19 -0700772 nsecs_t currentTime, MotionEntry* entry, DropReason* dropReason, nsecs_t* nextWakeupTime) {
Jeff Browne46a0a42010-11-02 17:58:22 -0700773 // Preprocessing.
774 if (! entry->dispatchInProgress) {
775 entry->dispatchInProgress = true;
Jeff Browne46a0a42010-11-02 17:58:22 -0700776
777 logOutboundMotionDetailsLocked("dispatchMotion - ", entry);
778 }
779
Jeff Brown54a18252010-09-16 14:07:33 -0700780 // Clean up if dropping the event.
Jeff Browne20c9e02010-10-11 14:20:19 -0700781 if (*dropReason != DROP_REASON_NOT_DROPPED) {
Jeff Brown3122e442010-10-11 23:32:49 -0700782 setInjectionResultLocked(entry, *dropReason == DROP_REASON_POLICY
783 ? INPUT_EVENT_INJECTION_SUCCEEDED : INPUT_EVENT_INJECTION_FAILED);
Jeff Brown54a18252010-09-16 14:07:33 -0700784 return true;
785 }
786
Jeff Brownb88102f2010-09-08 11:49:43 -0700787 bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER;
788
789 // Identify targets.
Jeff Browne9bb9be2012-02-06 15:47:55 -0800790 Vector<InputTarget> inputTargets;
791
Jeff Browncc0c1592011-02-19 05:07:28 -0800792 bool conflictingPointerActions = false;
Jeff Browne9bb9be2012-02-06 15:47:55 -0800793 int32_t injectionResult;
794 if (isPointerEvent) {
795 // Pointer event. (eg. touchscreen)
796 injectionResult = findTouchedWindowTargetsLocked(currentTime,
797 entry, inputTargets, nextWakeupTime, &conflictingPointerActions);
798 } else {
799 // Non touch event. (eg. trackball)
800 injectionResult = findFocusedWindowTargetsLocked(currentTime,
801 entry, inputTargets, nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700802 }
Jeff Browne9bb9be2012-02-06 15:47:55 -0800803 if (injectionResult == INPUT_EVENT_INJECTION_PENDING) {
804 return false;
805 }
806
807 setInjectionResultLocked(entry, injectionResult);
808 if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) {
809 return true;
810 }
811
812 addMonitoringTargetsLocked(inputTargets);
Jeff Brownb88102f2010-09-08 11:49:43 -0700813
814 // Dispatch the motion.
Jeff Browncc0c1592011-02-19 05:07:28 -0800815 if (conflictingPointerActions) {
Jeff Brownda3d5a92011-03-29 15:11:34 -0700816 CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS,
817 "conflicting pointer actions");
818 synthesizeCancelationEventsForAllConnectionsLocked(options);
Jeff Browncc0c1592011-02-19 05:07:28 -0800819 }
Jeff Browne9bb9be2012-02-06 15:47:55 -0800820 dispatchEventLocked(currentTime, entry, inputTargets);
Jeff Brownb88102f2010-09-08 11:49:43 -0700821 return true;
822}
823
824
825void InputDispatcher::logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry) {
826#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +0000827 ALOGD("%seventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, "
Jeff Brown85a31762010-09-01 17:01:00 -0700828 "action=0x%x, flags=0x%x, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700829 "metaState=0x%x, buttonState=0x%x, "
830 "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%lld",
Jeff Brownb88102f2010-09-08 11:49:43 -0700831 prefix,
Jeff Brown85a31762010-09-01 17:01:00 -0700832 entry->eventTime, entry->deviceId, entry->source, entry->policyFlags,
833 entry->action, entry->flags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700834 entry->metaState, entry->buttonState,
835 entry->edgeFlags, entry->xPrecision, entry->yPrecision,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700836 entry->downTime);
837
Jeff Brown46b9ac02010-04-22 18:58:52 -0700838 for (uint32_t i = 0; i < entry->pointerCount; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +0000839 ALOGD(" Pointer %d: id=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700840 "x=%f, y=%f, pressure=%f, size=%f, "
Jeff Brown85a31762010-09-01 17:01:00 -0700841 "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, "
Jeff Brown8d608662010-08-30 03:02:23 -0700842 "orientation=%f",
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700843 i, entry->pointerProperties[i].id,
844 entry->pointerProperties[i].toolType,
Jeff Brown3241b6b2012-02-03 15:08:02 -0800845 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
846 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
847 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
848 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
849 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
850 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
851 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
852 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
853 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
Jeff Brown46b9ac02010-04-22 18:58:52 -0700854 }
855#endif
Jeff Brown46b9ac02010-04-22 18:58:52 -0700856}
857
Jeff Browne9bb9be2012-02-06 15:47:55 -0800858void InputDispatcher::dispatchEventLocked(nsecs_t currentTime,
859 EventEntry* eventEntry, const Vector<InputTarget>& inputTargets) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700860#if DEBUG_DISPATCH_CYCLE
Jeff Brown3241b6b2012-02-03 15:08:02 -0800861 ALOGD("dispatchEventToCurrentInputTargets");
Jeff Brown46b9ac02010-04-22 18:58:52 -0700862#endif
863
Steve Blockec193de2012-01-09 18:35:44 +0000864 ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true
Jeff Brown9c3cda02010-06-15 01:31:58 -0700865
Jeff Browne2fe69e2010-10-18 13:21:23 -0700866 pokeUserActivityLocked(eventEntry);
867
Jeff Browne9bb9be2012-02-06 15:47:55 -0800868 for (size_t i = 0; i < inputTargets.size(); i++) {
869 const InputTarget& inputTarget = inputTargets.itemAt(i);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700870
Jeff Brown519e0242010-09-15 15:18:56 -0700871 ssize_t connectionIndex = getConnectionIndexLocked(inputTarget.inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700872 if (connectionIndex >= 0) {
Jeff Browncbee6d62012-02-03 20:11:27 -0800873 sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex);
Jeff Brown3241b6b2012-02-03 15:08:02 -0800874 prepareDispatchCycleLocked(currentTime, connection, eventEntry, &inputTarget);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700875 } else {
Jeff Brownb6997262010-10-08 22:31:17 -0700876#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +0000877 ALOGD("Dropping event delivery to target with channel '%s' because it "
Jeff Brownb6997262010-10-08 22:31:17 -0700878 "is no longer registered with the input dispatcher.",
Jeff Brown46b9ac02010-04-22 18:58:52 -0700879 inputTarget.inputChannel->getName().string());
Jeff Brownb6997262010-10-08 22:31:17 -0700880#endif
Jeff Brown46b9ac02010-04-22 18:58:52 -0700881 }
882 }
883}
884
Jeff Brownb88102f2010-09-08 11:49:43 -0700885int32_t InputDispatcher::handleTargetsNotReadyLocked(nsecs_t currentTime,
Jeff Brown9302c872011-07-13 22:51:29 -0700886 const EventEntry* entry,
887 const sp<InputApplicationHandle>& applicationHandle,
888 const sp<InputWindowHandle>& windowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700889 nsecs_t* nextWakeupTime) {
Jeff Brown9302c872011-07-13 22:51:29 -0700890 if (applicationHandle == NULL && windowHandle == NULL) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700891 if (mInputTargetWaitCause != INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY) {
892#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +0000893 ALOGD("Waiting for system to become ready for input.");
Jeff Brownb88102f2010-09-08 11:49:43 -0700894#endif
895 mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY;
896 mInputTargetWaitStartTime = currentTime;
897 mInputTargetWaitTimeoutTime = LONG_LONG_MAX;
898 mInputTargetWaitTimeoutExpired = false;
Jeff Brown9302c872011-07-13 22:51:29 -0700899 mInputTargetWaitApplicationHandle.clear();
Jeff Brownb88102f2010-09-08 11:49:43 -0700900 }
901 } else {
902 if (mInputTargetWaitCause != INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) {
903#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +0000904 ALOGD("Waiting for application to become ready for input: %s",
Jeff Brown9302c872011-07-13 22:51:29 -0700905 getApplicationWindowLabelLocked(applicationHandle, windowHandle).string());
Jeff Brownb88102f2010-09-08 11:49:43 -0700906#endif
Jeff Browncc4f7db2011-08-30 20:34:48 -0700907 nsecs_t timeout;
908 if (windowHandle != NULL) {
909 timeout = windowHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT);
910 } else if (applicationHandle != NULL) {
911 timeout = applicationHandle->getDispatchingTimeout(
912 DEFAULT_INPUT_DISPATCHING_TIMEOUT);
913 } else {
914 timeout = DEFAULT_INPUT_DISPATCHING_TIMEOUT;
915 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700916
917 mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY;
918 mInputTargetWaitStartTime = currentTime;
919 mInputTargetWaitTimeoutTime = currentTime + timeout;
920 mInputTargetWaitTimeoutExpired = false;
Jeff Brown9302c872011-07-13 22:51:29 -0700921 mInputTargetWaitApplicationHandle.clear();
Jeff Brown928e0542011-01-10 11:17:36 -0800922
Jeff Brown9302c872011-07-13 22:51:29 -0700923 if (windowHandle != NULL) {
924 mInputTargetWaitApplicationHandle = windowHandle->inputApplicationHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800925 }
Jeff Brown9302c872011-07-13 22:51:29 -0700926 if (mInputTargetWaitApplicationHandle == NULL && applicationHandle != NULL) {
927 mInputTargetWaitApplicationHandle = applicationHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800928 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700929 }
930 }
931
932 if (mInputTargetWaitTimeoutExpired) {
933 return INPUT_EVENT_INJECTION_TIMED_OUT;
934 }
935
936 if (currentTime >= mInputTargetWaitTimeoutTime) {
Jeff Brown9302c872011-07-13 22:51:29 -0700937 onANRLocked(currentTime, applicationHandle, windowHandle,
938 entry->eventTime, mInputTargetWaitStartTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700939
940 // Force poll loop to wake up immediately on next iteration once we get the
941 // ANR response back from the policy.
942 *nextWakeupTime = LONG_LONG_MIN;
943 return INPUT_EVENT_INJECTION_PENDING;
944 } else {
945 // Force poll loop to wake up when timeout is due.
946 if (mInputTargetWaitTimeoutTime < *nextWakeupTime) {
947 *nextWakeupTime = mInputTargetWaitTimeoutTime;
948 }
949 return INPUT_EVENT_INJECTION_PENDING;
950 }
951}
952
Jeff Brown519e0242010-09-15 15:18:56 -0700953void InputDispatcher::resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
954 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700955 if (newTimeout > 0) {
956 // Extend the timeout.
957 mInputTargetWaitTimeoutTime = now() + newTimeout;
958 } else {
959 // Give up.
960 mInputTargetWaitTimeoutExpired = true;
Jeff Brown519e0242010-09-15 15:18:56 -0700961
Jeff Brown01ce2e92010-09-26 22:20:12 -0700962 // Release the touch targets.
963 mTouchState.reset();
Jeff Brown2a95c2a2010-09-16 12:31:46 -0700964
Jeff Brown519e0242010-09-15 15:18:56 -0700965 // Input state will not be realistic. Mark it out of sync.
Jeff Browndc3e0052010-09-16 11:02:16 -0700966 if (inputChannel.get()) {
967 ssize_t connectionIndex = getConnectionIndexLocked(inputChannel);
968 if (connectionIndex >= 0) {
Jeff Browncbee6d62012-02-03 20:11:27 -0800969 sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex);
Jeff Brown00045a72010-12-09 18:10:30 -0800970 if (connection->status == Connection::STATUS_NORMAL) {
Jeff Brownda3d5a92011-03-29 15:11:34 -0700971 CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS,
Jeff Brown00045a72010-12-09 18:10:30 -0800972 "application not responding");
Jeff Brownda3d5a92011-03-29 15:11:34 -0700973 synthesizeCancelationEventsForConnectionLocked(connection, options);
Jeff Brown00045a72010-12-09 18:10:30 -0800974 }
Jeff Browndc3e0052010-09-16 11:02:16 -0700975 }
Jeff Brown519e0242010-09-15 15:18:56 -0700976 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700977 }
978}
979
Jeff Brown519e0242010-09-15 15:18:56 -0700980nsecs_t InputDispatcher::getTimeSpentWaitingForApplicationLocked(
Jeff Brownb88102f2010-09-08 11:49:43 -0700981 nsecs_t currentTime) {
982 if (mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) {
983 return currentTime - mInputTargetWaitStartTime;
984 }
985 return 0;
986}
987
988void InputDispatcher::resetANRTimeoutsLocked() {
989#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +0000990 ALOGD("Resetting ANR timeouts.");
Jeff Brownb88102f2010-09-08 11:49:43 -0700991#endif
992
Jeff Brownb88102f2010-09-08 11:49:43 -0700993 // Reset input target wait timeout.
994 mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_NONE;
Jeff Brown5ea29ab2011-07-27 11:50:51 -0700995 mInputTargetWaitApplicationHandle.clear();
Jeff Brownb88102f2010-09-08 11:49:43 -0700996}
997
Jeff Brown01ce2e92010-09-26 22:20:12 -0700998int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime,
Jeff Browne9bb9be2012-02-06 15:47:55 -0800999 const EventEntry* entry, Vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001000 int32_t injectionResult;
1001
1002 // If there is no currently focused window and no focused application
1003 // then drop the event.
Jeff Brown9302c872011-07-13 22:51:29 -07001004 if (mFocusedWindowHandle == NULL) {
1005 if (mFocusedApplicationHandle != NULL) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001006#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001007 ALOGD("Waiting because there is no focused window but there is a "
Jeff Brown519e0242010-09-15 15:18:56 -07001008 "focused application that may eventually add a window: %s.",
Jeff Brown9302c872011-07-13 22:51:29 -07001009 getApplicationWindowLabelLocked(mFocusedApplicationHandle, NULL).string());
Jeff Brownb88102f2010-09-08 11:49:43 -07001010#endif
1011 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown9302c872011-07-13 22:51:29 -07001012 mFocusedApplicationHandle, NULL, nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07001013 goto Unresponsive;
1014 }
1015
Steve Block6215d3f2012-01-04 20:05:49 +00001016 ALOGI("Dropping event because there is no focused window or focused application.");
Jeff Brownb88102f2010-09-08 11:49:43 -07001017 injectionResult = INPUT_EVENT_INJECTION_FAILED;
1018 goto Failed;
1019 }
1020
1021 // Check permissions.
Jeff Brown9302c872011-07-13 22:51:29 -07001022 if (! checkInjectionPermission(mFocusedWindowHandle, entry->injectionState)) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001023 injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED;
1024 goto Failed;
1025 }
1026
1027 // If the currently focused window is paused then keep waiting.
Jeff Browncc4f7db2011-08-30 20:34:48 -07001028 if (mFocusedWindowHandle->getInfo()->paused) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001029#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001030 ALOGD("Waiting because focused window is paused.");
Jeff Brownb88102f2010-09-08 11:49:43 -07001031#endif
1032 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown9302c872011-07-13 22:51:29 -07001033 mFocusedApplicationHandle, mFocusedWindowHandle, nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07001034 goto Unresponsive;
1035 }
1036
Jeff Brown519e0242010-09-15 15:18:56 -07001037 // If the currently focused window is still working on previous events then keep waiting.
Jeff Brown9302c872011-07-13 22:51:29 -07001038 if (! isWindowFinishedWithPreviousInputLocked(mFocusedWindowHandle)) {
Jeff Brown519e0242010-09-15 15:18:56 -07001039#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001040 ALOGD("Waiting because focused window still processing previous input.");
Jeff Brown519e0242010-09-15 15:18:56 -07001041#endif
1042 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown9302c872011-07-13 22:51:29 -07001043 mFocusedApplicationHandle, mFocusedWindowHandle, nextWakeupTime);
Jeff Brown519e0242010-09-15 15:18:56 -07001044 goto Unresponsive;
1045 }
1046
Jeff Brownb88102f2010-09-08 11:49:43 -07001047 // Success! Output targets.
1048 injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED;
Jeff Brown9302c872011-07-13 22:51:29 -07001049 addWindowTargetLocked(mFocusedWindowHandle,
Jeff Browne9bb9be2012-02-06 15:47:55 -08001050 InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, BitSet32(0),
1051 inputTargets);
Jeff Brownb88102f2010-09-08 11:49:43 -07001052
1053 // Done.
1054Failed:
1055Unresponsive:
Jeff Brown519e0242010-09-15 15:18:56 -07001056 nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime);
1057 updateDispatchStatisticsLocked(currentTime, entry,
1058 injectionResult, timeSpentWaitingForApplication);
Jeff Brownb88102f2010-09-08 11:49:43 -07001059#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001060 ALOGD("findFocusedWindow finished: injectionResult=%d, "
Jeff Brown519e0242010-09-15 15:18:56 -07001061 "timeSpendWaitingForApplication=%0.1fms",
1062 injectionResult, timeSpentWaitingForApplication / 1000000.0);
Jeff Brownb88102f2010-09-08 11:49:43 -07001063#endif
1064 return injectionResult;
1065}
1066
Jeff Brown01ce2e92010-09-26 22:20:12 -07001067int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime,
Jeff Browne9bb9be2012-02-06 15:47:55 -08001068 const MotionEntry* entry, Vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime,
1069 bool* outConflictingPointerActions) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001070 enum InjectionPermission {
1071 INJECTION_PERMISSION_UNKNOWN,
1072 INJECTION_PERMISSION_GRANTED,
1073 INJECTION_PERMISSION_DENIED
1074 };
1075
Jeff Brownb88102f2010-09-08 11:49:43 -07001076 nsecs_t startTime = now();
1077
1078 // For security reasons, we defer updating the touch state until we are sure that
1079 // event injection will be allowed.
1080 //
1081 // FIXME In the original code, screenWasOff could never be set to true.
1082 // The reason is that the POLICY_FLAG_WOKE_HERE
1083 // and POLICY_FLAG_BRIGHT_HERE flags were set only when preprocessing raw
1084 // EV_KEY, EV_REL and EV_ABS events. As it happens, the touch event was
1085 // actually enqueued using the policyFlags that appeared in the final EV_SYN
1086 // events upon which no preprocessing took place. So policyFlags was always 0.
1087 // In the new native input dispatcher we're a bit more careful about event
1088 // preprocessing so the touches we receive can actually have non-zero policyFlags.
1089 // Unfortunately we obtain undesirable behavior.
1090 //
1091 // Here's what happens:
1092 //
1093 // When the device dims in anticipation of going to sleep, touches
1094 // in windows which have FLAG_TOUCHABLE_WHEN_WAKING cause
1095 // the device to brighten and reset the user activity timer.
1096 // Touches on other windows (such as the launcher window)
1097 // are dropped. Then after a moment, the device goes to sleep. Oops.
1098 //
1099 // Also notice how screenWasOff was being initialized using POLICY_FLAG_BRIGHT_HERE
1100 // instead of POLICY_FLAG_WOKE_HERE...
1101 //
1102 bool screenWasOff = false; // original policy: policyFlags & POLICY_FLAG_BRIGHT_HERE;
1103
1104 int32_t action = entry->action;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001105 int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK;
Jeff Brownb88102f2010-09-08 11:49:43 -07001106
1107 // Update the touch state as needed based on the properties of the touch event.
Jeff Brown01ce2e92010-09-26 22:20:12 -07001108 int32_t injectionResult = INPUT_EVENT_INJECTION_PENDING;
1109 InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN;
Jeff Brown9302c872011-07-13 22:51:29 -07001110 sp<InputWindowHandle> newHoverWindowHandle;
Jeff Browncc0c1592011-02-19 05:07:28 -08001111
1112 bool isSplit = mTouchState.split;
Jeff Brown2717eff2011-06-30 23:53:07 -07001113 bool switchedDevice = mTouchState.deviceId >= 0
1114 && (mTouchState.deviceId != entry->deviceId
1115 || mTouchState.source != entry->source);
Jeff Browna032cc02011-03-07 16:56:21 -08001116 bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE
1117 || maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER
1118 || maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT);
1119 bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN
1120 || maskedAction == AMOTION_EVENT_ACTION_SCROLL
1121 || isHoverAction);
Jeff Brown81346812011-06-28 20:08:48 -07001122 bool wrongDevice = false;
Jeff Browna032cc02011-03-07 16:56:21 -08001123 if (newGesture) {
Jeff Browncc0c1592011-02-19 05:07:28 -08001124 bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN;
Jeff Brown81346812011-06-28 20:08:48 -07001125 if (switchedDevice && mTouchState.down && !down) {
1126#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001127 ALOGD("Dropping event because a pointer for a different device is already down.");
Jeff Brown81346812011-06-28 20:08:48 -07001128#endif
Jeff Browncc0c1592011-02-19 05:07:28 -08001129 mTempTouchState.copyFrom(mTouchState);
Jeff Brown81346812011-06-28 20:08:48 -07001130 injectionResult = INPUT_EVENT_INJECTION_FAILED;
1131 switchedDevice = false;
1132 wrongDevice = true;
1133 goto Failed;
Jeff Browncc0c1592011-02-19 05:07:28 -08001134 }
Jeff Brown81346812011-06-28 20:08:48 -07001135 mTempTouchState.reset();
1136 mTempTouchState.down = down;
1137 mTempTouchState.deviceId = entry->deviceId;
1138 mTempTouchState.source = entry->source;
1139 isSplit = false;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001140 } else {
1141 mTempTouchState.copyFrom(mTouchState);
Jeff Browncc0c1592011-02-19 05:07:28 -08001142 }
Jeff Brownb88102f2010-09-08 11:49:43 -07001143
Jeff Browna032cc02011-03-07 16:56:21 -08001144 if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08001145 /* Case 1: New splittable pointer going down, or need target for hover or scroll. */
Jeff Brownb88102f2010-09-08 11:49:43 -07001146
Jeff Brown01ce2e92010-09-26 22:20:12 -07001147 int32_t pointerIndex = getMotionEventActionPointerIndex(action);
Jeff Brown3241b6b2012-02-03 15:08:02 -08001148 int32_t x = int32_t(entry->pointerCoords[pointerIndex].
Jeff Brownebbd5d12011-02-17 13:01:34 -08001149 getAxisValue(AMOTION_EVENT_AXIS_X));
Jeff Brown3241b6b2012-02-03 15:08:02 -08001150 int32_t y = int32_t(entry->pointerCoords[pointerIndex].
Jeff Brownebbd5d12011-02-17 13:01:34 -08001151 getAxisValue(AMOTION_EVENT_AXIS_Y));
Jeff Brown9302c872011-07-13 22:51:29 -07001152 sp<InputWindowHandle> newTouchedWindowHandle;
1153 sp<InputWindowHandle> topErrorWindowHandle;
Jeff Browna032cc02011-03-07 16:56:21 -08001154 bool isTouchModal = false;
Jeff Brownb88102f2010-09-08 11:49:43 -07001155
1156 // Traverse windows from front to back to find touched window and outside targets.
Jeff Brown9302c872011-07-13 22:51:29 -07001157 size_t numWindows = mWindowHandles.size();
Jeff Brownb88102f2010-09-08 11:49:43 -07001158 for (size_t i = 0; i < numWindows; i++) {
Jeff Brown9302c872011-07-13 22:51:29 -07001159 sp<InputWindowHandle> windowHandle = mWindowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001160 const InputWindowInfo* windowInfo = windowHandle->getInfo();
1161 int32_t flags = windowInfo->layoutParamsFlags;
Jeff Brownb88102f2010-09-08 11:49:43 -07001162
Jeff Browncc4f7db2011-08-30 20:34:48 -07001163 if (flags & InputWindowInfo::FLAG_SYSTEM_ERROR) {
Jeff Brown9302c872011-07-13 22:51:29 -07001164 if (topErrorWindowHandle == NULL) {
1165 topErrorWindowHandle = windowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -07001166 }
1167 }
1168
Jeff Browncc4f7db2011-08-30 20:34:48 -07001169 if (windowInfo->visible) {
1170 if (! (flags & InputWindowInfo::FLAG_NOT_TOUCHABLE)) {
1171 isTouchModal = (flags & (InputWindowInfo::FLAG_NOT_FOCUSABLE
1172 | InputWindowInfo::FLAG_NOT_TOUCH_MODAL)) == 0;
1173 if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) {
Jeff Brown9302c872011-07-13 22:51:29 -07001174 if (! screenWasOff
Jeff Browncc4f7db2011-08-30 20:34:48 -07001175 || (flags & InputWindowInfo::FLAG_TOUCHABLE_WHEN_WAKING)) {
Jeff Brown9302c872011-07-13 22:51:29 -07001176 newTouchedWindowHandle = windowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -07001177 }
1178 break; // found touched window, exit window loop
1179 }
1180 }
1181
Jeff Brown01ce2e92010-09-26 22:20:12 -07001182 if (maskedAction == AMOTION_EVENT_ACTION_DOWN
Jeff Browncc4f7db2011-08-30 20:34:48 -07001183 && (flags & InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH)) {
Jeff Browna032cc02011-03-07 16:56:21 -08001184 int32_t outsideTargetFlags = InputTarget::FLAG_DISPATCH_AS_OUTSIDE;
Jeff Brown9302c872011-07-13 22:51:29 -07001185 if (isWindowObscuredAtPointLocked(windowHandle, x, y)) {
Jeff Brown19dfc832010-10-05 12:26:23 -07001186 outsideTargetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED;
1187 }
1188
Jeff Brown9302c872011-07-13 22:51:29 -07001189 mTempTouchState.addOrUpdateWindow(
1190 windowHandle, outsideTargetFlags, BitSet32(0));
Jeff Brownb88102f2010-09-08 11:49:43 -07001191 }
1192 }
1193 }
1194
1195 // If there is an error window but it is not taking focus (typically because
1196 // it is invisible) then wait for it. Any other focused window may in
1197 // fact be in ANR state.
Jeff Brown9302c872011-07-13 22:51:29 -07001198 if (topErrorWindowHandle != NULL && newTouchedWindowHandle != topErrorWindowHandle) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001199#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001200 ALOGD("Waiting because system error window is pending.");
Jeff Brownb88102f2010-09-08 11:49:43 -07001201#endif
1202 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
1203 NULL, NULL, nextWakeupTime);
1204 injectionPermission = INJECTION_PERMISSION_UNKNOWN;
1205 goto Unresponsive;
1206 }
1207
Jeff Brown01ce2e92010-09-26 22:20:12 -07001208 // Figure out whether splitting will be allowed for this window.
Jeff Browncc4f7db2011-08-30 20:34:48 -07001209 if (newTouchedWindowHandle != NULL
1210 && newTouchedWindowHandle->getInfo()->supportsSplitTouch()) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001211 // New window supports splitting.
1212 isSplit = true;
1213 } else if (isSplit) {
1214 // New window does not support splitting but we have already split events.
1215 // Assign the pointer to the first foreground window we find.
1216 // (May be NULL which is why we put this code block before the next check.)
Jeff Brown9302c872011-07-13 22:51:29 -07001217 newTouchedWindowHandle = mTempTouchState.getFirstForegroundWindowHandle();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001218 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001219
Jeff Brownb88102f2010-09-08 11:49:43 -07001220 // If we did not find a touched window then fail.
Jeff Brown9302c872011-07-13 22:51:29 -07001221 if (newTouchedWindowHandle == NULL) {
1222 if (mFocusedApplicationHandle != NULL) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001223#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001224 ALOGD("Waiting because there is no touched window but there is a "
Jeff Brown519e0242010-09-15 15:18:56 -07001225 "focused application that may eventually add a new window: %s.",
Jeff Brown9302c872011-07-13 22:51:29 -07001226 getApplicationWindowLabelLocked(mFocusedApplicationHandle, NULL).string());
Jeff Brownb88102f2010-09-08 11:49:43 -07001227#endif
1228 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown9302c872011-07-13 22:51:29 -07001229 mFocusedApplicationHandle, NULL, nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07001230 goto Unresponsive;
1231 }
1232
Steve Block6215d3f2012-01-04 20:05:49 +00001233 ALOGI("Dropping event because there is no touched window or focused application.");
Jeff Brownb88102f2010-09-08 11:49:43 -07001234 injectionResult = INPUT_EVENT_INJECTION_FAILED;
Jeff Brownb88102f2010-09-08 11:49:43 -07001235 goto Failed;
1236 }
1237
Jeff Brown19dfc832010-10-05 12:26:23 -07001238 // Set target flags.
Jeff Browna032cc02011-03-07 16:56:21 -08001239 int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS;
Jeff Brown19dfc832010-10-05 12:26:23 -07001240 if (isSplit) {
1241 targetFlags |= InputTarget::FLAG_SPLIT;
1242 }
Jeff Brown9302c872011-07-13 22:51:29 -07001243 if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) {
Jeff Brown19dfc832010-10-05 12:26:23 -07001244 targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED;
1245 }
1246
Jeff Browna032cc02011-03-07 16:56:21 -08001247 // Update hover state.
1248 if (isHoverAction) {
Jeff Brown9302c872011-07-13 22:51:29 -07001249 newHoverWindowHandle = newTouchedWindowHandle;
Jeff Browna032cc02011-03-07 16:56:21 -08001250 } else if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) {
Jeff Brown9302c872011-07-13 22:51:29 -07001251 newHoverWindowHandle = mLastHoverWindowHandle;
Jeff Browna032cc02011-03-07 16:56:21 -08001252 }
1253
Jeff Brown01ce2e92010-09-26 22:20:12 -07001254 // Update the temporary touch state.
1255 BitSet32 pointerIds;
1256 if (isSplit) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001257 uint32_t pointerId = entry->pointerProperties[pointerIndex].id;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001258 pointerIds.markBit(pointerId);
Jeff Brownb88102f2010-09-08 11:49:43 -07001259 }
Jeff Brown9302c872011-07-13 22:51:29 -07001260 mTempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds);
Jeff Brownb88102f2010-09-08 11:49:43 -07001261 } else {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001262 /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */
Jeff Brownb88102f2010-09-08 11:49:43 -07001263
1264 // If the pointer is not currently down, then ignore the event.
Jeff Brown01ce2e92010-09-26 22:20:12 -07001265 if (! mTempTouchState.down) {
Jeff Browna2cc28d2011-03-25 11:58:46 -07001266#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001267 ALOGD("Dropping event because the pointer is not down or we previously "
Jeff Brown76860e32010-10-25 17:37:46 -07001268 "dropped the pointer down event.");
1269#endif
Jeff Brownb88102f2010-09-08 11:49:43 -07001270 injectionResult = INPUT_EVENT_INJECTION_FAILED;
Jeff Brownb88102f2010-09-08 11:49:43 -07001271 goto Failed;
1272 }
Jeff Brown98db5fa2011-06-08 15:37:10 -07001273
1274 // Check whether touches should slip outside of the current foreground window.
1275 if (maskedAction == AMOTION_EVENT_ACTION_MOVE
1276 && entry->pointerCount == 1
1277 && mTempTouchState.isSlippery()) {
Jeff Brown3241b6b2012-02-03 15:08:02 -08001278 int32_t x = int32_t(entry->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X));
1279 int32_t y = int32_t(entry->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y));
Jeff Brown98db5fa2011-06-08 15:37:10 -07001280
Jeff Brown9302c872011-07-13 22:51:29 -07001281 sp<InputWindowHandle> oldTouchedWindowHandle =
1282 mTempTouchState.getFirstForegroundWindowHandle();
1283 sp<InputWindowHandle> newTouchedWindowHandle = findTouchedWindowAtLocked(x, y);
1284 if (oldTouchedWindowHandle != newTouchedWindowHandle
1285 && newTouchedWindowHandle != NULL) {
Jeff Brown98db5fa2011-06-08 15:37:10 -07001286#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001287 ALOGD("Touch is slipping out of window %s into window %s.",
Jeff Browncc4f7db2011-08-30 20:34:48 -07001288 oldTouchedWindowHandle->getName().string(),
1289 newTouchedWindowHandle->getName().string());
Jeff Brown98db5fa2011-06-08 15:37:10 -07001290#endif
1291 // Make a slippery exit from the old window.
Jeff Brown9302c872011-07-13 22:51:29 -07001292 mTempTouchState.addOrUpdateWindow(oldTouchedWindowHandle,
Jeff Brown98db5fa2011-06-08 15:37:10 -07001293 InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, BitSet32(0));
1294
1295 // Make a slippery entrance into the new window.
Jeff Browncc4f7db2011-08-30 20:34:48 -07001296 if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) {
Jeff Brown98db5fa2011-06-08 15:37:10 -07001297 isSplit = true;
1298 }
1299
1300 int32_t targetFlags = InputTarget::FLAG_FOREGROUND
1301 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER;
1302 if (isSplit) {
1303 targetFlags |= InputTarget::FLAG_SPLIT;
1304 }
Jeff Brown9302c872011-07-13 22:51:29 -07001305 if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) {
Jeff Brown98db5fa2011-06-08 15:37:10 -07001306 targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED;
1307 }
1308
1309 BitSet32 pointerIds;
1310 if (isSplit) {
1311 pointerIds.markBit(entry->pointerProperties[0].id);
1312 }
Jeff Brown9302c872011-07-13 22:51:29 -07001313 mTempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds);
Jeff Brown98db5fa2011-06-08 15:37:10 -07001314 }
1315 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001316 }
Jeff Brownb88102f2010-09-08 11:49:43 -07001317
Jeff Brown9302c872011-07-13 22:51:29 -07001318 if (newHoverWindowHandle != mLastHoverWindowHandle) {
Jeff Browna032cc02011-03-07 16:56:21 -08001319 // Let the previous window know that the hover sequence is over.
Jeff Brown9302c872011-07-13 22:51:29 -07001320 if (mLastHoverWindowHandle != NULL) {
Jeff Browna032cc02011-03-07 16:56:21 -08001321#if DEBUG_HOVER
Steve Block5baa3a62011-12-20 16:23:08 +00001322 ALOGD("Sending hover exit event to window %s.",
Jeff Browncc4f7db2011-08-30 20:34:48 -07001323 mLastHoverWindowHandle->getName().string());
Jeff Browna032cc02011-03-07 16:56:21 -08001324#endif
Jeff Brown9302c872011-07-13 22:51:29 -07001325 mTempTouchState.addOrUpdateWindow(mLastHoverWindowHandle,
Jeff Browna032cc02011-03-07 16:56:21 -08001326 InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0));
1327 }
1328
1329 // Let the new window know that the hover sequence is starting.
Jeff Brown9302c872011-07-13 22:51:29 -07001330 if (newHoverWindowHandle != NULL) {
Jeff Browna032cc02011-03-07 16:56:21 -08001331#if DEBUG_HOVER
Steve Block5baa3a62011-12-20 16:23:08 +00001332 ALOGD("Sending hover enter event to window %s.",
Jeff Browncc4f7db2011-08-30 20:34:48 -07001333 newHoverWindowHandle->getName().string());
Jeff Browna032cc02011-03-07 16:56:21 -08001334#endif
Jeff Brown9302c872011-07-13 22:51:29 -07001335 mTempTouchState.addOrUpdateWindow(newHoverWindowHandle,
Jeff Browna032cc02011-03-07 16:56:21 -08001336 InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, BitSet32(0));
1337 }
1338 }
1339
Jeff Brown01ce2e92010-09-26 22:20:12 -07001340 // Check permission to inject into all touched foreground windows and ensure there
1341 // is at least one touched foreground window.
1342 {
1343 bool haveForegroundWindow = false;
1344 for (size_t i = 0; i < mTempTouchState.windows.size(); i++) {
1345 const TouchedWindow& touchedWindow = mTempTouchState.windows[i];
1346 if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) {
1347 haveForegroundWindow = true;
Jeff Brown9302c872011-07-13 22:51:29 -07001348 if (! checkInjectionPermission(touchedWindow.windowHandle,
1349 entry->injectionState)) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001350 injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED;
1351 injectionPermission = INJECTION_PERMISSION_DENIED;
1352 goto Failed;
1353 }
1354 }
1355 }
1356 if (! haveForegroundWindow) {
Jeff Browna2cc28d2011-03-25 11:58:46 -07001357#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001358 ALOGD("Dropping event because there is no touched foreground window to receive it.");
Jeff Brownb88102f2010-09-08 11:49:43 -07001359#endif
1360 injectionResult = INPUT_EVENT_INJECTION_FAILED;
Jeff Brownb88102f2010-09-08 11:49:43 -07001361 goto Failed;
1362 }
1363
Jeff Brown01ce2e92010-09-26 22:20:12 -07001364 // Permission granted to injection into all touched foreground windows.
1365 injectionPermission = INJECTION_PERMISSION_GRANTED;
1366 }
Jeff Brown519e0242010-09-15 15:18:56 -07001367
Kenny Root7a9db182011-06-02 15:16:05 -07001368 // Check whether windows listening for outside touches are owned by the same UID. If it is
1369 // set the policy flag that we will not reveal coordinate information to this window.
1370 if (maskedAction == AMOTION_EVENT_ACTION_DOWN) {
Jeff Brown9302c872011-07-13 22:51:29 -07001371 sp<InputWindowHandle> foregroundWindowHandle =
1372 mTempTouchState.getFirstForegroundWindowHandle();
Jeff Browncc4f7db2011-08-30 20:34:48 -07001373 const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid;
Kenny Root7a9db182011-06-02 15:16:05 -07001374 for (size_t i = 0; i < mTempTouchState.windows.size(); i++) {
1375 const TouchedWindow& touchedWindow = mTempTouchState.windows[i];
1376 if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) {
Jeff Brown9302c872011-07-13 22:51:29 -07001377 sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle;
Jeff Browncc4f7db2011-08-30 20:34:48 -07001378 if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) {
Jeff Brown9302c872011-07-13 22:51:29 -07001379 mTempTouchState.addOrUpdateWindow(inputWindowHandle,
Kenny Root7a9db182011-06-02 15:16:05 -07001380 InputTarget::FLAG_ZERO_COORDS, BitSet32(0));
1381 }
1382 }
1383 }
1384 }
1385
Jeff Brown01ce2e92010-09-26 22:20:12 -07001386 // Ensure all touched foreground windows are ready for new input.
1387 for (size_t i = 0; i < mTempTouchState.windows.size(); i++) {
1388 const TouchedWindow& touchedWindow = mTempTouchState.windows[i];
1389 if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) {
1390 // If the touched window is paused then keep waiting.
Jeff Browncc4f7db2011-08-30 20:34:48 -07001391 if (touchedWindow.windowHandle->getInfo()->paused) {
Jeff Browna2cc28d2011-03-25 11:58:46 -07001392#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001393 ALOGD("Waiting because touched window is paused.");
Jeff Brown519e0242010-09-15 15:18:56 -07001394#endif
Jeff Brown01ce2e92010-09-26 22:20:12 -07001395 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown9302c872011-07-13 22:51:29 -07001396 NULL, touchedWindow.windowHandle, nextWakeupTime);
Jeff Brown01ce2e92010-09-26 22:20:12 -07001397 goto Unresponsive;
1398 }
1399
1400 // If the touched window is still working on previous events then keep waiting.
Jeff Brown9302c872011-07-13 22:51:29 -07001401 if (! isWindowFinishedWithPreviousInputLocked(touchedWindow.windowHandle)) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001402#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001403 ALOGD("Waiting because touched window still processing previous input.");
Jeff Brown01ce2e92010-09-26 22:20:12 -07001404#endif
1405 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown9302c872011-07-13 22:51:29 -07001406 NULL, touchedWindow.windowHandle, nextWakeupTime);
Jeff Brown01ce2e92010-09-26 22:20:12 -07001407 goto Unresponsive;
1408 }
1409 }
1410 }
1411
1412 // If this is the first pointer going down and the touched window has a wallpaper
1413 // then also add the touched wallpaper windows so they are locked in for the duration
1414 // of the touch gesture.
Jeff Brown33bbfd22011-02-24 20:55:35 -08001415 // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper
1416 // engine only supports touch events. We would need to add a mechanism similar
1417 // to View.onGenericMotionEvent to enable wallpapers to handle these events.
1418 if (maskedAction == AMOTION_EVENT_ACTION_DOWN) {
Jeff Brown9302c872011-07-13 22:51:29 -07001419 sp<InputWindowHandle> foregroundWindowHandle =
1420 mTempTouchState.getFirstForegroundWindowHandle();
Jeff Browncc4f7db2011-08-30 20:34:48 -07001421 if (foregroundWindowHandle->getInfo()->hasWallpaper) {
Jeff Brown9302c872011-07-13 22:51:29 -07001422 for (size_t i = 0; i < mWindowHandles.size(); i++) {
1423 sp<InputWindowHandle> windowHandle = mWindowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001424 if (windowHandle->getInfo()->layoutParamsType
1425 == InputWindowInfo::TYPE_WALLPAPER) {
Jeff Brown9302c872011-07-13 22:51:29 -07001426 mTempTouchState.addOrUpdateWindow(windowHandle,
Jeff Browna032cc02011-03-07 16:56:21 -08001427 InputTarget::FLAG_WINDOW_IS_OBSCURED
1428 | InputTarget::FLAG_DISPATCH_AS_IS,
1429 BitSet32(0));
Jeff Brown01ce2e92010-09-26 22:20:12 -07001430 }
1431 }
1432 }
1433 }
1434
Jeff Brownb88102f2010-09-08 11:49:43 -07001435 // Success! Output targets.
1436 injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED;
Jeff Brownb88102f2010-09-08 11:49:43 -07001437
Jeff Brown01ce2e92010-09-26 22:20:12 -07001438 for (size_t i = 0; i < mTempTouchState.windows.size(); i++) {
1439 const TouchedWindow& touchedWindow = mTempTouchState.windows.itemAt(i);
Jeff Brown9302c872011-07-13 22:51:29 -07001440 addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags,
Jeff Browne9bb9be2012-02-06 15:47:55 -08001441 touchedWindow.pointerIds, inputTargets);
Jeff Brownb88102f2010-09-08 11:49:43 -07001442 }
1443
Jeff Browna032cc02011-03-07 16:56:21 -08001444 // Drop the outside or hover touch windows since we will not care about them
1445 // in the next iteration.
1446 mTempTouchState.filterNonAsIsTouchWindows();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001447
Jeff Brownb88102f2010-09-08 11:49:43 -07001448Failed:
1449 // Check injection permission once and for all.
1450 if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001451 if (checkInjectionPermission(NULL, entry->injectionState)) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001452 injectionPermission = INJECTION_PERMISSION_GRANTED;
1453 } else {
1454 injectionPermission = INJECTION_PERMISSION_DENIED;
1455 }
1456 }
1457
1458 // Update final pieces of touch state if the injector had permission.
1459 if (injectionPermission == INJECTION_PERMISSION_GRANTED) {
Jeff Brown95712852011-01-04 19:41:59 -08001460 if (!wrongDevice) {
Jeff Brown81346812011-06-28 20:08:48 -07001461 if (switchedDevice) {
1462#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001463 ALOGD("Conflicting pointer actions: Switched to a different device.");
Jeff Brown81346812011-06-28 20:08:48 -07001464#endif
1465 *outConflictingPointerActions = true;
1466 }
1467
1468 if (isHoverAction) {
1469 // Started hovering, therefore no longer down.
1470 if (mTouchState.down) {
1471#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001472 ALOGD("Conflicting pointer actions: Hover received while pointer was down.");
Jeff Brown81346812011-06-28 20:08:48 -07001473#endif
1474 *outConflictingPointerActions = true;
1475 }
1476 mTouchState.reset();
1477 if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER
1478 || maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) {
1479 mTouchState.deviceId = entry->deviceId;
1480 mTouchState.source = entry->source;
1481 }
1482 } else if (maskedAction == AMOTION_EVENT_ACTION_UP
1483 || maskedAction == AMOTION_EVENT_ACTION_CANCEL) {
Jeff Brown95712852011-01-04 19:41:59 -08001484 // All pointers up or canceled.
Jeff Brown33bbfd22011-02-24 20:55:35 -08001485 mTouchState.reset();
Jeff Brown95712852011-01-04 19:41:59 -08001486 } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) {
1487 // First pointer went down.
1488 if (mTouchState.down) {
Jeff Brownb6997262010-10-08 22:31:17 -07001489#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001490 ALOGD("Conflicting pointer actions: Down received while already down.");
Jeff Brownb6997262010-10-08 22:31:17 -07001491#endif
Jeff Brown81346812011-06-28 20:08:48 -07001492 *outConflictingPointerActions = true;
Jeff Brown95712852011-01-04 19:41:59 -08001493 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001494 mTouchState.copyFrom(mTempTouchState);
Jeff Brown95712852011-01-04 19:41:59 -08001495 } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) {
1496 // One pointer went up.
1497 if (isSplit) {
1498 int32_t pointerIndex = getMotionEventActionPointerIndex(action);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001499 uint32_t pointerId = entry->pointerProperties[pointerIndex].id;
Jeff Brownb88102f2010-09-08 11:49:43 -07001500
Jeff Brown95712852011-01-04 19:41:59 -08001501 for (size_t i = 0; i < mTempTouchState.windows.size(); ) {
1502 TouchedWindow& touchedWindow = mTempTouchState.windows.editItemAt(i);
1503 if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) {
1504 touchedWindow.pointerIds.clearBit(pointerId);
1505 if (touchedWindow.pointerIds.isEmpty()) {
1506 mTempTouchState.windows.removeAt(i);
1507 continue;
1508 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001509 }
Jeff Brown95712852011-01-04 19:41:59 -08001510 i += 1;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001511 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001512 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001513 mTouchState.copyFrom(mTempTouchState);
1514 } else if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) {
1515 // Discard temporary touch state since it was only valid for this action.
1516 } else {
1517 // Save changes to touch state as-is for all other actions.
1518 mTouchState.copyFrom(mTempTouchState);
Jeff Brownb88102f2010-09-08 11:49:43 -07001519 }
Jeff Browna032cc02011-03-07 16:56:21 -08001520
1521 // Update hover state.
Jeff Brown9302c872011-07-13 22:51:29 -07001522 mLastHoverWindowHandle = newHoverWindowHandle;
Jeff Brown95712852011-01-04 19:41:59 -08001523 }
Jeff Brownb88102f2010-09-08 11:49:43 -07001524 } else {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001525#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001526 ALOGD("Not updating touch focus because injection was denied.");
Jeff Brown01ce2e92010-09-26 22:20:12 -07001527#endif
Jeff Brownb88102f2010-09-08 11:49:43 -07001528 }
1529
1530Unresponsive:
Jeff Brown120a4592010-10-27 18:43:51 -07001531 // Reset temporary touch state to ensure we release unnecessary references to input channels.
1532 mTempTouchState.reset();
1533
Jeff Brown519e0242010-09-15 15:18:56 -07001534 nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime);
1535 updateDispatchStatisticsLocked(currentTime, entry,
1536 injectionResult, timeSpentWaitingForApplication);
Jeff Brownb88102f2010-09-08 11:49:43 -07001537#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001538 ALOGD("findTouchedWindow finished: injectionResult=%d, injectionPermission=%d, "
Jeff Brown01ce2e92010-09-26 22:20:12 -07001539 "timeSpentWaitingForApplication=%0.1fms",
Jeff Brown519e0242010-09-15 15:18:56 -07001540 injectionResult, injectionPermission, timeSpentWaitingForApplication / 1000000.0);
Jeff Brownb88102f2010-09-08 11:49:43 -07001541#endif
1542 return injectionResult;
1543}
1544
Jeff Brown9302c872011-07-13 22:51:29 -07001545void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle,
Jeff Browne9bb9be2012-02-06 15:47:55 -08001546 int32_t targetFlags, BitSet32 pointerIds, Vector<InputTarget>& inputTargets) {
1547 inputTargets.push();
Jeff Brownb88102f2010-09-08 11:49:43 -07001548
Jeff Browncc4f7db2011-08-30 20:34:48 -07001549 const InputWindowInfo* windowInfo = windowHandle->getInfo();
Jeff Browne9bb9be2012-02-06 15:47:55 -08001550 InputTarget& target = inputTargets.editTop();
Jeff Browncc4f7db2011-08-30 20:34:48 -07001551 target.inputChannel = windowInfo->inputChannel;
Jeff Brownb88102f2010-09-08 11:49:43 -07001552 target.flags = targetFlags;
Jeff Browncc4f7db2011-08-30 20:34:48 -07001553 target.xOffset = - windowInfo->frameLeft;
1554 target.yOffset = - windowInfo->frameTop;
1555 target.scaleFactor = windowInfo->scaleFactor;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001556 target.pointerIds = pointerIds;
Jeff Brownb88102f2010-09-08 11:49:43 -07001557}
1558
Jeff Browne9bb9be2012-02-06 15:47:55 -08001559void InputDispatcher::addMonitoringTargetsLocked(Vector<InputTarget>& inputTargets) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001560 for (size_t i = 0; i < mMonitoringChannels.size(); i++) {
Jeff Browne9bb9be2012-02-06 15:47:55 -08001561 inputTargets.push();
Jeff Brownb88102f2010-09-08 11:49:43 -07001562
Jeff Browne9bb9be2012-02-06 15:47:55 -08001563 InputTarget& target = inputTargets.editTop();
Jeff Brownb88102f2010-09-08 11:49:43 -07001564 target.inputChannel = mMonitoringChannels[i];
Jeff Brownb6110c22011-04-01 16:15:13 -07001565 target.flags = InputTarget::FLAG_DISPATCH_AS_IS;
Jeff Brownb88102f2010-09-08 11:49:43 -07001566 target.xOffset = 0;
1567 target.yOffset = 0;
Jeff Brownb6110c22011-04-01 16:15:13 -07001568 target.pointerIds.clear();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001569 target.scaleFactor = 1.0f;
Jeff Brownb88102f2010-09-08 11:49:43 -07001570 }
1571}
1572
Jeff Brown9302c872011-07-13 22:51:29 -07001573bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
Jeff Brown01ce2e92010-09-26 22:20:12 -07001574 const InjectionState* injectionState) {
1575 if (injectionState
Jeff Browncc4f7db2011-08-30 20:34:48 -07001576 && (windowHandle == NULL
1577 || windowHandle->getInfo()->ownerUid != injectionState->injectorUid)
Jeff Brownb6997262010-10-08 22:31:17 -07001578 && !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) {
Jeff Brown9302c872011-07-13 22:51:29 -07001579 if (windowHandle != NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00001580 ALOGW("Permission denied: injecting event from pid %d uid %d to window %s "
Jeff Brown9302c872011-07-13 22:51:29 -07001581 "owned by uid %d",
Jeff Brownb6997262010-10-08 22:31:17 -07001582 injectionState->injectorPid, injectionState->injectorUid,
Jeff Browncc4f7db2011-08-30 20:34:48 -07001583 windowHandle->getName().string(),
1584 windowHandle->getInfo()->ownerUid);
Jeff Brownb6997262010-10-08 22:31:17 -07001585 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00001586 ALOGW("Permission denied: injecting event from pid %d uid %d",
Jeff Brownb6997262010-10-08 22:31:17 -07001587 injectionState->injectorPid, injectionState->injectorUid);
Jeff Brownb88102f2010-09-08 11:49:43 -07001588 }
Jeff Brownb6997262010-10-08 22:31:17 -07001589 return false;
Jeff Brownb88102f2010-09-08 11:49:43 -07001590 }
1591 return true;
1592}
1593
Jeff Brown19dfc832010-10-05 12:26:23 -07001594bool InputDispatcher::isWindowObscuredAtPointLocked(
Jeff Brown9302c872011-07-13 22:51:29 -07001595 const sp<InputWindowHandle>& windowHandle, int32_t x, int32_t y) const {
1596 size_t numWindows = mWindowHandles.size();
Jeff Brownb88102f2010-09-08 11:49:43 -07001597 for (size_t i = 0; i < numWindows; i++) {
Jeff Brown9302c872011-07-13 22:51:29 -07001598 sp<InputWindowHandle> otherHandle = mWindowHandles.itemAt(i);
1599 if (otherHandle == windowHandle) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001600 break;
1601 }
Jeff Browncc4f7db2011-08-30 20:34:48 -07001602
1603 const InputWindowInfo* otherInfo = otherHandle->getInfo();
1604 if (otherInfo->visible && ! otherInfo->isTrustedOverlay()
1605 && otherInfo->frameContainsPoint(x, y)) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001606 return true;
1607 }
1608 }
1609 return false;
1610}
1611
Jeff Brown9302c872011-07-13 22:51:29 -07001612bool InputDispatcher::isWindowFinishedWithPreviousInputLocked(
1613 const sp<InputWindowHandle>& windowHandle) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001614 ssize_t connectionIndex = getConnectionIndexLocked(windowHandle->getInputChannel());
Jeff Brown519e0242010-09-15 15:18:56 -07001615 if (connectionIndex >= 0) {
Jeff Browncbee6d62012-02-03 20:11:27 -08001616 sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex);
Jeff Brown519e0242010-09-15 15:18:56 -07001617 return connection->outboundQueue.isEmpty();
1618 } else {
1619 return true;
1620 }
1621}
1622
Jeff Brown9302c872011-07-13 22:51:29 -07001623String8 InputDispatcher::getApplicationWindowLabelLocked(
1624 const sp<InputApplicationHandle>& applicationHandle,
1625 const sp<InputWindowHandle>& windowHandle) {
1626 if (applicationHandle != NULL) {
1627 if (windowHandle != NULL) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001628 String8 label(applicationHandle->getName());
Jeff Brown519e0242010-09-15 15:18:56 -07001629 label.append(" - ");
Jeff Browncc4f7db2011-08-30 20:34:48 -07001630 label.append(windowHandle->getName());
Jeff Brown519e0242010-09-15 15:18:56 -07001631 return label;
1632 } else {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001633 return applicationHandle->getName();
Jeff Brown519e0242010-09-15 15:18:56 -07001634 }
Jeff Brown9302c872011-07-13 22:51:29 -07001635 } else if (windowHandle != NULL) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001636 return windowHandle->getName();
Jeff Brown519e0242010-09-15 15:18:56 -07001637 } else {
1638 return String8("<unknown application or window>");
1639 }
1640}
1641
Jeff Browne2fe69e2010-10-18 13:21:23 -07001642void InputDispatcher::pokeUserActivityLocked(const EventEntry* eventEntry) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001643 int32_t eventType = POWER_MANAGER_OTHER_EVENT;
Jeff Brown4d396052010-10-29 21:50:21 -07001644 switch (eventEntry->type) {
1645 case EventEntry::TYPE_MOTION: {
Jeff Browne2fe69e2010-10-18 13:21:23 -07001646 const MotionEntry* motionEntry = static_cast<const MotionEntry*>(eventEntry);
Jeff Brown4d396052010-10-29 21:50:21 -07001647 if (motionEntry->action == AMOTION_EVENT_ACTION_CANCEL) {
1648 return;
1649 }
1650
Jeff Brown56194eb2011-03-02 19:23:13 -08001651 if (MotionEvent::isTouchEvent(motionEntry->source, motionEntry->action)) {
Joe Onorato1a542c72010-11-08 09:48:20 -08001652 eventType = POWER_MANAGER_TOUCH_EVENT;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001653 }
Jeff Brown4d396052010-10-29 21:50:21 -07001654 break;
1655 }
1656 case EventEntry::TYPE_KEY: {
1657 const KeyEntry* keyEntry = static_cast<const KeyEntry*>(eventEntry);
1658 if (keyEntry->flags & AKEY_EVENT_FLAG_CANCELED) {
1659 return;
1660 }
Jeff Brown56194eb2011-03-02 19:23:13 -08001661 eventType = POWER_MANAGER_BUTTON_EVENT;
Jeff Brown4d396052010-10-29 21:50:21 -07001662 break;
1663 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001664 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001665
Jeff Brownb88102f2010-09-08 11:49:43 -07001666 CommandEntry* commandEntry = postCommandLocked(
1667 & InputDispatcher::doPokeUserActivityLockedInterruptible);
Jeff Browne2fe69e2010-10-18 13:21:23 -07001668 commandEntry->eventTime = eventEntry->eventTime;
Jeff Brownb88102f2010-09-08 11:49:43 -07001669 commandEntry->userActivityEventType = eventType;
1670}
1671
Jeff Brown7fbdc842010-06-17 20:52:56 -07001672void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001673 const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001674#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001675 ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, "
Jeff Brown9cc695c2011-08-23 18:35:04 -07001676 "xOffset=%f, yOffset=%f, scaleFactor=%f, "
Jeff Brown3241b6b2012-02-03 15:08:02 -08001677 "pointerIds=0x%x",
Jeff Brown519e0242010-09-15 15:18:56 -07001678 connection->getInputChannelName(), inputTarget->flags,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001679 inputTarget->xOffset, inputTarget->yOffset,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001680 inputTarget->scaleFactor, inputTarget->pointerIds.value);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001681#endif
1682
1683 // Skip this event if the connection status is not normal.
Jeff Brown519e0242010-09-15 15:18:56 -07001684 // We don't want to enqueue additional outbound events if the connection is broken.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001685 if (connection->status != Connection::STATUS_NORMAL) {
Jeff Brownb6997262010-10-08 22:31:17 -07001686#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001687 ALOGD("channel '%s' ~ Dropping event because the channel status is %s",
Jeff Brownb88102f2010-09-08 11:49:43 -07001688 connection->getInputChannelName(), connection->getStatusLabel());
Jeff Brownb6997262010-10-08 22:31:17 -07001689#endif
Jeff Brown46b9ac02010-04-22 18:58:52 -07001690 return;
1691 }
1692
Jeff Brown01ce2e92010-09-26 22:20:12 -07001693 // Split a motion event if needed.
Jeff Brown3241b6b2012-02-03 15:08:02 -08001694 if (inputTarget->flags & InputTarget::FLAG_SPLIT) {
Steve Blockec193de2012-01-09 18:35:44 +00001695 ALOG_ASSERT(eventEntry->type == EventEntry::TYPE_MOTION);
Jeff Brown01ce2e92010-09-26 22:20:12 -07001696
1697 MotionEntry* originalMotionEntry = static_cast<MotionEntry*>(eventEntry);
1698 if (inputTarget->pointerIds.count() != originalMotionEntry->pointerCount) {
1699 MotionEntry* splitMotionEntry = splitMotionEvent(
1700 originalMotionEntry, inputTarget->pointerIds);
Jeff Brown58a2da82011-01-25 16:02:22 -08001701 if (!splitMotionEntry) {
1702 return; // split event was dropped
1703 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001704#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001705 ALOGD("channel '%s' ~ Split motion event.",
Jeff Brown01ce2e92010-09-26 22:20:12 -07001706 connection->getInputChannelName());
1707 logOutboundMotionDetailsLocked(" ", splitMotionEntry);
1708#endif
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001709 enqueueDispatchEntriesLocked(currentTime, connection,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001710 splitMotionEntry, inputTarget);
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001711 splitMotionEntry->release();
1712 return;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001713 }
1714 }
1715
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001716 // Not splitting. Enqueue dispatch entries for the event as is.
Jeff Brown3241b6b2012-02-03 15:08:02 -08001717 enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget);
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001718}
1719
1720void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001721 const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001722 bool wasEmpty = connection->outboundQueue.isEmpty();
1723
Jeff Browna032cc02011-03-07 16:56:21 -08001724 // Enqueue dispatch entries for the requested modes.
1725 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001726 InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT);
Jeff Browna032cc02011-03-07 16:56:21 -08001727 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001728 InputTarget::FLAG_DISPATCH_AS_OUTSIDE);
Jeff Browna032cc02011-03-07 16:56:21 -08001729 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001730 InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER);
Jeff Browna032cc02011-03-07 16:56:21 -08001731 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001732 InputTarget::FLAG_DISPATCH_AS_IS);
Jeff Brown98db5fa2011-06-08 15:37:10 -07001733 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001734 InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT);
Jeff Brown98db5fa2011-06-08 15:37:10 -07001735 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001736 InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER);
Jeff Browna032cc02011-03-07 16:56:21 -08001737
1738 // If the outbound queue was previously empty, start the dispatch cycle going.
Jeff Brownb6110c22011-04-01 16:15:13 -07001739 if (wasEmpty && !connection->outboundQueue.isEmpty()) {
Jeff Browna032cc02011-03-07 16:56:21 -08001740 activateConnectionLocked(connection.get());
1741 startDispatchCycleLocked(currentTime, connection);
1742 }
1743}
1744
1745void InputDispatcher::enqueueDispatchEntryLocked(
1746 const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001747 int32_t dispatchMode) {
Jeff Browna032cc02011-03-07 16:56:21 -08001748 int32_t inputTargetFlags = inputTarget->flags;
1749 if (!(inputTargetFlags & dispatchMode)) {
1750 return;
1751 }
1752 inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode;
1753
Jeff Brown46b9ac02010-04-22 18:58:52 -07001754 // This is a new event.
1755 // Enqueue a new dispatch entry onto the outbound queue for this connection.
Jeff Brownac386072011-07-20 15:19:50 -07001756 DispatchEntry* dispatchEntry = new DispatchEntry(eventEntry, // increments ref
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -07001757 inputTargetFlags, inputTarget->xOffset, inputTarget->yOffset,
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001758 inputTarget->scaleFactor);
Jeff Brown6ec402b2010-07-28 15:48:59 -07001759
Jeff Brown81346812011-06-28 20:08:48 -07001760 // Apply target flags and update the connection's input state.
1761 switch (eventEntry->type) {
1762 case EventEntry::TYPE_KEY: {
1763 KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry);
1764 dispatchEntry->resolvedAction = keyEntry->action;
1765 dispatchEntry->resolvedFlags = keyEntry->flags;
1766
1767 if (!connection->inputState.trackKey(keyEntry,
1768 dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags)) {
1769#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001770 ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event",
Jeff Brown81346812011-06-28 20:08:48 -07001771 connection->getInputChannelName());
1772#endif
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001773 delete dispatchEntry;
Jeff Brown81346812011-06-28 20:08:48 -07001774 return; // skip the inconsistent event
1775 }
1776 break;
1777 }
1778
1779 case EventEntry::TYPE_MOTION: {
1780 MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry);
1781 if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) {
1782 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE;
1783 } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) {
1784 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT;
1785 } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) {
1786 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER;
1787 } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) {
1788 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL;
1789 } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) {
1790 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN;
1791 } else {
1792 dispatchEntry->resolvedAction = motionEntry->action;
1793 }
1794 if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE
1795 && !connection->inputState.isHovering(
1796 motionEntry->deviceId, motionEntry->source)) {
1797#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001798 ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter event",
Jeff Brown81346812011-06-28 20:08:48 -07001799 connection->getInputChannelName());
1800#endif
1801 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER;
1802 }
1803
1804 dispatchEntry->resolvedFlags = motionEntry->flags;
1805 if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) {
1806 dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
1807 }
1808
1809 if (!connection->inputState.trackMotion(motionEntry,
1810 dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags)) {
1811#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001812 ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion event",
Jeff Brown81346812011-06-28 20:08:48 -07001813 connection->getInputChannelName());
1814#endif
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001815 delete dispatchEntry;
Jeff Brown81346812011-06-28 20:08:48 -07001816 return; // skip the inconsistent event
1817 }
1818 break;
1819 }
1820 }
1821
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001822 // Remember that we are waiting for this dispatch to complete.
1823 if (dispatchEntry->hasForegroundTarget()) {
1824 incrementPendingForegroundDispatchesLocked(eventEntry);
1825 }
1826
Jeff Brown46b9ac02010-04-22 18:58:52 -07001827 // Enqueue the dispatch entry.
1828 connection->outboundQueue.enqueueAtTail(dispatchEntry);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001829}
1830
Jeff Brown7fbdc842010-06-17 20:52:56 -07001831void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime,
Jeff Brown519e0242010-09-15 15:18:56 -07001832 const sp<Connection>& connection) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001833#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001834 ALOGD("channel '%s' ~ startDispatchCycle",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001835 connection->getInputChannelName());
1836#endif
1837
Steve Blockec193de2012-01-09 18:35:44 +00001838 ALOG_ASSERT(connection->status == Connection::STATUS_NORMAL);
1839 ALOG_ASSERT(! connection->outboundQueue.isEmpty());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001840
Jeff Brownac386072011-07-20 15:19:50 -07001841 DispatchEntry* dispatchEntry = connection->outboundQueue.head;
Steve Blockec193de2012-01-09 18:35:44 +00001842 ALOG_ASSERT(! dispatchEntry->inProgress);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001843
Jeff Brownb88102f2010-09-08 11:49:43 -07001844 // Mark the dispatch entry as in progress.
1845 dispatchEntry->inProgress = true;
1846
Jeff Brown46b9ac02010-04-22 18:58:52 -07001847 // Publish the event.
1848 status_t status;
Jeff Browna032cc02011-03-07 16:56:21 -08001849 EventEntry* eventEntry = dispatchEntry->eventEntry;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001850 switch (eventEntry->type) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001851 case EventEntry::TYPE_KEY: {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001852 KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001853
Jeff Brown46b9ac02010-04-22 18:58:52 -07001854 // Publish the key event.
Jeff Brown81346812011-06-28 20:08:48 -07001855 status = connection->inputPublisher.publishKeyEvent(
1856 keyEntry->deviceId, keyEntry->source,
1857 dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags,
1858 keyEntry->keyCode, keyEntry->scanCode,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001859 keyEntry->metaState, keyEntry->repeatCount, keyEntry->downTime,
1860 keyEntry->eventTime);
1861
1862 if (status) {
Steve Block3762c312012-01-06 19:20:56 +00001863 ALOGE("channel '%s' ~ Could not publish key event, "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001864 "status=%d", connection->getInputChannelName(), status);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001865 abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001866 return;
1867 }
1868 break;
1869 }
1870
1871 case EventEntry::TYPE_MOTION: {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001872 MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001873
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001874 PointerCoords scaledCoords[MAX_POINTERS];
Jeff Brown3241b6b2012-02-03 15:08:02 -08001875 const PointerCoords* usingCoords = motionEntry->pointerCoords;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001876
Jeff Brownd3616592010-07-16 17:21:06 -07001877 // Set the X and Y offset depending on the input source.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001878 float xOffset, yOffset, scaleFactor;
Kenny Root7a9db182011-06-02 15:16:05 -07001879 if (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER
1880 && !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001881 scaleFactor = dispatchEntry->scaleFactor;
1882 xOffset = dispatchEntry->xOffset * scaleFactor;
1883 yOffset = dispatchEntry->yOffset * scaleFactor;
1884 if (scaleFactor != 1.0f) {
1885 for (size_t i = 0; i < motionEntry->pointerCount; i++) {
Jeff Brown3241b6b2012-02-03 15:08:02 -08001886 scaledCoords[i] = motionEntry->pointerCoords[i];
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001887 scaledCoords[i].scale(scaleFactor);
1888 }
1889 usingCoords = scaledCoords;
1890 }
Jeff Brownd3616592010-07-16 17:21:06 -07001891 } else {
1892 xOffset = 0.0f;
1893 yOffset = 0.0f;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001894 scaleFactor = 1.0f;
Kenny Root7a9db182011-06-02 15:16:05 -07001895
1896 // We don't want the dispatch target to know.
1897 if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) {
1898 for (size_t i = 0; i < motionEntry->pointerCount; i++) {
1899 scaledCoords[i].clear();
1900 }
1901 usingCoords = scaledCoords;
1902 }
Jeff Brownd3616592010-07-16 17:21:06 -07001903 }
1904
Jeff Brown3241b6b2012-02-03 15:08:02 -08001905 // Publish the motion event.
Jeff Brown81346812011-06-28 20:08:48 -07001906 status = connection->inputPublisher.publishMotionEvent(
1907 motionEntry->deviceId, motionEntry->source,
1908 dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags,
1909 motionEntry->edgeFlags, motionEntry->metaState, motionEntry->buttonState,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001910 xOffset, yOffset,
1911 motionEntry->xPrecision, motionEntry->yPrecision,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001912 motionEntry->downTime, motionEntry->eventTime,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001913 motionEntry->pointerCount, motionEntry->pointerProperties,
1914 usingCoords);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001915
1916 if (status) {
Steve Block3762c312012-01-06 19:20:56 +00001917 ALOGE("channel '%s' ~ Could not publish motion event, "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001918 "status=%d", connection->getInputChannelName(), status);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001919 abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001920 return;
1921 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001922 break;
1923 }
1924
1925 default: {
Steve Blockec193de2012-01-09 18:35:44 +00001926 ALOG_ASSERT(false);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001927 }
1928 }
1929
Jeff Brown46b9ac02010-04-22 18:58:52 -07001930 // Record information about the newly started dispatch cycle.
Jeff Brown01ce2e92010-09-26 22:20:12 -07001931 connection->lastEventTime = eventEntry->eventTime;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001932 connection->lastDispatchTime = currentTime;
1933
Jeff Brown46b9ac02010-04-22 18:58:52 -07001934 // Notify other system components.
1935 onDispatchCycleStartedLocked(currentTime, connection);
1936}
1937
Jeff Brown7fbdc842010-06-17 20:52:56 -07001938void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime,
Jeff Brown3915bb82010-11-05 15:02:16 -07001939 const sp<Connection>& connection, bool handled) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001940#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001941 ALOGD("channel '%s' ~ finishDispatchCycle - %01.1fms since event, "
Jeff Brown3915bb82010-11-05 15:02:16 -07001942 "%01.1fms since dispatch, handled=%s",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001943 connection->getInputChannelName(),
1944 connection->getEventLatencyMillis(currentTime),
Jeff Brown3915bb82010-11-05 15:02:16 -07001945 connection->getDispatchLatencyMillis(currentTime),
1946 toString(handled));
Jeff Brown46b9ac02010-04-22 18:58:52 -07001947#endif
1948
Jeff Brown9c3cda02010-06-15 01:31:58 -07001949 if (connection->status == Connection::STATUS_BROKEN
1950 || connection->status == Connection::STATUS_ZOMBIE) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001951 return;
1952 }
1953
Jeff Brown3915bb82010-11-05 15:02:16 -07001954 // Notify other system components and prepare to start the next dispatch cycle.
1955 onDispatchCycleFinishedLocked(currentTime, connection, handled);
Jeff Brownb88102f2010-09-08 11:49:43 -07001956}
1957
1958void InputDispatcher::startNextDispatchCycleLocked(nsecs_t currentTime,
1959 const sp<Connection>& connection) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001960 // Start the next dispatch cycle for this connection.
1961 while (! connection->outboundQueue.isEmpty()) {
Jeff Brownac386072011-07-20 15:19:50 -07001962 DispatchEntry* dispatchEntry = connection->outboundQueue.head;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001963 if (dispatchEntry->inProgress) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001964 // Finished.
1965 connection->outboundQueue.dequeueAtHead();
Jeff Brown519e0242010-09-15 15:18:56 -07001966 if (dispatchEntry->hasForegroundTarget()) {
1967 decrementPendingForegroundDispatchesLocked(dispatchEntry->eventEntry);
Jeff Brown6ec402b2010-07-28 15:48:59 -07001968 }
Jeff Brownac386072011-07-20 15:19:50 -07001969 delete dispatchEntry;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001970 } else {
1971 // If the head is not in progress, then we must have already dequeued the in
Jeff Brown519e0242010-09-15 15:18:56 -07001972 // progress event, which means we actually aborted it.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001973 // So just start the next event for this connection.
Jeff Brown519e0242010-09-15 15:18:56 -07001974 startDispatchCycleLocked(currentTime, connection);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001975 return;
1976 }
1977 }
1978
1979 // Outbound queue is empty, deactivate the connection.
Jeff Brown7fbdc842010-06-17 20:52:56 -07001980 deactivateConnectionLocked(connection.get());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001981}
1982
Jeff Brownb6997262010-10-08 22:31:17 -07001983void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime,
Jeff Browncc4f7db2011-08-30 20:34:48 -07001984 const sp<Connection>& connection, bool notify) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001985#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001986 ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s",
Jeff Browncc4f7db2011-08-30 20:34:48 -07001987 connection->getInputChannelName(), toString(notify));
Jeff Brown46b9ac02010-04-22 18:58:52 -07001988#endif
1989
Jeff Brownb88102f2010-09-08 11:49:43 -07001990 // Clear the outbound queue.
Jeff Brown519e0242010-09-15 15:18:56 -07001991 drainOutboundQueueLocked(connection.get());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001992
Jeff Brownb6997262010-10-08 22:31:17 -07001993 // The connection appears to be unrecoverably broken.
Jeff Brown9c3cda02010-06-15 01:31:58 -07001994 // Ignore already broken or zombie connections.
Jeff Brownb6997262010-10-08 22:31:17 -07001995 if (connection->status == Connection::STATUS_NORMAL) {
1996 connection->status = Connection::STATUS_BROKEN;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001997
Jeff Browncc4f7db2011-08-30 20:34:48 -07001998 if (notify) {
1999 // Notify other system components.
2000 onDispatchCycleBrokenLocked(currentTime, connection);
2001 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002002 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002003}
2004
Jeff Brown519e0242010-09-15 15:18:56 -07002005void InputDispatcher::drainOutboundQueueLocked(Connection* connection) {
2006 while (! connection->outboundQueue.isEmpty()) {
2007 DispatchEntry* dispatchEntry = connection->outboundQueue.dequeueAtHead();
2008 if (dispatchEntry->hasForegroundTarget()) {
2009 decrementPendingForegroundDispatchesLocked(dispatchEntry->eventEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07002010 }
Jeff Brownac386072011-07-20 15:19:50 -07002011 delete dispatchEntry;
Jeff Brownb88102f2010-09-08 11:49:43 -07002012 }
2013
Jeff Brown519e0242010-09-15 15:18:56 -07002014 deactivateConnectionLocked(connection);
Jeff Brownb88102f2010-09-08 11:49:43 -07002015}
2016
Jeff Browncbee6d62012-02-03 20:11:27 -08002017int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002018 InputDispatcher* d = static_cast<InputDispatcher*>(data);
2019
2020 { // acquire lock
2021 AutoMutex _l(d->mLock);
2022
Jeff Browncbee6d62012-02-03 20:11:27 -08002023 ssize_t connectionIndex = d->mConnectionsByFd.indexOfKey(fd);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002024 if (connectionIndex < 0) {
Steve Block3762c312012-01-06 19:20:56 +00002025 ALOGE("Received spurious receive callback for unknown input channel. "
Jeff Browncbee6d62012-02-03 20:11:27 -08002026 "fd=%d, events=0x%x", fd, events);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002027 return 0; // remove the callback
Jeff Brown46b9ac02010-04-22 18:58:52 -07002028 }
2029
Jeff Browncc4f7db2011-08-30 20:34:48 -07002030 bool notify;
Jeff Browncbee6d62012-02-03 20:11:27 -08002031 sp<Connection> connection = d->mConnectionsByFd.valueAt(connectionIndex);
Jeff Browncc4f7db2011-08-30 20:34:48 -07002032 if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) {
2033 if (!(events & ALOOPER_EVENT_INPUT)) {
Steve Block8564c8d2012-01-05 23:22:43 +00002034 ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. "
Jeff Browncc4f7db2011-08-30 20:34:48 -07002035 "events=0x%x", connection->getInputChannelName(), events);
2036 return 1;
2037 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002038
Jeff Browncc4f7db2011-08-30 20:34:48 -07002039 bool handled = false;
2040 status_t status = connection->inputPublisher.receiveFinishedSignal(&handled);
2041 if (!status) {
2042 nsecs_t currentTime = now();
2043 d->finishDispatchCycleLocked(currentTime, connection, handled);
2044 d->runCommandsLockedInterruptible();
2045 return 1;
2046 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002047
Steve Block3762c312012-01-06 19:20:56 +00002048 ALOGE("channel '%s' ~ Failed to receive finished signal. status=%d",
Jeff Brown46b9ac02010-04-22 18:58:52 -07002049 connection->getInputChannelName(), status);
Jeff Browncc4f7db2011-08-30 20:34:48 -07002050 notify = true;
2051 } else {
2052 // Monitor channels are never explicitly unregistered.
2053 // We do it automatically when the remote endpoint is closed so don't warn
2054 // about them.
2055 notify = !connection->monitor;
2056 if (notify) {
Steve Block8564c8d2012-01-05 23:22:43 +00002057 ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. "
Jeff Browncc4f7db2011-08-30 20:34:48 -07002058 "events=0x%x", connection->getInputChannelName(), events);
2059 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002060 }
2061
Jeff Browncc4f7db2011-08-30 20:34:48 -07002062 // Unregister the channel.
2063 d->unregisterInputChannelLocked(connection->inputChannel, notify);
2064 return 0; // remove the callback
Jeff Brown46b9ac02010-04-22 18:58:52 -07002065 } // release lock
2066}
2067
Jeff Brownb6997262010-10-08 22:31:17 -07002068void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked(
Jeff Brownda3d5a92011-03-29 15:11:34 -07002069 const CancelationOptions& options) {
Jeff Browncbee6d62012-02-03 20:11:27 -08002070 for (size_t i = 0; i < mConnectionsByFd.size(); i++) {
Jeff Brownb6997262010-10-08 22:31:17 -07002071 synthesizeCancelationEventsForConnectionLocked(
Jeff Browncbee6d62012-02-03 20:11:27 -08002072 mConnectionsByFd.valueAt(i), options);
Jeff Brownb6997262010-10-08 22:31:17 -07002073 }
2074}
2075
2076void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked(
Jeff Brownda3d5a92011-03-29 15:11:34 -07002077 const sp<InputChannel>& channel, const CancelationOptions& options) {
Jeff Brownb6997262010-10-08 22:31:17 -07002078 ssize_t index = getConnectionIndexLocked(channel);
2079 if (index >= 0) {
2080 synthesizeCancelationEventsForConnectionLocked(
Jeff Browncbee6d62012-02-03 20:11:27 -08002081 mConnectionsByFd.valueAt(index), options);
Jeff Brownb6997262010-10-08 22:31:17 -07002082 }
2083}
2084
2085void InputDispatcher::synthesizeCancelationEventsForConnectionLocked(
Jeff Brownda3d5a92011-03-29 15:11:34 -07002086 const sp<Connection>& connection, const CancelationOptions& options) {
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08002087 if (connection->status == Connection::STATUS_BROKEN) {
2088 return;
2089 }
2090
Jeff Brownb6997262010-10-08 22:31:17 -07002091 nsecs_t currentTime = now();
2092
2093 mTempCancelationEvents.clear();
Jeff Brownac386072011-07-20 15:19:50 -07002094 connection->inputState.synthesizeCancelationEvents(currentTime,
Jeff Brownb6997262010-10-08 22:31:17 -07002095 mTempCancelationEvents, options);
2096
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08002097 if (!mTempCancelationEvents.isEmpty()) {
Jeff Brownb6997262010-10-08 22:31:17 -07002098#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002099 ALOGD("channel '%s' ~ Synthesized %d cancelation events to bring channel back in sync "
Jeff Brownda3d5a92011-03-29 15:11:34 -07002100 "with reality: %s, mode=%d.",
2101 connection->getInputChannelName(), mTempCancelationEvents.size(),
2102 options.reason, options.mode);
Jeff Brownb6997262010-10-08 22:31:17 -07002103#endif
2104 for (size_t i = 0; i < mTempCancelationEvents.size(); i++) {
2105 EventEntry* cancelationEventEntry = mTempCancelationEvents.itemAt(i);
2106 switch (cancelationEventEntry->type) {
2107 case EventEntry::TYPE_KEY:
2108 logOutboundKeyDetailsLocked("cancel - ",
2109 static_cast<KeyEntry*>(cancelationEventEntry));
2110 break;
2111 case EventEntry::TYPE_MOTION:
2112 logOutboundMotionDetailsLocked("cancel - ",
2113 static_cast<MotionEntry*>(cancelationEventEntry));
2114 break;
2115 }
2116
Jeff Brown81346812011-06-28 20:08:48 -07002117 InputTarget target;
Jeff Brown9302c872011-07-13 22:51:29 -07002118 sp<InputWindowHandle> windowHandle = getWindowHandleLocked(connection->inputChannel);
2119 if (windowHandle != NULL) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07002120 const InputWindowInfo* windowInfo = windowHandle->getInfo();
2121 target.xOffset = -windowInfo->frameLeft;
2122 target.yOffset = -windowInfo->frameTop;
2123 target.scaleFactor = windowInfo->scaleFactor;
Jeff Brownb6997262010-10-08 22:31:17 -07002124 } else {
Jeff Brown81346812011-06-28 20:08:48 -07002125 target.xOffset = 0;
2126 target.yOffset = 0;
2127 target.scaleFactor = 1.0f;
Jeff Brownb6997262010-10-08 22:31:17 -07002128 }
Jeff Brown81346812011-06-28 20:08:48 -07002129 target.inputChannel = connection->inputChannel;
2130 target.flags = InputTarget::FLAG_DISPATCH_AS_IS;
Jeff Brownb6997262010-10-08 22:31:17 -07002131
Jeff Brown81346812011-06-28 20:08:48 -07002132 enqueueDispatchEntryLocked(connection, cancelationEventEntry, // increments ref
Jeff Brown3241b6b2012-02-03 15:08:02 -08002133 &target, InputTarget::FLAG_DISPATCH_AS_IS);
Jeff Brownb6997262010-10-08 22:31:17 -07002134
Jeff Brownac386072011-07-20 15:19:50 -07002135 cancelationEventEntry->release();
Jeff Brownb6997262010-10-08 22:31:17 -07002136 }
2137
Jeff Brownac386072011-07-20 15:19:50 -07002138 if (!connection->outboundQueue.head->inProgress) {
Jeff Brownb6997262010-10-08 22:31:17 -07002139 startDispatchCycleLocked(currentTime, connection);
2140 }
2141 }
2142}
2143
Jeff Brown01ce2e92010-09-26 22:20:12 -07002144InputDispatcher::MotionEntry*
2145InputDispatcher::splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds) {
Steve Blockec193de2012-01-09 18:35:44 +00002146 ALOG_ASSERT(pointerIds.value != 0);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002147
2148 uint32_t splitPointerIndexMap[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002149 PointerProperties splitPointerProperties[MAX_POINTERS];
Jeff Brown01ce2e92010-09-26 22:20:12 -07002150 PointerCoords splitPointerCoords[MAX_POINTERS];
2151
2152 uint32_t originalPointerCount = originalMotionEntry->pointerCount;
2153 uint32_t splitPointerCount = 0;
2154
2155 for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount;
2156 originalPointerIndex++) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002157 const PointerProperties& pointerProperties =
2158 originalMotionEntry->pointerProperties[originalPointerIndex];
2159 uint32_t pointerId = uint32_t(pointerProperties.id);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002160 if (pointerIds.hasBit(pointerId)) {
2161 splitPointerIndexMap[splitPointerCount] = originalPointerIndex;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002162 splitPointerProperties[splitPointerCount].copyFrom(pointerProperties);
Jeff Brownace13b12011-03-09 17:39:48 -08002163 splitPointerCoords[splitPointerCount].copyFrom(
Jeff Brown3241b6b2012-02-03 15:08:02 -08002164 originalMotionEntry->pointerCoords[originalPointerIndex]);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002165 splitPointerCount += 1;
2166 }
2167 }
Jeff Brown58a2da82011-01-25 16:02:22 -08002168
2169 if (splitPointerCount != pointerIds.count()) {
2170 // This is bad. We are missing some of the pointers that we expected to deliver.
2171 // Most likely this indicates that we received an ACTION_MOVE events that has
2172 // different pointer ids than we expected based on the previous ACTION_DOWN
2173 // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers
2174 // in this way.
Steve Block8564c8d2012-01-05 23:22:43 +00002175 ALOGW("Dropping split motion event because the pointer count is %d but "
Jeff Brown58a2da82011-01-25 16:02:22 -08002176 "we expected there to be %d pointers. This probably means we received "
2177 "a broken sequence of pointer ids from the input device.",
2178 splitPointerCount, pointerIds.count());
2179 return NULL;
2180 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07002181
2182 int32_t action = originalMotionEntry->action;
2183 int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK;
2184 if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN
2185 || maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) {
2186 int32_t originalPointerIndex = getMotionEventActionPointerIndex(action);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002187 const PointerProperties& pointerProperties =
2188 originalMotionEntry->pointerProperties[originalPointerIndex];
2189 uint32_t pointerId = uint32_t(pointerProperties.id);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002190 if (pointerIds.hasBit(pointerId)) {
2191 if (pointerIds.count() == 1) {
2192 // The first/last pointer went down/up.
2193 action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN
2194 ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
Jeff Brown9a01d052010-09-27 16:35:11 -07002195 } else {
2196 // A secondary pointer went down/up.
2197 uint32_t splitPointerIndex = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002198 while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) {
Jeff Brown9a01d052010-09-27 16:35:11 -07002199 splitPointerIndex += 1;
2200 }
2201 action = maskedAction | (splitPointerIndex
2202 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002203 }
2204 } else {
2205 // An unrelated pointer changed.
2206 action = AMOTION_EVENT_ACTION_MOVE;
2207 }
2208 }
2209
Jeff Brownac386072011-07-20 15:19:50 -07002210 MotionEntry* splitMotionEntry = new MotionEntry(
Jeff Brown01ce2e92010-09-26 22:20:12 -07002211 originalMotionEntry->eventTime,
2212 originalMotionEntry->deviceId,
2213 originalMotionEntry->source,
2214 originalMotionEntry->policyFlags,
2215 action,
2216 originalMotionEntry->flags,
2217 originalMotionEntry->metaState,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002218 originalMotionEntry->buttonState,
Jeff Brown01ce2e92010-09-26 22:20:12 -07002219 originalMotionEntry->edgeFlags,
2220 originalMotionEntry->xPrecision,
2221 originalMotionEntry->yPrecision,
2222 originalMotionEntry->downTime,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002223 splitPointerCount, splitPointerProperties, splitPointerCoords);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002224
Jeff Browna032cc02011-03-07 16:56:21 -08002225 if (originalMotionEntry->injectionState) {
2226 splitMotionEntry->injectionState = originalMotionEntry->injectionState;
2227 splitMotionEntry->injectionState->refCount += 1;
2228 }
2229
Jeff Brown01ce2e92010-09-26 22:20:12 -07002230 return splitMotionEntry;
2231}
2232
Jeff Brownbe1aa822011-07-27 16:04:54 -07002233void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002234#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002235 ALOGD("notifyConfigurationChanged - eventTime=%lld", args->eventTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002236#endif
2237
Jeff Brownb88102f2010-09-08 11:49:43 -07002238 bool needWake;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002239 { // acquire lock
2240 AutoMutex _l(mLock);
2241
Jeff Brownbe1aa822011-07-27 16:04:54 -07002242 ConfigurationChangedEntry* newEntry = new ConfigurationChangedEntry(args->eventTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07002243 needWake = enqueueInboundEventLocked(newEntry);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002244 } // release lock
2245
Jeff Brownb88102f2010-09-08 11:49:43 -07002246 if (needWake) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002247 mLooper->wake();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002248 }
2249}
2250
Jeff Brownbe1aa822011-07-27 16:04:54 -07002251void InputDispatcher::notifyKey(const NotifyKeyArgs* args) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002252#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002253 ALOGD("notifyKey - eventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, action=0x%x, "
Jeff Brown46b9ac02010-04-22 18:58:52 -07002254 "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07002255 args->eventTime, args->deviceId, args->source, args->policyFlags,
2256 args->action, args->flags, args->keyCode, args->scanCode,
2257 args->metaState, args->downTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002258#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07002259 if (!validateKeyEvent(args->action)) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002260 return;
2261 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002262
Jeff Brownbe1aa822011-07-27 16:04:54 -07002263 uint32_t policyFlags = args->policyFlags;
2264 int32_t flags = args->flags;
2265 int32_t metaState = args->metaState;
Jeff Brown1f245102010-11-18 20:53:46 -08002266 if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) {
2267 policyFlags |= POLICY_FLAG_VIRTUAL;
2268 flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2269 }
Jeff Brown924c4d42011-03-07 16:40:47 -08002270 if (policyFlags & POLICY_FLAG_ALT) {
2271 metaState |= AMETA_ALT_ON | AMETA_ALT_LEFT_ON;
2272 }
2273 if (policyFlags & POLICY_FLAG_ALT_GR) {
2274 metaState |= AMETA_ALT_ON | AMETA_ALT_RIGHT_ON;
2275 }
2276 if (policyFlags & POLICY_FLAG_SHIFT) {
2277 metaState |= AMETA_SHIFT_ON | AMETA_SHIFT_LEFT_ON;
2278 }
2279 if (policyFlags & POLICY_FLAG_CAPS_LOCK) {
2280 metaState |= AMETA_CAPS_LOCK_ON;
2281 }
2282 if (policyFlags & POLICY_FLAG_FUNCTION) {
2283 metaState |= AMETA_FUNCTION_ON;
2284 }
Jeff Brown1f245102010-11-18 20:53:46 -08002285
Jeff Browne20c9e02010-10-11 14:20:19 -07002286 policyFlags |= POLICY_FLAG_TRUSTED;
Jeff Brown1f245102010-11-18 20:53:46 -08002287
2288 KeyEvent event;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002289 event.initialize(args->deviceId, args->source, args->action,
2290 flags, args->keyCode, args->scanCode, metaState, 0,
2291 args->downTime, args->eventTime);
Jeff Brown1f245102010-11-18 20:53:46 -08002292
2293 mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags);
2294
2295 if (policyFlags & POLICY_FLAG_WOKE_HERE) {
2296 flags |= AKEY_EVENT_FLAG_WOKE_HERE;
2297 }
Jeff Brownb6997262010-10-08 22:31:17 -07002298
Jeff Brownb88102f2010-09-08 11:49:43 -07002299 bool needWake;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002300 { // acquire lock
Jeff Brown0029c662011-03-30 02:25:18 -07002301 mLock.lock();
2302
2303 if (mInputFilterEnabled) {
2304 mLock.unlock();
2305
2306 policyFlags |= POLICY_FLAG_FILTERED;
2307 if (!mPolicy->filterInputEvent(&event, policyFlags)) {
2308 return; // event was consumed by the filter
2309 }
2310
2311 mLock.lock();
2312 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002313
Jeff Brown7fbdc842010-06-17 20:52:56 -07002314 int32_t repeatCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002315 KeyEntry* newEntry = new KeyEntry(args->eventTime,
2316 args->deviceId, args->source, policyFlags,
2317 args->action, flags, args->keyCode, args->scanCode,
2318 metaState, repeatCount, args->downTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002319
Jeff Brownb88102f2010-09-08 11:49:43 -07002320 needWake = enqueueInboundEventLocked(newEntry);
Jeff Brown0029c662011-03-30 02:25:18 -07002321 mLock.unlock();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002322 } // release lock
2323
Jeff Brownb88102f2010-09-08 11:49:43 -07002324 if (needWake) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002325 mLooper->wake();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002326 }
2327}
2328
Jeff Brownbe1aa822011-07-27 16:04:54 -07002329void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002330#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002331 ALOGD("notifyMotion - eventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002332 "action=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, edgeFlags=0x%x, "
Jeff Brown85a31762010-09-01 17:01:00 -07002333 "xPrecision=%f, yPrecision=%f, downTime=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07002334 args->eventTime, args->deviceId, args->source, args->policyFlags,
2335 args->action, args->flags, args->metaState, args->buttonState,
2336 args->edgeFlags, args->xPrecision, args->yPrecision, args->downTime);
2337 for (uint32_t i = 0; i < args->pointerCount; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00002338 ALOGD(" Pointer %d: id=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002339 "x=%f, y=%f, pressure=%f, size=%f, "
Jeff Brown85a31762010-09-01 17:01:00 -07002340 "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, "
Jeff Brown8d608662010-08-30 03:02:23 -07002341 "orientation=%f",
Jeff Brownbe1aa822011-07-27 16:04:54 -07002342 i, args->pointerProperties[i].id,
2343 args->pointerProperties[i].toolType,
2344 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
2345 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
2346 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2347 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
2348 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2349 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2350 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2351 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2352 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
Jeff Brown46b9ac02010-04-22 18:58:52 -07002353 }
2354#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07002355 if (!validateMotionEvent(args->action, args->pointerCount, args->pointerProperties)) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002356 return;
2357 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002358
Jeff Brownbe1aa822011-07-27 16:04:54 -07002359 uint32_t policyFlags = args->policyFlags;
Jeff Browne20c9e02010-10-11 14:20:19 -07002360 policyFlags |= POLICY_FLAG_TRUSTED;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002361 mPolicy->interceptMotionBeforeQueueing(args->eventTime, /*byref*/ policyFlags);
Jeff Brownb6997262010-10-08 22:31:17 -07002362
Jeff Brownb88102f2010-09-08 11:49:43 -07002363 bool needWake;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002364 { // acquire lock
Jeff Brown0029c662011-03-30 02:25:18 -07002365 mLock.lock();
2366
2367 if (mInputFilterEnabled) {
2368 mLock.unlock();
2369
2370 MotionEvent event;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002371 event.initialize(args->deviceId, args->source, args->action, args->flags,
2372 args->edgeFlags, args->metaState, args->buttonState, 0, 0,
2373 args->xPrecision, args->yPrecision,
2374 args->downTime, args->eventTime,
2375 args->pointerCount, args->pointerProperties, args->pointerCoords);
Jeff Brown0029c662011-03-30 02:25:18 -07002376
2377 policyFlags |= POLICY_FLAG_FILTERED;
2378 if (!mPolicy->filterInputEvent(&event, policyFlags)) {
2379 return; // event was consumed by the filter
2380 }
2381
2382 mLock.lock();
2383 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002384
Jeff Brown46b9ac02010-04-22 18:58:52 -07002385 // Just enqueue a new motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002386 MotionEntry* newEntry = new MotionEntry(args->eventTime,
2387 args->deviceId, args->source, policyFlags,
2388 args->action, args->flags, args->metaState, args->buttonState,
2389 args->edgeFlags, args->xPrecision, args->yPrecision, args->downTime,
2390 args->pointerCount, args->pointerProperties, args->pointerCoords);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002391
Jeff Brownb88102f2010-09-08 11:49:43 -07002392 needWake = enqueueInboundEventLocked(newEntry);
Jeff Brown0029c662011-03-30 02:25:18 -07002393 mLock.unlock();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002394 } // release lock
2395
Jeff Brownb88102f2010-09-08 11:49:43 -07002396 if (needWake) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002397 mLooper->wake();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002398 }
2399}
2400
Jeff Brownbe1aa822011-07-27 16:04:54 -07002401void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) {
Jeff Brownb6997262010-10-08 22:31:17 -07002402#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002403 ALOGD("notifySwitch - eventTime=%lld, policyFlags=0x%x, switchCode=%d, switchValue=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07002404 args->eventTime, args->policyFlags,
2405 args->switchCode, args->switchValue);
Jeff Brownb6997262010-10-08 22:31:17 -07002406#endif
2407
Jeff Brownbe1aa822011-07-27 16:04:54 -07002408 uint32_t policyFlags = args->policyFlags;
Jeff Browne20c9e02010-10-11 14:20:19 -07002409 policyFlags |= POLICY_FLAG_TRUSTED;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002410 mPolicy->notifySwitch(args->eventTime,
2411 args->switchCode, args->switchValue, policyFlags);
Jeff Brownb6997262010-10-08 22:31:17 -07002412}
2413
Jeff Brown65fd2512011-08-18 11:20:58 -07002414void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) {
2415#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002416 ALOGD("notifyDeviceReset - eventTime=%lld, deviceId=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07002417 args->eventTime, args->deviceId);
2418#endif
2419
2420 bool needWake;
2421 { // acquire lock
2422 AutoMutex _l(mLock);
2423
2424 DeviceResetEntry* newEntry = new DeviceResetEntry(args->eventTime, args->deviceId);
2425 needWake = enqueueInboundEventLocked(newEntry);
2426 } // release lock
2427
2428 if (needWake) {
2429 mLooper->wake();
2430 }
2431}
2432
Jeff Brown7fbdc842010-06-17 20:52:56 -07002433int32_t InputDispatcher::injectInputEvent(const InputEvent* event,
Jeff Brown0029c662011-03-30 02:25:18 -07002434 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
2435 uint32_t policyFlags) {
Jeff Brown7fbdc842010-06-17 20:52:56 -07002436#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002437 ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, "
Jeff Brown0029c662011-03-30 02:25:18 -07002438 "syncMode=%d, timeoutMillis=%d, policyFlags=0x%08x",
2439 event->getType(), injectorPid, injectorUid, syncMode, timeoutMillis, policyFlags);
Jeff Brown7fbdc842010-06-17 20:52:56 -07002440#endif
2441
2442 nsecs_t endTime = now() + milliseconds_to_nanoseconds(timeoutMillis);
Jeff Browne20c9e02010-10-11 14:20:19 -07002443
Jeff Brown0029c662011-03-30 02:25:18 -07002444 policyFlags |= POLICY_FLAG_INJECTED;
Jeff Browne20c9e02010-10-11 14:20:19 -07002445 if (hasInjectionPermission(injectorPid, injectorUid)) {
2446 policyFlags |= POLICY_FLAG_TRUSTED;
2447 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07002448
Jeff Brown3241b6b2012-02-03 15:08:02 -08002449 EventEntry* firstInjectedEntry;
2450 EventEntry* lastInjectedEntry;
Jeff Brownb6997262010-10-08 22:31:17 -07002451 switch (event->getType()) {
2452 case AINPUT_EVENT_TYPE_KEY: {
2453 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(event);
2454 int32_t action = keyEvent->getAction();
2455 if (! validateKeyEvent(action)) {
Jeff Brownb88102f2010-09-08 11:49:43 -07002456 return INPUT_EVENT_INJECTION_FAILED;
2457 }
2458
Jeff Brownb6997262010-10-08 22:31:17 -07002459 int32_t flags = keyEvent->getFlags();
Jeff Brown1f245102010-11-18 20:53:46 -08002460 if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) {
2461 policyFlags |= POLICY_FLAG_VIRTUAL;
2462 }
2463
Jeff Brown0029c662011-03-30 02:25:18 -07002464 if (!(policyFlags & POLICY_FLAG_FILTERED)) {
2465 mPolicy->interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags);
2466 }
Jeff Brown1f245102010-11-18 20:53:46 -08002467
2468 if (policyFlags & POLICY_FLAG_WOKE_HERE) {
2469 flags |= AKEY_EVENT_FLAG_WOKE_HERE;
2470 }
Jeff Brown6ec402b2010-07-28 15:48:59 -07002471
Jeff Brownb6997262010-10-08 22:31:17 -07002472 mLock.lock();
Jeff Brown3241b6b2012-02-03 15:08:02 -08002473 firstInjectedEntry = new KeyEntry(keyEvent->getEventTime(),
Jeff Brown1f245102010-11-18 20:53:46 -08002474 keyEvent->getDeviceId(), keyEvent->getSource(),
2475 policyFlags, action, flags,
2476 keyEvent->getKeyCode(), keyEvent->getScanCode(), keyEvent->getMetaState(),
Jeff Brownb6997262010-10-08 22:31:17 -07002477 keyEvent->getRepeatCount(), keyEvent->getDownTime());
Jeff Brown3241b6b2012-02-03 15:08:02 -08002478 lastInjectedEntry = firstInjectedEntry;
Jeff Brownb6997262010-10-08 22:31:17 -07002479 break;
2480 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07002481
Jeff Brownb6997262010-10-08 22:31:17 -07002482 case AINPUT_EVENT_TYPE_MOTION: {
2483 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event);
2484 int32_t action = motionEvent->getAction();
2485 size_t pointerCount = motionEvent->getPointerCount();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002486 const PointerProperties* pointerProperties = motionEvent->getPointerProperties();
2487 if (! validateMotionEvent(action, pointerCount, pointerProperties)) {
Jeff Brownb6997262010-10-08 22:31:17 -07002488 return INPUT_EVENT_INJECTION_FAILED;
2489 }
2490
Jeff Brown0029c662011-03-30 02:25:18 -07002491 if (!(policyFlags & POLICY_FLAG_FILTERED)) {
2492 nsecs_t eventTime = motionEvent->getEventTime();
2493 mPolicy->interceptMotionBeforeQueueing(eventTime, /*byref*/ policyFlags);
2494 }
Jeff Brownb6997262010-10-08 22:31:17 -07002495
2496 mLock.lock();
2497 const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes();
2498 const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords();
Jeff Brown3241b6b2012-02-03 15:08:02 -08002499 firstInjectedEntry = new MotionEntry(*sampleEventTimes,
Jeff Brownb6997262010-10-08 22:31:17 -07002500 motionEvent->getDeviceId(), motionEvent->getSource(), policyFlags,
2501 action, motionEvent->getFlags(),
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002502 motionEvent->getMetaState(), motionEvent->getButtonState(),
2503 motionEvent->getEdgeFlags(),
Jeff Brownb6997262010-10-08 22:31:17 -07002504 motionEvent->getXPrecision(), motionEvent->getYPrecision(),
2505 motionEvent->getDownTime(), uint32_t(pointerCount),
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002506 pointerProperties, samplePointerCoords);
Jeff Brown3241b6b2012-02-03 15:08:02 -08002507 lastInjectedEntry = firstInjectedEntry;
Jeff Brownb6997262010-10-08 22:31:17 -07002508 for (size_t i = motionEvent->getHistorySize(); i > 0; i--) {
2509 sampleEventTimes += 1;
2510 samplePointerCoords += pointerCount;
Jeff Brown3241b6b2012-02-03 15:08:02 -08002511 MotionEntry* nextInjectedEntry = new MotionEntry(*sampleEventTimes,
2512 motionEvent->getDeviceId(), motionEvent->getSource(), policyFlags,
2513 action, motionEvent->getFlags(),
2514 motionEvent->getMetaState(), motionEvent->getButtonState(),
2515 motionEvent->getEdgeFlags(),
2516 motionEvent->getXPrecision(), motionEvent->getYPrecision(),
2517 motionEvent->getDownTime(), uint32_t(pointerCount),
2518 pointerProperties, samplePointerCoords);
2519 lastInjectedEntry->next = nextInjectedEntry;
2520 lastInjectedEntry = nextInjectedEntry;
Jeff Brownb6997262010-10-08 22:31:17 -07002521 }
Jeff Brownb6997262010-10-08 22:31:17 -07002522 break;
2523 }
2524
2525 default:
Steve Block8564c8d2012-01-05 23:22:43 +00002526 ALOGW("Cannot inject event of type %d", event->getType());
Jeff Brownb6997262010-10-08 22:31:17 -07002527 return INPUT_EVENT_INJECTION_FAILED;
2528 }
2529
Jeff Brownac386072011-07-20 15:19:50 -07002530 InjectionState* injectionState = new InjectionState(injectorPid, injectorUid);
Jeff Brownb6997262010-10-08 22:31:17 -07002531 if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) {
2532 injectionState->injectionIsAsync = true;
2533 }
2534
2535 injectionState->refCount += 1;
Jeff Brown3241b6b2012-02-03 15:08:02 -08002536 lastInjectedEntry->injectionState = injectionState;
Jeff Brownb6997262010-10-08 22:31:17 -07002537
Jeff Brown3241b6b2012-02-03 15:08:02 -08002538 bool needWake = false;
2539 for (EventEntry* entry = firstInjectedEntry; entry != NULL; ) {
2540 EventEntry* nextEntry = entry->next;
2541 needWake |= enqueueInboundEventLocked(entry);
2542 entry = nextEntry;
2543 }
2544
Jeff Brownb6997262010-10-08 22:31:17 -07002545 mLock.unlock();
Jeff Brown7fbdc842010-06-17 20:52:56 -07002546
Jeff Brownb88102f2010-09-08 11:49:43 -07002547 if (needWake) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002548 mLooper->wake();
Jeff Brown7fbdc842010-06-17 20:52:56 -07002549 }
2550
2551 int32_t injectionResult;
2552 { // acquire lock
2553 AutoMutex _l(mLock);
2554
Jeff Brown6ec402b2010-07-28 15:48:59 -07002555 if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) {
2556 injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED;
2557 } else {
2558 for (;;) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002559 injectionResult = injectionState->injectionResult;
Jeff Brown6ec402b2010-07-28 15:48:59 -07002560 if (injectionResult != INPUT_EVENT_INJECTION_PENDING) {
2561 break;
2562 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07002563
Jeff Brown7fbdc842010-06-17 20:52:56 -07002564 nsecs_t remainingTimeout = endTime - now();
2565 if (remainingTimeout <= 0) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002566#if DEBUG_INJECTION
Steve Block5baa3a62011-12-20 16:23:08 +00002567 ALOGD("injectInputEvent - Timed out waiting for injection result "
Jeff Brown6ec402b2010-07-28 15:48:59 -07002568 "to become available.");
2569#endif
Jeff Brown7fbdc842010-06-17 20:52:56 -07002570 injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT;
2571 break;
2572 }
2573
Jeff Brown6ec402b2010-07-28 15:48:59 -07002574 mInjectionResultAvailableCondition.waitRelative(mLock, remainingTimeout);
2575 }
2576
2577 if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED
2578 && syncMode == INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002579 while (injectionState->pendingForegroundDispatches != 0) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002580#if DEBUG_INJECTION
Steve Block5baa3a62011-12-20 16:23:08 +00002581 ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.",
Jeff Brown01ce2e92010-09-26 22:20:12 -07002582 injectionState->pendingForegroundDispatches);
Jeff Brown6ec402b2010-07-28 15:48:59 -07002583#endif
2584 nsecs_t remainingTimeout = endTime - now();
2585 if (remainingTimeout <= 0) {
2586#if DEBUG_INJECTION
Steve Block5baa3a62011-12-20 16:23:08 +00002587 ALOGD("injectInputEvent - Timed out waiting for pending foreground "
Jeff Brown6ec402b2010-07-28 15:48:59 -07002588 "dispatches to finish.");
2589#endif
2590 injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT;
2591 break;
2592 }
2593
2594 mInjectionSyncFinishedCondition.waitRelative(mLock, remainingTimeout);
2595 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07002596 }
2597 }
2598
Jeff Brownac386072011-07-20 15:19:50 -07002599 injectionState->release();
Jeff Brown7fbdc842010-06-17 20:52:56 -07002600 } // release lock
2601
Jeff Brown6ec402b2010-07-28 15:48:59 -07002602#if DEBUG_INJECTION
Steve Block5baa3a62011-12-20 16:23:08 +00002603 ALOGD("injectInputEvent - Finished with result %d. "
Jeff Brown6ec402b2010-07-28 15:48:59 -07002604 "injectorPid=%d, injectorUid=%d",
2605 injectionResult, injectorPid, injectorUid);
2606#endif
2607
Jeff Brown7fbdc842010-06-17 20:52:56 -07002608 return injectionResult;
2609}
2610
Jeff Brownb6997262010-10-08 22:31:17 -07002611bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) {
2612 return injectorUid == 0
2613 || mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid);
2614}
2615
Jeff Brown7fbdc842010-06-17 20:52:56 -07002616void InputDispatcher::setInjectionResultLocked(EventEntry* entry, int32_t injectionResult) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002617 InjectionState* injectionState = entry->injectionState;
2618 if (injectionState) {
Jeff Brown7fbdc842010-06-17 20:52:56 -07002619#if DEBUG_INJECTION
Steve Block5baa3a62011-12-20 16:23:08 +00002620 ALOGD("Setting input event injection result to %d. "
Jeff Brown7fbdc842010-06-17 20:52:56 -07002621 "injectorPid=%d, injectorUid=%d",
Jeff Brown01ce2e92010-09-26 22:20:12 -07002622 injectionResult, injectionState->injectorPid, injectionState->injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -07002623#endif
2624
Jeff Brown0029c662011-03-30 02:25:18 -07002625 if (injectionState->injectionIsAsync
2626 && !(entry->policyFlags & POLICY_FLAG_FILTERED)) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002627 // Log the outcome since the injector did not wait for the injection result.
2628 switch (injectionResult) {
2629 case INPUT_EVENT_INJECTION_SUCCEEDED:
Steve Block71f2cf12011-10-20 11:56:00 +01002630 ALOGV("Asynchronous input event injection succeeded.");
Jeff Brown6ec402b2010-07-28 15:48:59 -07002631 break;
2632 case INPUT_EVENT_INJECTION_FAILED:
Steve Block8564c8d2012-01-05 23:22:43 +00002633 ALOGW("Asynchronous input event injection failed.");
Jeff Brown6ec402b2010-07-28 15:48:59 -07002634 break;
2635 case INPUT_EVENT_INJECTION_PERMISSION_DENIED:
Steve Block8564c8d2012-01-05 23:22:43 +00002636 ALOGW("Asynchronous input event injection permission denied.");
Jeff Brown6ec402b2010-07-28 15:48:59 -07002637 break;
2638 case INPUT_EVENT_INJECTION_TIMED_OUT:
Steve Block8564c8d2012-01-05 23:22:43 +00002639 ALOGW("Asynchronous input event injection timed out.");
Jeff Brown6ec402b2010-07-28 15:48:59 -07002640 break;
2641 }
2642 }
2643
Jeff Brown01ce2e92010-09-26 22:20:12 -07002644 injectionState->injectionResult = injectionResult;
Jeff Brown7fbdc842010-06-17 20:52:56 -07002645 mInjectionResultAvailableCondition.broadcast();
2646 }
2647}
2648
Jeff Brown01ce2e92010-09-26 22:20:12 -07002649void InputDispatcher::incrementPendingForegroundDispatchesLocked(EventEntry* entry) {
2650 InjectionState* injectionState = entry->injectionState;
2651 if (injectionState) {
2652 injectionState->pendingForegroundDispatches += 1;
2653 }
2654}
2655
Jeff Brown519e0242010-09-15 15:18:56 -07002656void InputDispatcher::decrementPendingForegroundDispatchesLocked(EventEntry* entry) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002657 InjectionState* injectionState = entry->injectionState;
2658 if (injectionState) {
2659 injectionState->pendingForegroundDispatches -= 1;
Jeff Brown6ec402b2010-07-28 15:48:59 -07002660
Jeff Brown01ce2e92010-09-26 22:20:12 -07002661 if (injectionState->pendingForegroundDispatches == 0) {
2662 mInjectionSyncFinishedCondition.broadcast();
2663 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002664 }
2665}
2666
Jeff Brown9302c872011-07-13 22:51:29 -07002667sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked(
2668 const sp<InputChannel>& inputChannel) const {
2669 size_t numWindows = mWindowHandles.size();
2670 for (size_t i = 0; i < numWindows; i++) {
2671 const sp<InputWindowHandle>& windowHandle = mWindowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -07002672 if (windowHandle->getInputChannel() == inputChannel) {
Jeff Brown9302c872011-07-13 22:51:29 -07002673 return windowHandle;
Jeff Brown01ce2e92010-09-26 22:20:12 -07002674 }
2675 }
2676 return NULL;
2677}
2678
Jeff Brown9302c872011-07-13 22:51:29 -07002679bool InputDispatcher::hasWindowHandleLocked(
2680 const sp<InputWindowHandle>& windowHandle) const {
2681 size_t numWindows = mWindowHandles.size();
2682 for (size_t i = 0; i < numWindows; i++) {
2683 if (mWindowHandles.itemAt(i) == windowHandle) {
2684 return true;
2685 }
2686 }
2687 return false;
2688}
2689
2690void InputDispatcher::setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles) {
Jeff Brownb88102f2010-09-08 11:49:43 -07002691#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002692 ALOGD("setInputWindows");
Jeff Brownb88102f2010-09-08 11:49:43 -07002693#endif
2694 { // acquire lock
2695 AutoMutex _l(mLock);
2696
Jeff Browncc4f7db2011-08-30 20:34:48 -07002697 Vector<sp<InputWindowHandle> > oldWindowHandles = mWindowHandles;
Jeff Brown9302c872011-07-13 22:51:29 -07002698 mWindowHandles = inputWindowHandles;
Jeff Brownb6997262010-10-08 22:31:17 -07002699
Jeff Brown9302c872011-07-13 22:51:29 -07002700 sp<InputWindowHandle> newFocusedWindowHandle;
2701 bool foundHoveredWindow = false;
2702 for (size_t i = 0; i < mWindowHandles.size(); i++) {
2703 const sp<InputWindowHandle>& windowHandle = mWindowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -07002704 if (!windowHandle->updateInfo() || windowHandle->getInputChannel() == NULL) {
Jeff Brown9302c872011-07-13 22:51:29 -07002705 mWindowHandles.removeAt(i--);
2706 continue;
2707 }
Jeff Browncc4f7db2011-08-30 20:34:48 -07002708 if (windowHandle->getInfo()->hasFocus) {
Jeff Brown9302c872011-07-13 22:51:29 -07002709 newFocusedWindowHandle = windowHandle;
2710 }
2711 if (windowHandle == mLastHoverWindowHandle) {
2712 foundHoveredWindow = true;
Jeff Brownb88102f2010-09-08 11:49:43 -07002713 }
2714 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07002715
Jeff Brown9302c872011-07-13 22:51:29 -07002716 if (!foundHoveredWindow) {
2717 mLastHoverWindowHandle = NULL;
2718 }
2719
2720 if (mFocusedWindowHandle != newFocusedWindowHandle) {
2721 if (mFocusedWindowHandle != NULL) {
Jeff Brownb6997262010-10-08 22:31:17 -07002722#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002723 ALOGD("Focus left window: %s",
Jeff Browncc4f7db2011-08-30 20:34:48 -07002724 mFocusedWindowHandle->getName().string());
Jeff Brownb6997262010-10-08 22:31:17 -07002725#endif
Jeff Browncc4f7db2011-08-30 20:34:48 -07002726 sp<InputChannel> focusedInputChannel = mFocusedWindowHandle->getInputChannel();
2727 if (focusedInputChannel != NULL) {
Christopher Tated9be36c2011-08-16 16:09:33 -07002728 CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS,
2729 "focus left window");
2730 synthesizeCancelationEventsForInputChannelLocked(
Jeff Browncc4f7db2011-08-30 20:34:48 -07002731 focusedInputChannel, options);
Christopher Tated9be36c2011-08-16 16:09:33 -07002732 }
Jeff Brownb6997262010-10-08 22:31:17 -07002733 }
Jeff Brown9302c872011-07-13 22:51:29 -07002734 if (newFocusedWindowHandle != NULL) {
Jeff Brownb6997262010-10-08 22:31:17 -07002735#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002736 ALOGD("Focus entered window: %s",
Jeff Browncc4f7db2011-08-30 20:34:48 -07002737 newFocusedWindowHandle->getName().string());
Jeff Brownb6997262010-10-08 22:31:17 -07002738#endif
Jeff Brown9302c872011-07-13 22:51:29 -07002739 }
2740 mFocusedWindowHandle = newFocusedWindowHandle;
Jeff Brownb6997262010-10-08 22:31:17 -07002741 }
2742
Jeff Brown9302c872011-07-13 22:51:29 -07002743 for (size_t i = 0; i < mTouchState.windows.size(); i++) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002744 TouchedWindow& touchedWindow = mTouchState.windows.editItemAt(i);
Jeff Brown9302c872011-07-13 22:51:29 -07002745 if (!hasWindowHandleLocked(touchedWindow.windowHandle)) {
Jeff Brownb6997262010-10-08 22:31:17 -07002746#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002747 ALOGD("Touched window was removed: %s",
Jeff Browncc4f7db2011-08-30 20:34:48 -07002748 touchedWindow.windowHandle->getName().string());
Jeff Brownb6997262010-10-08 22:31:17 -07002749#endif
Jeff Browncc4f7db2011-08-30 20:34:48 -07002750 sp<InputChannel> touchedInputChannel =
2751 touchedWindow.windowHandle->getInputChannel();
2752 if (touchedInputChannel != NULL) {
Christopher Tated9be36c2011-08-16 16:09:33 -07002753 CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS,
2754 "touched window was removed");
2755 synthesizeCancelationEventsForInputChannelLocked(
Jeff Browncc4f7db2011-08-30 20:34:48 -07002756 touchedInputChannel, options);
Christopher Tated9be36c2011-08-16 16:09:33 -07002757 }
Jeff Brown9302c872011-07-13 22:51:29 -07002758 mTouchState.windows.removeAt(i--);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002759 }
2760 }
Jeff Browncc4f7db2011-08-30 20:34:48 -07002761
2762 // Release information for windows that are no longer present.
2763 // This ensures that unused input channels are released promptly.
2764 // Otherwise, they might stick around until the window handle is destroyed
2765 // which might not happen until the next GC.
2766 for (size_t i = 0; i < oldWindowHandles.size(); i++) {
2767 const sp<InputWindowHandle>& oldWindowHandle = oldWindowHandles.itemAt(i);
2768 if (!hasWindowHandleLocked(oldWindowHandle)) {
2769#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002770 ALOGD("Window went away: %s", oldWindowHandle->getName().string());
Jeff Browncc4f7db2011-08-30 20:34:48 -07002771#endif
2772 oldWindowHandle->releaseInfo();
2773 }
2774 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002775 } // release lock
2776
2777 // Wake up poll loop since it may need to make new input dispatching choices.
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002778 mLooper->wake();
Jeff Brownb88102f2010-09-08 11:49:43 -07002779}
2780
Jeff Brown9302c872011-07-13 22:51:29 -07002781void InputDispatcher::setFocusedApplication(
2782 const sp<InputApplicationHandle>& inputApplicationHandle) {
Jeff Brownb88102f2010-09-08 11:49:43 -07002783#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002784 ALOGD("setFocusedApplication");
Jeff Brownb88102f2010-09-08 11:49:43 -07002785#endif
2786 { // acquire lock
2787 AutoMutex _l(mLock);
2788
Jeff Browncc4f7db2011-08-30 20:34:48 -07002789 if (inputApplicationHandle != NULL && inputApplicationHandle->updateInfo()) {
Jeff Brown5ea29ab2011-07-27 11:50:51 -07002790 if (mFocusedApplicationHandle != inputApplicationHandle) {
2791 if (mFocusedApplicationHandle != NULL) {
Jeff Browne9bb9be2012-02-06 15:47:55 -08002792 resetANRTimeoutsLocked();
Jeff Browncc4f7db2011-08-30 20:34:48 -07002793 mFocusedApplicationHandle->releaseInfo();
Jeff Brown5ea29ab2011-07-27 11:50:51 -07002794 }
2795 mFocusedApplicationHandle = inputApplicationHandle;
2796 }
2797 } else if (mFocusedApplicationHandle != NULL) {
Jeff Browne9bb9be2012-02-06 15:47:55 -08002798 resetANRTimeoutsLocked();
Jeff Browncc4f7db2011-08-30 20:34:48 -07002799 mFocusedApplicationHandle->releaseInfo();
Jeff Brown9302c872011-07-13 22:51:29 -07002800 mFocusedApplicationHandle.clear();
Jeff Brownb88102f2010-09-08 11:49:43 -07002801 }
2802
2803#if DEBUG_FOCUS
Jeff Brownb6997262010-10-08 22:31:17 -07002804 //logDispatchStateLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -07002805#endif
2806 } // release lock
2807
2808 // Wake up poll loop since it may need to make new input dispatching choices.
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002809 mLooper->wake();
Jeff Brownb88102f2010-09-08 11:49:43 -07002810}
2811
Jeff Brownb88102f2010-09-08 11:49:43 -07002812void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) {
2813#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002814 ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen);
Jeff Brownb88102f2010-09-08 11:49:43 -07002815#endif
2816
2817 bool changed;
2818 { // acquire lock
2819 AutoMutex _l(mLock);
2820
2821 if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) {
Jeff Brown120a4592010-10-27 18:43:51 -07002822 if (mDispatchFrozen && !frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -07002823 resetANRTimeoutsLocked();
2824 }
2825
Jeff Brown120a4592010-10-27 18:43:51 -07002826 if (mDispatchEnabled && !enabled) {
2827 resetAndDropEverythingLocked("dispatcher is being disabled");
2828 }
2829
Jeff Brownb88102f2010-09-08 11:49:43 -07002830 mDispatchEnabled = enabled;
2831 mDispatchFrozen = frozen;
2832 changed = true;
2833 } else {
2834 changed = false;
2835 }
2836
2837#if DEBUG_FOCUS
Jeff Brownb6997262010-10-08 22:31:17 -07002838 //logDispatchStateLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -07002839#endif
2840 } // release lock
2841
2842 if (changed) {
2843 // Wake up poll loop since it may need to make new input dispatching choices.
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002844 mLooper->wake();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002845 }
2846}
2847
Jeff Brown0029c662011-03-30 02:25:18 -07002848void InputDispatcher::setInputFilterEnabled(bool enabled) {
2849#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002850 ALOGD("setInputFilterEnabled: enabled=%d", enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07002851#endif
2852
2853 { // acquire lock
2854 AutoMutex _l(mLock);
2855
2856 if (mInputFilterEnabled == enabled) {
2857 return;
2858 }
2859
2860 mInputFilterEnabled = enabled;
2861 resetAndDropEverythingLocked("input filter is being enabled or disabled");
2862 } // release lock
2863
2864 // Wake up poll loop since there might be work to do to drop everything.
2865 mLooper->wake();
2866}
2867
Jeff Browne6504122010-09-27 14:52:15 -07002868bool InputDispatcher::transferTouchFocus(const sp<InputChannel>& fromChannel,
2869 const sp<InputChannel>& toChannel) {
2870#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002871 ALOGD("transferTouchFocus: fromChannel=%s, toChannel=%s",
Jeff Browne6504122010-09-27 14:52:15 -07002872 fromChannel->getName().string(), toChannel->getName().string());
2873#endif
2874 { // acquire lock
2875 AutoMutex _l(mLock);
2876
Jeff Brown9302c872011-07-13 22:51:29 -07002877 sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromChannel);
2878 sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toChannel);
2879 if (fromWindowHandle == NULL || toWindowHandle == NULL) {
Jeff Browne6504122010-09-27 14:52:15 -07002880#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002881 ALOGD("Cannot transfer focus because from or to window not found.");
Jeff Browne6504122010-09-27 14:52:15 -07002882#endif
2883 return false;
2884 }
Jeff Brown9302c872011-07-13 22:51:29 -07002885 if (fromWindowHandle == toWindowHandle) {
Jeff Browne6504122010-09-27 14:52:15 -07002886#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002887 ALOGD("Trivial transfer to same window.");
Jeff Browne6504122010-09-27 14:52:15 -07002888#endif
2889 return true;
2890 }
2891
2892 bool found = false;
2893 for (size_t i = 0; i < mTouchState.windows.size(); i++) {
2894 const TouchedWindow& touchedWindow = mTouchState.windows[i];
Jeff Brown9302c872011-07-13 22:51:29 -07002895 if (touchedWindow.windowHandle == fromWindowHandle) {
Jeff Browne6504122010-09-27 14:52:15 -07002896 int32_t oldTargetFlags = touchedWindow.targetFlags;
2897 BitSet32 pointerIds = touchedWindow.pointerIds;
2898
2899 mTouchState.windows.removeAt(i);
2900
Jeff Brown46e75292010-11-10 16:53:45 -08002901 int32_t newTargetFlags = oldTargetFlags
Jeff Browna032cc02011-03-07 16:56:21 -08002902 & (InputTarget::FLAG_FOREGROUND
2903 | InputTarget::FLAG_SPLIT | InputTarget::FLAG_DISPATCH_AS_IS);
Jeff Brown9302c872011-07-13 22:51:29 -07002904 mTouchState.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds);
Jeff Browne6504122010-09-27 14:52:15 -07002905
2906 found = true;
2907 break;
2908 }
2909 }
2910
2911 if (! found) {
2912#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002913 ALOGD("Focus transfer failed because from window did not have focus.");
Jeff Browne6504122010-09-27 14:52:15 -07002914#endif
2915 return false;
2916 }
2917
Jeff Brown9c9f1a32010-10-11 18:32:20 -07002918 ssize_t fromConnectionIndex = getConnectionIndexLocked(fromChannel);
2919 ssize_t toConnectionIndex = getConnectionIndexLocked(toChannel);
2920 if (fromConnectionIndex >= 0 && toConnectionIndex >= 0) {
Jeff Browncbee6d62012-02-03 20:11:27 -08002921 sp<Connection> fromConnection = mConnectionsByFd.valueAt(fromConnectionIndex);
2922 sp<Connection> toConnection = mConnectionsByFd.valueAt(toConnectionIndex);
Jeff Brown9c9f1a32010-10-11 18:32:20 -07002923
2924 fromConnection->inputState.copyPointerStateTo(toConnection->inputState);
Jeff Brownda3d5a92011-03-29 15:11:34 -07002925 CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS,
Jeff Brown9c9f1a32010-10-11 18:32:20 -07002926 "transferring touch focus from this window to another window");
Jeff Brownda3d5a92011-03-29 15:11:34 -07002927 synthesizeCancelationEventsForConnectionLocked(fromConnection, options);
Jeff Brown9c9f1a32010-10-11 18:32:20 -07002928 }
2929
Jeff Browne6504122010-09-27 14:52:15 -07002930#if DEBUG_FOCUS
2931 logDispatchStateLocked();
2932#endif
2933 } // release lock
2934
2935 // Wake up poll loop since it may need to make new input dispatching choices.
2936 mLooper->wake();
2937 return true;
2938}
2939
Jeff Brown120a4592010-10-27 18:43:51 -07002940void InputDispatcher::resetAndDropEverythingLocked(const char* reason) {
2941#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002942 ALOGD("Resetting and dropping all events (%s).", reason);
Jeff Brown120a4592010-10-27 18:43:51 -07002943#endif
2944
Jeff Brownda3d5a92011-03-29 15:11:34 -07002945 CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason);
2946 synthesizeCancelationEventsForAllConnectionsLocked(options);
Jeff Brown120a4592010-10-27 18:43:51 -07002947
2948 resetKeyRepeatLocked();
2949 releasePendingEventLocked();
2950 drainInboundQueueLocked();
Jeff Browne9bb9be2012-02-06 15:47:55 -08002951 resetANRTimeoutsLocked();
Jeff Brown120a4592010-10-27 18:43:51 -07002952
2953 mTouchState.reset();
Jeff Brown9302c872011-07-13 22:51:29 -07002954 mLastHoverWindowHandle.clear();
Jeff Brown120a4592010-10-27 18:43:51 -07002955}
2956
Jeff Brownb88102f2010-09-08 11:49:43 -07002957void InputDispatcher::logDispatchStateLocked() {
2958 String8 dump;
2959 dumpDispatchStateLocked(dump);
Jeff Brown2a95c2a2010-09-16 12:31:46 -07002960
2961 char* text = dump.lockBuffer(dump.size());
2962 char* start = text;
2963 while (*start != '\0') {
2964 char* end = strchr(start, '\n');
2965 if (*end == '\n') {
2966 *(end++) = '\0';
2967 }
Steve Block5baa3a62011-12-20 16:23:08 +00002968 ALOGD("%s", start);
Jeff Brown2a95c2a2010-09-16 12:31:46 -07002969 start = end;
2970 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002971}
2972
2973void InputDispatcher::dumpDispatchStateLocked(String8& dump) {
Jeff Brownf2f48712010-10-01 17:46:21 -07002974 dump.appendFormat(INDENT "DispatchEnabled: %d\n", mDispatchEnabled);
2975 dump.appendFormat(INDENT "DispatchFrozen: %d\n", mDispatchFrozen);
Jeff Brownb88102f2010-09-08 11:49:43 -07002976
Jeff Brown9302c872011-07-13 22:51:29 -07002977 if (mFocusedApplicationHandle != NULL) {
Jeff Brownf2f48712010-10-01 17:46:21 -07002978 dump.appendFormat(INDENT "FocusedApplication: name='%s', dispatchingTimeout=%0.3fms\n",
Jeff Browncc4f7db2011-08-30 20:34:48 -07002979 mFocusedApplicationHandle->getName().string(),
2980 mFocusedApplicationHandle->getDispatchingTimeout(
2981 DEFAULT_INPUT_DISPATCHING_TIMEOUT) / 1000000.0);
Jeff Brownb88102f2010-09-08 11:49:43 -07002982 } else {
Jeff Brownf2f48712010-10-01 17:46:21 -07002983 dump.append(INDENT "FocusedApplication: <null>\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07002984 }
Jeff Brownf2f48712010-10-01 17:46:21 -07002985 dump.appendFormat(INDENT "FocusedWindow: name='%s'\n",
Jeff Browncc4f7db2011-08-30 20:34:48 -07002986 mFocusedWindowHandle != NULL ? mFocusedWindowHandle->getName().string() : "<null>");
Jeff Brownf2f48712010-10-01 17:46:21 -07002987
2988 dump.appendFormat(INDENT "TouchDown: %s\n", toString(mTouchState.down));
2989 dump.appendFormat(INDENT "TouchSplit: %s\n", toString(mTouchState.split));
Jeff Brown95712852011-01-04 19:41:59 -08002990 dump.appendFormat(INDENT "TouchDeviceId: %d\n", mTouchState.deviceId);
Jeff Brown58a2da82011-01-25 16:02:22 -08002991 dump.appendFormat(INDENT "TouchSource: 0x%08x\n", mTouchState.source);
Jeff Brownf2f48712010-10-01 17:46:21 -07002992 if (!mTouchState.windows.isEmpty()) {
2993 dump.append(INDENT "TouchedWindows:\n");
2994 for (size_t i = 0; i < mTouchState.windows.size(); i++) {
2995 const TouchedWindow& touchedWindow = mTouchState.windows[i];
2996 dump.appendFormat(INDENT2 "%d: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n",
Jeff Browncc4f7db2011-08-30 20:34:48 -07002997 i, touchedWindow.windowHandle->getName().string(),
2998 touchedWindow.pointerIds.value,
Jeff Brownf2f48712010-10-01 17:46:21 -07002999 touchedWindow.targetFlags);
3000 }
3001 } else {
3002 dump.append(INDENT "TouchedWindows: <none>\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003003 }
3004
Jeff Brown9302c872011-07-13 22:51:29 -07003005 if (!mWindowHandles.isEmpty()) {
Jeff Brownf2f48712010-10-01 17:46:21 -07003006 dump.append(INDENT "Windows:\n");
Jeff Brown9302c872011-07-13 22:51:29 -07003007 for (size_t i = 0; i < mWindowHandles.size(); i++) {
3008 const sp<InputWindowHandle>& windowHandle = mWindowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -07003009 const InputWindowInfo* windowInfo = windowHandle->getInfo();
3010
Jeff Brownf2f48712010-10-01 17:46:21 -07003011 dump.appendFormat(INDENT2 "%d: name='%s', paused=%s, hasFocus=%s, hasWallpaper=%s, "
3012 "visible=%s, canReceiveKeys=%s, flags=0x%08x, type=0x%08x, layer=%d, "
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003013 "frame=[%d,%d][%d,%d], scale=%f, "
Jeff Brownfbf09772011-01-16 14:06:57 -08003014 "touchableRegion=",
Jeff Browncc4f7db2011-08-30 20:34:48 -07003015 i, windowInfo->name.string(),
3016 toString(windowInfo->paused),
3017 toString(windowInfo->hasFocus),
3018 toString(windowInfo->hasWallpaper),
3019 toString(windowInfo->visible),
3020 toString(windowInfo->canReceiveKeys),
3021 windowInfo->layoutParamsFlags, windowInfo->layoutParamsType,
3022 windowInfo->layer,
3023 windowInfo->frameLeft, windowInfo->frameTop,
3024 windowInfo->frameRight, windowInfo->frameBottom,
3025 windowInfo->scaleFactor);
3026 dumpRegion(dump, windowInfo->touchableRegion);
3027 dump.appendFormat(", inputFeatures=0x%08x", windowInfo->inputFeatures);
Jeff Brownfbf09772011-01-16 14:06:57 -08003028 dump.appendFormat(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%0.3fms\n",
Jeff Browncc4f7db2011-08-30 20:34:48 -07003029 windowInfo->ownerPid, windowInfo->ownerUid,
3030 windowInfo->dispatchingTimeout / 1000000.0);
Jeff Brownf2f48712010-10-01 17:46:21 -07003031 }
3032 } else {
3033 dump.append(INDENT "Windows: <none>\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003034 }
3035
Jeff Brownf2f48712010-10-01 17:46:21 -07003036 if (!mMonitoringChannels.isEmpty()) {
3037 dump.append(INDENT "MonitoringChannels:\n");
3038 for (size_t i = 0; i < mMonitoringChannels.size(); i++) {
3039 const sp<InputChannel>& channel = mMonitoringChannels[i];
3040 dump.appendFormat(INDENT2 "%d: '%s'\n", i, channel->getName().string());
3041 }
3042 } else {
3043 dump.append(INDENT "MonitoringChannels: <none>\n");
3044 }
Jeff Brown519e0242010-09-15 15:18:56 -07003045
Jeff Brownf2f48712010-10-01 17:46:21 -07003046 dump.appendFormat(INDENT "InboundQueue: length=%u\n", mInboundQueue.count());
3047
3048 if (!mActiveConnections.isEmpty()) {
3049 dump.append(INDENT "ActiveConnections:\n");
3050 for (size_t i = 0; i < mActiveConnections.size(); i++) {
3051 const Connection* connection = mActiveConnections[i];
Jeff Brown76860e32010-10-25 17:37:46 -07003052 dump.appendFormat(INDENT2 "%d: '%s', status=%s, outboundQueueLength=%u, "
Jeff Brownb6997262010-10-08 22:31:17 -07003053 "inputState.isNeutral=%s\n",
Jeff Brownf2f48712010-10-01 17:46:21 -07003054 i, connection->getInputChannelName(), connection->getStatusLabel(),
3055 connection->outboundQueue.count(),
Jeff Brownb6997262010-10-08 22:31:17 -07003056 toString(connection->inputState.isNeutral()));
Jeff Brownf2f48712010-10-01 17:46:21 -07003057 }
3058 } else {
3059 dump.append(INDENT "ActiveConnections: <none>\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003060 }
3061
3062 if (isAppSwitchPendingLocked()) {
Jeff Brownf2f48712010-10-01 17:46:21 -07003063 dump.appendFormat(INDENT "AppSwitch: pending, due in %01.1fms\n",
Jeff Brownb88102f2010-09-08 11:49:43 -07003064 (mAppSwitchDueTime - now()) / 1000000.0);
3065 } else {
Jeff Brownf2f48712010-10-01 17:46:21 -07003066 dump.append(INDENT "AppSwitch: not pending\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003067 }
3068}
3069
Jeff Brown928e0542011-01-10 11:17:36 -08003070status_t InputDispatcher::registerInputChannel(const sp<InputChannel>& inputChannel,
3071 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
Jeff Brown9c3cda02010-06-15 01:31:58 -07003072#if DEBUG_REGISTRATION
Steve Block5baa3a62011-12-20 16:23:08 +00003073 ALOGD("channel '%s' ~ registerInputChannel - monitor=%s", inputChannel->getName().string(),
Jeff Brownb88102f2010-09-08 11:49:43 -07003074 toString(monitor));
Jeff Brown9c3cda02010-06-15 01:31:58 -07003075#endif
3076
Jeff Brown46b9ac02010-04-22 18:58:52 -07003077 { // acquire lock
3078 AutoMutex _l(mLock);
3079
Jeff Brown519e0242010-09-15 15:18:56 -07003080 if (getConnectionIndexLocked(inputChannel) >= 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003081 ALOGW("Attempted to register already registered input channel '%s'",
Jeff Brown46b9ac02010-04-22 18:58:52 -07003082 inputChannel->getName().string());
3083 return BAD_VALUE;
3084 }
3085
Jeff Browncc4f7db2011-08-30 20:34:48 -07003086 sp<Connection> connection = new Connection(inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003087
Jeff Browncbee6d62012-02-03 20:11:27 -08003088 int32_t fd = inputChannel->getFd();
3089 mConnectionsByFd.add(fd, connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -07003090
Jeff Brownb88102f2010-09-08 11:49:43 -07003091 if (monitor) {
3092 mMonitoringChannels.push(inputChannel);
3093 }
3094
Jeff Browncbee6d62012-02-03 20:11:27 -08003095 mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this);
Jeff Brown2cbecea2010-08-17 15:59:26 -07003096
Jeff Brown9c3cda02010-06-15 01:31:58 -07003097 runCommandsLockedInterruptible();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003098 } // release lock
Jeff Brown46b9ac02010-04-22 18:58:52 -07003099 return OK;
3100}
3101
3102status_t InputDispatcher::unregisterInputChannel(const sp<InputChannel>& inputChannel) {
Jeff Brown9c3cda02010-06-15 01:31:58 -07003103#if DEBUG_REGISTRATION
Steve Block5baa3a62011-12-20 16:23:08 +00003104 ALOGD("channel '%s' ~ unregisterInputChannel", inputChannel->getName().string());
Jeff Brown9c3cda02010-06-15 01:31:58 -07003105#endif
3106
Jeff Brown46b9ac02010-04-22 18:58:52 -07003107 { // acquire lock
3108 AutoMutex _l(mLock);
3109
Jeff Browncc4f7db2011-08-30 20:34:48 -07003110 status_t status = unregisterInputChannelLocked(inputChannel, false /*notify*/);
3111 if (status) {
3112 return status;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003113 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003114 } // release lock
3115
Jeff Brown46b9ac02010-04-22 18:58:52 -07003116 // Wake the poll loop because removing the connection may have changed the current
3117 // synchronization state.
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07003118 mLooper->wake();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003119 return OK;
3120}
3121
Jeff Browncc4f7db2011-08-30 20:34:48 -07003122status_t InputDispatcher::unregisterInputChannelLocked(const sp<InputChannel>& inputChannel,
3123 bool notify) {
3124 ssize_t connectionIndex = getConnectionIndexLocked(inputChannel);
3125 if (connectionIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003126 ALOGW("Attempted to unregister already unregistered input channel '%s'",
Jeff Browncc4f7db2011-08-30 20:34:48 -07003127 inputChannel->getName().string());
3128 return BAD_VALUE;
3129 }
3130
Jeff Browncbee6d62012-02-03 20:11:27 -08003131 sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex);
3132 mConnectionsByFd.removeItemsAt(connectionIndex);
Jeff Browncc4f7db2011-08-30 20:34:48 -07003133
3134 if (connection->monitor) {
3135 removeMonitorChannelLocked(inputChannel);
3136 }
3137
Jeff Browncbee6d62012-02-03 20:11:27 -08003138 mLooper->removeFd(inputChannel->getFd());
Jeff Browncc4f7db2011-08-30 20:34:48 -07003139
3140 nsecs_t currentTime = now();
3141 abortBrokenDispatchCycleLocked(currentTime, connection, notify);
3142
3143 runCommandsLockedInterruptible();
3144
3145 connection->status = Connection::STATUS_ZOMBIE;
3146 return OK;
3147}
3148
3149void InputDispatcher::removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) {
3150 for (size_t i = 0; i < mMonitoringChannels.size(); i++) {
3151 if (mMonitoringChannels[i] == inputChannel) {
3152 mMonitoringChannels.removeAt(i);
3153 break;
3154 }
3155 }
3156}
3157
Jeff Brown519e0242010-09-15 15:18:56 -07003158ssize_t InputDispatcher::getConnectionIndexLocked(const sp<InputChannel>& inputChannel) {
Jeff Browncbee6d62012-02-03 20:11:27 -08003159 ssize_t connectionIndex = mConnectionsByFd.indexOfKey(inputChannel->getFd());
Jeff Brown2cbecea2010-08-17 15:59:26 -07003160 if (connectionIndex >= 0) {
Jeff Browncbee6d62012-02-03 20:11:27 -08003161 sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex);
Jeff Brown2cbecea2010-08-17 15:59:26 -07003162 if (connection->inputChannel.get() == inputChannel.get()) {
3163 return connectionIndex;
3164 }
3165 }
3166
3167 return -1;
3168}
3169
Jeff Brown46b9ac02010-04-22 18:58:52 -07003170void InputDispatcher::activateConnectionLocked(Connection* connection) {
3171 for (size_t i = 0; i < mActiveConnections.size(); i++) {
3172 if (mActiveConnections.itemAt(i) == connection) {
3173 return;
3174 }
3175 }
3176 mActiveConnections.add(connection);
3177}
3178
3179void InputDispatcher::deactivateConnectionLocked(Connection* connection) {
3180 for (size_t i = 0; i < mActiveConnections.size(); i++) {
3181 if (mActiveConnections.itemAt(i) == connection) {
3182 mActiveConnections.removeAt(i);
3183 return;
3184 }
3185 }
3186}
3187
Jeff Brown9c3cda02010-06-15 01:31:58 -07003188void InputDispatcher::onDispatchCycleStartedLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07003189 nsecs_t currentTime, const sp<Connection>& connection) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003190}
3191
Jeff Brown9c3cda02010-06-15 01:31:58 -07003192void InputDispatcher::onDispatchCycleFinishedLocked(
Jeff Brown3915bb82010-11-05 15:02:16 -07003193 nsecs_t currentTime, const sp<Connection>& connection, bool handled) {
3194 CommandEntry* commandEntry = postCommandLocked(
3195 & InputDispatcher::doDispatchCycleFinishedLockedInterruptible);
3196 commandEntry->connection = connection;
3197 commandEntry->handled = handled;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003198}
3199
Jeff Brown9c3cda02010-06-15 01:31:58 -07003200void InputDispatcher::onDispatchCycleBrokenLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07003201 nsecs_t currentTime, const sp<Connection>& connection) {
Steve Block3762c312012-01-06 19:20:56 +00003202 ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!",
Jeff Brown46b9ac02010-04-22 18:58:52 -07003203 connection->getInputChannelName());
3204
Jeff Brown9c3cda02010-06-15 01:31:58 -07003205 CommandEntry* commandEntry = postCommandLocked(
3206 & InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible);
Jeff Brown7fbdc842010-06-17 20:52:56 -07003207 commandEntry->connection = connection;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003208}
3209
Jeff Brown519e0242010-09-15 15:18:56 -07003210void InputDispatcher::onANRLocked(
Jeff Brown9302c872011-07-13 22:51:29 -07003211 nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
3212 const sp<InputWindowHandle>& windowHandle,
Jeff Brown519e0242010-09-15 15:18:56 -07003213 nsecs_t eventTime, nsecs_t waitStartTime) {
Steve Block6215d3f2012-01-04 20:05:49 +00003214 ALOGI("Application is not responding: %s. "
Jeff Brown519e0242010-09-15 15:18:56 -07003215 "%01.1fms since event, %01.1fms since wait started",
Jeff Brown9302c872011-07-13 22:51:29 -07003216 getApplicationWindowLabelLocked(applicationHandle, windowHandle).string(),
Jeff Brown519e0242010-09-15 15:18:56 -07003217 (currentTime - eventTime) / 1000000.0,
3218 (currentTime - waitStartTime) / 1000000.0);
3219
3220 CommandEntry* commandEntry = postCommandLocked(
3221 & InputDispatcher::doNotifyANRLockedInterruptible);
Jeff Brown9302c872011-07-13 22:51:29 -07003222 commandEntry->inputApplicationHandle = applicationHandle;
3223 commandEntry->inputWindowHandle = windowHandle;
Jeff Brown519e0242010-09-15 15:18:56 -07003224}
3225
Jeff Brownb88102f2010-09-08 11:49:43 -07003226void InputDispatcher::doNotifyConfigurationChangedInterruptible(
3227 CommandEntry* commandEntry) {
3228 mLock.unlock();
3229
3230 mPolicy->notifyConfigurationChanged(commandEntry->eventTime);
3231
3232 mLock.lock();
3233}
3234
Jeff Brown9c3cda02010-06-15 01:31:58 -07003235void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(
3236 CommandEntry* commandEntry) {
Jeff Brown7fbdc842010-06-17 20:52:56 -07003237 sp<Connection> connection = commandEntry->connection;
Jeff Brown9c3cda02010-06-15 01:31:58 -07003238
Jeff Brown7fbdc842010-06-17 20:52:56 -07003239 if (connection->status != Connection::STATUS_ZOMBIE) {
3240 mLock.unlock();
Jeff Brown9c3cda02010-06-15 01:31:58 -07003241
Jeff Brown928e0542011-01-10 11:17:36 -08003242 mPolicy->notifyInputChannelBroken(connection->inputWindowHandle);
Jeff Brown7fbdc842010-06-17 20:52:56 -07003243
3244 mLock.lock();
3245 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07003246}
3247
Jeff Brown519e0242010-09-15 15:18:56 -07003248void InputDispatcher::doNotifyANRLockedInterruptible(
Jeff Brown9c3cda02010-06-15 01:31:58 -07003249 CommandEntry* commandEntry) {
Jeff Brown519e0242010-09-15 15:18:56 -07003250 mLock.unlock();
Jeff Brown9c3cda02010-06-15 01:31:58 -07003251
Jeff Brown519e0242010-09-15 15:18:56 -07003252 nsecs_t newTimeout = mPolicy->notifyANR(
Jeff Brown928e0542011-01-10 11:17:36 -08003253 commandEntry->inputApplicationHandle, commandEntry->inputWindowHandle);
Jeff Brown9c3cda02010-06-15 01:31:58 -07003254
Jeff Brown519e0242010-09-15 15:18:56 -07003255 mLock.lock();
Jeff Brown7fbdc842010-06-17 20:52:56 -07003256
Jeff Brown9302c872011-07-13 22:51:29 -07003257 resumeAfterTargetsNotReadyTimeoutLocked(newTimeout,
3258 commandEntry->inputWindowHandle != NULL
Jeff Browncc4f7db2011-08-30 20:34:48 -07003259 ? commandEntry->inputWindowHandle->getInputChannel() : NULL);
Jeff Brown9c3cda02010-06-15 01:31:58 -07003260}
3261
Jeff Brownb88102f2010-09-08 11:49:43 -07003262void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible(
3263 CommandEntry* commandEntry) {
3264 KeyEntry* entry = commandEntry->keyEntry;
Jeff Brown1f245102010-11-18 20:53:46 -08003265
3266 KeyEvent event;
3267 initializeKeyEvent(&event, entry);
Jeff Brownb88102f2010-09-08 11:49:43 -07003268
3269 mLock.unlock();
3270
Jeff Brown905805a2011-10-12 13:57:59 -07003271 nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(commandEntry->inputWindowHandle,
Jeff Brown1f245102010-11-18 20:53:46 -08003272 &event, entry->policyFlags);
Jeff Brownb88102f2010-09-08 11:49:43 -07003273
3274 mLock.lock();
3275
Jeff Brown905805a2011-10-12 13:57:59 -07003276 if (delay < 0) {
3277 entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP;
3278 } else if (!delay) {
3279 entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE;
3280 } else {
3281 entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER;
3282 entry->interceptKeyWakeupTime = now() + delay;
3283 }
Jeff Brownac386072011-07-20 15:19:50 -07003284 entry->release();
Jeff Brownb88102f2010-09-08 11:49:43 -07003285}
3286
Jeff Brown3915bb82010-11-05 15:02:16 -07003287void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(
3288 CommandEntry* commandEntry) {
3289 sp<Connection> connection = commandEntry->connection;
3290 bool handled = commandEntry->handled;
3291
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003292 bool skipNext = false;
Jeff Brown49ed71d2010-12-06 17:13:33 -08003293 if (!connection->outboundQueue.isEmpty()) {
Jeff Brownac386072011-07-20 15:19:50 -07003294 DispatchEntry* dispatchEntry = connection->outboundQueue.head;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003295 if (dispatchEntry->inProgress) {
3296 if (dispatchEntry->eventEntry->type == EventEntry::TYPE_KEY) {
3297 KeyEntry* keyEntry = static_cast<KeyEntry*>(dispatchEntry->eventEntry);
3298 skipNext = afterKeyEventLockedInterruptible(connection,
3299 dispatchEntry, keyEntry, handled);
3300 } else if (dispatchEntry->eventEntry->type == EventEntry::TYPE_MOTION) {
3301 MotionEntry* motionEntry = static_cast<MotionEntry*>(dispatchEntry->eventEntry);
3302 skipNext = afterMotionEventLockedInterruptible(connection,
3303 dispatchEntry, motionEntry, handled);
Jeff Brown49ed71d2010-12-06 17:13:33 -08003304 }
Jeff Brown3915bb82010-11-05 15:02:16 -07003305 }
3306 }
3307
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003308 if (!skipNext) {
3309 startNextDispatchCycleLocked(now(), connection);
3310 }
3311}
3312
3313bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection,
3314 DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled) {
3315 if (!(keyEntry->flags & AKEY_EVENT_FLAG_FALLBACK)) {
3316 // Get the fallback key state.
3317 // Clear it out after dispatching the UP.
3318 int32_t originalKeyCode = keyEntry->keyCode;
3319 int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode);
3320 if (keyEntry->action == AKEY_EVENT_ACTION_UP) {
3321 connection->inputState.removeFallbackKey(originalKeyCode);
3322 }
3323
3324 if (handled || !dispatchEntry->hasForegroundTarget()) {
3325 // If the application handles the original key for which we previously
3326 // generated a fallback or if the window is not a foreground window,
3327 // then cancel the associated fallback key, if any.
3328 if (fallbackKeyCode != -1) {
3329 if (fallbackKeyCode != AKEYCODE_UNKNOWN) {
3330 CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS,
3331 "application handled the original non-fallback key "
3332 "or is no longer a foreground target, "
3333 "canceling previously dispatched fallback key");
3334 options.keyCode = fallbackKeyCode;
3335 synthesizeCancelationEventsForConnectionLocked(connection, options);
3336 }
3337 connection->inputState.removeFallbackKey(originalKeyCode);
3338 }
3339 } else {
3340 // If the application did not handle a non-fallback key, first check
3341 // that we are in a good state to perform unhandled key event processing
3342 // Then ask the policy what to do with it.
3343 bool initialDown = keyEntry->action == AKEY_EVENT_ACTION_DOWN
3344 && keyEntry->repeatCount == 0;
3345 if (fallbackKeyCode == -1 && !initialDown) {
3346#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003347 ALOGD("Unhandled key event: Skipping unhandled key event processing "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003348 "since this is not an initial down. "
3349 "keyCode=%d, action=%d, repeatCount=%d",
3350 originalKeyCode, keyEntry->action, keyEntry->repeatCount);
3351#endif
3352 return false;
3353 }
3354
3355 // Dispatch the unhandled key to the policy.
3356#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003357 ALOGD("Unhandled key event: Asking policy to perform fallback action. "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003358 "keyCode=%d, action=%d, repeatCount=%d",
3359 keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount);
3360#endif
3361 KeyEvent event;
3362 initializeKeyEvent(&event, keyEntry);
3363
3364 mLock.unlock();
3365
3366 bool fallback = mPolicy->dispatchUnhandledKey(connection->inputWindowHandle,
3367 &event, keyEntry->policyFlags, &event);
3368
3369 mLock.lock();
3370
3371 if (connection->status != Connection::STATUS_NORMAL) {
3372 connection->inputState.removeFallbackKey(originalKeyCode);
3373 return true; // skip next cycle
3374 }
3375
Steve Blockec193de2012-01-09 18:35:44 +00003376 ALOG_ASSERT(connection->outboundQueue.head == dispatchEntry);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003377
3378 // Latch the fallback keycode for this key on an initial down.
3379 // The fallback keycode cannot change at any other point in the lifecycle.
3380 if (initialDown) {
3381 if (fallback) {
3382 fallbackKeyCode = event.getKeyCode();
3383 } else {
3384 fallbackKeyCode = AKEYCODE_UNKNOWN;
3385 }
3386 connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode);
3387 }
3388
Steve Blockec193de2012-01-09 18:35:44 +00003389 ALOG_ASSERT(fallbackKeyCode != -1);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003390
3391 // Cancel the fallback key if the policy decides not to send it anymore.
3392 // We will continue to dispatch the key to the policy but we will no
3393 // longer dispatch a fallback key to the application.
3394 if (fallbackKeyCode != AKEYCODE_UNKNOWN
3395 && (!fallback || fallbackKeyCode != event.getKeyCode())) {
3396#if DEBUG_OUTBOUND_EVENT_DETAILS
3397 if (fallback) {
Steve Block5baa3a62011-12-20 16:23:08 +00003398 ALOGD("Unhandled key event: Policy requested to send key %d"
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003399 "as a fallback for %d, but on the DOWN it had requested "
3400 "to send %d instead. Fallback canceled.",
3401 event.getKeyCode(), originalKeyCode, fallbackKeyCode);
3402 } else {
Steve Block5baa3a62011-12-20 16:23:08 +00003403 ALOGD("Unhandled key event: Policy did not request fallback for %d,"
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003404 "but on the DOWN it had requested to send %d. "
3405 "Fallback canceled.",
3406 originalKeyCode, fallbackKeyCode);
3407 }
3408#endif
3409
3410 CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS,
3411 "canceling fallback, policy no longer desires it");
3412 options.keyCode = fallbackKeyCode;
3413 synthesizeCancelationEventsForConnectionLocked(connection, options);
3414
3415 fallback = false;
3416 fallbackKeyCode = AKEYCODE_UNKNOWN;
3417 if (keyEntry->action != AKEY_EVENT_ACTION_UP) {
3418 connection->inputState.setFallbackKey(originalKeyCode,
3419 fallbackKeyCode);
3420 }
3421 }
3422
3423#if DEBUG_OUTBOUND_EVENT_DETAILS
3424 {
3425 String8 msg;
3426 const KeyedVector<int32_t, int32_t>& fallbackKeys =
3427 connection->inputState.getFallbackKeys();
3428 for (size_t i = 0; i < fallbackKeys.size(); i++) {
3429 msg.appendFormat(", %d->%d", fallbackKeys.keyAt(i),
3430 fallbackKeys.valueAt(i));
3431 }
Steve Block5baa3a62011-12-20 16:23:08 +00003432 ALOGD("Unhandled key event: %d currently tracked fallback keys%s.",
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003433 fallbackKeys.size(), msg.string());
3434 }
3435#endif
3436
3437 if (fallback) {
3438 // Restart the dispatch cycle using the fallback key.
3439 keyEntry->eventTime = event.getEventTime();
3440 keyEntry->deviceId = event.getDeviceId();
3441 keyEntry->source = event.getSource();
3442 keyEntry->flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK;
3443 keyEntry->keyCode = fallbackKeyCode;
3444 keyEntry->scanCode = event.getScanCode();
3445 keyEntry->metaState = event.getMetaState();
3446 keyEntry->repeatCount = event.getRepeatCount();
3447 keyEntry->downTime = event.getDownTime();
3448 keyEntry->syntheticRepeat = false;
3449
3450#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003451 ALOGD("Unhandled key event: Dispatching fallback key. "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003452 "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x",
3453 originalKeyCode, fallbackKeyCode, keyEntry->metaState);
3454#endif
3455
3456 dispatchEntry->inProgress = false;
3457 startDispatchCycleLocked(now(), connection);
3458 return true; // already started next cycle
3459 } else {
3460#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003461 ALOGD("Unhandled key event: No fallback key.");
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003462#endif
3463 }
3464 }
3465 }
3466 return false;
3467}
3468
3469bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection,
3470 DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled) {
3471 return false;
Jeff Brown3915bb82010-11-05 15:02:16 -07003472}
3473
Jeff Brownb88102f2010-09-08 11:49:43 -07003474void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) {
3475 mLock.unlock();
3476
Jeff Brown01ce2e92010-09-26 22:20:12 -07003477 mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType);
Jeff Brownb88102f2010-09-08 11:49:43 -07003478
3479 mLock.lock();
3480}
3481
Jeff Brown3915bb82010-11-05 15:02:16 -07003482void InputDispatcher::initializeKeyEvent(KeyEvent* event, const KeyEntry* entry) {
3483 event->initialize(entry->deviceId, entry->source, entry->action, entry->flags,
3484 entry->keyCode, entry->scanCode, entry->metaState, entry->repeatCount,
3485 entry->downTime, entry->eventTime);
3486}
3487
Jeff Brown519e0242010-09-15 15:18:56 -07003488void InputDispatcher::updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
3489 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication) {
3490 // TODO Write some statistics about how long we spend waiting.
Jeff Brownb88102f2010-09-08 11:49:43 -07003491}
3492
3493void InputDispatcher::dump(String8& dump) {
Jeff Brown89ef0722011-08-10 16:25:21 -07003494 AutoMutex _l(mLock);
3495
Jeff Brownf2f48712010-10-01 17:46:21 -07003496 dump.append("Input Dispatcher State:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003497 dumpDispatchStateLocked(dump);
Jeff Brown214eaf42011-05-26 19:17:02 -07003498
3499 dump.append(INDENT "Configuration:\n");
Jeff Brown214eaf42011-05-26 19:17:02 -07003500 dump.appendFormat(INDENT2 "KeyRepeatDelay: %0.1fms\n", mConfig.keyRepeatDelay * 0.000001f);
3501 dump.appendFormat(INDENT2 "KeyRepeatTimeout: %0.1fms\n", mConfig.keyRepeatTimeout * 0.000001f);
Jeff Brownb88102f2010-09-08 11:49:43 -07003502}
3503
Jeff Brown89ef0722011-08-10 16:25:21 -07003504void InputDispatcher::monitor() {
3505 // Acquire and release the lock to ensure that the dispatcher has not deadlocked.
3506 mLock.lock();
Jeff Brown112b5f52012-01-27 17:32:06 -08003507 mLooper->wake();
3508 mDispatcherIsAliveCondition.wait(mLock);
Jeff Brown89ef0722011-08-10 16:25:21 -07003509 mLock.unlock();
3510}
3511
Jeff Brown9c3cda02010-06-15 01:31:58 -07003512
Jeff Brown519e0242010-09-15 15:18:56 -07003513// --- InputDispatcher::Queue ---
3514
3515template <typename T>
3516uint32_t InputDispatcher::Queue<T>::count() const {
3517 uint32_t result = 0;
Jeff Brownac386072011-07-20 15:19:50 -07003518 for (const T* entry = head; entry; entry = entry->next) {
Jeff Brown519e0242010-09-15 15:18:56 -07003519 result += 1;
3520 }
3521 return result;
3522}
3523
3524
Jeff Brownac386072011-07-20 15:19:50 -07003525// --- InputDispatcher::InjectionState ---
Jeff Brown46b9ac02010-04-22 18:58:52 -07003526
Jeff Brownac386072011-07-20 15:19:50 -07003527InputDispatcher::InjectionState::InjectionState(int32_t injectorPid, int32_t injectorUid) :
3528 refCount(1),
3529 injectorPid(injectorPid), injectorUid(injectorUid),
3530 injectionResult(INPUT_EVENT_INJECTION_PENDING), injectionIsAsync(false),
3531 pendingForegroundDispatches(0) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003532}
3533
Jeff Brownac386072011-07-20 15:19:50 -07003534InputDispatcher::InjectionState::~InjectionState() {
Jeff Brown01ce2e92010-09-26 22:20:12 -07003535}
3536
Jeff Brownac386072011-07-20 15:19:50 -07003537void InputDispatcher::InjectionState::release() {
3538 refCount -= 1;
3539 if (refCount == 0) {
3540 delete this;
3541 } else {
Steve Blockec193de2012-01-09 18:35:44 +00003542 ALOG_ASSERT(refCount > 0);
Jeff Brown01ce2e92010-09-26 22:20:12 -07003543 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07003544}
3545
Jeff Brownac386072011-07-20 15:19:50 -07003546
3547// --- InputDispatcher::EventEntry ---
3548
3549InputDispatcher::EventEntry::EventEntry(int32_t type, nsecs_t eventTime, uint32_t policyFlags) :
3550 refCount(1), type(type), eventTime(eventTime), policyFlags(policyFlags),
3551 injectionState(NULL), dispatchInProgress(false) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003552}
3553
Jeff Brownac386072011-07-20 15:19:50 -07003554InputDispatcher::EventEntry::~EventEntry() {
3555 releaseInjectionState();
3556}
3557
3558void InputDispatcher::EventEntry::release() {
3559 refCount -= 1;
3560 if (refCount == 0) {
3561 delete this;
3562 } else {
Steve Blockec193de2012-01-09 18:35:44 +00003563 ALOG_ASSERT(refCount > 0);
Jeff Brownac386072011-07-20 15:19:50 -07003564 }
3565}
3566
3567void InputDispatcher::EventEntry::releaseInjectionState() {
3568 if (injectionState) {
3569 injectionState->release();
3570 injectionState = NULL;
3571 }
3572}
3573
3574
3575// --- InputDispatcher::ConfigurationChangedEntry ---
3576
3577InputDispatcher::ConfigurationChangedEntry::ConfigurationChangedEntry(nsecs_t eventTime) :
3578 EventEntry(TYPE_CONFIGURATION_CHANGED, eventTime, 0) {
3579}
3580
3581InputDispatcher::ConfigurationChangedEntry::~ConfigurationChangedEntry() {
3582}
3583
3584
Jeff Brown65fd2512011-08-18 11:20:58 -07003585// --- InputDispatcher::DeviceResetEntry ---
3586
3587InputDispatcher::DeviceResetEntry::DeviceResetEntry(nsecs_t eventTime, int32_t deviceId) :
3588 EventEntry(TYPE_DEVICE_RESET, eventTime, 0),
3589 deviceId(deviceId) {
3590}
3591
3592InputDispatcher::DeviceResetEntry::~DeviceResetEntry() {
3593}
3594
3595
Jeff Brownac386072011-07-20 15:19:50 -07003596// --- InputDispatcher::KeyEntry ---
3597
3598InputDispatcher::KeyEntry::KeyEntry(nsecs_t eventTime,
Jeff Brown58a2da82011-01-25 16:02:22 -08003599 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown7fbdc842010-06-17 20:52:56 -07003600 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
Jeff Brownac386072011-07-20 15:19:50 -07003601 int32_t repeatCount, nsecs_t downTime) :
3602 EventEntry(TYPE_KEY, eventTime, policyFlags),
3603 deviceId(deviceId), source(source), action(action), flags(flags),
3604 keyCode(keyCode), scanCode(scanCode), metaState(metaState),
3605 repeatCount(repeatCount), downTime(downTime),
Jeff Brown905805a2011-10-12 13:57:59 -07003606 syntheticRepeat(false), interceptKeyResult(KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN),
3607 interceptKeyWakeupTime(0) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003608}
3609
Jeff Brownac386072011-07-20 15:19:50 -07003610InputDispatcher::KeyEntry::~KeyEntry() {
3611}
Jeff Brown7fbdc842010-06-17 20:52:56 -07003612
Jeff Brownac386072011-07-20 15:19:50 -07003613void InputDispatcher::KeyEntry::recycle() {
3614 releaseInjectionState();
3615
3616 dispatchInProgress = false;
3617 syntheticRepeat = false;
3618 interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN;
Jeff Brown905805a2011-10-12 13:57:59 -07003619 interceptKeyWakeupTime = 0;
Jeff Brownac386072011-07-20 15:19:50 -07003620}
3621
3622
Jeff Brownae9fc032010-08-18 15:51:08 -07003623// --- InputDispatcher::MotionEntry ---
3624
Jeff Brownac386072011-07-20 15:19:50 -07003625InputDispatcher::MotionEntry::MotionEntry(nsecs_t eventTime,
3626 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action, int32_t flags,
3627 int32_t metaState, int32_t buttonState,
3628 int32_t edgeFlags, float xPrecision, float yPrecision,
3629 nsecs_t downTime, uint32_t pointerCount,
3630 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords) :
3631 EventEntry(TYPE_MOTION, eventTime, policyFlags),
Jeff Brown3241b6b2012-02-03 15:08:02 -08003632 eventTime(eventTime),
Jeff Brownac386072011-07-20 15:19:50 -07003633 deviceId(deviceId), source(source), action(action), flags(flags),
3634 metaState(metaState), buttonState(buttonState), edgeFlags(edgeFlags),
3635 xPrecision(xPrecision), yPrecision(yPrecision),
Jeff Brown3241b6b2012-02-03 15:08:02 -08003636 downTime(downTime), pointerCount(pointerCount) {
Jeff Brownac386072011-07-20 15:19:50 -07003637 for (uint32_t i = 0; i < pointerCount; i++) {
3638 this->pointerProperties[i].copyFrom(pointerProperties[i]);
Jeff Brown3241b6b2012-02-03 15:08:02 -08003639 this->pointerCoords[i].copyFrom(pointerCoords[i]);
Jeff Brownac386072011-07-20 15:19:50 -07003640 }
3641}
3642
3643InputDispatcher::MotionEntry::~MotionEntry() {
Jeff Brownac386072011-07-20 15:19:50 -07003644}
3645
3646
3647// --- InputDispatcher::DispatchEntry ---
3648
3649InputDispatcher::DispatchEntry::DispatchEntry(EventEntry* eventEntry,
3650 int32_t targetFlags, float xOffset, float yOffset, float scaleFactor) :
3651 eventEntry(eventEntry), targetFlags(targetFlags),
3652 xOffset(xOffset), yOffset(yOffset), scaleFactor(scaleFactor),
3653 inProgress(false),
Jeff Brown3241b6b2012-02-03 15:08:02 -08003654 resolvedAction(0), resolvedFlags(0) {
Jeff Brownac386072011-07-20 15:19:50 -07003655 eventEntry->refCount += 1;
3656}
3657
3658InputDispatcher::DispatchEntry::~DispatchEntry() {
3659 eventEntry->release();
3660}
3661
Jeff Brownb88102f2010-09-08 11:49:43 -07003662
3663// --- InputDispatcher::InputState ---
3664
Jeff Brownb6997262010-10-08 22:31:17 -07003665InputDispatcher::InputState::InputState() {
Jeff Brownb88102f2010-09-08 11:49:43 -07003666}
3667
3668InputDispatcher::InputState::~InputState() {
3669}
3670
3671bool InputDispatcher::InputState::isNeutral() const {
3672 return mKeyMementos.isEmpty() && mMotionMementos.isEmpty();
3673}
3674
Jeff Brown81346812011-06-28 20:08:48 -07003675bool InputDispatcher::InputState::isHovering(int32_t deviceId, uint32_t source) const {
3676 for (size_t i = 0; i < mMotionMementos.size(); i++) {
3677 const MotionMemento& memento = mMotionMementos.itemAt(i);
3678 if (memento.deviceId == deviceId
3679 && memento.source == source
3680 && memento.hovering) {
3681 return true;
3682 }
3683 }
3684 return false;
3685}
Jeff Brownb88102f2010-09-08 11:49:43 -07003686
Jeff Brown81346812011-06-28 20:08:48 -07003687bool InputDispatcher::InputState::trackKey(const KeyEntry* entry,
3688 int32_t action, int32_t flags) {
3689 switch (action) {
3690 case AKEY_EVENT_ACTION_UP: {
3691 if (entry->flags & AKEY_EVENT_FLAG_FALLBACK) {
3692 for (size_t i = 0; i < mFallbackKeys.size(); ) {
3693 if (mFallbackKeys.valueAt(i) == entry->keyCode) {
3694 mFallbackKeys.removeItemsAt(i);
3695 } else {
3696 i += 1;
3697 }
3698 }
3699 }
3700 ssize_t index = findKeyMemento(entry);
3701 if (index >= 0) {
3702 mKeyMementos.removeAt(index);
3703 return true;
3704 }
Jeff Brown68b909d2011-12-07 16:36:01 -08003705 /* FIXME: We can't just drop the key up event because that prevents creating
3706 * popup windows that are automatically shown when a key is held and then
3707 * dismissed when the key is released. The problem is that the popup will
3708 * not have received the original key down, so the key up will be considered
3709 * to be inconsistent with its observed state. We could perhaps handle this
3710 * by synthesizing a key down but that will cause other problems.
3711 *
3712 * So for now, allow inconsistent key up events to be dispatched.
3713 *
Jeff Brown81346812011-06-28 20:08:48 -07003714#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003715 ALOGD("Dropping inconsistent key up event: deviceId=%d, source=%08x, "
Jeff Brown81346812011-06-28 20:08:48 -07003716 "keyCode=%d, scanCode=%d",
3717 entry->deviceId, entry->source, entry->keyCode, entry->scanCode);
3718#endif
3719 return false;
Jeff Brown68b909d2011-12-07 16:36:01 -08003720 */
3721 return true;
Jeff Brown81346812011-06-28 20:08:48 -07003722 }
3723
3724 case AKEY_EVENT_ACTION_DOWN: {
3725 ssize_t index = findKeyMemento(entry);
3726 if (index >= 0) {
3727 mKeyMementos.removeAt(index);
3728 }
3729 addKeyMemento(entry, flags);
3730 return true;
3731 }
3732
3733 default:
3734 return true;
Jeff Brownb88102f2010-09-08 11:49:43 -07003735 }
3736}
3737
Jeff Brown81346812011-06-28 20:08:48 -07003738bool InputDispatcher::InputState::trackMotion(const MotionEntry* entry,
3739 int32_t action, int32_t flags) {
3740 int32_t actionMasked = action & AMOTION_EVENT_ACTION_MASK;
3741 switch (actionMasked) {
3742 case AMOTION_EVENT_ACTION_UP:
3743 case AMOTION_EVENT_ACTION_CANCEL: {
3744 ssize_t index = findMotionMemento(entry, false /*hovering*/);
3745 if (index >= 0) {
3746 mMotionMementos.removeAt(index);
3747 return true;
Jeff Brownda3d5a92011-03-29 15:11:34 -07003748 }
Jeff Brown81346812011-06-28 20:08:48 -07003749#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003750 ALOGD("Dropping inconsistent motion up or cancel event: deviceId=%d, source=%08x, "
Jeff Brown81346812011-06-28 20:08:48 -07003751 "actionMasked=%d",
3752 entry->deviceId, entry->source, actionMasked);
3753#endif
3754 return false;
Jeff Brownda3d5a92011-03-29 15:11:34 -07003755 }
3756
Jeff Brown81346812011-06-28 20:08:48 -07003757 case AMOTION_EVENT_ACTION_DOWN: {
3758 ssize_t index = findMotionMemento(entry, false /*hovering*/);
3759 if (index >= 0) {
3760 mMotionMementos.removeAt(index);
3761 }
3762 addMotionMemento(entry, flags, false /*hovering*/);
3763 return true;
3764 }
3765
3766 case AMOTION_EVENT_ACTION_POINTER_UP:
3767 case AMOTION_EVENT_ACTION_POINTER_DOWN:
3768 case AMOTION_EVENT_ACTION_MOVE: {
3769 ssize_t index = findMotionMemento(entry, false /*hovering*/);
3770 if (index >= 0) {
3771 MotionMemento& memento = mMotionMementos.editItemAt(index);
3772 memento.setPointers(entry);
3773 return true;
3774 }
Jeff Brown2e45fb62011-06-29 21:19:05 -07003775 if (actionMasked == AMOTION_EVENT_ACTION_MOVE
3776 && (entry->source & (AINPUT_SOURCE_CLASS_JOYSTICK
3777 | AINPUT_SOURCE_CLASS_NAVIGATION))) {
3778 // Joysticks and trackballs can send MOVE events without corresponding DOWN or UP.
3779 return true;
3780 }
Jeff Brown81346812011-06-28 20:08:48 -07003781#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003782 ALOGD("Dropping inconsistent motion pointer up/down or move event: "
Jeff Brown81346812011-06-28 20:08:48 -07003783 "deviceId=%d, source=%08x, actionMasked=%d",
3784 entry->deviceId, entry->source, actionMasked);
3785#endif
3786 return false;
3787 }
3788
3789 case AMOTION_EVENT_ACTION_HOVER_EXIT: {
3790 ssize_t index = findMotionMemento(entry, true /*hovering*/);
3791 if (index >= 0) {
3792 mMotionMementos.removeAt(index);
3793 return true;
3794 }
3795#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003796 ALOGD("Dropping inconsistent motion hover exit event: deviceId=%d, source=%08x",
Jeff Brown81346812011-06-28 20:08:48 -07003797 entry->deviceId, entry->source);
3798#endif
3799 return false;
3800 }
3801
3802 case AMOTION_EVENT_ACTION_HOVER_ENTER:
3803 case AMOTION_EVENT_ACTION_HOVER_MOVE: {
3804 ssize_t index = findMotionMemento(entry, true /*hovering*/);
3805 if (index >= 0) {
3806 mMotionMementos.removeAt(index);
3807 }
3808 addMotionMemento(entry, flags, true /*hovering*/);
3809 return true;
3810 }
3811
3812 default:
3813 return true;
3814 }
3815}
3816
3817ssize_t InputDispatcher::InputState::findKeyMemento(const KeyEntry* entry) const {
Jeff Brownb88102f2010-09-08 11:49:43 -07003818 for (size_t i = 0; i < mKeyMementos.size(); i++) {
Jeff Brown81346812011-06-28 20:08:48 -07003819 const KeyMemento& memento = mKeyMementos.itemAt(i);
Jeff Brownb88102f2010-09-08 11:49:43 -07003820 if (memento.deviceId == entry->deviceId
3821 && memento.source == entry->source
3822 && memento.keyCode == entry->keyCode
3823 && memento.scanCode == entry->scanCode) {
Jeff Brown81346812011-06-28 20:08:48 -07003824 return i;
Jeff Brownb88102f2010-09-08 11:49:43 -07003825 }
3826 }
Jeff Brown81346812011-06-28 20:08:48 -07003827 return -1;
Jeff Brownb88102f2010-09-08 11:49:43 -07003828}
3829
Jeff Brown81346812011-06-28 20:08:48 -07003830ssize_t InputDispatcher::InputState::findMotionMemento(const MotionEntry* entry,
3831 bool hovering) const {
Jeff Brownb88102f2010-09-08 11:49:43 -07003832 for (size_t i = 0; i < mMotionMementos.size(); i++) {
Jeff Brown81346812011-06-28 20:08:48 -07003833 const MotionMemento& memento = mMotionMementos.itemAt(i);
Jeff Brownb88102f2010-09-08 11:49:43 -07003834 if (memento.deviceId == entry->deviceId
Jeff Brown81346812011-06-28 20:08:48 -07003835 && memento.source == entry->source
3836 && memento.hovering == hovering) {
3837 return i;
Jeff Brownb88102f2010-09-08 11:49:43 -07003838 }
3839 }
Jeff Brown81346812011-06-28 20:08:48 -07003840 return -1;
3841}
Jeff Brownb88102f2010-09-08 11:49:43 -07003842
Jeff Brown81346812011-06-28 20:08:48 -07003843void InputDispatcher::InputState::addKeyMemento(const KeyEntry* entry, int32_t flags) {
3844 mKeyMementos.push();
3845 KeyMemento& memento = mKeyMementos.editTop();
3846 memento.deviceId = entry->deviceId;
3847 memento.source = entry->source;
3848 memento.keyCode = entry->keyCode;
3849 memento.scanCode = entry->scanCode;
3850 memento.flags = flags;
3851 memento.downTime = entry->downTime;
3852}
3853
3854void InputDispatcher::InputState::addMotionMemento(const MotionEntry* entry,
3855 int32_t flags, bool hovering) {
3856 mMotionMementos.push();
3857 MotionMemento& memento = mMotionMementos.editTop();
3858 memento.deviceId = entry->deviceId;
3859 memento.source = entry->source;
3860 memento.flags = flags;
3861 memento.xPrecision = entry->xPrecision;
3862 memento.yPrecision = entry->yPrecision;
3863 memento.downTime = entry->downTime;
3864 memento.setPointers(entry);
3865 memento.hovering = hovering;
Jeff Brownb88102f2010-09-08 11:49:43 -07003866}
3867
3868void InputDispatcher::InputState::MotionMemento::setPointers(const MotionEntry* entry) {
3869 pointerCount = entry->pointerCount;
3870 for (uint32_t i = 0; i < entry->pointerCount; i++) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003871 pointerProperties[i].copyFrom(entry->pointerProperties[i]);
Jeff Brown3241b6b2012-02-03 15:08:02 -08003872 pointerCoords[i].copyFrom(entry->pointerCoords[i]);
Jeff Brownb88102f2010-09-08 11:49:43 -07003873 }
3874}
3875
Jeff Brownb6997262010-10-08 22:31:17 -07003876void InputDispatcher::InputState::synthesizeCancelationEvents(nsecs_t currentTime,
Jeff Brownac386072011-07-20 15:19:50 -07003877 Vector<EventEntry*>& outEvents, const CancelationOptions& options) {
Jeff Brown81346812011-06-28 20:08:48 -07003878 for (size_t i = 0; i < mKeyMementos.size(); i++) {
Jeff Brownb88102f2010-09-08 11:49:43 -07003879 const KeyMemento& memento = mKeyMementos.itemAt(i);
Jeff Brown49ed71d2010-12-06 17:13:33 -08003880 if (shouldCancelKey(memento, options)) {
Jeff Brownac386072011-07-20 15:19:50 -07003881 outEvents.push(new KeyEntry(currentTime,
Jeff Brownb6997262010-10-08 22:31:17 -07003882 memento.deviceId, memento.source, 0,
Jeff Brown49ed71d2010-12-06 17:13:33 -08003883 AKEY_EVENT_ACTION_UP, memento.flags | AKEY_EVENT_FLAG_CANCELED,
Jeff Brownb6997262010-10-08 22:31:17 -07003884 memento.keyCode, memento.scanCode, 0, 0, memento.downTime));
Jeff Brownb6997262010-10-08 22:31:17 -07003885 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003886 }
3887
Jeff Brown81346812011-06-28 20:08:48 -07003888 for (size_t i = 0; i < mMotionMementos.size(); i++) {
Jeff Brownb88102f2010-09-08 11:49:43 -07003889 const MotionMemento& memento = mMotionMementos.itemAt(i);
Jeff Brown49ed71d2010-12-06 17:13:33 -08003890 if (shouldCancelMotion(memento, options)) {
Jeff Brownac386072011-07-20 15:19:50 -07003891 outEvents.push(new MotionEntry(currentTime,
Jeff Brownb6997262010-10-08 22:31:17 -07003892 memento.deviceId, memento.source, 0,
Jeff Browna032cc02011-03-07 16:56:21 -08003893 memento.hovering
3894 ? AMOTION_EVENT_ACTION_HOVER_EXIT
3895 : AMOTION_EVENT_ACTION_CANCEL,
Jeff Brown81346812011-06-28 20:08:48 -07003896 memento.flags, 0, 0, 0,
Jeff Brownb6997262010-10-08 22:31:17 -07003897 memento.xPrecision, memento.yPrecision, memento.downTime,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003898 memento.pointerCount, memento.pointerProperties, memento.pointerCoords));
Jeff Brownb6997262010-10-08 22:31:17 -07003899 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003900 }
3901}
3902
3903void InputDispatcher::InputState::clear() {
3904 mKeyMementos.clear();
3905 mMotionMementos.clear();
Jeff Brownda3d5a92011-03-29 15:11:34 -07003906 mFallbackKeys.clear();
Jeff Brownb6997262010-10-08 22:31:17 -07003907}
3908
Jeff Brown9c9f1a32010-10-11 18:32:20 -07003909void InputDispatcher::InputState::copyPointerStateTo(InputState& other) const {
3910 for (size_t i = 0; i < mMotionMementos.size(); i++) {
3911 const MotionMemento& memento = mMotionMementos.itemAt(i);
3912 if (memento.source & AINPUT_SOURCE_CLASS_POINTER) {
3913 for (size_t j = 0; j < other.mMotionMementos.size(); ) {
3914 const MotionMemento& otherMemento = other.mMotionMementos.itemAt(j);
3915 if (memento.deviceId == otherMemento.deviceId
3916 && memento.source == otherMemento.source) {
3917 other.mMotionMementos.removeAt(j);
3918 } else {
3919 j += 1;
3920 }
3921 }
3922 other.mMotionMementos.push(memento);
3923 }
3924 }
3925}
3926
Jeff Brownda3d5a92011-03-29 15:11:34 -07003927int32_t InputDispatcher::InputState::getFallbackKey(int32_t originalKeyCode) {
3928 ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode);
3929 return index >= 0 ? mFallbackKeys.valueAt(index) : -1;
3930}
3931
3932void InputDispatcher::InputState::setFallbackKey(int32_t originalKeyCode,
3933 int32_t fallbackKeyCode) {
3934 ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode);
3935 if (index >= 0) {
3936 mFallbackKeys.replaceValueAt(index, fallbackKeyCode);
3937 } else {
3938 mFallbackKeys.add(originalKeyCode, fallbackKeyCode);
3939 }
3940}
3941
3942void InputDispatcher::InputState::removeFallbackKey(int32_t originalKeyCode) {
3943 mFallbackKeys.removeItem(originalKeyCode);
3944}
3945
Jeff Brown49ed71d2010-12-06 17:13:33 -08003946bool InputDispatcher::InputState::shouldCancelKey(const KeyMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -07003947 const CancelationOptions& options) {
3948 if (options.keyCode != -1 && memento.keyCode != options.keyCode) {
3949 return false;
3950 }
3951
Jeff Brown65fd2512011-08-18 11:20:58 -07003952 if (options.deviceId != -1 && memento.deviceId != options.deviceId) {
3953 return false;
3954 }
3955
Jeff Brownda3d5a92011-03-29 15:11:34 -07003956 switch (options.mode) {
3957 case CancelationOptions::CANCEL_ALL_EVENTS:
3958 case CancelationOptions::CANCEL_NON_POINTER_EVENTS:
Jeff Brownb6997262010-10-08 22:31:17 -07003959 return true;
Jeff Brownda3d5a92011-03-29 15:11:34 -07003960 case CancelationOptions::CANCEL_FALLBACK_EVENTS:
Jeff Brown49ed71d2010-12-06 17:13:33 -08003961 return memento.flags & AKEY_EVENT_FLAG_FALLBACK;
3962 default:
3963 return false;
3964 }
3965}
3966
3967bool InputDispatcher::InputState::shouldCancelMotion(const MotionMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -07003968 const CancelationOptions& options) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003969 if (options.deviceId != -1 && memento.deviceId != options.deviceId) {
3970 return false;
3971 }
3972
Jeff Brownda3d5a92011-03-29 15:11:34 -07003973 switch (options.mode) {
3974 case CancelationOptions::CANCEL_ALL_EVENTS:
Jeff Brown49ed71d2010-12-06 17:13:33 -08003975 return true;
Jeff Brownda3d5a92011-03-29 15:11:34 -07003976 case CancelationOptions::CANCEL_POINTER_EVENTS:
Jeff Brown49ed71d2010-12-06 17:13:33 -08003977 return memento.source & AINPUT_SOURCE_CLASS_POINTER;
Jeff Brownda3d5a92011-03-29 15:11:34 -07003978 case CancelationOptions::CANCEL_NON_POINTER_EVENTS:
Jeff Brown49ed71d2010-12-06 17:13:33 -08003979 return !(memento.source & AINPUT_SOURCE_CLASS_POINTER);
3980 default:
3981 return false;
Jeff Brownb6997262010-10-08 22:31:17 -07003982 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003983}
3984
3985
Jeff Brown46b9ac02010-04-22 18:58:52 -07003986// --- InputDispatcher::Connection ---
3987
Jeff Brown928e0542011-01-10 11:17:36 -08003988InputDispatcher::Connection::Connection(const sp<InputChannel>& inputChannel,
Jeff Browncc4f7db2011-08-30 20:34:48 -07003989 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) :
Jeff Brown928e0542011-01-10 11:17:36 -08003990 status(STATUS_NORMAL), inputChannel(inputChannel), inputWindowHandle(inputWindowHandle),
Jeff Browncc4f7db2011-08-30 20:34:48 -07003991 monitor(monitor),
Jeff Brown928e0542011-01-10 11:17:36 -08003992 inputPublisher(inputChannel),
Jeff Brownda3d5a92011-03-29 15:11:34 -07003993 lastEventTime(LONG_LONG_MAX), lastDispatchTime(LONG_LONG_MAX) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003994}
3995
3996InputDispatcher::Connection::~Connection() {
3997}
3998
Jeff Brown9c3cda02010-06-15 01:31:58 -07003999const char* InputDispatcher::Connection::getStatusLabel() const {
4000 switch (status) {
4001 case STATUS_NORMAL:
4002 return "NORMAL";
4003
4004 case STATUS_BROKEN:
4005 return "BROKEN";
4006
Jeff Brown9c3cda02010-06-15 01:31:58 -07004007 case STATUS_ZOMBIE:
4008 return "ZOMBIE";
4009
4010 default:
4011 return "UNKNOWN";
4012 }
4013}
4014
Jeff Brown46b9ac02010-04-22 18:58:52 -07004015InputDispatcher::DispatchEntry* InputDispatcher::Connection::findQueuedDispatchEntryForEvent(
4016 const EventEntry* eventEntry) const {
Jeff Brownac386072011-07-20 15:19:50 -07004017 for (DispatchEntry* dispatchEntry = outboundQueue.tail; dispatchEntry;
4018 dispatchEntry = dispatchEntry->prev) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07004019 if (dispatchEntry->eventEntry == eventEntry) {
4020 return dispatchEntry;
4021 }
4022 }
4023 return NULL;
4024}
4025
Jeff Brownb88102f2010-09-08 11:49:43 -07004026
Jeff Brown9c3cda02010-06-15 01:31:58 -07004027// --- InputDispatcher::CommandEntry ---
4028
Jeff Brownac386072011-07-20 15:19:50 -07004029InputDispatcher::CommandEntry::CommandEntry(Command command) :
4030 command(command), eventTime(0), keyEntry(NULL), userActivityEventType(0), handled(false) {
Jeff Brown9c3cda02010-06-15 01:31:58 -07004031}
4032
4033InputDispatcher::CommandEntry::~CommandEntry() {
4034}
4035
Jeff Brown46b9ac02010-04-22 18:58:52 -07004036
Jeff Brown01ce2e92010-09-26 22:20:12 -07004037// --- InputDispatcher::TouchState ---
4038
4039InputDispatcher::TouchState::TouchState() :
Jeff Brown58a2da82011-01-25 16:02:22 -08004040 down(false), split(false), deviceId(-1), source(0) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07004041}
4042
4043InputDispatcher::TouchState::~TouchState() {
4044}
4045
4046void InputDispatcher::TouchState::reset() {
4047 down = false;
4048 split = false;
Jeff Brown95712852011-01-04 19:41:59 -08004049 deviceId = -1;
Jeff Brown58a2da82011-01-25 16:02:22 -08004050 source = 0;
Jeff Brown01ce2e92010-09-26 22:20:12 -07004051 windows.clear();
4052}
4053
4054void InputDispatcher::TouchState::copyFrom(const TouchState& other) {
4055 down = other.down;
4056 split = other.split;
Jeff Brown95712852011-01-04 19:41:59 -08004057 deviceId = other.deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -08004058 source = other.source;
Jeff Brown9302c872011-07-13 22:51:29 -07004059 windows = other.windows;
Jeff Brown01ce2e92010-09-26 22:20:12 -07004060}
4061
Jeff Brown9302c872011-07-13 22:51:29 -07004062void InputDispatcher::TouchState::addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle,
Jeff Brown01ce2e92010-09-26 22:20:12 -07004063 int32_t targetFlags, BitSet32 pointerIds) {
4064 if (targetFlags & InputTarget::FLAG_SPLIT) {
4065 split = true;
4066 }
4067
4068 for (size_t i = 0; i < windows.size(); i++) {
4069 TouchedWindow& touchedWindow = windows.editItemAt(i);
Jeff Brown9302c872011-07-13 22:51:29 -07004070 if (touchedWindow.windowHandle == windowHandle) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07004071 touchedWindow.targetFlags |= targetFlags;
Jeff Brown98db5fa2011-06-08 15:37:10 -07004072 if (targetFlags & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) {
4073 touchedWindow.targetFlags &= ~InputTarget::FLAG_DISPATCH_AS_IS;
4074 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07004075 touchedWindow.pointerIds.value |= pointerIds.value;
4076 return;
4077 }
4078 }
4079
4080 windows.push();
4081
4082 TouchedWindow& touchedWindow = windows.editTop();
Jeff Brown9302c872011-07-13 22:51:29 -07004083 touchedWindow.windowHandle = windowHandle;
Jeff Brown01ce2e92010-09-26 22:20:12 -07004084 touchedWindow.targetFlags = targetFlags;
4085 touchedWindow.pointerIds = pointerIds;
Jeff Brown01ce2e92010-09-26 22:20:12 -07004086}
4087
Jeff Browna032cc02011-03-07 16:56:21 -08004088void InputDispatcher::TouchState::filterNonAsIsTouchWindows() {
Jeff Brown01ce2e92010-09-26 22:20:12 -07004089 for (size_t i = 0 ; i < windows.size(); ) {
Jeff Browna032cc02011-03-07 16:56:21 -08004090 TouchedWindow& window = windows.editItemAt(i);
Jeff Brown98db5fa2011-06-08 15:37:10 -07004091 if (window.targetFlags & (InputTarget::FLAG_DISPATCH_AS_IS
4092 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER)) {
Jeff Browna032cc02011-03-07 16:56:21 -08004093 window.targetFlags &= ~InputTarget::FLAG_DISPATCH_MASK;
4094 window.targetFlags |= InputTarget::FLAG_DISPATCH_AS_IS;
Jeff Brown01ce2e92010-09-26 22:20:12 -07004095 i += 1;
Jeff Browna032cc02011-03-07 16:56:21 -08004096 } else {
4097 windows.removeAt(i);
Jeff Brown01ce2e92010-09-26 22:20:12 -07004098 }
4099 }
4100}
4101
Jeff Brown9302c872011-07-13 22:51:29 -07004102sp<InputWindowHandle> InputDispatcher::TouchState::getFirstForegroundWindowHandle() const {
Jeff Brown01ce2e92010-09-26 22:20:12 -07004103 for (size_t i = 0; i < windows.size(); i++) {
Jeff Brown98db5fa2011-06-08 15:37:10 -07004104 const TouchedWindow& window = windows.itemAt(i);
4105 if (window.targetFlags & InputTarget::FLAG_FOREGROUND) {
Jeff Brown9302c872011-07-13 22:51:29 -07004106 return window.windowHandle;
Jeff Brown01ce2e92010-09-26 22:20:12 -07004107 }
4108 }
4109 return NULL;
4110}
4111
Jeff Brown98db5fa2011-06-08 15:37:10 -07004112bool InputDispatcher::TouchState::isSlippery() const {
4113 // Must have exactly one foreground window.
4114 bool haveSlipperyForegroundWindow = false;
4115 for (size_t i = 0; i < windows.size(); i++) {
4116 const TouchedWindow& window = windows.itemAt(i);
4117 if (window.targetFlags & InputTarget::FLAG_FOREGROUND) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07004118 if (haveSlipperyForegroundWindow
4119 || !(window.windowHandle->getInfo()->layoutParamsFlags
4120 & InputWindowInfo::FLAG_SLIPPERY)) {
Jeff Brown98db5fa2011-06-08 15:37:10 -07004121 return false;
4122 }
4123 haveSlipperyForegroundWindow = true;
4124 }
4125 }
4126 return haveSlipperyForegroundWindow;
4127}
4128
Jeff Brown01ce2e92010-09-26 22:20:12 -07004129
Jeff Brown46b9ac02010-04-22 18:58:52 -07004130// --- InputDispatcherThread ---
4131
4132InputDispatcherThread::InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher) :
4133 Thread(/*canCallJava*/ true), mDispatcher(dispatcher) {
4134}
4135
4136InputDispatcherThread::~InputDispatcherThread() {
4137}
4138
4139bool InputDispatcherThread::threadLoop() {
4140 mDispatcher->dispatchOnce();
4141 return true;
4142}
4143
4144} // namespace android