blob: 87cf0dea9248b9a32aed02b739a103afda8712ad [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 Brownf2f487182010-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 Brownb88102f2010-09-08 11:49:43 -0700181 mCurrentInputTargetsValid(false),
Jeff Brown9302c872011-07-13 22:51:29 -0700182 mInputTargetWaitCause(INPUT_TARGET_WAIT_CAUSE_NONE) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700183 mLooper = new Looper(false);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700184
Jeff Brown46b9ac02010-04-22 18:58:52 -0700185 mKeyRepeatState.lastKeyEntry = NULL;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700186
Jeff Brown214eaf42011-05-26 19:17:02 -0700187 policy->getDispatcherConfiguration(&mConfig);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700188}
189
190InputDispatcher::~InputDispatcher() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700191 { // acquire lock
192 AutoMutex _l(mLock);
193
194 resetKeyRepeatLocked();
Jeff Brown54a18252010-09-16 14:07:33 -0700195 releasePendingEventLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700196 drainInboundQueueLocked();
197 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700198
Jeff Browncbee6d62012-02-03 20:11:27 -0800199 while (mConnectionsByFd.size() != 0) {
200 unregisterInputChannel(mConnectionsByFd.valueAt(0)->inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700201 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700202}
203
204void InputDispatcher::dispatchOnce() {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700205 nsecs_t nextWakeupTime = LONG_LONG_MAX;
206 { // acquire lock
207 AutoMutex _l(mLock);
Jeff Brown112b5f52012-01-27 17:32:06 -0800208 mDispatcherIsAliveCondition.broadcast();
209
Jeff Brown214eaf42011-05-26 19:17:02 -0700210 dispatchOnceInnerLocked(&nextWakeupTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700211
Jeff Brownb88102f2010-09-08 11:49:43 -0700212 if (runCommandsLockedInterruptible()) {
213 nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately
Jeff Brown46b9ac02010-04-22 18:58:52 -0700214 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700215 } // release lock
216
Jeff Brownb88102f2010-09-08 11:49:43 -0700217 // Wait for callback or timeout or wake. (make sure we round up, not down)
218 nsecs_t currentTime = now();
Jeff Brownaa3855d2011-03-17 01:34:19 -0700219 int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700220 mLooper->pollOnce(timeoutMillis);
Jeff Brownb88102f2010-09-08 11:49:43 -0700221}
222
Jeff Brown214eaf42011-05-26 19:17:02 -0700223void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700224 nsecs_t currentTime = now();
225
226 // Reset the key repeat timer whenever we disallow key events, even if the next event
227 // is not a key. This is to ensure that we abort a key repeat if the device is just coming
228 // out of sleep.
Jeff Brown214eaf42011-05-26 19:17:02 -0700229 if (!mPolicy->isKeyRepeatEnabled()) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700230 resetKeyRepeatLocked();
231 }
232
Jeff Brownb88102f2010-09-08 11:49:43 -0700233 // If dispatching is frozen, do not process timeouts or try to deliver any new events.
234 if (mDispatchFrozen) {
235#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +0000236 ALOGD("Dispatch frozen. Waiting some more.");
Jeff Brownb88102f2010-09-08 11:49:43 -0700237#endif
238 return;
239 }
240
241 // Optimize latency of app switches.
242 // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has
243 // been pressed. When it expires, we preempt dispatch and drop all other pending events.
244 bool isAppSwitchDue = mAppSwitchDueTime <= currentTime;
245 if (mAppSwitchDueTime < *nextWakeupTime) {
246 *nextWakeupTime = mAppSwitchDueTime;
247 }
248
Jeff Brownb88102f2010-09-08 11:49:43 -0700249 // Ready to start a new event.
250 // If we don't already have a pending event, go grab one.
251 if (! mPendingEvent) {
252 if (mInboundQueue.isEmpty()) {
253 if (isAppSwitchDue) {
254 // The inbound queue is empty so the app switch key we were waiting
255 // for will never arrive. Stop waiting for it.
256 resetPendingAppSwitchLocked(false);
257 isAppSwitchDue = false;
258 }
259
260 // Synthesize a key repeat if appropriate.
261 if (mKeyRepeatState.lastKeyEntry) {
262 if (currentTime >= mKeyRepeatState.nextRepeatTime) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700263 mPendingEvent = synthesizeKeyRepeatLocked(currentTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700264 } else {
265 if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) {
266 *nextWakeupTime = mKeyRepeatState.nextRepeatTime;
267 }
268 }
269 }
Jeff Browncc4f7db2011-08-30 20:34:48 -0700270
271 // Nothing to do if there is no pending event.
Jeff Brownb88102f2010-09-08 11:49:43 -0700272 if (! mPendingEvent) {
Jeff Browncc4f7db2011-08-30 20:34:48 -0700273 if (mActiveConnections.isEmpty()) {
274 dispatchIdleLocked();
275 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700276 return;
277 }
278 } else {
279 // Inbound queue has at least one entry.
Jeff Brown30802802012-02-03 13:35:13 -0800280 EventEntry* entry = mInboundQueue.dequeueAtHead();
Jeff Brownb88102f2010-09-08 11:49:43 -0700281 mPendingEvent = entry;
282 }
Jeff Browne2fe69e2010-10-18 13:21:23 -0700283
284 // Poke user activity for this event.
285 if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) {
286 pokeUserActivityLocked(mPendingEvent);
287 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700288 }
289
290 // Now we have an event to dispatch.
Jeff Brown928e0542011-01-10 11:17:36 -0800291 // All events are eventually dequeued and processed this way, even if we intend to drop them.
Steve Blockec193de2012-01-09 18:35:44 +0000292 ALOG_ASSERT(mPendingEvent != NULL);
Jeff Brown54a18252010-09-16 14:07:33 -0700293 bool done = false;
Jeff Brownb6997262010-10-08 22:31:17 -0700294 DropReason dropReason = DROP_REASON_NOT_DROPPED;
295 if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) {
296 dropReason = DROP_REASON_POLICY;
297 } else if (!mDispatchEnabled) {
298 dropReason = DROP_REASON_DISABLED;
299 }
Jeff Brown928e0542011-01-10 11:17:36 -0800300
301 if (mNextUnblockedEvent == mPendingEvent) {
302 mNextUnblockedEvent = NULL;
303 }
304
Jeff Brownb88102f2010-09-08 11:49:43 -0700305 switch (mPendingEvent->type) {
306 case EventEntry::TYPE_CONFIGURATION_CHANGED: {
307 ConfigurationChangedEntry* typedEntry =
308 static_cast<ConfigurationChangedEntry*>(mPendingEvent);
Jeff Brown54a18252010-09-16 14:07:33 -0700309 done = dispatchConfigurationChangedLocked(currentTime, typedEntry);
Jeff Brownb6997262010-10-08 22:31:17 -0700310 dropReason = DROP_REASON_NOT_DROPPED; // configuration changes are never dropped
Jeff Brownb88102f2010-09-08 11:49:43 -0700311 break;
312 }
313
Jeff Brown65fd2512011-08-18 11:20:58 -0700314 case EventEntry::TYPE_DEVICE_RESET: {
315 DeviceResetEntry* typedEntry =
316 static_cast<DeviceResetEntry*>(mPendingEvent);
317 done = dispatchDeviceResetLocked(currentTime, typedEntry);
318 dropReason = DROP_REASON_NOT_DROPPED; // device resets are never dropped
319 break;
320 }
321
Jeff Brownb88102f2010-09-08 11:49:43 -0700322 case EventEntry::TYPE_KEY: {
323 KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent);
Jeff Brownb6997262010-10-08 22:31:17 -0700324 if (isAppSwitchDue) {
325 if (isAppSwitchKeyEventLocked(typedEntry)) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700326 resetPendingAppSwitchLocked(true);
Jeff Brownb6997262010-10-08 22:31:17 -0700327 isAppSwitchDue = false;
328 } else if (dropReason == DROP_REASON_NOT_DROPPED) {
329 dropReason = DROP_REASON_APP_SWITCH;
Jeff Brownb88102f2010-09-08 11:49:43 -0700330 }
331 }
Jeff Brown928e0542011-01-10 11:17:36 -0800332 if (dropReason == DROP_REASON_NOT_DROPPED
333 && isStaleEventLocked(currentTime, typedEntry)) {
334 dropReason = DROP_REASON_STALE;
335 }
336 if (dropReason == DROP_REASON_NOT_DROPPED && mNextUnblockedEvent) {
337 dropReason = DROP_REASON_BLOCKED;
338 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700339 done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700340 break;
341 }
342
343 case EventEntry::TYPE_MOTION: {
344 MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent);
Jeff Brownb6997262010-10-08 22:31:17 -0700345 if (dropReason == DROP_REASON_NOT_DROPPED && isAppSwitchDue) {
346 dropReason = DROP_REASON_APP_SWITCH;
Jeff Brownb88102f2010-09-08 11:49:43 -0700347 }
Jeff Brown928e0542011-01-10 11:17:36 -0800348 if (dropReason == DROP_REASON_NOT_DROPPED
349 && isStaleEventLocked(currentTime, typedEntry)) {
350 dropReason = DROP_REASON_STALE;
351 }
352 if (dropReason == DROP_REASON_NOT_DROPPED && mNextUnblockedEvent) {
353 dropReason = DROP_REASON_BLOCKED;
354 }
Jeff Brownb6997262010-10-08 22:31:17 -0700355 done = dispatchMotionLocked(currentTime, typedEntry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700356 &dropReason, nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700357 break;
358 }
359
360 default:
Steve Blockec193de2012-01-09 18:35:44 +0000361 ALOG_ASSERT(false);
Jeff Brownb88102f2010-09-08 11:49:43 -0700362 break;
363 }
364
Jeff Brown54a18252010-09-16 14:07:33 -0700365 if (done) {
Jeff Brownb6997262010-10-08 22:31:17 -0700366 if (dropReason != DROP_REASON_NOT_DROPPED) {
367 dropInboundEventLocked(mPendingEvent, dropReason);
368 }
369
Jeff Brown54a18252010-09-16 14:07:33 -0700370 releasePendingEventLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700371 *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately
372 }
373}
374
Jeff Browncc4f7db2011-08-30 20:34:48 -0700375void InputDispatcher::dispatchIdleLocked() {
376#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +0000377 ALOGD("Dispatcher idle. There are no pending events or active connections.");
Jeff Browncc4f7db2011-08-30 20:34:48 -0700378#endif
379
380 // Reset targets when idle, to release input channels and other resources
381 // they are holding onto.
382 resetTargetsLocked();
383}
384
Jeff Brownb88102f2010-09-08 11:49:43 -0700385bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) {
386 bool needWake = mInboundQueue.isEmpty();
387 mInboundQueue.enqueueAtTail(entry);
388
389 switch (entry->type) {
Jeff Brownb6997262010-10-08 22:31:17 -0700390 case EventEntry::TYPE_KEY: {
Jeff Brown928e0542011-01-10 11:17:36 -0800391 // Optimize app switch latency.
392 // If the application takes too long to catch up then we drop all events preceding
393 // the app switch key.
Jeff Brownb6997262010-10-08 22:31:17 -0700394 KeyEntry* keyEntry = static_cast<KeyEntry*>(entry);
395 if (isAppSwitchKeyEventLocked(keyEntry)) {
396 if (keyEntry->action == AKEY_EVENT_ACTION_DOWN) {
397 mAppSwitchSawKeyDown = true;
398 } else if (keyEntry->action == AKEY_EVENT_ACTION_UP) {
399 if (mAppSwitchSawKeyDown) {
400#if DEBUG_APP_SWITCH
Steve Block5baa3a62011-12-20 16:23:08 +0000401 ALOGD("App switch is pending!");
Jeff Brownb6997262010-10-08 22:31:17 -0700402#endif
403 mAppSwitchDueTime = keyEntry->eventTime + APP_SWITCH_TIMEOUT;
404 mAppSwitchSawKeyDown = false;
405 needWake = true;
406 }
407 }
408 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700409 break;
410 }
Jeff Brown928e0542011-01-10 11:17:36 -0800411
412 case EventEntry::TYPE_MOTION: {
413 // Optimize case where the current application is unresponsive and the user
414 // decides to touch a window in a different application.
415 // If the application takes too long to catch up then we drop all events preceding
416 // the touch into the other window.
417 MotionEntry* motionEntry = static_cast<MotionEntry*>(entry);
Jeff Brown33bbfd22011-02-24 20:55:35 -0800418 if (motionEntry->action == AMOTION_EVENT_ACTION_DOWN
Jeff Brown928e0542011-01-10 11:17:36 -0800419 && (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER)
420 && mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY
Jeff Brown9302c872011-07-13 22:51:29 -0700421 && mInputTargetWaitApplicationHandle != NULL) {
Jeff Brown3241b6b2012-02-03 15:08:02 -0800422 int32_t x = int32_t(motionEntry->pointerCoords[0].
Jeff Brownebbd5d12011-02-17 13:01:34 -0800423 getAxisValue(AMOTION_EVENT_AXIS_X));
Jeff Brown3241b6b2012-02-03 15:08:02 -0800424 int32_t y = int32_t(motionEntry->pointerCoords[0].
Jeff Brownebbd5d12011-02-17 13:01:34 -0800425 getAxisValue(AMOTION_EVENT_AXIS_Y));
Jeff Brown9302c872011-07-13 22:51:29 -0700426 sp<InputWindowHandle> touchedWindowHandle = findTouchedWindowAtLocked(x, y);
427 if (touchedWindowHandle != NULL
428 && touchedWindowHandle->inputApplicationHandle
429 != mInputTargetWaitApplicationHandle) {
Jeff Brown928e0542011-01-10 11:17:36 -0800430 // User touched a different application than the one we are waiting on.
431 // Flag the event, and start pruning the input queue.
432 mNextUnblockedEvent = motionEntry;
433 needWake = true;
434 }
435 }
436 break;
437 }
Jeff Brownb6997262010-10-08 22:31:17 -0700438 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700439
440 return needWake;
441}
442
Jeff Brown9302c872011-07-13 22:51:29 -0700443sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t x, int32_t y) {
Jeff Brown928e0542011-01-10 11:17:36 -0800444 // Traverse windows from front to back to find touched window.
Jeff Brown9302c872011-07-13 22:51:29 -0700445 size_t numWindows = mWindowHandles.size();
Jeff Brown928e0542011-01-10 11:17:36 -0800446 for (size_t i = 0; i < numWindows; i++) {
Jeff Brown9302c872011-07-13 22:51:29 -0700447 sp<InputWindowHandle> windowHandle = mWindowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700448 const InputWindowInfo* windowInfo = windowHandle->getInfo();
449 int32_t flags = windowInfo->layoutParamsFlags;
Jeff Brown928e0542011-01-10 11:17:36 -0800450
Jeff Browncc4f7db2011-08-30 20:34:48 -0700451 if (windowInfo->visible) {
452 if (!(flags & InputWindowInfo::FLAG_NOT_TOUCHABLE)) {
453 bool isTouchModal = (flags & (InputWindowInfo::FLAG_NOT_FOCUSABLE
454 | InputWindowInfo::FLAG_NOT_TOUCH_MODAL)) == 0;
455 if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) {
Jeff Brown928e0542011-01-10 11:17:36 -0800456 // Found window.
Jeff Brown9302c872011-07-13 22:51:29 -0700457 return windowHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800458 }
459 }
460 }
461
Jeff Browncc4f7db2011-08-30 20:34:48 -0700462 if (flags & InputWindowInfo::FLAG_SYSTEM_ERROR) {
Jeff Brown928e0542011-01-10 11:17:36 -0800463 // Error window is on top but not visible, so touch is dropped.
464 return NULL;
465 }
466 }
467 return NULL;
468}
469
Jeff Brownb6997262010-10-08 22:31:17 -0700470void InputDispatcher::dropInboundEventLocked(EventEntry* entry, DropReason dropReason) {
471 const char* reason;
472 switch (dropReason) {
473 case DROP_REASON_POLICY:
Jeff Browne20c9e02010-10-11 14:20:19 -0700474#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +0000475 ALOGD("Dropped event because policy consumed it.");
Jeff Browne20c9e02010-10-11 14:20:19 -0700476#endif
Jeff Brown3122e442010-10-11 23:32:49 -0700477 reason = "inbound event was dropped because the policy consumed it";
Jeff Brownb6997262010-10-08 22:31:17 -0700478 break;
479 case DROP_REASON_DISABLED:
Steve Block6215d3f2012-01-04 20:05:49 +0000480 ALOGI("Dropped event because input dispatch is disabled.");
Jeff Brownb6997262010-10-08 22:31:17 -0700481 reason = "inbound event was dropped because input dispatch is disabled";
482 break;
483 case DROP_REASON_APP_SWITCH:
Steve Block6215d3f2012-01-04 20:05:49 +0000484 ALOGI("Dropped event because of pending overdue app switch.");
Jeff Brownb6997262010-10-08 22:31:17 -0700485 reason = "inbound event was dropped because of pending overdue app switch";
486 break;
Jeff Brown928e0542011-01-10 11:17:36 -0800487 case DROP_REASON_BLOCKED:
Steve Block6215d3f2012-01-04 20:05:49 +0000488 ALOGI("Dropped event because the current application is not responding and the user "
Jeff Brown81346812011-06-28 20:08:48 -0700489 "has started interacting with a different application.");
Jeff Brown928e0542011-01-10 11:17:36 -0800490 reason = "inbound event was dropped because the current application is not responding "
Jeff Brown81346812011-06-28 20:08:48 -0700491 "and the user has started interacting with a different application";
Jeff Brown928e0542011-01-10 11:17:36 -0800492 break;
493 case DROP_REASON_STALE:
Steve Block6215d3f2012-01-04 20:05:49 +0000494 ALOGI("Dropped event because it is stale.");
Jeff Brown928e0542011-01-10 11:17:36 -0800495 reason = "inbound event was dropped because it is stale";
496 break;
Jeff Brownb6997262010-10-08 22:31:17 -0700497 default:
Steve Blockec193de2012-01-09 18:35:44 +0000498 ALOG_ASSERT(false);
Jeff Brownb6997262010-10-08 22:31:17 -0700499 return;
500 }
501
502 switch (entry->type) {
Jeff Brownda3d5a92011-03-29 15:11:34 -0700503 case EventEntry::TYPE_KEY: {
504 CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason);
505 synthesizeCancelationEventsForAllConnectionsLocked(options);
Jeff Brownb6997262010-10-08 22:31:17 -0700506 break;
Jeff Brownda3d5a92011-03-29 15:11:34 -0700507 }
Jeff Brownb6997262010-10-08 22:31:17 -0700508 case EventEntry::TYPE_MOTION: {
509 MotionEntry* motionEntry = static_cast<MotionEntry*>(entry);
510 if (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) {
Jeff Brownda3d5a92011-03-29 15:11:34 -0700511 CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason);
512 synthesizeCancelationEventsForAllConnectionsLocked(options);
Jeff Brownb6997262010-10-08 22:31:17 -0700513 } else {
Jeff Brownda3d5a92011-03-29 15:11:34 -0700514 CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason);
515 synthesizeCancelationEventsForAllConnectionsLocked(options);
Jeff Brownb6997262010-10-08 22:31:17 -0700516 }
517 break;
518 }
519 }
520}
521
522bool InputDispatcher::isAppSwitchKeyCode(int32_t keyCode) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700523 return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL;
524}
525
Jeff Brownb6997262010-10-08 22:31:17 -0700526bool InputDispatcher::isAppSwitchKeyEventLocked(KeyEntry* keyEntry) {
527 return ! (keyEntry->flags & AKEY_EVENT_FLAG_CANCELED)
528 && isAppSwitchKeyCode(keyEntry->keyCode)
Jeff Browne20c9e02010-10-11 14:20:19 -0700529 && (keyEntry->policyFlags & POLICY_FLAG_TRUSTED)
Jeff Brownb6997262010-10-08 22:31:17 -0700530 && (keyEntry->policyFlags & POLICY_FLAG_PASS_TO_USER);
531}
532
Jeff Brownb88102f2010-09-08 11:49:43 -0700533bool InputDispatcher::isAppSwitchPendingLocked() {
534 return mAppSwitchDueTime != LONG_LONG_MAX;
535}
536
Jeff Brownb88102f2010-09-08 11:49:43 -0700537void InputDispatcher::resetPendingAppSwitchLocked(bool handled) {
538 mAppSwitchDueTime = LONG_LONG_MAX;
539
540#if DEBUG_APP_SWITCH
541 if (handled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000542 ALOGD("App switch has arrived.");
Jeff Brownb88102f2010-09-08 11:49:43 -0700543 } else {
Steve Block5baa3a62011-12-20 16:23:08 +0000544 ALOGD("App switch was abandoned.");
Jeff Brownb88102f2010-09-08 11:49:43 -0700545 }
546#endif
Jeff Brown46b9ac02010-04-22 18:58:52 -0700547}
548
Jeff Brown928e0542011-01-10 11:17:36 -0800549bool InputDispatcher::isStaleEventLocked(nsecs_t currentTime, EventEntry* entry) {
550 return currentTime - entry->eventTime >= STALE_EVENT_TIMEOUT;
551}
552
Jeff Brown9c3cda02010-06-15 01:31:58 -0700553bool InputDispatcher::runCommandsLockedInterruptible() {
554 if (mCommandQueue.isEmpty()) {
555 return false;
556 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700557
Jeff Brown9c3cda02010-06-15 01:31:58 -0700558 do {
559 CommandEntry* commandEntry = mCommandQueue.dequeueAtHead();
560
561 Command command = commandEntry->command;
562 (this->*command)(commandEntry); // commands are implicitly 'LockedInterruptible'
563
Jeff Brown7fbdc842010-06-17 20:52:56 -0700564 commandEntry->connection.clear();
Jeff Brownac386072011-07-20 15:19:50 -0700565 delete commandEntry;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700566 } while (! mCommandQueue.isEmpty());
567 return true;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700568}
569
Jeff Brown9c3cda02010-06-15 01:31:58 -0700570InputDispatcher::CommandEntry* InputDispatcher::postCommandLocked(Command command) {
Jeff Brownac386072011-07-20 15:19:50 -0700571 CommandEntry* commandEntry = new CommandEntry(command);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700572 mCommandQueue.enqueueAtTail(commandEntry);
573 return commandEntry;
574}
575
Jeff Brownb88102f2010-09-08 11:49:43 -0700576void InputDispatcher::drainInboundQueueLocked() {
577 while (! mInboundQueue.isEmpty()) {
578 EventEntry* entry = mInboundQueue.dequeueAtHead();
Jeff Brown54a18252010-09-16 14:07:33 -0700579 releaseInboundEventLocked(entry);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700580 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700581}
582
Jeff Brown54a18252010-09-16 14:07:33 -0700583void InputDispatcher::releasePendingEventLocked() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700584 if (mPendingEvent) {
Jeff Brown54a18252010-09-16 14:07:33 -0700585 releaseInboundEventLocked(mPendingEvent);
Jeff Brownb88102f2010-09-08 11:49:43 -0700586 mPendingEvent = NULL;
587 }
588}
589
Jeff Brown54a18252010-09-16 14:07:33 -0700590void InputDispatcher::releaseInboundEventLocked(EventEntry* entry) {
Jeff Brown01ce2e92010-09-26 22:20:12 -0700591 InjectionState* injectionState = entry->injectionState;
592 if (injectionState && injectionState->injectionResult == INPUT_EVENT_INJECTION_PENDING) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700593#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +0000594 ALOGD("Injected inbound event was dropped.");
Jeff Brownb88102f2010-09-08 11:49:43 -0700595#endif
596 setInjectionResultLocked(entry, INPUT_EVENT_INJECTION_FAILED);
597 }
Jeff Brownabb4d442011-08-15 12:55:32 -0700598 if (entry == mNextUnblockedEvent) {
599 mNextUnblockedEvent = NULL;
600 }
Jeff Brownac386072011-07-20 15:19:50 -0700601 entry->release();
Jeff Brownb88102f2010-09-08 11:49:43 -0700602}
603
Jeff Brownb88102f2010-09-08 11:49:43 -0700604void InputDispatcher::resetKeyRepeatLocked() {
605 if (mKeyRepeatState.lastKeyEntry) {
Jeff Brownac386072011-07-20 15:19:50 -0700606 mKeyRepeatState.lastKeyEntry->release();
Jeff Brownb88102f2010-09-08 11:49:43 -0700607 mKeyRepeatState.lastKeyEntry = NULL;
608 }
609}
610
Jeff Brown214eaf42011-05-26 19:17:02 -0700611InputDispatcher::KeyEntry* InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) {
Jeff Brown349703e2010-06-22 01:27:15 -0700612 KeyEntry* entry = mKeyRepeatState.lastKeyEntry;
613
Jeff Brown349703e2010-06-22 01:27:15 -0700614 // Reuse the repeated key entry if it is otherwise unreferenced.
Jeff Browne20c9e02010-10-11 14:20:19 -0700615 uint32_t policyFlags = (entry->policyFlags & POLICY_FLAG_RAW_MASK)
616 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700617 if (entry->refCount == 1) {
Jeff Brownac386072011-07-20 15:19:50 -0700618 entry->recycle();
Jeff Brown7fbdc842010-06-17 20:52:56 -0700619 entry->eventTime = currentTime;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700620 entry->policyFlags = policyFlags;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700621 entry->repeatCount += 1;
622 } else {
Jeff Brownac386072011-07-20 15:19:50 -0700623 KeyEntry* newEntry = new KeyEntry(currentTime,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700624 entry->deviceId, entry->source, policyFlags,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700625 entry->action, entry->flags, entry->keyCode, entry->scanCode,
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700626 entry->metaState, entry->repeatCount + 1, entry->downTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700627
628 mKeyRepeatState.lastKeyEntry = newEntry;
Jeff Brownac386072011-07-20 15:19:50 -0700629 entry->release();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700630
631 entry = newEntry;
632 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700633 entry->syntheticRepeat = true;
634
635 // Increment reference count since we keep a reference to the event in
636 // mKeyRepeatState.lastKeyEntry in addition to the one we return.
637 entry->refCount += 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700638
Jeff Brown214eaf42011-05-26 19:17:02 -0700639 mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay;
Jeff Brownb88102f2010-09-08 11:49:43 -0700640 return entry;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700641}
642
Jeff Brownb88102f2010-09-08 11:49:43 -0700643bool InputDispatcher::dispatchConfigurationChangedLocked(
644 nsecs_t currentTime, ConfigurationChangedEntry* entry) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700645#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +0000646 ALOGD("dispatchConfigurationChanged - eventTime=%lld", entry->eventTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700647#endif
648
649 // Reset key repeating in case a keyboard device was added or removed or something.
650 resetKeyRepeatLocked();
651
652 // Enqueue a command to run outside the lock to tell the policy that the configuration changed.
653 CommandEntry* commandEntry = postCommandLocked(
654 & InputDispatcher::doNotifyConfigurationChangedInterruptible);
655 commandEntry->eventTime = entry->eventTime;
656 return true;
657}
658
Jeff Brown65fd2512011-08-18 11:20:58 -0700659bool InputDispatcher::dispatchDeviceResetLocked(
660 nsecs_t currentTime, DeviceResetEntry* entry) {
661#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +0000662 ALOGD("dispatchDeviceReset - eventTime=%lld, deviceId=%d", entry->eventTime, entry->deviceId);
Jeff Brown65fd2512011-08-18 11:20:58 -0700663#endif
664
665 CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS,
666 "device was reset");
667 options.deviceId = entry->deviceId;
668 synthesizeCancelationEventsForAllConnectionsLocked(options);
669 return true;
670}
671
Jeff Brown214eaf42011-05-26 19:17:02 -0700672bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry,
Jeff Browne20c9e02010-10-11 14:20:19 -0700673 DropReason* dropReason, nsecs_t* nextWakeupTime) {
Jeff Browne46a0a42010-11-02 17:58:22 -0700674 // Preprocessing.
675 if (! entry->dispatchInProgress) {
676 if (entry->repeatCount == 0
677 && entry->action == AKEY_EVENT_ACTION_DOWN
678 && (entry->policyFlags & POLICY_FLAG_TRUSTED)
Jeff Brown0029c662011-03-30 02:25:18 -0700679 && (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) {
Jeff Browne46a0a42010-11-02 17:58:22 -0700680 if (mKeyRepeatState.lastKeyEntry
681 && mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode) {
682 // We have seen two identical key downs in a row which indicates that the device
683 // driver is automatically generating key repeats itself. We take note of the
684 // repeat here, but we disable our own next key repeat timer since it is clear that
685 // we will not need to synthesize key repeats ourselves.
686 entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1;
687 resetKeyRepeatLocked();
688 mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves
689 } else {
690 // Not a repeat. Save key down state in case we do see a repeat later.
691 resetKeyRepeatLocked();
Jeff Brown214eaf42011-05-26 19:17:02 -0700692 mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout;
Jeff Browne46a0a42010-11-02 17:58:22 -0700693 }
694 mKeyRepeatState.lastKeyEntry = entry;
695 entry->refCount += 1;
696 } else if (! entry->syntheticRepeat) {
697 resetKeyRepeatLocked();
698 }
699
Jeff Browne2e01262011-03-02 20:34:30 -0800700 if (entry->repeatCount == 1) {
701 entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS;
702 } else {
703 entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS;
704 }
705
Jeff Browne46a0a42010-11-02 17:58:22 -0700706 entry->dispatchInProgress = true;
707 resetTargetsLocked();
708
709 logOutboundKeyDetailsLocked("dispatchKey - ", entry);
710 }
711
Jeff Brown905805a2011-10-12 13:57:59 -0700712 // Handle case where the policy asked us to try again later last time.
713 if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) {
714 if (currentTime < entry->interceptKeyWakeupTime) {
715 if (entry->interceptKeyWakeupTime < *nextWakeupTime) {
716 *nextWakeupTime = entry->interceptKeyWakeupTime;
717 }
718 return false; // wait until next wakeup
719 }
720 entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN;
721 entry->interceptKeyWakeupTime = 0;
722 }
723
Jeff Brown54a18252010-09-16 14:07:33 -0700724 // Give the policy a chance to intercept the key.
725 if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700726 if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) {
Jeff Brown54a18252010-09-16 14:07:33 -0700727 CommandEntry* commandEntry = postCommandLocked(
728 & InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible);
Jeff Brown9302c872011-07-13 22:51:29 -0700729 if (mFocusedWindowHandle != NULL) {
730 commandEntry->inputWindowHandle = mFocusedWindowHandle;
Jeff Brown54a18252010-09-16 14:07:33 -0700731 }
732 commandEntry->keyEntry = entry;
733 entry->refCount += 1;
734 return false; // wait for the command to run
735 } else {
736 entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE;
737 }
738 } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700739 if (*dropReason == DROP_REASON_NOT_DROPPED) {
740 *dropReason = DROP_REASON_POLICY;
741 }
Jeff Brown54a18252010-09-16 14:07:33 -0700742 }
743
744 // Clean up if dropping the event.
Jeff Browne20c9e02010-10-11 14:20:19 -0700745 if (*dropReason != DROP_REASON_NOT_DROPPED) {
Jeff Brown54a18252010-09-16 14:07:33 -0700746 resetTargetsLocked();
Jeff Brown3122e442010-10-11 23:32:49 -0700747 setInjectionResultLocked(entry, *dropReason == DROP_REASON_POLICY
748 ? INPUT_EVENT_INJECTION_SUCCEEDED : INPUT_EVENT_INJECTION_FAILED);
Jeff Brown54a18252010-09-16 14:07:33 -0700749 return true;
750 }
751
Jeff Brownb88102f2010-09-08 11:49:43 -0700752 // Identify targets.
753 if (! mCurrentInputTargetsValid) {
Jeff Brown01ce2e92010-09-26 22:20:12 -0700754 int32_t injectionResult = findFocusedWindowTargetsLocked(currentTime,
755 entry, nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700756 if (injectionResult == INPUT_EVENT_INJECTION_PENDING) {
757 return false;
758 }
759
760 setInjectionResultLocked(entry, injectionResult);
761 if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) {
762 return true;
763 }
764
765 addMonitoringTargetsLocked();
Jeff Brown01ce2e92010-09-26 22:20:12 -0700766 commitTargetsLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700767 }
768
769 // Dispatch the key.
Jeff Brown3241b6b2012-02-03 15:08:02 -0800770 dispatchEventToCurrentInputTargetsLocked(currentTime, entry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700771 return true;
772}
773
774void InputDispatcher::logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry) {
775#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +0000776 ALOGD("%seventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, "
Jeff Brownb88102f2010-09-08 11:49:43 -0700777 "action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, "
Jeff Browne46a0a42010-11-02 17:58:22 -0700778 "repeatCount=%d, downTime=%lld",
Jeff Brownb88102f2010-09-08 11:49:43 -0700779 prefix,
780 entry->eventTime, entry->deviceId, entry->source, entry->policyFlags,
781 entry->action, entry->flags, entry->keyCode, entry->scanCode, entry->metaState,
Jeff Browne46a0a42010-11-02 17:58:22 -0700782 entry->repeatCount, entry->downTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700783#endif
784}
785
786bool InputDispatcher::dispatchMotionLocked(
Jeff Browne20c9e02010-10-11 14:20:19 -0700787 nsecs_t currentTime, MotionEntry* entry, DropReason* dropReason, nsecs_t* nextWakeupTime) {
Jeff Browne46a0a42010-11-02 17:58:22 -0700788 // Preprocessing.
789 if (! entry->dispatchInProgress) {
790 entry->dispatchInProgress = true;
791 resetTargetsLocked();
792
793 logOutboundMotionDetailsLocked("dispatchMotion - ", entry);
794 }
795
Jeff Brown54a18252010-09-16 14:07:33 -0700796 // Clean up if dropping the event.
Jeff Browne20c9e02010-10-11 14:20:19 -0700797 if (*dropReason != DROP_REASON_NOT_DROPPED) {
Jeff Brown54a18252010-09-16 14:07:33 -0700798 resetTargetsLocked();
Jeff Brown3122e442010-10-11 23:32:49 -0700799 setInjectionResultLocked(entry, *dropReason == DROP_REASON_POLICY
800 ? INPUT_EVENT_INJECTION_SUCCEEDED : INPUT_EVENT_INJECTION_FAILED);
Jeff Brown54a18252010-09-16 14:07:33 -0700801 return true;
802 }
803
Jeff Brownb88102f2010-09-08 11:49:43 -0700804 bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER;
805
806 // Identify targets.
Jeff Browncc0c1592011-02-19 05:07:28 -0800807 bool conflictingPointerActions = false;
Jeff Brownb88102f2010-09-08 11:49:43 -0700808 if (! mCurrentInputTargetsValid) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700809 int32_t injectionResult;
810 if (isPointerEvent) {
811 // Pointer event. (eg. touchscreen)
Jeff Brown01ce2e92010-09-26 22:20:12 -0700812 injectionResult = findTouchedWindowTargetsLocked(currentTime,
Jeff Brown3241b6b2012-02-03 15:08:02 -0800813 entry, nextWakeupTime, &conflictingPointerActions);
Jeff Brownb88102f2010-09-08 11:49:43 -0700814 } else {
815 // Non touch event. (eg. trackball)
Jeff Brown01ce2e92010-09-26 22:20:12 -0700816 injectionResult = findFocusedWindowTargetsLocked(currentTime,
817 entry, nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700818 }
819 if (injectionResult == INPUT_EVENT_INJECTION_PENDING) {
820 return false;
821 }
822
823 setInjectionResultLocked(entry, injectionResult);
824 if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) {
825 return true;
826 }
827
828 addMonitoringTargetsLocked();
Jeff Brown01ce2e92010-09-26 22:20:12 -0700829 commitTargetsLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700830 }
831
832 // Dispatch the motion.
Jeff Browncc0c1592011-02-19 05:07:28 -0800833 if (conflictingPointerActions) {
Jeff Brownda3d5a92011-03-29 15:11:34 -0700834 CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS,
835 "conflicting pointer actions");
836 synthesizeCancelationEventsForAllConnectionsLocked(options);
Jeff Browncc0c1592011-02-19 05:07:28 -0800837 }
Jeff Brown3241b6b2012-02-03 15:08:02 -0800838 dispatchEventToCurrentInputTargetsLocked(currentTime, entry);
Jeff Brownb88102f2010-09-08 11:49:43 -0700839 return true;
840}
841
842
843void InputDispatcher::logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry) {
844#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +0000845 ALOGD("%seventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, "
Jeff Brown85a31762010-09-01 17:01:00 -0700846 "action=0x%x, flags=0x%x, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700847 "metaState=0x%x, buttonState=0x%x, "
848 "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%lld",
Jeff Brownb88102f2010-09-08 11:49:43 -0700849 prefix,
Jeff Brown85a31762010-09-01 17:01:00 -0700850 entry->eventTime, entry->deviceId, entry->source, entry->policyFlags,
851 entry->action, entry->flags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700852 entry->metaState, entry->buttonState,
853 entry->edgeFlags, entry->xPrecision, entry->yPrecision,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700854 entry->downTime);
855
Jeff Brown46b9ac02010-04-22 18:58:52 -0700856 for (uint32_t i = 0; i < entry->pointerCount; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +0000857 ALOGD(" Pointer %d: id=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700858 "x=%f, y=%f, pressure=%f, size=%f, "
Jeff Brown85a31762010-09-01 17:01:00 -0700859 "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, "
Jeff Brown8d608662010-08-30 03:02:23 -0700860 "orientation=%f",
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700861 i, entry->pointerProperties[i].id,
862 entry->pointerProperties[i].toolType,
Jeff Brown3241b6b2012-02-03 15:08:02 -0800863 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
864 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
865 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
866 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
867 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
868 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
869 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
870 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
871 entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
Jeff Brown46b9ac02010-04-22 18:58:52 -0700872 }
873#endif
Jeff Brown46b9ac02010-04-22 18:58:52 -0700874}
875
876void InputDispatcher::dispatchEventToCurrentInputTargetsLocked(nsecs_t currentTime,
Jeff Brown3241b6b2012-02-03 15:08:02 -0800877 EventEntry* eventEntry) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700878#if DEBUG_DISPATCH_CYCLE
Jeff Brown3241b6b2012-02-03 15:08:02 -0800879 ALOGD("dispatchEventToCurrentInputTargets");
Jeff Brown46b9ac02010-04-22 18:58:52 -0700880#endif
881
Steve Blockec193de2012-01-09 18:35:44 +0000882 ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true
Jeff Brown9c3cda02010-06-15 01:31:58 -0700883
Jeff Browne2fe69e2010-10-18 13:21:23 -0700884 pokeUserActivityLocked(eventEntry);
885
Jeff Brown46b9ac02010-04-22 18:58:52 -0700886 for (size_t i = 0; i < mCurrentInputTargets.size(); i++) {
887 const InputTarget& inputTarget = mCurrentInputTargets.itemAt(i);
888
Jeff Brown519e0242010-09-15 15:18:56 -0700889 ssize_t connectionIndex = getConnectionIndexLocked(inputTarget.inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700890 if (connectionIndex >= 0) {
Jeff Browncbee6d62012-02-03 20:11:27 -0800891 sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex);
Jeff Brown3241b6b2012-02-03 15:08:02 -0800892 prepareDispatchCycleLocked(currentTime, connection, eventEntry, &inputTarget);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700893 } else {
Jeff Brownb6997262010-10-08 22:31:17 -0700894#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +0000895 ALOGD("Dropping event delivery to target with channel '%s' because it "
Jeff Brownb6997262010-10-08 22:31:17 -0700896 "is no longer registered with the input dispatcher.",
Jeff Brown46b9ac02010-04-22 18:58:52 -0700897 inputTarget.inputChannel->getName().string());
Jeff Brownb6997262010-10-08 22:31:17 -0700898#endif
Jeff Brown46b9ac02010-04-22 18:58:52 -0700899 }
900 }
901}
902
Jeff Brown54a18252010-09-16 14:07:33 -0700903void InputDispatcher::resetTargetsLocked() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700904 mCurrentInputTargetsValid = false;
905 mCurrentInputTargets.clear();
Jeff Brown5ea29ab2011-07-27 11:50:51 -0700906 resetANRTimeoutsLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700907}
908
Jeff Brown01ce2e92010-09-26 22:20:12 -0700909void InputDispatcher::commitTargetsLocked() {
Jeff Brownb88102f2010-09-08 11:49:43 -0700910 mCurrentInputTargetsValid = true;
911}
912
913int32_t InputDispatcher::handleTargetsNotReadyLocked(nsecs_t currentTime,
Jeff Brown9302c872011-07-13 22:51:29 -0700914 const EventEntry* entry,
915 const sp<InputApplicationHandle>& applicationHandle,
916 const sp<InputWindowHandle>& windowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700917 nsecs_t* nextWakeupTime) {
Jeff Brown9302c872011-07-13 22:51:29 -0700918 if (applicationHandle == NULL && windowHandle == NULL) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700919 if (mInputTargetWaitCause != INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY) {
920#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +0000921 ALOGD("Waiting for system to become ready for input.");
Jeff Brownb88102f2010-09-08 11:49:43 -0700922#endif
923 mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY;
924 mInputTargetWaitStartTime = currentTime;
925 mInputTargetWaitTimeoutTime = LONG_LONG_MAX;
926 mInputTargetWaitTimeoutExpired = false;
Jeff Brown9302c872011-07-13 22:51:29 -0700927 mInputTargetWaitApplicationHandle.clear();
Jeff Brownb88102f2010-09-08 11:49:43 -0700928 }
929 } else {
930 if (mInputTargetWaitCause != INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) {
931#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +0000932 ALOGD("Waiting for application to become ready for input: %s",
Jeff Brown9302c872011-07-13 22:51:29 -0700933 getApplicationWindowLabelLocked(applicationHandle, windowHandle).string());
Jeff Brownb88102f2010-09-08 11:49:43 -0700934#endif
Jeff Browncc4f7db2011-08-30 20:34:48 -0700935 nsecs_t timeout;
936 if (windowHandle != NULL) {
937 timeout = windowHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT);
938 } else if (applicationHandle != NULL) {
939 timeout = applicationHandle->getDispatchingTimeout(
940 DEFAULT_INPUT_DISPATCHING_TIMEOUT);
941 } else {
942 timeout = DEFAULT_INPUT_DISPATCHING_TIMEOUT;
943 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700944
945 mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY;
946 mInputTargetWaitStartTime = currentTime;
947 mInputTargetWaitTimeoutTime = currentTime + timeout;
948 mInputTargetWaitTimeoutExpired = false;
Jeff Brown9302c872011-07-13 22:51:29 -0700949 mInputTargetWaitApplicationHandle.clear();
Jeff Brown928e0542011-01-10 11:17:36 -0800950
Jeff Brown9302c872011-07-13 22:51:29 -0700951 if (windowHandle != NULL) {
952 mInputTargetWaitApplicationHandle = windowHandle->inputApplicationHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800953 }
Jeff Brown9302c872011-07-13 22:51:29 -0700954 if (mInputTargetWaitApplicationHandle == NULL && applicationHandle != NULL) {
955 mInputTargetWaitApplicationHandle = applicationHandle;
Jeff Brown928e0542011-01-10 11:17:36 -0800956 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700957 }
958 }
959
960 if (mInputTargetWaitTimeoutExpired) {
961 return INPUT_EVENT_INJECTION_TIMED_OUT;
962 }
963
964 if (currentTime >= mInputTargetWaitTimeoutTime) {
Jeff Brown9302c872011-07-13 22:51:29 -0700965 onANRLocked(currentTime, applicationHandle, windowHandle,
966 entry->eventTime, mInputTargetWaitStartTime);
Jeff Brownb88102f2010-09-08 11:49:43 -0700967
968 // Force poll loop to wake up immediately on next iteration once we get the
969 // ANR response back from the policy.
970 *nextWakeupTime = LONG_LONG_MIN;
971 return INPUT_EVENT_INJECTION_PENDING;
972 } else {
973 // Force poll loop to wake up when timeout is due.
974 if (mInputTargetWaitTimeoutTime < *nextWakeupTime) {
975 *nextWakeupTime = mInputTargetWaitTimeoutTime;
976 }
977 return INPUT_EVENT_INJECTION_PENDING;
978 }
979}
980
Jeff Brown519e0242010-09-15 15:18:56 -0700981void InputDispatcher::resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
982 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700983 if (newTimeout > 0) {
984 // Extend the timeout.
985 mInputTargetWaitTimeoutTime = now() + newTimeout;
986 } else {
987 // Give up.
988 mInputTargetWaitTimeoutExpired = true;
Jeff Brown519e0242010-09-15 15:18:56 -0700989
Jeff Brown01ce2e92010-09-26 22:20:12 -0700990 // Release the touch targets.
991 mTouchState.reset();
Jeff Brown2a95c2a2010-09-16 12:31:46 -0700992
Jeff Brown519e0242010-09-15 15:18:56 -0700993 // Input state will not be realistic. Mark it out of sync.
Jeff Browndc3e0052010-09-16 11:02:16 -0700994 if (inputChannel.get()) {
995 ssize_t connectionIndex = getConnectionIndexLocked(inputChannel);
996 if (connectionIndex >= 0) {
Jeff Browncbee6d62012-02-03 20:11:27 -0800997 sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex);
Jeff Brown00045a72010-12-09 18:10:30 -0800998 if (connection->status == Connection::STATUS_NORMAL) {
Jeff Brownda3d5a92011-03-29 15:11:34 -0700999 CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS,
Jeff Brown00045a72010-12-09 18:10:30 -08001000 "application not responding");
Jeff Brownda3d5a92011-03-29 15:11:34 -07001001 synthesizeCancelationEventsForConnectionLocked(connection, options);
Jeff Brown00045a72010-12-09 18:10:30 -08001002 }
Jeff Browndc3e0052010-09-16 11:02:16 -07001003 }
Jeff Brown519e0242010-09-15 15:18:56 -07001004 }
Jeff Brownb88102f2010-09-08 11:49:43 -07001005 }
1006}
1007
Jeff Brown519e0242010-09-15 15:18:56 -07001008nsecs_t InputDispatcher::getTimeSpentWaitingForApplicationLocked(
Jeff Brownb88102f2010-09-08 11:49:43 -07001009 nsecs_t currentTime) {
1010 if (mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) {
1011 return currentTime - mInputTargetWaitStartTime;
1012 }
1013 return 0;
1014}
1015
1016void InputDispatcher::resetANRTimeoutsLocked() {
1017#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001018 ALOGD("Resetting ANR timeouts.");
Jeff Brownb88102f2010-09-08 11:49:43 -07001019#endif
1020
Jeff Brownb88102f2010-09-08 11:49:43 -07001021 // Reset input target wait timeout.
1022 mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_NONE;
Jeff Brown5ea29ab2011-07-27 11:50:51 -07001023 mInputTargetWaitApplicationHandle.clear();
Jeff Brownb88102f2010-09-08 11:49:43 -07001024}
1025
Jeff Brown01ce2e92010-09-26 22:20:12 -07001026int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime,
1027 const EventEntry* entry, nsecs_t* nextWakeupTime) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001028 mCurrentInputTargets.clear();
1029
1030 int32_t injectionResult;
1031
1032 // If there is no currently focused window and no focused application
1033 // then drop the event.
Jeff Brown9302c872011-07-13 22:51:29 -07001034 if (mFocusedWindowHandle == NULL) {
1035 if (mFocusedApplicationHandle != NULL) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001036#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001037 ALOGD("Waiting because there is no focused window but there is a "
Jeff Brown519e0242010-09-15 15:18:56 -07001038 "focused application that may eventually add a window: %s.",
Jeff Brown9302c872011-07-13 22:51:29 -07001039 getApplicationWindowLabelLocked(mFocusedApplicationHandle, NULL).string());
Jeff Brownb88102f2010-09-08 11:49:43 -07001040#endif
1041 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown9302c872011-07-13 22:51:29 -07001042 mFocusedApplicationHandle, NULL, nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07001043 goto Unresponsive;
1044 }
1045
Steve Block6215d3f2012-01-04 20:05:49 +00001046 ALOGI("Dropping event because there is no focused window or focused application.");
Jeff Brownb88102f2010-09-08 11:49:43 -07001047 injectionResult = INPUT_EVENT_INJECTION_FAILED;
1048 goto Failed;
1049 }
1050
1051 // Check permissions.
Jeff Brown9302c872011-07-13 22:51:29 -07001052 if (! checkInjectionPermission(mFocusedWindowHandle, entry->injectionState)) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001053 injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED;
1054 goto Failed;
1055 }
1056
1057 // If the currently focused window is paused then keep waiting.
Jeff Browncc4f7db2011-08-30 20:34:48 -07001058 if (mFocusedWindowHandle->getInfo()->paused) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001059#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001060 ALOGD("Waiting because focused window is paused.");
Jeff Brownb88102f2010-09-08 11:49:43 -07001061#endif
1062 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown9302c872011-07-13 22:51:29 -07001063 mFocusedApplicationHandle, mFocusedWindowHandle, nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07001064 goto Unresponsive;
1065 }
1066
Jeff Brown519e0242010-09-15 15:18:56 -07001067 // If the currently focused window is still working on previous events then keep waiting.
Jeff Brown9302c872011-07-13 22:51:29 -07001068 if (! isWindowFinishedWithPreviousInputLocked(mFocusedWindowHandle)) {
Jeff Brown519e0242010-09-15 15:18:56 -07001069#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001070 ALOGD("Waiting because focused window still processing previous input.");
Jeff Brown519e0242010-09-15 15:18:56 -07001071#endif
1072 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown9302c872011-07-13 22:51:29 -07001073 mFocusedApplicationHandle, mFocusedWindowHandle, nextWakeupTime);
Jeff Brown519e0242010-09-15 15:18:56 -07001074 goto Unresponsive;
1075 }
1076
Jeff Brownb88102f2010-09-08 11:49:43 -07001077 // Success! Output targets.
1078 injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED;
Jeff Brown9302c872011-07-13 22:51:29 -07001079 addWindowTargetLocked(mFocusedWindowHandle,
Jeff Browna032cc02011-03-07 16:56:21 -08001080 InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, BitSet32(0));
Jeff Brownb88102f2010-09-08 11:49:43 -07001081
1082 // Done.
1083Failed:
1084Unresponsive:
Jeff Brown519e0242010-09-15 15:18:56 -07001085 nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime);
1086 updateDispatchStatisticsLocked(currentTime, entry,
1087 injectionResult, timeSpentWaitingForApplication);
Jeff Brownb88102f2010-09-08 11:49:43 -07001088#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001089 ALOGD("findFocusedWindow finished: injectionResult=%d, "
Jeff Brown519e0242010-09-15 15:18:56 -07001090 "timeSpendWaitingForApplication=%0.1fms",
1091 injectionResult, timeSpentWaitingForApplication / 1000000.0);
Jeff Brownb88102f2010-09-08 11:49:43 -07001092#endif
1093 return injectionResult;
1094}
1095
Jeff Brown01ce2e92010-09-26 22:20:12 -07001096int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001097 const MotionEntry* entry, nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001098 enum InjectionPermission {
1099 INJECTION_PERMISSION_UNKNOWN,
1100 INJECTION_PERMISSION_GRANTED,
1101 INJECTION_PERMISSION_DENIED
1102 };
1103
Jeff Brownb88102f2010-09-08 11:49:43 -07001104 mCurrentInputTargets.clear();
1105
1106 nsecs_t startTime = now();
1107
1108 // For security reasons, we defer updating the touch state until we are sure that
1109 // event injection will be allowed.
1110 //
1111 // FIXME In the original code, screenWasOff could never be set to true.
1112 // The reason is that the POLICY_FLAG_WOKE_HERE
1113 // and POLICY_FLAG_BRIGHT_HERE flags were set only when preprocessing raw
1114 // EV_KEY, EV_REL and EV_ABS events. As it happens, the touch event was
1115 // actually enqueued using the policyFlags that appeared in the final EV_SYN
1116 // events upon which no preprocessing took place. So policyFlags was always 0.
1117 // In the new native input dispatcher we're a bit more careful about event
1118 // preprocessing so the touches we receive can actually have non-zero policyFlags.
1119 // Unfortunately we obtain undesirable behavior.
1120 //
1121 // Here's what happens:
1122 //
1123 // When the device dims in anticipation of going to sleep, touches
1124 // in windows which have FLAG_TOUCHABLE_WHEN_WAKING cause
1125 // the device to brighten and reset the user activity timer.
1126 // Touches on other windows (such as the launcher window)
1127 // are dropped. Then after a moment, the device goes to sleep. Oops.
1128 //
1129 // Also notice how screenWasOff was being initialized using POLICY_FLAG_BRIGHT_HERE
1130 // instead of POLICY_FLAG_WOKE_HERE...
1131 //
1132 bool screenWasOff = false; // original policy: policyFlags & POLICY_FLAG_BRIGHT_HERE;
1133
1134 int32_t action = entry->action;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001135 int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK;
Jeff Brownb88102f2010-09-08 11:49:43 -07001136
1137 // Update the touch state as needed based on the properties of the touch event.
Jeff Brown01ce2e92010-09-26 22:20:12 -07001138 int32_t injectionResult = INPUT_EVENT_INJECTION_PENDING;
1139 InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN;
Jeff Brown9302c872011-07-13 22:51:29 -07001140 sp<InputWindowHandle> newHoverWindowHandle;
Jeff Browncc0c1592011-02-19 05:07:28 -08001141
1142 bool isSplit = mTouchState.split;
Jeff Brown2717eff2011-06-30 23:53:07 -07001143 bool switchedDevice = mTouchState.deviceId >= 0
1144 && (mTouchState.deviceId != entry->deviceId
1145 || mTouchState.source != entry->source);
Jeff Browna032cc02011-03-07 16:56:21 -08001146 bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE
1147 || maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER
1148 || maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT);
1149 bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN
1150 || maskedAction == AMOTION_EVENT_ACTION_SCROLL
1151 || isHoverAction);
Jeff Brown81346812011-06-28 20:08:48 -07001152 bool wrongDevice = false;
Jeff Browna032cc02011-03-07 16:56:21 -08001153 if (newGesture) {
Jeff Browncc0c1592011-02-19 05:07:28 -08001154 bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN;
Jeff Brown81346812011-06-28 20:08:48 -07001155 if (switchedDevice && mTouchState.down && !down) {
1156#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001157 ALOGD("Dropping event because a pointer for a different device is already down.");
Jeff Brown81346812011-06-28 20:08:48 -07001158#endif
Jeff Browncc0c1592011-02-19 05:07:28 -08001159 mTempTouchState.copyFrom(mTouchState);
Jeff Brown81346812011-06-28 20:08:48 -07001160 injectionResult = INPUT_EVENT_INJECTION_FAILED;
1161 switchedDevice = false;
1162 wrongDevice = true;
1163 goto Failed;
Jeff Browncc0c1592011-02-19 05:07:28 -08001164 }
Jeff Brown81346812011-06-28 20:08:48 -07001165 mTempTouchState.reset();
1166 mTempTouchState.down = down;
1167 mTempTouchState.deviceId = entry->deviceId;
1168 mTempTouchState.source = entry->source;
1169 isSplit = false;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001170 } else {
1171 mTempTouchState.copyFrom(mTouchState);
Jeff Browncc0c1592011-02-19 05:07:28 -08001172 }
Jeff Brownb88102f2010-09-08 11:49:43 -07001173
Jeff Browna032cc02011-03-07 16:56:21 -08001174 if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08001175 /* Case 1: New splittable pointer going down, or need target for hover or scroll. */
Jeff Brownb88102f2010-09-08 11:49:43 -07001176
Jeff Brown01ce2e92010-09-26 22:20:12 -07001177 int32_t pointerIndex = getMotionEventActionPointerIndex(action);
Jeff Brown3241b6b2012-02-03 15:08:02 -08001178 int32_t x = int32_t(entry->pointerCoords[pointerIndex].
Jeff Brownebbd5d12011-02-17 13:01:34 -08001179 getAxisValue(AMOTION_EVENT_AXIS_X));
Jeff Brown3241b6b2012-02-03 15:08:02 -08001180 int32_t y = int32_t(entry->pointerCoords[pointerIndex].
Jeff Brownebbd5d12011-02-17 13:01:34 -08001181 getAxisValue(AMOTION_EVENT_AXIS_Y));
Jeff Brown9302c872011-07-13 22:51:29 -07001182 sp<InputWindowHandle> newTouchedWindowHandle;
1183 sp<InputWindowHandle> topErrorWindowHandle;
Jeff Browna032cc02011-03-07 16:56:21 -08001184 bool isTouchModal = false;
Jeff Brownb88102f2010-09-08 11:49:43 -07001185
1186 // Traverse windows from front to back to find touched window and outside targets.
Jeff Brown9302c872011-07-13 22:51:29 -07001187 size_t numWindows = mWindowHandles.size();
Jeff Brownb88102f2010-09-08 11:49:43 -07001188 for (size_t i = 0; i < numWindows; i++) {
Jeff Brown9302c872011-07-13 22:51:29 -07001189 sp<InputWindowHandle> windowHandle = mWindowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001190 const InputWindowInfo* windowInfo = windowHandle->getInfo();
1191 int32_t flags = windowInfo->layoutParamsFlags;
Jeff Brownb88102f2010-09-08 11:49:43 -07001192
Jeff Browncc4f7db2011-08-30 20:34:48 -07001193 if (flags & InputWindowInfo::FLAG_SYSTEM_ERROR) {
Jeff Brown9302c872011-07-13 22:51:29 -07001194 if (topErrorWindowHandle == NULL) {
1195 topErrorWindowHandle = windowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -07001196 }
1197 }
1198
Jeff Browncc4f7db2011-08-30 20:34:48 -07001199 if (windowInfo->visible) {
1200 if (! (flags & InputWindowInfo::FLAG_NOT_TOUCHABLE)) {
1201 isTouchModal = (flags & (InputWindowInfo::FLAG_NOT_FOCUSABLE
1202 | InputWindowInfo::FLAG_NOT_TOUCH_MODAL)) == 0;
1203 if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) {
Jeff Brown9302c872011-07-13 22:51:29 -07001204 if (! screenWasOff
Jeff Browncc4f7db2011-08-30 20:34:48 -07001205 || (flags & InputWindowInfo::FLAG_TOUCHABLE_WHEN_WAKING)) {
Jeff Brown9302c872011-07-13 22:51:29 -07001206 newTouchedWindowHandle = windowHandle;
Jeff Brownb88102f2010-09-08 11:49:43 -07001207 }
1208 break; // found touched window, exit window loop
1209 }
1210 }
1211
Jeff Brown01ce2e92010-09-26 22:20:12 -07001212 if (maskedAction == AMOTION_EVENT_ACTION_DOWN
Jeff Browncc4f7db2011-08-30 20:34:48 -07001213 && (flags & InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH)) {
Jeff Browna032cc02011-03-07 16:56:21 -08001214 int32_t outsideTargetFlags = InputTarget::FLAG_DISPATCH_AS_OUTSIDE;
Jeff Brown9302c872011-07-13 22:51:29 -07001215 if (isWindowObscuredAtPointLocked(windowHandle, x, y)) {
Jeff Brown19dfc832010-10-05 12:26:23 -07001216 outsideTargetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED;
1217 }
1218
Jeff Brown9302c872011-07-13 22:51:29 -07001219 mTempTouchState.addOrUpdateWindow(
1220 windowHandle, outsideTargetFlags, BitSet32(0));
Jeff Brownb88102f2010-09-08 11:49:43 -07001221 }
1222 }
1223 }
1224
1225 // If there is an error window but it is not taking focus (typically because
1226 // it is invisible) then wait for it. Any other focused window may in
1227 // fact be in ANR state.
Jeff Brown9302c872011-07-13 22:51:29 -07001228 if (topErrorWindowHandle != NULL && newTouchedWindowHandle != topErrorWindowHandle) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001229#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001230 ALOGD("Waiting because system error window is pending.");
Jeff Brownb88102f2010-09-08 11:49:43 -07001231#endif
1232 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
1233 NULL, NULL, nextWakeupTime);
1234 injectionPermission = INJECTION_PERMISSION_UNKNOWN;
1235 goto Unresponsive;
1236 }
1237
Jeff Brown01ce2e92010-09-26 22:20:12 -07001238 // Figure out whether splitting will be allowed for this window.
Jeff Browncc4f7db2011-08-30 20:34:48 -07001239 if (newTouchedWindowHandle != NULL
1240 && newTouchedWindowHandle->getInfo()->supportsSplitTouch()) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001241 // New window supports splitting.
1242 isSplit = true;
1243 } else if (isSplit) {
1244 // New window does not support splitting but we have already split events.
1245 // Assign the pointer to the first foreground window we find.
1246 // (May be NULL which is why we put this code block before the next check.)
Jeff Brown9302c872011-07-13 22:51:29 -07001247 newTouchedWindowHandle = mTempTouchState.getFirstForegroundWindowHandle();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001248 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001249
Jeff Brownb88102f2010-09-08 11:49:43 -07001250 // If we did not find a touched window then fail.
Jeff Brown9302c872011-07-13 22:51:29 -07001251 if (newTouchedWindowHandle == NULL) {
1252 if (mFocusedApplicationHandle != NULL) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001253#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001254 ALOGD("Waiting because there is no touched window but there is a "
Jeff Brown519e0242010-09-15 15:18:56 -07001255 "focused application that may eventually add a new window: %s.",
Jeff Brown9302c872011-07-13 22:51:29 -07001256 getApplicationWindowLabelLocked(mFocusedApplicationHandle, NULL).string());
Jeff Brownb88102f2010-09-08 11:49:43 -07001257#endif
1258 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown9302c872011-07-13 22:51:29 -07001259 mFocusedApplicationHandle, NULL, nextWakeupTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07001260 goto Unresponsive;
1261 }
1262
Steve Block6215d3f2012-01-04 20:05:49 +00001263 ALOGI("Dropping event because there is no touched window or focused application.");
Jeff Brownb88102f2010-09-08 11:49:43 -07001264 injectionResult = INPUT_EVENT_INJECTION_FAILED;
Jeff Brownb88102f2010-09-08 11:49:43 -07001265 goto Failed;
1266 }
1267
Jeff Brown19dfc832010-10-05 12:26:23 -07001268 // Set target flags.
Jeff Browna032cc02011-03-07 16:56:21 -08001269 int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS;
Jeff Brown19dfc832010-10-05 12:26:23 -07001270 if (isSplit) {
1271 targetFlags |= InputTarget::FLAG_SPLIT;
1272 }
Jeff Brown9302c872011-07-13 22:51:29 -07001273 if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) {
Jeff Brown19dfc832010-10-05 12:26:23 -07001274 targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED;
1275 }
1276
Jeff Browna032cc02011-03-07 16:56:21 -08001277 // Update hover state.
1278 if (isHoverAction) {
Jeff Brown9302c872011-07-13 22:51:29 -07001279 newHoverWindowHandle = newTouchedWindowHandle;
Jeff Browna032cc02011-03-07 16:56:21 -08001280 } else if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) {
Jeff Brown9302c872011-07-13 22:51:29 -07001281 newHoverWindowHandle = mLastHoverWindowHandle;
Jeff Browna032cc02011-03-07 16:56:21 -08001282 }
1283
Jeff Brown01ce2e92010-09-26 22:20:12 -07001284 // Update the temporary touch state.
1285 BitSet32 pointerIds;
1286 if (isSplit) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001287 uint32_t pointerId = entry->pointerProperties[pointerIndex].id;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001288 pointerIds.markBit(pointerId);
Jeff Brownb88102f2010-09-08 11:49:43 -07001289 }
Jeff Brown9302c872011-07-13 22:51:29 -07001290 mTempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds);
Jeff Brownb88102f2010-09-08 11:49:43 -07001291 } else {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001292 /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */
Jeff Brownb88102f2010-09-08 11:49:43 -07001293
1294 // If the pointer is not currently down, then ignore the event.
Jeff Brown01ce2e92010-09-26 22:20:12 -07001295 if (! mTempTouchState.down) {
Jeff Browna2cc28d2011-03-25 11:58:46 -07001296#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001297 ALOGD("Dropping event because the pointer is not down or we previously "
Jeff Brown76860e32010-10-25 17:37:46 -07001298 "dropped the pointer down event.");
1299#endif
Jeff Brownb88102f2010-09-08 11:49:43 -07001300 injectionResult = INPUT_EVENT_INJECTION_FAILED;
Jeff Brownb88102f2010-09-08 11:49:43 -07001301 goto Failed;
1302 }
Jeff Brown98db5fa2011-06-08 15:37:10 -07001303
1304 // Check whether touches should slip outside of the current foreground window.
1305 if (maskedAction == AMOTION_EVENT_ACTION_MOVE
1306 && entry->pointerCount == 1
1307 && mTempTouchState.isSlippery()) {
Jeff Brown3241b6b2012-02-03 15:08:02 -08001308 int32_t x = int32_t(entry->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X));
1309 int32_t y = int32_t(entry->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y));
Jeff Brown98db5fa2011-06-08 15:37:10 -07001310
Jeff Brown9302c872011-07-13 22:51:29 -07001311 sp<InputWindowHandle> oldTouchedWindowHandle =
1312 mTempTouchState.getFirstForegroundWindowHandle();
1313 sp<InputWindowHandle> newTouchedWindowHandle = findTouchedWindowAtLocked(x, y);
1314 if (oldTouchedWindowHandle != newTouchedWindowHandle
1315 && newTouchedWindowHandle != NULL) {
Jeff Brown98db5fa2011-06-08 15:37:10 -07001316#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001317 ALOGD("Touch is slipping out of window %s into window %s.",
Jeff Browncc4f7db2011-08-30 20:34:48 -07001318 oldTouchedWindowHandle->getName().string(),
1319 newTouchedWindowHandle->getName().string());
Jeff Brown98db5fa2011-06-08 15:37:10 -07001320#endif
1321 // Make a slippery exit from the old window.
Jeff Brown9302c872011-07-13 22:51:29 -07001322 mTempTouchState.addOrUpdateWindow(oldTouchedWindowHandle,
Jeff Brown98db5fa2011-06-08 15:37:10 -07001323 InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, BitSet32(0));
1324
1325 // Make a slippery entrance into the new window.
Jeff Browncc4f7db2011-08-30 20:34:48 -07001326 if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) {
Jeff Brown98db5fa2011-06-08 15:37:10 -07001327 isSplit = true;
1328 }
1329
1330 int32_t targetFlags = InputTarget::FLAG_FOREGROUND
1331 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER;
1332 if (isSplit) {
1333 targetFlags |= InputTarget::FLAG_SPLIT;
1334 }
Jeff Brown9302c872011-07-13 22:51:29 -07001335 if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) {
Jeff Brown98db5fa2011-06-08 15:37:10 -07001336 targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED;
1337 }
1338
1339 BitSet32 pointerIds;
1340 if (isSplit) {
1341 pointerIds.markBit(entry->pointerProperties[0].id);
1342 }
Jeff Brown9302c872011-07-13 22:51:29 -07001343 mTempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds);
Jeff Brown98db5fa2011-06-08 15:37:10 -07001344 }
1345 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001346 }
Jeff Brownb88102f2010-09-08 11:49:43 -07001347
Jeff Brown9302c872011-07-13 22:51:29 -07001348 if (newHoverWindowHandle != mLastHoverWindowHandle) {
Jeff Browna032cc02011-03-07 16:56:21 -08001349 // Let the previous window know that the hover sequence is over.
Jeff Brown9302c872011-07-13 22:51:29 -07001350 if (mLastHoverWindowHandle != NULL) {
Jeff Browna032cc02011-03-07 16:56:21 -08001351#if DEBUG_HOVER
Steve Block5baa3a62011-12-20 16:23:08 +00001352 ALOGD("Sending hover exit event to window %s.",
Jeff Browncc4f7db2011-08-30 20:34:48 -07001353 mLastHoverWindowHandle->getName().string());
Jeff Browna032cc02011-03-07 16:56:21 -08001354#endif
Jeff Brown9302c872011-07-13 22:51:29 -07001355 mTempTouchState.addOrUpdateWindow(mLastHoverWindowHandle,
Jeff Browna032cc02011-03-07 16:56:21 -08001356 InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0));
1357 }
1358
1359 // Let the new window know that the hover sequence is starting.
Jeff Brown9302c872011-07-13 22:51:29 -07001360 if (newHoverWindowHandle != NULL) {
Jeff Browna032cc02011-03-07 16:56:21 -08001361#if DEBUG_HOVER
Steve Block5baa3a62011-12-20 16:23:08 +00001362 ALOGD("Sending hover enter event to window %s.",
Jeff Browncc4f7db2011-08-30 20:34:48 -07001363 newHoverWindowHandle->getName().string());
Jeff Browna032cc02011-03-07 16:56:21 -08001364#endif
Jeff Brown9302c872011-07-13 22:51:29 -07001365 mTempTouchState.addOrUpdateWindow(newHoverWindowHandle,
Jeff Browna032cc02011-03-07 16:56:21 -08001366 InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, BitSet32(0));
1367 }
1368 }
1369
Jeff Brown01ce2e92010-09-26 22:20:12 -07001370 // Check permission to inject into all touched foreground windows and ensure there
1371 // is at least one touched foreground window.
1372 {
1373 bool haveForegroundWindow = false;
1374 for (size_t i = 0; i < mTempTouchState.windows.size(); i++) {
1375 const TouchedWindow& touchedWindow = mTempTouchState.windows[i];
1376 if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) {
1377 haveForegroundWindow = true;
Jeff Brown9302c872011-07-13 22:51:29 -07001378 if (! checkInjectionPermission(touchedWindow.windowHandle,
1379 entry->injectionState)) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001380 injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED;
1381 injectionPermission = INJECTION_PERMISSION_DENIED;
1382 goto Failed;
1383 }
1384 }
1385 }
1386 if (! haveForegroundWindow) {
Jeff Browna2cc28d2011-03-25 11:58:46 -07001387#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001388 ALOGD("Dropping event because there is no touched foreground window to receive it.");
Jeff Brownb88102f2010-09-08 11:49:43 -07001389#endif
1390 injectionResult = INPUT_EVENT_INJECTION_FAILED;
Jeff Brownb88102f2010-09-08 11:49:43 -07001391 goto Failed;
1392 }
1393
Jeff Brown01ce2e92010-09-26 22:20:12 -07001394 // Permission granted to injection into all touched foreground windows.
1395 injectionPermission = INJECTION_PERMISSION_GRANTED;
1396 }
Jeff Brown519e0242010-09-15 15:18:56 -07001397
Kenny Root7a9db182011-06-02 15:16:05 -07001398 // Check whether windows listening for outside touches are owned by the same UID. If it is
1399 // set the policy flag that we will not reveal coordinate information to this window.
1400 if (maskedAction == AMOTION_EVENT_ACTION_DOWN) {
Jeff Brown9302c872011-07-13 22:51:29 -07001401 sp<InputWindowHandle> foregroundWindowHandle =
1402 mTempTouchState.getFirstForegroundWindowHandle();
Jeff Browncc4f7db2011-08-30 20:34:48 -07001403 const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid;
Kenny Root7a9db182011-06-02 15:16:05 -07001404 for (size_t i = 0; i < mTempTouchState.windows.size(); i++) {
1405 const TouchedWindow& touchedWindow = mTempTouchState.windows[i];
1406 if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) {
Jeff Brown9302c872011-07-13 22:51:29 -07001407 sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle;
Jeff Browncc4f7db2011-08-30 20:34:48 -07001408 if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) {
Jeff Brown9302c872011-07-13 22:51:29 -07001409 mTempTouchState.addOrUpdateWindow(inputWindowHandle,
Kenny Root7a9db182011-06-02 15:16:05 -07001410 InputTarget::FLAG_ZERO_COORDS, BitSet32(0));
1411 }
1412 }
1413 }
1414 }
1415
Jeff Brown01ce2e92010-09-26 22:20:12 -07001416 // Ensure all touched foreground windows are ready for new input.
1417 for (size_t i = 0; i < mTempTouchState.windows.size(); i++) {
1418 const TouchedWindow& touchedWindow = mTempTouchState.windows[i];
1419 if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) {
1420 // If the touched window is paused then keep waiting.
Jeff Browncc4f7db2011-08-30 20:34:48 -07001421 if (touchedWindow.windowHandle->getInfo()->paused) {
Jeff Browna2cc28d2011-03-25 11:58:46 -07001422#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001423 ALOGD("Waiting because touched window is paused.");
Jeff Brown519e0242010-09-15 15:18:56 -07001424#endif
Jeff Brown01ce2e92010-09-26 22:20:12 -07001425 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown9302c872011-07-13 22:51:29 -07001426 NULL, touchedWindow.windowHandle, nextWakeupTime);
Jeff Brown01ce2e92010-09-26 22:20:12 -07001427 goto Unresponsive;
1428 }
1429
1430 // If the touched window is still working on previous events then keep waiting.
Jeff Brown9302c872011-07-13 22:51:29 -07001431 if (! isWindowFinishedWithPreviousInputLocked(touchedWindow.windowHandle)) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001432#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001433 ALOGD("Waiting because touched window still processing previous input.");
Jeff Brown01ce2e92010-09-26 22:20:12 -07001434#endif
1435 injectionResult = handleTargetsNotReadyLocked(currentTime, entry,
Jeff Brown9302c872011-07-13 22:51:29 -07001436 NULL, touchedWindow.windowHandle, nextWakeupTime);
Jeff Brown01ce2e92010-09-26 22:20:12 -07001437 goto Unresponsive;
1438 }
1439 }
1440 }
1441
1442 // If this is the first pointer going down and the touched window has a wallpaper
1443 // then also add the touched wallpaper windows so they are locked in for the duration
1444 // of the touch gesture.
Jeff Brown33bbfd22011-02-24 20:55:35 -08001445 // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper
1446 // engine only supports touch events. We would need to add a mechanism similar
1447 // to View.onGenericMotionEvent to enable wallpapers to handle these events.
1448 if (maskedAction == AMOTION_EVENT_ACTION_DOWN) {
Jeff Brown9302c872011-07-13 22:51:29 -07001449 sp<InputWindowHandle> foregroundWindowHandle =
1450 mTempTouchState.getFirstForegroundWindowHandle();
Jeff Browncc4f7db2011-08-30 20:34:48 -07001451 if (foregroundWindowHandle->getInfo()->hasWallpaper) {
Jeff Brown9302c872011-07-13 22:51:29 -07001452 for (size_t i = 0; i < mWindowHandles.size(); i++) {
1453 sp<InputWindowHandle> windowHandle = mWindowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001454 if (windowHandle->getInfo()->layoutParamsType
1455 == InputWindowInfo::TYPE_WALLPAPER) {
Jeff Brown9302c872011-07-13 22:51:29 -07001456 mTempTouchState.addOrUpdateWindow(windowHandle,
Jeff Browna032cc02011-03-07 16:56:21 -08001457 InputTarget::FLAG_WINDOW_IS_OBSCURED
1458 | InputTarget::FLAG_DISPATCH_AS_IS,
1459 BitSet32(0));
Jeff Brown01ce2e92010-09-26 22:20:12 -07001460 }
1461 }
1462 }
1463 }
1464
Jeff Brownb88102f2010-09-08 11:49:43 -07001465 // Success! Output targets.
1466 injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED;
Jeff Brownb88102f2010-09-08 11:49:43 -07001467
Jeff Brown01ce2e92010-09-26 22:20:12 -07001468 for (size_t i = 0; i < mTempTouchState.windows.size(); i++) {
1469 const TouchedWindow& touchedWindow = mTempTouchState.windows.itemAt(i);
Jeff Brown9302c872011-07-13 22:51:29 -07001470 addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags,
Jeff Brown01ce2e92010-09-26 22:20:12 -07001471 touchedWindow.pointerIds);
Jeff Brownb88102f2010-09-08 11:49:43 -07001472 }
1473
Jeff Browna032cc02011-03-07 16:56:21 -08001474 // Drop the outside or hover touch windows since we will not care about them
1475 // in the next iteration.
1476 mTempTouchState.filterNonAsIsTouchWindows();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001477
Jeff Brownb88102f2010-09-08 11:49:43 -07001478Failed:
1479 // Check injection permission once and for all.
1480 if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001481 if (checkInjectionPermission(NULL, entry->injectionState)) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001482 injectionPermission = INJECTION_PERMISSION_GRANTED;
1483 } else {
1484 injectionPermission = INJECTION_PERMISSION_DENIED;
1485 }
1486 }
1487
1488 // Update final pieces of touch state if the injector had permission.
1489 if (injectionPermission == INJECTION_PERMISSION_GRANTED) {
Jeff Brown95712852011-01-04 19:41:59 -08001490 if (!wrongDevice) {
Jeff Brown81346812011-06-28 20:08:48 -07001491 if (switchedDevice) {
1492#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001493 ALOGD("Conflicting pointer actions: Switched to a different device.");
Jeff Brown81346812011-06-28 20:08:48 -07001494#endif
1495 *outConflictingPointerActions = true;
1496 }
1497
1498 if (isHoverAction) {
1499 // Started hovering, therefore no longer down.
1500 if (mTouchState.down) {
1501#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001502 ALOGD("Conflicting pointer actions: Hover received while pointer was down.");
Jeff Brown81346812011-06-28 20:08:48 -07001503#endif
1504 *outConflictingPointerActions = true;
1505 }
1506 mTouchState.reset();
1507 if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER
1508 || maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) {
1509 mTouchState.deviceId = entry->deviceId;
1510 mTouchState.source = entry->source;
1511 }
1512 } else if (maskedAction == AMOTION_EVENT_ACTION_UP
1513 || maskedAction == AMOTION_EVENT_ACTION_CANCEL) {
Jeff Brown95712852011-01-04 19:41:59 -08001514 // All pointers up or canceled.
Jeff Brown33bbfd22011-02-24 20:55:35 -08001515 mTouchState.reset();
Jeff Brown95712852011-01-04 19:41:59 -08001516 } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) {
1517 // First pointer went down.
1518 if (mTouchState.down) {
Jeff Brownb6997262010-10-08 22:31:17 -07001519#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001520 ALOGD("Conflicting pointer actions: Down received while already down.");
Jeff Brownb6997262010-10-08 22:31:17 -07001521#endif
Jeff Brown81346812011-06-28 20:08:48 -07001522 *outConflictingPointerActions = true;
Jeff Brown95712852011-01-04 19:41:59 -08001523 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001524 mTouchState.copyFrom(mTempTouchState);
Jeff Brown95712852011-01-04 19:41:59 -08001525 } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) {
1526 // One pointer went up.
1527 if (isSplit) {
1528 int32_t pointerIndex = getMotionEventActionPointerIndex(action);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001529 uint32_t pointerId = entry->pointerProperties[pointerIndex].id;
Jeff Brownb88102f2010-09-08 11:49:43 -07001530
Jeff Brown95712852011-01-04 19:41:59 -08001531 for (size_t i = 0; i < mTempTouchState.windows.size(); ) {
1532 TouchedWindow& touchedWindow = mTempTouchState.windows.editItemAt(i);
1533 if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) {
1534 touchedWindow.pointerIds.clearBit(pointerId);
1535 if (touchedWindow.pointerIds.isEmpty()) {
1536 mTempTouchState.windows.removeAt(i);
1537 continue;
1538 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001539 }
Jeff Brown95712852011-01-04 19:41:59 -08001540 i += 1;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001541 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001542 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001543 mTouchState.copyFrom(mTempTouchState);
1544 } else if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) {
1545 // Discard temporary touch state since it was only valid for this action.
1546 } else {
1547 // Save changes to touch state as-is for all other actions.
1548 mTouchState.copyFrom(mTempTouchState);
Jeff Brownb88102f2010-09-08 11:49:43 -07001549 }
Jeff Browna032cc02011-03-07 16:56:21 -08001550
1551 // Update hover state.
Jeff Brown9302c872011-07-13 22:51:29 -07001552 mLastHoverWindowHandle = newHoverWindowHandle;
Jeff Brown95712852011-01-04 19:41:59 -08001553 }
Jeff Brownb88102f2010-09-08 11:49:43 -07001554 } else {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001555#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001556 ALOGD("Not updating touch focus because injection was denied.");
Jeff Brown01ce2e92010-09-26 22:20:12 -07001557#endif
Jeff Brownb88102f2010-09-08 11:49:43 -07001558 }
1559
1560Unresponsive:
Jeff Brown120a4592010-10-27 18:43:51 -07001561 // Reset temporary touch state to ensure we release unnecessary references to input channels.
1562 mTempTouchState.reset();
1563
Jeff Brown519e0242010-09-15 15:18:56 -07001564 nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime);
1565 updateDispatchStatisticsLocked(currentTime, entry,
1566 injectionResult, timeSpentWaitingForApplication);
Jeff Brownb88102f2010-09-08 11:49:43 -07001567#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001568 ALOGD("findTouchedWindow finished: injectionResult=%d, injectionPermission=%d, "
Jeff Brown01ce2e92010-09-26 22:20:12 -07001569 "timeSpentWaitingForApplication=%0.1fms",
Jeff Brown519e0242010-09-15 15:18:56 -07001570 injectionResult, injectionPermission, timeSpentWaitingForApplication / 1000000.0);
Jeff Brownb88102f2010-09-08 11:49:43 -07001571#endif
1572 return injectionResult;
1573}
1574
Jeff Brown9302c872011-07-13 22:51:29 -07001575void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle,
1576 int32_t targetFlags, BitSet32 pointerIds) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001577 mCurrentInputTargets.push();
1578
Jeff Browncc4f7db2011-08-30 20:34:48 -07001579 const InputWindowInfo* windowInfo = windowHandle->getInfo();
Jeff Brownb88102f2010-09-08 11:49:43 -07001580 InputTarget& target = mCurrentInputTargets.editTop();
Jeff Browncc4f7db2011-08-30 20:34:48 -07001581 target.inputChannel = windowInfo->inputChannel;
Jeff Brownb88102f2010-09-08 11:49:43 -07001582 target.flags = targetFlags;
Jeff Browncc4f7db2011-08-30 20:34:48 -07001583 target.xOffset = - windowInfo->frameLeft;
1584 target.yOffset = - windowInfo->frameTop;
1585 target.scaleFactor = windowInfo->scaleFactor;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001586 target.pointerIds = pointerIds;
Jeff Brownb88102f2010-09-08 11:49:43 -07001587}
1588
1589void InputDispatcher::addMonitoringTargetsLocked() {
1590 for (size_t i = 0; i < mMonitoringChannels.size(); i++) {
1591 mCurrentInputTargets.push();
1592
1593 InputTarget& target = mCurrentInputTargets.editTop();
1594 target.inputChannel = mMonitoringChannels[i];
Jeff Brownb6110c22011-04-01 16:15:13 -07001595 target.flags = InputTarget::FLAG_DISPATCH_AS_IS;
Jeff Brownb88102f2010-09-08 11:49:43 -07001596 target.xOffset = 0;
1597 target.yOffset = 0;
Jeff Brownb6110c22011-04-01 16:15:13 -07001598 target.pointerIds.clear();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001599 target.scaleFactor = 1.0f;
Jeff Brownb88102f2010-09-08 11:49:43 -07001600 }
1601}
1602
Jeff Brown9302c872011-07-13 22:51:29 -07001603bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
Jeff Brown01ce2e92010-09-26 22:20:12 -07001604 const InjectionState* injectionState) {
1605 if (injectionState
Jeff Browncc4f7db2011-08-30 20:34:48 -07001606 && (windowHandle == NULL
1607 || windowHandle->getInfo()->ownerUid != injectionState->injectorUid)
Jeff Brownb6997262010-10-08 22:31:17 -07001608 && !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) {
Jeff Brown9302c872011-07-13 22:51:29 -07001609 if (windowHandle != NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00001610 ALOGW("Permission denied: injecting event from pid %d uid %d to window %s "
Jeff Brown9302c872011-07-13 22:51:29 -07001611 "owned by uid %d",
Jeff Brownb6997262010-10-08 22:31:17 -07001612 injectionState->injectorPid, injectionState->injectorUid,
Jeff Browncc4f7db2011-08-30 20:34:48 -07001613 windowHandle->getName().string(),
1614 windowHandle->getInfo()->ownerUid);
Jeff Brownb6997262010-10-08 22:31:17 -07001615 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00001616 ALOGW("Permission denied: injecting event from pid %d uid %d",
Jeff Brownb6997262010-10-08 22:31:17 -07001617 injectionState->injectorPid, injectionState->injectorUid);
Jeff Brownb88102f2010-09-08 11:49:43 -07001618 }
Jeff Brownb6997262010-10-08 22:31:17 -07001619 return false;
Jeff Brownb88102f2010-09-08 11:49:43 -07001620 }
1621 return true;
1622}
1623
Jeff Brown19dfc832010-10-05 12:26:23 -07001624bool InputDispatcher::isWindowObscuredAtPointLocked(
Jeff Brown9302c872011-07-13 22:51:29 -07001625 const sp<InputWindowHandle>& windowHandle, int32_t x, int32_t y) const {
1626 size_t numWindows = mWindowHandles.size();
Jeff Brownb88102f2010-09-08 11:49:43 -07001627 for (size_t i = 0; i < numWindows; i++) {
Jeff Brown9302c872011-07-13 22:51:29 -07001628 sp<InputWindowHandle> otherHandle = mWindowHandles.itemAt(i);
1629 if (otherHandle == windowHandle) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001630 break;
1631 }
Jeff Browncc4f7db2011-08-30 20:34:48 -07001632
1633 const InputWindowInfo* otherInfo = otherHandle->getInfo();
1634 if (otherInfo->visible && ! otherInfo->isTrustedOverlay()
1635 && otherInfo->frameContainsPoint(x, y)) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001636 return true;
1637 }
1638 }
1639 return false;
1640}
1641
Jeff Brown9302c872011-07-13 22:51:29 -07001642bool InputDispatcher::isWindowFinishedWithPreviousInputLocked(
1643 const sp<InputWindowHandle>& windowHandle) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001644 ssize_t connectionIndex = getConnectionIndexLocked(windowHandle->getInputChannel());
Jeff Brown519e0242010-09-15 15:18:56 -07001645 if (connectionIndex >= 0) {
Jeff Browncbee6d62012-02-03 20:11:27 -08001646 sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex);
Jeff Brown519e0242010-09-15 15:18:56 -07001647 return connection->outboundQueue.isEmpty();
1648 } else {
1649 return true;
1650 }
1651}
1652
Jeff Brown9302c872011-07-13 22:51:29 -07001653String8 InputDispatcher::getApplicationWindowLabelLocked(
1654 const sp<InputApplicationHandle>& applicationHandle,
1655 const sp<InputWindowHandle>& windowHandle) {
1656 if (applicationHandle != NULL) {
1657 if (windowHandle != NULL) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001658 String8 label(applicationHandle->getName());
Jeff Brown519e0242010-09-15 15:18:56 -07001659 label.append(" - ");
Jeff Browncc4f7db2011-08-30 20:34:48 -07001660 label.append(windowHandle->getName());
Jeff Brown519e0242010-09-15 15:18:56 -07001661 return label;
1662 } else {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001663 return applicationHandle->getName();
Jeff Brown519e0242010-09-15 15:18:56 -07001664 }
Jeff Brown9302c872011-07-13 22:51:29 -07001665 } else if (windowHandle != NULL) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001666 return windowHandle->getName();
Jeff Brown519e0242010-09-15 15:18:56 -07001667 } else {
1668 return String8("<unknown application or window>");
1669 }
1670}
1671
Jeff Browne2fe69e2010-10-18 13:21:23 -07001672void InputDispatcher::pokeUserActivityLocked(const EventEntry* eventEntry) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001673 int32_t eventType = POWER_MANAGER_OTHER_EVENT;
Jeff Brown4d396052010-10-29 21:50:21 -07001674 switch (eventEntry->type) {
1675 case EventEntry::TYPE_MOTION: {
Jeff Browne2fe69e2010-10-18 13:21:23 -07001676 const MotionEntry* motionEntry = static_cast<const MotionEntry*>(eventEntry);
Jeff Brown4d396052010-10-29 21:50:21 -07001677 if (motionEntry->action == AMOTION_EVENT_ACTION_CANCEL) {
1678 return;
1679 }
1680
Jeff Brown56194eb2011-03-02 19:23:13 -08001681 if (MotionEvent::isTouchEvent(motionEntry->source, motionEntry->action)) {
Joe Onorato1a542c72010-11-08 09:48:20 -08001682 eventType = POWER_MANAGER_TOUCH_EVENT;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001683 }
Jeff Brown4d396052010-10-29 21:50:21 -07001684 break;
1685 }
1686 case EventEntry::TYPE_KEY: {
1687 const KeyEntry* keyEntry = static_cast<const KeyEntry*>(eventEntry);
1688 if (keyEntry->flags & AKEY_EVENT_FLAG_CANCELED) {
1689 return;
1690 }
Jeff Brown56194eb2011-03-02 19:23:13 -08001691 eventType = POWER_MANAGER_BUTTON_EVENT;
Jeff Brown4d396052010-10-29 21:50:21 -07001692 break;
1693 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001694 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001695
Jeff Brownb88102f2010-09-08 11:49:43 -07001696 CommandEntry* commandEntry = postCommandLocked(
1697 & InputDispatcher::doPokeUserActivityLockedInterruptible);
Jeff Browne2fe69e2010-10-18 13:21:23 -07001698 commandEntry->eventTime = eventEntry->eventTime;
Jeff Brownb88102f2010-09-08 11:49:43 -07001699 commandEntry->userActivityEventType = eventType;
1700}
1701
Jeff Brown7fbdc842010-06-17 20:52:56 -07001702void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001703 const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001704#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001705 ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, "
Jeff Brown9cc695c2011-08-23 18:35:04 -07001706 "xOffset=%f, yOffset=%f, scaleFactor=%f, "
Jeff Brown3241b6b2012-02-03 15:08:02 -08001707 "pointerIds=0x%x",
Jeff Brown519e0242010-09-15 15:18:56 -07001708 connection->getInputChannelName(), inputTarget->flags,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001709 inputTarget->xOffset, inputTarget->yOffset,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001710 inputTarget->scaleFactor, inputTarget->pointerIds.value);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001711#endif
1712
1713 // Skip this event if the connection status is not normal.
Jeff Brown519e0242010-09-15 15:18:56 -07001714 // We don't want to enqueue additional outbound events if the connection is broken.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001715 if (connection->status != Connection::STATUS_NORMAL) {
Jeff Brownb6997262010-10-08 22:31:17 -07001716#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001717 ALOGD("channel '%s' ~ Dropping event because the channel status is %s",
Jeff Brownb88102f2010-09-08 11:49:43 -07001718 connection->getInputChannelName(), connection->getStatusLabel());
Jeff Brownb6997262010-10-08 22:31:17 -07001719#endif
Jeff Brown46b9ac02010-04-22 18:58:52 -07001720 return;
1721 }
1722
Jeff Brown01ce2e92010-09-26 22:20:12 -07001723 // Split a motion event if needed.
Jeff Brown3241b6b2012-02-03 15:08:02 -08001724 if (inputTarget->flags & InputTarget::FLAG_SPLIT) {
Steve Blockec193de2012-01-09 18:35:44 +00001725 ALOG_ASSERT(eventEntry->type == EventEntry::TYPE_MOTION);
Jeff Brown01ce2e92010-09-26 22:20:12 -07001726
1727 MotionEntry* originalMotionEntry = static_cast<MotionEntry*>(eventEntry);
1728 if (inputTarget->pointerIds.count() != originalMotionEntry->pointerCount) {
1729 MotionEntry* splitMotionEntry = splitMotionEvent(
1730 originalMotionEntry, inputTarget->pointerIds);
Jeff Brown58a2da82011-01-25 16:02:22 -08001731 if (!splitMotionEntry) {
1732 return; // split event was dropped
1733 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07001734#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00001735 ALOGD("channel '%s' ~ Split motion event.",
Jeff Brown01ce2e92010-09-26 22:20:12 -07001736 connection->getInputChannelName());
1737 logOutboundMotionDetailsLocked(" ", splitMotionEntry);
1738#endif
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001739 enqueueDispatchEntriesLocked(currentTime, connection,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001740 splitMotionEntry, inputTarget);
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001741 splitMotionEntry->release();
1742 return;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001743 }
1744 }
1745
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001746 // Not splitting. Enqueue dispatch entries for the event as is.
Jeff Brown3241b6b2012-02-03 15:08:02 -08001747 enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget);
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001748}
1749
1750void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001751 const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001752 bool wasEmpty = connection->outboundQueue.isEmpty();
1753
Jeff Browna032cc02011-03-07 16:56:21 -08001754 // Enqueue dispatch entries for the requested modes.
1755 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001756 InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT);
Jeff Browna032cc02011-03-07 16:56:21 -08001757 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001758 InputTarget::FLAG_DISPATCH_AS_OUTSIDE);
Jeff Browna032cc02011-03-07 16:56:21 -08001759 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001760 InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER);
Jeff Browna032cc02011-03-07 16:56:21 -08001761 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001762 InputTarget::FLAG_DISPATCH_AS_IS);
Jeff Brown98db5fa2011-06-08 15:37:10 -07001763 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001764 InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT);
Jeff Brown98db5fa2011-06-08 15:37:10 -07001765 enqueueDispatchEntryLocked(connection, eventEntry, inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001766 InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER);
Jeff Browna032cc02011-03-07 16:56:21 -08001767
1768 // If the outbound queue was previously empty, start the dispatch cycle going.
Jeff Brownb6110c22011-04-01 16:15:13 -07001769 if (wasEmpty && !connection->outboundQueue.isEmpty()) {
Jeff Browna032cc02011-03-07 16:56:21 -08001770 activateConnectionLocked(connection.get());
1771 startDispatchCycleLocked(currentTime, connection);
1772 }
1773}
1774
1775void InputDispatcher::enqueueDispatchEntryLocked(
1776 const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001777 int32_t dispatchMode) {
Jeff Browna032cc02011-03-07 16:56:21 -08001778 int32_t inputTargetFlags = inputTarget->flags;
1779 if (!(inputTargetFlags & dispatchMode)) {
1780 return;
1781 }
1782 inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode;
1783
Jeff Brown46b9ac02010-04-22 18:58:52 -07001784 // This is a new event.
1785 // Enqueue a new dispatch entry onto the outbound queue for this connection.
Jeff Brownac386072011-07-20 15:19:50 -07001786 DispatchEntry* dispatchEntry = new DispatchEntry(eventEntry, // increments ref
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -07001787 inputTargetFlags, inputTarget->xOffset, inputTarget->yOffset,
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001788 inputTarget->scaleFactor);
Jeff Brown6ec402b2010-07-28 15:48:59 -07001789
Jeff Brown81346812011-06-28 20:08:48 -07001790 // Apply target flags and update the connection's input state.
1791 switch (eventEntry->type) {
1792 case EventEntry::TYPE_KEY: {
1793 KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry);
1794 dispatchEntry->resolvedAction = keyEntry->action;
1795 dispatchEntry->resolvedFlags = keyEntry->flags;
1796
1797 if (!connection->inputState.trackKey(keyEntry,
1798 dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags)) {
1799#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001800 ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event",
Jeff Brown81346812011-06-28 20:08:48 -07001801 connection->getInputChannelName());
1802#endif
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001803 delete dispatchEntry;
Jeff Brown81346812011-06-28 20:08:48 -07001804 return; // skip the inconsistent event
1805 }
1806 break;
1807 }
1808
1809 case EventEntry::TYPE_MOTION: {
1810 MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry);
1811 if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) {
1812 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE;
1813 } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) {
1814 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT;
1815 } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) {
1816 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER;
1817 } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) {
1818 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL;
1819 } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) {
1820 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN;
1821 } else {
1822 dispatchEntry->resolvedAction = motionEntry->action;
1823 }
1824 if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE
1825 && !connection->inputState.isHovering(
1826 motionEntry->deviceId, motionEntry->source)) {
1827#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001828 ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter event",
Jeff Brown81346812011-06-28 20:08:48 -07001829 connection->getInputChannelName());
1830#endif
1831 dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER;
1832 }
1833
1834 dispatchEntry->resolvedFlags = motionEntry->flags;
1835 if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) {
1836 dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
1837 }
1838
1839 if (!connection->inputState.trackMotion(motionEntry,
1840 dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags)) {
1841#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001842 ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion event",
Jeff Brown81346812011-06-28 20:08:48 -07001843 connection->getInputChannelName());
1844#endif
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001845 delete dispatchEntry;
Jeff Brown81346812011-06-28 20:08:48 -07001846 return; // skip the inconsistent event
1847 }
1848 break;
1849 }
1850 }
1851
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08001852 // Remember that we are waiting for this dispatch to complete.
1853 if (dispatchEntry->hasForegroundTarget()) {
1854 incrementPendingForegroundDispatchesLocked(eventEntry);
1855 }
1856
Jeff Brown46b9ac02010-04-22 18:58:52 -07001857 // Enqueue the dispatch entry.
1858 connection->outboundQueue.enqueueAtTail(dispatchEntry);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001859}
1860
Jeff Brown7fbdc842010-06-17 20:52:56 -07001861void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime,
Jeff Brown519e0242010-09-15 15:18:56 -07001862 const sp<Connection>& connection) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001863#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001864 ALOGD("channel '%s' ~ startDispatchCycle",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001865 connection->getInputChannelName());
1866#endif
1867
Steve Blockec193de2012-01-09 18:35:44 +00001868 ALOG_ASSERT(connection->status == Connection::STATUS_NORMAL);
1869 ALOG_ASSERT(! connection->outboundQueue.isEmpty());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001870
Jeff Brownac386072011-07-20 15:19:50 -07001871 DispatchEntry* dispatchEntry = connection->outboundQueue.head;
Steve Blockec193de2012-01-09 18:35:44 +00001872 ALOG_ASSERT(! dispatchEntry->inProgress);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001873
Jeff Brownb88102f2010-09-08 11:49:43 -07001874 // Mark the dispatch entry as in progress.
1875 dispatchEntry->inProgress = true;
1876
Jeff Brown46b9ac02010-04-22 18:58:52 -07001877 // Publish the event.
1878 status_t status;
Jeff Browna032cc02011-03-07 16:56:21 -08001879 EventEntry* eventEntry = dispatchEntry->eventEntry;
Jeff Brown01ce2e92010-09-26 22:20:12 -07001880 switch (eventEntry->type) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001881 case EventEntry::TYPE_KEY: {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001882 KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001883
Jeff Brown46b9ac02010-04-22 18:58:52 -07001884 // Publish the key event.
Jeff Brown81346812011-06-28 20:08:48 -07001885 status = connection->inputPublisher.publishKeyEvent(
1886 keyEntry->deviceId, keyEntry->source,
1887 dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags,
1888 keyEntry->keyCode, keyEntry->scanCode,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001889 keyEntry->metaState, keyEntry->repeatCount, keyEntry->downTime,
1890 keyEntry->eventTime);
1891
1892 if (status) {
Steve Block3762c312012-01-06 19:20:56 +00001893 ALOGE("channel '%s' ~ Could not publish key event, "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001894 "status=%d", connection->getInputChannelName(), status);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001895 abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001896 return;
1897 }
1898 break;
1899 }
1900
1901 case EventEntry::TYPE_MOTION: {
Jeff Brown01ce2e92010-09-26 22:20:12 -07001902 MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001903
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001904 PointerCoords scaledCoords[MAX_POINTERS];
Jeff Brown3241b6b2012-02-03 15:08:02 -08001905 const PointerCoords* usingCoords = motionEntry->pointerCoords;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001906
Jeff Brownd3616592010-07-16 17:21:06 -07001907 // Set the X and Y offset depending on the input source.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001908 float xOffset, yOffset, scaleFactor;
Kenny Root7a9db182011-06-02 15:16:05 -07001909 if (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER
1910 && !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001911 scaleFactor = dispatchEntry->scaleFactor;
1912 xOffset = dispatchEntry->xOffset * scaleFactor;
1913 yOffset = dispatchEntry->yOffset * scaleFactor;
1914 if (scaleFactor != 1.0f) {
1915 for (size_t i = 0; i < motionEntry->pointerCount; i++) {
Jeff Brown3241b6b2012-02-03 15:08:02 -08001916 scaledCoords[i] = motionEntry->pointerCoords[i];
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001917 scaledCoords[i].scale(scaleFactor);
1918 }
1919 usingCoords = scaledCoords;
1920 }
Jeff Brownd3616592010-07-16 17:21:06 -07001921 } else {
1922 xOffset = 0.0f;
1923 yOffset = 0.0f;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001924 scaleFactor = 1.0f;
Kenny Root7a9db182011-06-02 15:16:05 -07001925
1926 // We don't want the dispatch target to know.
1927 if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) {
1928 for (size_t i = 0; i < motionEntry->pointerCount; i++) {
1929 scaledCoords[i].clear();
1930 }
1931 usingCoords = scaledCoords;
1932 }
Jeff Brownd3616592010-07-16 17:21:06 -07001933 }
1934
Jeff Brown3241b6b2012-02-03 15:08:02 -08001935 // Publish the motion event.
Jeff Brown81346812011-06-28 20:08:48 -07001936 status = connection->inputPublisher.publishMotionEvent(
1937 motionEntry->deviceId, motionEntry->source,
1938 dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags,
1939 motionEntry->edgeFlags, motionEntry->metaState, motionEntry->buttonState,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001940 xOffset, yOffset,
1941 motionEntry->xPrecision, motionEntry->yPrecision,
Jeff Brown3241b6b2012-02-03 15:08:02 -08001942 motionEntry->downTime, motionEntry->eventTime,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001943 motionEntry->pointerCount, motionEntry->pointerProperties,
1944 usingCoords);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001945
1946 if (status) {
Steve Block3762c312012-01-06 19:20:56 +00001947 ALOGE("channel '%s' ~ Could not publish motion event, "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001948 "status=%d", connection->getInputChannelName(), status);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001949 abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001950 return;
1951 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001952 break;
1953 }
1954
1955 default: {
Steve Blockec193de2012-01-09 18:35:44 +00001956 ALOG_ASSERT(false);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001957 }
1958 }
1959
Jeff Brown46b9ac02010-04-22 18:58:52 -07001960 // Record information about the newly started dispatch cycle.
Jeff Brown01ce2e92010-09-26 22:20:12 -07001961 connection->lastEventTime = eventEntry->eventTime;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001962 connection->lastDispatchTime = currentTime;
1963
Jeff Brown46b9ac02010-04-22 18:58:52 -07001964 // Notify other system components.
1965 onDispatchCycleStartedLocked(currentTime, connection);
1966}
1967
Jeff Brown7fbdc842010-06-17 20:52:56 -07001968void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime,
Jeff Brown3915bb82010-11-05 15:02:16 -07001969 const sp<Connection>& connection, bool handled) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001970#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00001971 ALOGD("channel '%s' ~ finishDispatchCycle - %01.1fms since event, "
Jeff Brown3915bb82010-11-05 15:02:16 -07001972 "%01.1fms since dispatch, handled=%s",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001973 connection->getInputChannelName(),
1974 connection->getEventLatencyMillis(currentTime),
Jeff Brown3915bb82010-11-05 15:02:16 -07001975 connection->getDispatchLatencyMillis(currentTime),
1976 toString(handled));
Jeff Brown46b9ac02010-04-22 18:58:52 -07001977#endif
1978
Jeff Brown9c3cda02010-06-15 01:31:58 -07001979 if (connection->status == Connection::STATUS_BROKEN
1980 || connection->status == Connection::STATUS_ZOMBIE) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001981 return;
1982 }
1983
Jeff Brown3915bb82010-11-05 15:02:16 -07001984 // Notify other system components and prepare to start the next dispatch cycle.
1985 onDispatchCycleFinishedLocked(currentTime, connection, handled);
Jeff Brownb88102f2010-09-08 11:49:43 -07001986}
1987
1988void InputDispatcher::startNextDispatchCycleLocked(nsecs_t currentTime,
1989 const sp<Connection>& connection) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001990 // Start the next dispatch cycle for this connection.
1991 while (! connection->outboundQueue.isEmpty()) {
Jeff Brownac386072011-07-20 15:19:50 -07001992 DispatchEntry* dispatchEntry = connection->outboundQueue.head;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001993 if (dispatchEntry->inProgress) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001994 // Finished.
1995 connection->outboundQueue.dequeueAtHead();
Jeff Brown519e0242010-09-15 15:18:56 -07001996 if (dispatchEntry->hasForegroundTarget()) {
1997 decrementPendingForegroundDispatchesLocked(dispatchEntry->eventEntry);
Jeff Brown6ec402b2010-07-28 15:48:59 -07001998 }
Jeff Brownac386072011-07-20 15:19:50 -07001999 delete dispatchEntry;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002000 } else {
2001 // If the head is not in progress, then we must have already dequeued the in
Jeff Brown519e0242010-09-15 15:18:56 -07002002 // progress event, which means we actually aborted it.
Jeff Brown46b9ac02010-04-22 18:58:52 -07002003 // So just start the next event for this connection.
Jeff Brown519e0242010-09-15 15:18:56 -07002004 startDispatchCycleLocked(currentTime, connection);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002005 return;
2006 }
2007 }
2008
2009 // Outbound queue is empty, deactivate the connection.
Jeff Brown7fbdc842010-06-17 20:52:56 -07002010 deactivateConnectionLocked(connection.get());
Jeff Brown46b9ac02010-04-22 18:58:52 -07002011}
2012
Jeff Brownb6997262010-10-08 22:31:17 -07002013void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime,
Jeff Browncc4f7db2011-08-30 20:34:48 -07002014 const sp<Connection>& connection, bool notify) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002015#if DEBUG_DISPATCH_CYCLE
Steve Block5baa3a62011-12-20 16:23:08 +00002016 ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s",
Jeff Browncc4f7db2011-08-30 20:34:48 -07002017 connection->getInputChannelName(), toString(notify));
Jeff Brown46b9ac02010-04-22 18:58:52 -07002018#endif
2019
Jeff Brownb88102f2010-09-08 11:49:43 -07002020 // Clear the outbound queue.
Jeff Brown519e0242010-09-15 15:18:56 -07002021 drainOutboundQueueLocked(connection.get());
Jeff Brown46b9ac02010-04-22 18:58:52 -07002022
Jeff Brownb6997262010-10-08 22:31:17 -07002023 // The connection appears to be unrecoverably broken.
Jeff Brown9c3cda02010-06-15 01:31:58 -07002024 // Ignore already broken or zombie connections.
Jeff Brownb6997262010-10-08 22:31:17 -07002025 if (connection->status == Connection::STATUS_NORMAL) {
2026 connection->status = Connection::STATUS_BROKEN;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002027
Jeff Browncc4f7db2011-08-30 20:34:48 -07002028 if (notify) {
2029 // Notify other system components.
2030 onDispatchCycleBrokenLocked(currentTime, connection);
2031 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002032 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002033}
2034
Jeff Brown519e0242010-09-15 15:18:56 -07002035void InputDispatcher::drainOutboundQueueLocked(Connection* connection) {
2036 while (! connection->outboundQueue.isEmpty()) {
2037 DispatchEntry* dispatchEntry = connection->outboundQueue.dequeueAtHead();
2038 if (dispatchEntry->hasForegroundTarget()) {
2039 decrementPendingForegroundDispatchesLocked(dispatchEntry->eventEntry);
Jeff Brownb88102f2010-09-08 11:49:43 -07002040 }
Jeff Brownac386072011-07-20 15:19:50 -07002041 delete dispatchEntry;
Jeff Brownb88102f2010-09-08 11:49:43 -07002042 }
2043
Jeff Brown519e0242010-09-15 15:18:56 -07002044 deactivateConnectionLocked(connection);
Jeff Brownb88102f2010-09-08 11:49:43 -07002045}
2046
Jeff Browncbee6d62012-02-03 20:11:27 -08002047int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002048 InputDispatcher* d = static_cast<InputDispatcher*>(data);
2049
2050 { // acquire lock
2051 AutoMutex _l(d->mLock);
2052
Jeff Browncbee6d62012-02-03 20:11:27 -08002053 ssize_t connectionIndex = d->mConnectionsByFd.indexOfKey(fd);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002054 if (connectionIndex < 0) {
Steve Block3762c312012-01-06 19:20:56 +00002055 ALOGE("Received spurious receive callback for unknown input channel. "
Jeff Browncbee6d62012-02-03 20:11:27 -08002056 "fd=%d, events=0x%x", fd, events);
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002057 return 0; // remove the callback
Jeff Brown46b9ac02010-04-22 18:58:52 -07002058 }
2059
Jeff Browncc4f7db2011-08-30 20:34:48 -07002060 bool notify;
Jeff Browncbee6d62012-02-03 20:11:27 -08002061 sp<Connection> connection = d->mConnectionsByFd.valueAt(connectionIndex);
Jeff Browncc4f7db2011-08-30 20:34:48 -07002062 if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) {
2063 if (!(events & ALOOPER_EVENT_INPUT)) {
Steve Block8564c8d2012-01-05 23:22:43 +00002064 ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. "
Jeff Browncc4f7db2011-08-30 20:34:48 -07002065 "events=0x%x", connection->getInputChannelName(), events);
2066 return 1;
2067 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002068
Jeff Browncc4f7db2011-08-30 20:34:48 -07002069 bool handled = false;
2070 status_t status = connection->inputPublisher.receiveFinishedSignal(&handled);
2071 if (!status) {
2072 nsecs_t currentTime = now();
2073 d->finishDispatchCycleLocked(currentTime, connection, handled);
2074 d->runCommandsLockedInterruptible();
2075 return 1;
2076 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002077
Steve Block3762c312012-01-06 19:20:56 +00002078 ALOGE("channel '%s' ~ Failed to receive finished signal. status=%d",
Jeff Brown46b9ac02010-04-22 18:58:52 -07002079 connection->getInputChannelName(), status);
Jeff Browncc4f7db2011-08-30 20:34:48 -07002080 notify = true;
2081 } else {
2082 // Monitor channels are never explicitly unregistered.
2083 // We do it automatically when the remote endpoint is closed so don't warn
2084 // about them.
2085 notify = !connection->monitor;
2086 if (notify) {
Steve Block8564c8d2012-01-05 23:22:43 +00002087 ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. "
Jeff Browncc4f7db2011-08-30 20:34:48 -07002088 "events=0x%x", connection->getInputChannelName(), events);
2089 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002090 }
2091
Jeff Browncc4f7db2011-08-30 20:34:48 -07002092 // Unregister the channel.
2093 d->unregisterInputChannelLocked(connection->inputChannel, notify);
2094 return 0; // remove the callback
Jeff Brown46b9ac02010-04-22 18:58:52 -07002095 } // release lock
2096}
2097
Jeff Brownb6997262010-10-08 22:31:17 -07002098void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked(
Jeff Brownda3d5a92011-03-29 15:11:34 -07002099 const CancelationOptions& options) {
Jeff Browncbee6d62012-02-03 20:11:27 -08002100 for (size_t i = 0; i < mConnectionsByFd.size(); i++) {
Jeff Brownb6997262010-10-08 22:31:17 -07002101 synthesizeCancelationEventsForConnectionLocked(
Jeff Browncbee6d62012-02-03 20:11:27 -08002102 mConnectionsByFd.valueAt(i), options);
Jeff Brownb6997262010-10-08 22:31:17 -07002103 }
2104}
2105
2106void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked(
Jeff Brownda3d5a92011-03-29 15:11:34 -07002107 const sp<InputChannel>& channel, const CancelationOptions& options) {
Jeff Brownb6997262010-10-08 22:31:17 -07002108 ssize_t index = getConnectionIndexLocked(channel);
2109 if (index >= 0) {
2110 synthesizeCancelationEventsForConnectionLocked(
Jeff Browncbee6d62012-02-03 20:11:27 -08002111 mConnectionsByFd.valueAt(index), options);
Jeff Brownb6997262010-10-08 22:31:17 -07002112 }
2113}
2114
2115void InputDispatcher::synthesizeCancelationEventsForConnectionLocked(
Jeff Brownda3d5a92011-03-29 15:11:34 -07002116 const sp<Connection>& connection, const CancelationOptions& options) {
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08002117 if (connection->status == Connection::STATUS_BROKEN) {
2118 return;
2119 }
2120
Jeff Brownb6997262010-10-08 22:31:17 -07002121 nsecs_t currentTime = now();
2122
2123 mTempCancelationEvents.clear();
Jeff Brownac386072011-07-20 15:19:50 -07002124 connection->inputState.synthesizeCancelationEvents(currentTime,
Jeff Brownb6997262010-10-08 22:31:17 -07002125 mTempCancelationEvents, options);
2126
Jeff Brownc0cb3dc2012-01-12 18:30:12 -08002127 if (!mTempCancelationEvents.isEmpty()) {
Jeff Brownb6997262010-10-08 22:31:17 -07002128#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002129 ALOGD("channel '%s' ~ Synthesized %d cancelation events to bring channel back in sync "
Jeff Brownda3d5a92011-03-29 15:11:34 -07002130 "with reality: %s, mode=%d.",
2131 connection->getInputChannelName(), mTempCancelationEvents.size(),
2132 options.reason, options.mode);
Jeff Brownb6997262010-10-08 22:31:17 -07002133#endif
2134 for (size_t i = 0; i < mTempCancelationEvents.size(); i++) {
2135 EventEntry* cancelationEventEntry = mTempCancelationEvents.itemAt(i);
2136 switch (cancelationEventEntry->type) {
2137 case EventEntry::TYPE_KEY:
2138 logOutboundKeyDetailsLocked("cancel - ",
2139 static_cast<KeyEntry*>(cancelationEventEntry));
2140 break;
2141 case EventEntry::TYPE_MOTION:
2142 logOutboundMotionDetailsLocked("cancel - ",
2143 static_cast<MotionEntry*>(cancelationEventEntry));
2144 break;
2145 }
2146
Jeff Brown81346812011-06-28 20:08:48 -07002147 InputTarget target;
Jeff Brown9302c872011-07-13 22:51:29 -07002148 sp<InputWindowHandle> windowHandle = getWindowHandleLocked(connection->inputChannel);
2149 if (windowHandle != NULL) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07002150 const InputWindowInfo* windowInfo = windowHandle->getInfo();
2151 target.xOffset = -windowInfo->frameLeft;
2152 target.yOffset = -windowInfo->frameTop;
2153 target.scaleFactor = windowInfo->scaleFactor;
Jeff Brownb6997262010-10-08 22:31:17 -07002154 } else {
Jeff Brown81346812011-06-28 20:08:48 -07002155 target.xOffset = 0;
2156 target.yOffset = 0;
2157 target.scaleFactor = 1.0f;
Jeff Brownb6997262010-10-08 22:31:17 -07002158 }
Jeff Brown81346812011-06-28 20:08:48 -07002159 target.inputChannel = connection->inputChannel;
2160 target.flags = InputTarget::FLAG_DISPATCH_AS_IS;
Jeff Brownb6997262010-10-08 22:31:17 -07002161
Jeff Brown81346812011-06-28 20:08:48 -07002162 enqueueDispatchEntryLocked(connection, cancelationEventEntry, // increments ref
Jeff Brown3241b6b2012-02-03 15:08:02 -08002163 &target, InputTarget::FLAG_DISPATCH_AS_IS);
Jeff Brownb6997262010-10-08 22:31:17 -07002164
Jeff Brownac386072011-07-20 15:19:50 -07002165 cancelationEventEntry->release();
Jeff Brownb6997262010-10-08 22:31:17 -07002166 }
2167
Jeff Brownac386072011-07-20 15:19:50 -07002168 if (!connection->outboundQueue.head->inProgress) {
Jeff Brownb6997262010-10-08 22:31:17 -07002169 startDispatchCycleLocked(currentTime, connection);
2170 }
2171 }
2172}
2173
Jeff Brown01ce2e92010-09-26 22:20:12 -07002174InputDispatcher::MotionEntry*
2175InputDispatcher::splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds) {
Steve Blockec193de2012-01-09 18:35:44 +00002176 ALOG_ASSERT(pointerIds.value != 0);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002177
2178 uint32_t splitPointerIndexMap[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002179 PointerProperties splitPointerProperties[MAX_POINTERS];
Jeff Brown01ce2e92010-09-26 22:20:12 -07002180 PointerCoords splitPointerCoords[MAX_POINTERS];
2181
2182 uint32_t originalPointerCount = originalMotionEntry->pointerCount;
2183 uint32_t splitPointerCount = 0;
2184
2185 for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount;
2186 originalPointerIndex++) {
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 splitPointerIndexMap[splitPointerCount] = originalPointerIndex;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002192 splitPointerProperties[splitPointerCount].copyFrom(pointerProperties);
Jeff Brownace13b12011-03-09 17:39:48 -08002193 splitPointerCoords[splitPointerCount].copyFrom(
Jeff Brown3241b6b2012-02-03 15:08:02 -08002194 originalMotionEntry->pointerCoords[originalPointerIndex]);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002195 splitPointerCount += 1;
2196 }
2197 }
Jeff Brown58a2da82011-01-25 16:02:22 -08002198
2199 if (splitPointerCount != pointerIds.count()) {
2200 // This is bad. We are missing some of the pointers that we expected to deliver.
2201 // Most likely this indicates that we received an ACTION_MOVE events that has
2202 // different pointer ids than we expected based on the previous ACTION_DOWN
2203 // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers
2204 // in this way.
Steve Block8564c8d2012-01-05 23:22:43 +00002205 ALOGW("Dropping split motion event because the pointer count is %d but "
Jeff Brown58a2da82011-01-25 16:02:22 -08002206 "we expected there to be %d pointers. This probably means we received "
2207 "a broken sequence of pointer ids from the input device.",
2208 splitPointerCount, pointerIds.count());
2209 return NULL;
2210 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07002211
2212 int32_t action = originalMotionEntry->action;
2213 int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK;
2214 if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN
2215 || maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) {
2216 int32_t originalPointerIndex = getMotionEventActionPointerIndex(action);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002217 const PointerProperties& pointerProperties =
2218 originalMotionEntry->pointerProperties[originalPointerIndex];
2219 uint32_t pointerId = uint32_t(pointerProperties.id);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002220 if (pointerIds.hasBit(pointerId)) {
2221 if (pointerIds.count() == 1) {
2222 // The first/last pointer went down/up.
2223 action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN
2224 ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
Jeff Brown9a01d052010-09-27 16:35:11 -07002225 } else {
2226 // A secondary pointer went down/up.
2227 uint32_t splitPointerIndex = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002228 while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) {
Jeff Brown9a01d052010-09-27 16:35:11 -07002229 splitPointerIndex += 1;
2230 }
2231 action = maskedAction | (splitPointerIndex
2232 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002233 }
2234 } else {
2235 // An unrelated pointer changed.
2236 action = AMOTION_EVENT_ACTION_MOVE;
2237 }
2238 }
2239
Jeff Brownac386072011-07-20 15:19:50 -07002240 MotionEntry* splitMotionEntry = new MotionEntry(
Jeff Brown01ce2e92010-09-26 22:20:12 -07002241 originalMotionEntry->eventTime,
2242 originalMotionEntry->deviceId,
2243 originalMotionEntry->source,
2244 originalMotionEntry->policyFlags,
2245 action,
2246 originalMotionEntry->flags,
2247 originalMotionEntry->metaState,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002248 originalMotionEntry->buttonState,
Jeff Brown01ce2e92010-09-26 22:20:12 -07002249 originalMotionEntry->edgeFlags,
2250 originalMotionEntry->xPrecision,
2251 originalMotionEntry->yPrecision,
2252 originalMotionEntry->downTime,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002253 splitPointerCount, splitPointerProperties, splitPointerCoords);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002254
Jeff Browna032cc02011-03-07 16:56:21 -08002255 if (originalMotionEntry->injectionState) {
2256 splitMotionEntry->injectionState = originalMotionEntry->injectionState;
2257 splitMotionEntry->injectionState->refCount += 1;
2258 }
2259
Jeff Brown01ce2e92010-09-26 22:20:12 -07002260 return splitMotionEntry;
2261}
2262
Jeff Brownbe1aa822011-07-27 16:04:54 -07002263void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002264#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002265 ALOGD("notifyConfigurationChanged - eventTime=%lld", args->eventTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002266#endif
2267
Jeff Brownb88102f2010-09-08 11:49:43 -07002268 bool needWake;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002269 { // acquire lock
2270 AutoMutex _l(mLock);
2271
Jeff Brownbe1aa822011-07-27 16:04:54 -07002272 ConfigurationChangedEntry* newEntry = new ConfigurationChangedEntry(args->eventTime);
Jeff Brownb88102f2010-09-08 11:49:43 -07002273 needWake = enqueueInboundEventLocked(newEntry);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002274 } // release lock
2275
Jeff Brownb88102f2010-09-08 11:49:43 -07002276 if (needWake) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002277 mLooper->wake();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002278 }
2279}
2280
Jeff Brownbe1aa822011-07-27 16:04:54 -07002281void InputDispatcher::notifyKey(const NotifyKeyArgs* args) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002282#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002283 ALOGD("notifyKey - eventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, action=0x%x, "
Jeff Brown46b9ac02010-04-22 18:58:52 -07002284 "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07002285 args->eventTime, args->deviceId, args->source, args->policyFlags,
2286 args->action, args->flags, args->keyCode, args->scanCode,
2287 args->metaState, args->downTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002288#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07002289 if (!validateKeyEvent(args->action)) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002290 return;
2291 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002292
Jeff Brownbe1aa822011-07-27 16:04:54 -07002293 uint32_t policyFlags = args->policyFlags;
2294 int32_t flags = args->flags;
2295 int32_t metaState = args->metaState;
Jeff Brown1f245102010-11-18 20:53:46 -08002296 if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) {
2297 policyFlags |= POLICY_FLAG_VIRTUAL;
2298 flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2299 }
Jeff Brown924c4d42011-03-07 16:40:47 -08002300 if (policyFlags & POLICY_FLAG_ALT) {
2301 metaState |= AMETA_ALT_ON | AMETA_ALT_LEFT_ON;
2302 }
2303 if (policyFlags & POLICY_FLAG_ALT_GR) {
2304 metaState |= AMETA_ALT_ON | AMETA_ALT_RIGHT_ON;
2305 }
2306 if (policyFlags & POLICY_FLAG_SHIFT) {
2307 metaState |= AMETA_SHIFT_ON | AMETA_SHIFT_LEFT_ON;
2308 }
2309 if (policyFlags & POLICY_FLAG_CAPS_LOCK) {
2310 metaState |= AMETA_CAPS_LOCK_ON;
2311 }
2312 if (policyFlags & POLICY_FLAG_FUNCTION) {
2313 metaState |= AMETA_FUNCTION_ON;
2314 }
Jeff Brown1f245102010-11-18 20:53:46 -08002315
Jeff Browne20c9e02010-10-11 14:20:19 -07002316 policyFlags |= POLICY_FLAG_TRUSTED;
Jeff Brown1f245102010-11-18 20:53:46 -08002317
2318 KeyEvent event;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002319 event.initialize(args->deviceId, args->source, args->action,
2320 flags, args->keyCode, args->scanCode, metaState, 0,
2321 args->downTime, args->eventTime);
Jeff Brown1f245102010-11-18 20:53:46 -08002322
2323 mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags);
2324
2325 if (policyFlags & POLICY_FLAG_WOKE_HERE) {
2326 flags |= AKEY_EVENT_FLAG_WOKE_HERE;
2327 }
Jeff Brownb6997262010-10-08 22:31:17 -07002328
Jeff Brownb88102f2010-09-08 11:49:43 -07002329 bool needWake;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002330 { // acquire lock
Jeff Brown0029c662011-03-30 02:25:18 -07002331 mLock.lock();
2332
2333 if (mInputFilterEnabled) {
2334 mLock.unlock();
2335
2336 policyFlags |= POLICY_FLAG_FILTERED;
2337 if (!mPolicy->filterInputEvent(&event, policyFlags)) {
2338 return; // event was consumed by the filter
2339 }
2340
2341 mLock.lock();
2342 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002343
Jeff Brown7fbdc842010-06-17 20:52:56 -07002344 int32_t repeatCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002345 KeyEntry* newEntry = new KeyEntry(args->eventTime,
2346 args->deviceId, args->source, policyFlags,
2347 args->action, flags, args->keyCode, args->scanCode,
2348 metaState, repeatCount, args->downTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002349
Jeff Brownb88102f2010-09-08 11:49:43 -07002350 needWake = enqueueInboundEventLocked(newEntry);
Jeff Brown0029c662011-03-30 02:25:18 -07002351 mLock.unlock();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002352 } // release lock
2353
Jeff Brownb88102f2010-09-08 11:49:43 -07002354 if (needWake) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002355 mLooper->wake();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002356 }
2357}
2358
Jeff Brownbe1aa822011-07-27 16:04:54 -07002359void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002360#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002361 ALOGD("notifyMotion - eventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002362 "action=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, edgeFlags=0x%x, "
Jeff Brown85a31762010-09-01 17:01:00 -07002363 "xPrecision=%f, yPrecision=%f, downTime=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07002364 args->eventTime, args->deviceId, args->source, args->policyFlags,
2365 args->action, args->flags, args->metaState, args->buttonState,
2366 args->edgeFlags, args->xPrecision, args->yPrecision, args->downTime);
2367 for (uint32_t i = 0; i < args->pointerCount; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00002368 ALOGD(" Pointer %d: id=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002369 "x=%f, y=%f, pressure=%f, size=%f, "
Jeff Brown85a31762010-09-01 17:01:00 -07002370 "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, "
Jeff Brown8d608662010-08-30 03:02:23 -07002371 "orientation=%f",
Jeff Brownbe1aa822011-07-27 16:04:54 -07002372 i, args->pointerProperties[i].id,
2373 args->pointerProperties[i].toolType,
2374 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X),
2375 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y),
2376 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2377 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE),
2378 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2379 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2380 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2381 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2382 args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
Jeff Brown46b9ac02010-04-22 18:58:52 -07002383 }
2384#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07002385 if (!validateMotionEvent(args->action, args->pointerCount, args->pointerProperties)) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002386 return;
2387 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002388
Jeff Brownbe1aa822011-07-27 16:04:54 -07002389 uint32_t policyFlags = args->policyFlags;
Jeff Browne20c9e02010-10-11 14:20:19 -07002390 policyFlags |= POLICY_FLAG_TRUSTED;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002391 mPolicy->interceptMotionBeforeQueueing(args->eventTime, /*byref*/ policyFlags);
Jeff Brownb6997262010-10-08 22:31:17 -07002392
Jeff Brownb88102f2010-09-08 11:49:43 -07002393 bool needWake;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002394 { // acquire lock
Jeff Brown0029c662011-03-30 02:25:18 -07002395 mLock.lock();
2396
2397 if (mInputFilterEnabled) {
2398 mLock.unlock();
2399
2400 MotionEvent event;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002401 event.initialize(args->deviceId, args->source, args->action, args->flags,
2402 args->edgeFlags, args->metaState, args->buttonState, 0, 0,
2403 args->xPrecision, args->yPrecision,
2404 args->downTime, args->eventTime,
2405 args->pointerCount, args->pointerProperties, args->pointerCoords);
Jeff Brown0029c662011-03-30 02:25:18 -07002406
2407 policyFlags |= POLICY_FLAG_FILTERED;
2408 if (!mPolicy->filterInputEvent(&event, policyFlags)) {
2409 return; // event was consumed by the filter
2410 }
2411
2412 mLock.lock();
2413 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002414
Jeff Brown46b9ac02010-04-22 18:58:52 -07002415 // Just enqueue a new motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002416 MotionEntry* newEntry = new MotionEntry(args->eventTime,
2417 args->deviceId, args->source, policyFlags,
2418 args->action, args->flags, args->metaState, args->buttonState,
2419 args->edgeFlags, args->xPrecision, args->yPrecision, args->downTime,
2420 args->pointerCount, args->pointerProperties, args->pointerCoords);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002421
Jeff Brownb88102f2010-09-08 11:49:43 -07002422 needWake = enqueueInboundEventLocked(newEntry);
Jeff Brown0029c662011-03-30 02:25:18 -07002423 mLock.unlock();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002424 } // release lock
2425
Jeff Brownb88102f2010-09-08 11:49:43 -07002426 if (needWake) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002427 mLooper->wake();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002428 }
2429}
2430
Jeff Brownbe1aa822011-07-27 16:04:54 -07002431void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) {
Jeff Brownb6997262010-10-08 22:31:17 -07002432#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002433 ALOGD("notifySwitch - eventTime=%lld, policyFlags=0x%x, switchCode=%d, switchValue=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07002434 args->eventTime, args->policyFlags,
2435 args->switchCode, args->switchValue);
Jeff Brownb6997262010-10-08 22:31:17 -07002436#endif
2437
Jeff Brownbe1aa822011-07-27 16:04:54 -07002438 uint32_t policyFlags = args->policyFlags;
Jeff Browne20c9e02010-10-11 14:20:19 -07002439 policyFlags |= POLICY_FLAG_TRUSTED;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002440 mPolicy->notifySwitch(args->eventTime,
2441 args->switchCode, args->switchValue, policyFlags);
Jeff Brownb6997262010-10-08 22:31:17 -07002442}
2443
Jeff Brown65fd2512011-08-18 11:20:58 -07002444void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) {
2445#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002446 ALOGD("notifyDeviceReset - eventTime=%lld, deviceId=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07002447 args->eventTime, args->deviceId);
2448#endif
2449
2450 bool needWake;
2451 { // acquire lock
2452 AutoMutex _l(mLock);
2453
2454 DeviceResetEntry* newEntry = new DeviceResetEntry(args->eventTime, args->deviceId);
2455 needWake = enqueueInboundEventLocked(newEntry);
2456 } // release lock
2457
2458 if (needWake) {
2459 mLooper->wake();
2460 }
2461}
2462
Jeff Brown7fbdc842010-06-17 20:52:56 -07002463int32_t InputDispatcher::injectInputEvent(const InputEvent* event,
Jeff Brown0029c662011-03-30 02:25:18 -07002464 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
2465 uint32_t policyFlags) {
Jeff Brown7fbdc842010-06-17 20:52:56 -07002466#if DEBUG_INBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00002467 ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, "
Jeff Brown0029c662011-03-30 02:25:18 -07002468 "syncMode=%d, timeoutMillis=%d, policyFlags=0x%08x",
2469 event->getType(), injectorPid, injectorUid, syncMode, timeoutMillis, policyFlags);
Jeff Brown7fbdc842010-06-17 20:52:56 -07002470#endif
2471
2472 nsecs_t endTime = now() + milliseconds_to_nanoseconds(timeoutMillis);
Jeff Browne20c9e02010-10-11 14:20:19 -07002473
Jeff Brown0029c662011-03-30 02:25:18 -07002474 policyFlags |= POLICY_FLAG_INJECTED;
Jeff Browne20c9e02010-10-11 14:20:19 -07002475 if (hasInjectionPermission(injectorPid, injectorUid)) {
2476 policyFlags |= POLICY_FLAG_TRUSTED;
2477 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07002478
Jeff Brown3241b6b2012-02-03 15:08:02 -08002479 EventEntry* firstInjectedEntry;
2480 EventEntry* lastInjectedEntry;
Jeff Brownb6997262010-10-08 22:31:17 -07002481 switch (event->getType()) {
2482 case AINPUT_EVENT_TYPE_KEY: {
2483 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(event);
2484 int32_t action = keyEvent->getAction();
2485 if (! validateKeyEvent(action)) {
Jeff Brownb88102f2010-09-08 11:49:43 -07002486 return INPUT_EVENT_INJECTION_FAILED;
2487 }
2488
Jeff Brownb6997262010-10-08 22:31:17 -07002489 int32_t flags = keyEvent->getFlags();
Jeff Brown1f245102010-11-18 20:53:46 -08002490 if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) {
2491 policyFlags |= POLICY_FLAG_VIRTUAL;
2492 }
2493
Jeff Brown0029c662011-03-30 02:25:18 -07002494 if (!(policyFlags & POLICY_FLAG_FILTERED)) {
2495 mPolicy->interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags);
2496 }
Jeff Brown1f245102010-11-18 20:53:46 -08002497
2498 if (policyFlags & POLICY_FLAG_WOKE_HERE) {
2499 flags |= AKEY_EVENT_FLAG_WOKE_HERE;
2500 }
Jeff Brown6ec402b2010-07-28 15:48:59 -07002501
Jeff Brownb6997262010-10-08 22:31:17 -07002502 mLock.lock();
Jeff Brown3241b6b2012-02-03 15:08:02 -08002503 firstInjectedEntry = new KeyEntry(keyEvent->getEventTime(),
Jeff Brown1f245102010-11-18 20:53:46 -08002504 keyEvent->getDeviceId(), keyEvent->getSource(),
2505 policyFlags, action, flags,
2506 keyEvent->getKeyCode(), keyEvent->getScanCode(), keyEvent->getMetaState(),
Jeff Brownb6997262010-10-08 22:31:17 -07002507 keyEvent->getRepeatCount(), keyEvent->getDownTime());
Jeff Brown3241b6b2012-02-03 15:08:02 -08002508 lastInjectedEntry = firstInjectedEntry;
Jeff Brownb6997262010-10-08 22:31:17 -07002509 break;
2510 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07002511
Jeff Brownb6997262010-10-08 22:31:17 -07002512 case AINPUT_EVENT_TYPE_MOTION: {
2513 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event);
2514 int32_t action = motionEvent->getAction();
2515 size_t pointerCount = motionEvent->getPointerCount();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002516 const PointerProperties* pointerProperties = motionEvent->getPointerProperties();
2517 if (! validateMotionEvent(action, pointerCount, pointerProperties)) {
Jeff Brownb6997262010-10-08 22:31:17 -07002518 return INPUT_EVENT_INJECTION_FAILED;
2519 }
2520
Jeff Brown0029c662011-03-30 02:25:18 -07002521 if (!(policyFlags & POLICY_FLAG_FILTERED)) {
2522 nsecs_t eventTime = motionEvent->getEventTime();
2523 mPolicy->interceptMotionBeforeQueueing(eventTime, /*byref*/ policyFlags);
2524 }
Jeff Brownb6997262010-10-08 22:31:17 -07002525
2526 mLock.lock();
2527 const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes();
2528 const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords();
Jeff Brown3241b6b2012-02-03 15:08:02 -08002529 firstInjectedEntry = new MotionEntry(*sampleEventTimes,
Jeff Brownb6997262010-10-08 22:31:17 -07002530 motionEvent->getDeviceId(), motionEvent->getSource(), policyFlags,
2531 action, motionEvent->getFlags(),
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002532 motionEvent->getMetaState(), motionEvent->getButtonState(),
2533 motionEvent->getEdgeFlags(),
Jeff Brownb6997262010-10-08 22:31:17 -07002534 motionEvent->getXPrecision(), motionEvent->getYPrecision(),
2535 motionEvent->getDownTime(), uint32_t(pointerCount),
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002536 pointerProperties, samplePointerCoords);
Jeff Brown3241b6b2012-02-03 15:08:02 -08002537 lastInjectedEntry = firstInjectedEntry;
Jeff Brownb6997262010-10-08 22:31:17 -07002538 for (size_t i = motionEvent->getHistorySize(); i > 0; i--) {
2539 sampleEventTimes += 1;
2540 samplePointerCoords += pointerCount;
Jeff Brown3241b6b2012-02-03 15:08:02 -08002541 MotionEntry* nextInjectedEntry = new MotionEntry(*sampleEventTimes,
2542 motionEvent->getDeviceId(), motionEvent->getSource(), policyFlags,
2543 action, motionEvent->getFlags(),
2544 motionEvent->getMetaState(), motionEvent->getButtonState(),
2545 motionEvent->getEdgeFlags(),
2546 motionEvent->getXPrecision(), motionEvent->getYPrecision(),
2547 motionEvent->getDownTime(), uint32_t(pointerCount),
2548 pointerProperties, samplePointerCoords);
2549 lastInjectedEntry->next = nextInjectedEntry;
2550 lastInjectedEntry = nextInjectedEntry;
Jeff Brownb6997262010-10-08 22:31:17 -07002551 }
Jeff Brownb6997262010-10-08 22:31:17 -07002552 break;
2553 }
2554
2555 default:
Steve Block8564c8d2012-01-05 23:22:43 +00002556 ALOGW("Cannot inject event of type %d", event->getType());
Jeff Brownb6997262010-10-08 22:31:17 -07002557 return INPUT_EVENT_INJECTION_FAILED;
2558 }
2559
Jeff Brownac386072011-07-20 15:19:50 -07002560 InjectionState* injectionState = new InjectionState(injectorPid, injectorUid);
Jeff Brownb6997262010-10-08 22:31:17 -07002561 if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) {
2562 injectionState->injectionIsAsync = true;
2563 }
2564
2565 injectionState->refCount += 1;
Jeff Brown3241b6b2012-02-03 15:08:02 -08002566 lastInjectedEntry->injectionState = injectionState;
Jeff Brownb6997262010-10-08 22:31:17 -07002567
Jeff Brown3241b6b2012-02-03 15:08:02 -08002568 bool needWake = false;
2569 for (EventEntry* entry = firstInjectedEntry; entry != NULL; ) {
2570 EventEntry* nextEntry = entry->next;
2571 needWake |= enqueueInboundEventLocked(entry);
2572 entry = nextEntry;
2573 }
2574
Jeff Brownb6997262010-10-08 22:31:17 -07002575 mLock.unlock();
Jeff Brown7fbdc842010-06-17 20:52:56 -07002576
Jeff Brownb88102f2010-09-08 11:49:43 -07002577 if (needWake) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002578 mLooper->wake();
Jeff Brown7fbdc842010-06-17 20:52:56 -07002579 }
2580
2581 int32_t injectionResult;
2582 { // acquire lock
2583 AutoMutex _l(mLock);
2584
Jeff Brown6ec402b2010-07-28 15:48:59 -07002585 if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) {
2586 injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED;
2587 } else {
2588 for (;;) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002589 injectionResult = injectionState->injectionResult;
Jeff Brown6ec402b2010-07-28 15:48:59 -07002590 if (injectionResult != INPUT_EVENT_INJECTION_PENDING) {
2591 break;
2592 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07002593
Jeff Brown7fbdc842010-06-17 20:52:56 -07002594 nsecs_t remainingTimeout = endTime - now();
2595 if (remainingTimeout <= 0) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002596#if DEBUG_INJECTION
Steve Block5baa3a62011-12-20 16:23:08 +00002597 ALOGD("injectInputEvent - Timed out waiting for injection result "
Jeff Brown6ec402b2010-07-28 15:48:59 -07002598 "to become available.");
2599#endif
Jeff Brown7fbdc842010-06-17 20:52:56 -07002600 injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT;
2601 break;
2602 }
2603
Jeff Brown6ec402b2010-07-28 15:48:59 -07002604 mInjectionResultAvailableCondition.waitRelative(mLock, remainingTimeout);
2605 }
2606
2607 if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED
2608 && syncMode == INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002609 while (injectionState->pendingForegroundDispatches != 0) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002610#if DEBUG_INJECTION
Steve Block5baa3a62011-12-20 16:23:08 +00002611 ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.",
Jeff Brown01ce2e92010-09-26 22:20:12 -07002612 injectionState->pendingForegroundDispatches);
Jeff Brown6ec402b2010-07-28 15:48:59 -07002613#endif
2614 nsecs_t remainingTimeout = endTime - now();
2615 if (remainingTimeout <= 0) {
2616#if DEBUG_INJECTION
Steve Block5baa3a62011-12-20 16:23:08 +00002617 ALOGD("injectInputEvent - Timed out waiting for pending foreground "
Jeff Brown6ec402b2010-07-28 15:48:59 -07002618 "dispatches to finish.");
2619#endif
2620 injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT;
2621 break;
2622 }
2623
2624 mInjectionSyncFinishedCondition.waitRelative(mLock, remainingTimeout);
2625 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07002626 }
2627 }
2628
Jeff Brownac386072011-07-20 15:19:50 -07002629 injectionState->release();
Jeff Brown7fbdc842010-06-17 20:52:56 -07002630 } // release lock
2631
Jeff Brown6ec402b2010-07-28 15:48:59 -07002632#if DEBUG_INJECTION
Steve Block5baa3a62011-12-20 16:23:08 +00002633 ALOGD("injectInputEvent - Finished with result %d. "
Jeff Brown6ec402b2010-07-28 15:48:59 -07002634 "injectorPid=%d, injectorUid=%d",
2635 injectionResult, injectorPid, injectorUid);
2636#endif
2637
Jeff Brown7fbdc842010-06-17 20:52:56 -07002638 return injectionResult;
2639}
2640
Jeff Brownb6997262010-10-08 22:31:17 -07002641bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) {
2642 return injectorUid == 0
2643 || mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid);
2644}
2645
Jeff Brown7fbdc842010-06-17 20:52:56 -07002646void InputDispatcher::setInjectionResultLocked(EventEntry* entry, int32_t injectionResult) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002647 InjectionState* injectionState = entry->injectionState;
2648 if (injectionState) {
Jeff Brown7fbdc842010-06-17 20:52:56 -07002649#if DEBUG_INJECTION
Steve Block5baa3a62011-12-20 16:23:08 +00002650 ALOGD("Setting input event injection result to %d. "
Jeff Brown7fbdc842010-06-17 20:52:56 -07002651 "injectorPid=%d, injectorUid=%d",
Jeff Brown01ce2e92010-09-26 22:20:12 -07002652 injectionResult, injectionState->injectorPid, injectionState->injectorUid);
Jeff Brown7fbdc842010-06-17 20:52:56 -07002653#endif
2654
Jeff Brown0029c662011-03-30 02:25:18 -07002655 if (injectionState->injectionIsAsync
2656 && !(entry->policyFlags & POLICY_FLAG_FILTERED)) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07002657 // Log the outcome since the injector did not wait for the injection result.
2658 switch (injectionResult) {
2659 case INPUT_EVENT_INJECTION_SUCCEEDED:
Steve Block71f2cf12011-10-20 11:56:00 +01002660 ALOGV("Asynchronous input event injection succeeded.");
Jeff Brown6ec402b2010-07-28 15:48:59 -07002661 break;
2662 case INPUT_EVENT_INJECTION_FAILED:
Steve Block8564c8d2012-01-05 23:22:43 +00002663 ALOGW("Asynchronous input event injection failed.");
Jeff Brown6ec402b2010-07-28 15:48:59 -07002664 break;
2665 case INPUT_EVENT_INJECTION_PERMISSION_DENIED:
Steve Block8564c8d2012-01-05 23:22:43 +00002666 ALOGW("Asynchronous input event injection permission denied.");
Jeff Brown6ec402b2010-07-28 15:48:59 -07002667 break;
2668 case INPUT_EVENT_INJECTION_TIMED_OUT:
Steve Block8564c8d2012-01-05 23:22:43 +00002669 ALOGW("Asynchronous input event injection timed out.");
Jeff Brown6ec402b2010-07-28 15:48:59 -07002670 break;
2671 }
2672 }
2673
Jeff Brown01ce2e92010-09-26 22:20:12 -07002674 injectionState->injectionResult = injectionResult;
Jeff Brown7fbdc842010-06-17 20:52:56 -07002675 mInjectionResultAvailableCondition.broadcast();
2676 }
2677}
2678
Jeff Brown01ce2e92010-09-26 22:20:12 -07002679void InputDispatcher::incrementPendingForegroundDispatchesLocked(EventEntry* entry) {
2680 InjectionState* injectionState = entry->injectionState;
2681 if (injectionState) {
2682 injectionState->pendingForegroundDispatches += 1;
2683 }
2684}
2685
Jeff Brown519e0242010-09-15 15:18:56 -07002686void InputDispatcher::decrementPendingForegroundDispatchesLocked(EventEntry* entry) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002687 InjectionState* injectionState = entry->injectionState;
2688 if (injectionState) {
2689 injectionState->pendingForegroundDispatches -= 1;
Jeff Brown6ec402b2010-07-28 15:48:59 -07002690
Jeff Brown01ce2e92010-09-26 22:20:12 -07002691 if (injectionState->pendingForegroundDispatches == 0) {
2692 mInjectionSyncFinishedCondition.broadcast();
2693 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002694 }
2695}
2696
Jeff Brown9302c872011-07-13 22:51:29 -07002697sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked(
2698 const sp<InputChannel>& inputChannel) const {
2699 size_t numWindows = mWindowHandles.size();
2700 for (size_t i = 0; i < numWindows; i++) {
2701 const sp<InputWindowHandle>& windowHandle = mWindowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -07002702 if (windowHandle->getInputChannel() == inputChannel) {
Jeff Brown9302c872011-07-13 22:51:29 -07002703 return windowHandle;
Jeff Brown01ce2e92010-09-26 22:20:12 -07002704 }
2705 }
2706 return NULL;
2707}
2708
Jeff Brown9302c872011-07-13 22:51:29 -07002709bool InputDispatcher::hasWindowHandleLocked(
2710 const sp<InputWindowHandle>& windowHandle) const {
2711 size_t numWindows = mWindowHandles.size();
2712 for (size_t i = 0; i < numWindows; i++) {
2713 if (mWindowHandles.itemAt(i) == windowHandle) {
2714 return true;
2715 }
2716 }
2717 return false;
2718}
2719
2720void InputDispatcher::setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles) {
Jeff Brownb88102f2010-09-08 11:49:43 -07002721#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002722 ALOGD("setInputWindows");
Jeff Brownb88102f2010-09-08 11:49:43 -07002723#endif
2724 { // acquire lock
2725 AutoMutex _l(mLock);
2726
Jeff Browncc4f7db2011-08-30 20:34:48 -07002727 Vector<sp<InputWindowHandle> > oldWindowHandles = mWindowHandles;
Jeff Brown9302c872011-07-13 22:51:29 -07002728 mWindowHandles = inputWindowHandles;
Jeff Brownb6997262010-10-08 22:31:17 -07002729
Jeff Brown9302c872011-07-13 22:51:29 -07002730 sp<InputWindowHandle> newFocusedWindowHandle;
2731 bool foundHoveredWindow = false;
2732 for (size_t i = 0; i < mWindowHandles.size(); i++) {
2733 const sp<InputWindowHandle>& windowHandle = mWindowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -07002734 if (!windowHandle->updateInfo() || windowHandle->getInputChannel() == NULL) {
Jeff Brown9302c872011-07-13 22:51:29 -07002735 mWindowHandles.removeAt(i--);
2736 continue;
2737 }
Jeff Browncc4f7db2011-08-30 20:34:48 -07002738 if (windowHandle->getInfo()->hasFocus) {
Jeff Brown9302c872011-07-13 22:51:29 -07002739 newFocusedWindowHandle = windowHandle;
2740 }
2741 if (windowHandle == mLastHoverWindowHandle) {
2742 foundHoveredWindow = true;
Jeff Brownb88102f2010-09-08 11:49:43 -07002743 }
2744 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07002745
Jeff Brown9302c872011-07-13 22:51:29 -07002746 if (!foundHoveredWindow) {
2747 mLastHoverWindowHandle = NULL;
2748 }
2749
2750 if (mFocusedWindowHandle != newFocusedWindowHandle) {
2751 if (mFocusedWindowHandle != NULL) {
Jeff Brownb6997262010-10-08 22:31:17 -07002752#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002753 ALOGD("Focus left window: %s",
Jeff Browncc4f7db2011-08-30 20:34:48 -07002754 mFocusedWindowHandle->getName().string());
Jeff Brownb6997262010-10-08 22:31:17 -07002755#endif
Jeff Browncc4f7db2011-08-30 20:34:48 -07002756 sp<InputChannel> focusedInputChannel = mFocusedWindowHandle->getInputChannel();
2757 if (focusedInputChannel != NULL) {
Christopher Tated9be36c2011-08-16 16:09:33 -07002758 CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS,
2759 "focus left window");
2760 synthesizeCancelationEventsForInputChannelLocked(
Jeff Browncc4f7db2011-08-30 20:34:48 -07002761 focusedInputChannel, options);
Christopher Tated9be36c2011-08-16 16:09:33 -07002762 }
Jeff Brownb6997262010-10-08 22:31:17 -07002763 }
Jeff Brown9302c872011-07-13 22:51:29 -07002764 if (newFocusedWindowHandle != NULL) {
Jeff Brownb6997262010-10-08 22:31:17 -07002765#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002766 ALOGD("Focus entered window: %s",
Jeff Browncc4f7db2011-08-30 20:34:48 -07002767 newFocusedWindowHandle->getName().string());
Jeff Brownb6997262010-10-08 22:31:17 -07002768#endif
Jeff Brown9302c872011-07-13 22:51:29 -07002769 }
2770 mFocusedWindowHandle = newFocusedWindowHandle;
Jeff Brownb6997262010-10-08 22:31:17 -07002771 }
2772
Jeff Brown9302c872011-07-13 22:51:29 -07002773 for (size_t i = 0; i < mTouchState.windows.size(); i++) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07002774 TouchedWindow& touchedWindow = mTouchState.windows.editItemAt(i);
Jeff Brown9302c872011-07-13 22:51:29 -07002775 if (!hasWindowHandleLocked(touchedWindow.windowHandle)) {
Jeff Brownb6997262010-10-08 22:31:17 -07002776#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002777 ALOGD("Touched window was removed: %s",
Jeff Browncc4f7db2011-08-30 20:34:48 -07002778 touchedWindow.windowHandle->getName().string());
Jeff Brownb6997262010-10-08 22:31:17 -07002779#endif
Jeff Browncc4f7db2011-08-30 20:34:48 -07002780 sp<InputChannel> touchedInputChannel =
2781 touchedWindow.windowHandle->getInputChannel();
2782 if (touchedInputChannel != NULL) {
Christopher Tated9be36c2011-08-16 16:09:33 -07002783 CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS,
2784 "touched window was removed");
2785 synthesizeCancelationEventsForInputChannelLocked(
Jeff Browncc4f7db2011-08-30 20:34:48 -07002786 touchedInputChannel, options);
Christopher Tated9be36c2011-08-16 16:09:33 -07002787 }
Jeff Brown9302c872011-07-13 22:51:29 -07002788 mTouchState.windows.removeAt(i--);
Jeff Brown01ce2e92010-09-26 22:20:12 -07002789 }
2790 }
Jeff Browncc4f7db2011-08-30 20:34:48 -07002791
2792 // Release information for windows that are no longer present.
2793 // This ensures that unused input channels are released promptly.
2794 // Otherwise, they might stick around until the window handle is destroyed
2795 // which might not happen until the next GC.
2796 for (size_t i = 0; i < oldWindowHandles.size(); i++) {
2797 const sp<InputWindowHandle>& oldWindowHandle = oldWindowHandles.itemAt(i);
2798 if (!hasWindowHandleLocked(oldWindowHandle)) {
2799#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002800 ALOGD("Window went away: %s", oldWindowHandle->getName().string());
Jeff Browncc4f7db2011-08-30 20:34:48 -07002801#endif
2802 oldWindowHandle->releaseInfo();
2803 }
2804 }
Jeff Brownb88102f2010-09-08 11:49:43 -07002805 } // release lock
2806
2807 // Wake up poll loop since it may need to make new input dispatching choices.
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002808 mLooper->wake();
Jeff Brownb88102f2010-09-08 11:49:43 -07002809}
2810
Jeff Brown9302c872011-07-13 22:51:29 -07002811void InputDispatcher::setFocusedApplication(
2812 const sp<InputApplicationHandle>& inputApplicationHandle) {
Jeff Brownb88102f2010-09-08 11:49:43 -07002813#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002814 ALOGD("setFocusedApplication");
Jeff Brownb88102f2010-09-08 11:49:43 -07002815#endif
2816 { // acquire lock
2817 AutoMutex _l(mLock);
2818
Jeff Browncc4f7db2011-08-30 20:34:48 -07002819 if (inputApplicationHandle != NULL && inputApplicationHandle->updateInfo()) {
Jeff Brown5ea29ab2011-07-27 11:50:51 -07002820 if (mFocusedApplicationHandle != inputApplicationHandle) {
2821 if (mFocusedApplicationHandle != NULL) {
2822 resetTargetsLocked();
Jeff Browncc4f7db2011-08-30 20:34:48 -07002823 mFocusedApplicationHandle->releaseInfo();
Jeff Brown5ea29ab2011-07-27 11:50:51 -07002824 }
2825 mFocusedApplicationHandle = inputApplicationHandle;
2826 }
2827 } else if (mFocusedApplicationHandle != NULL) {
2828 resetTargetsLocked();
Jeff Browncc4f7db2011-08-30 20:34:48 -07002829 mFocusedApplicationHandle->releaseInfo();
Jeff Brown9302c872011-07-13 22:51:29 -07002830 mFocusedApplicationHandle.clear();
Jeff Brownb88102f2010-09-08 11:49:43 -07002831 }
2832
2833#if DEBUG_FOCUS
Jeff Brownb6997262010-10-08 22:31:17 -07002834 //logDispatchStateLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -07002835#endif
2836 } // release lock
2837
2838 // Wake up poll loop since it may need to make new input dispatching choices.
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002839 mLooper->wake();
Jeff Brownb88102f2010-09-08 11:49:43 -07002840}
2841
Jeff Brownb88102f2010-09-08 11:49:43 -07002842void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) {
2843#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002844 ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen);
Jeff Brownb88102f2010-09-08 11:49:43 -07002845#endif
2846
2847 bool changed;
2848 { // acquire lock
2849 AutoMutex _l(mLock);
2850
2851 if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) {
Jeff Brown120a4592010-10-27 18:43:51 -07002852 if (mDispatchFrozen && !frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -07002853 resetANRTimeoutsLocked();
2854 }
2855
Jeff Brown120a4592010-10-27 18:43:51 -07002856 if (mDispatchEnabled && !enabled) {
2857 resetAndDropEverythingLocked("dispatcher is being disabled");
2858 }
2859
Jeff Brownb88102f2010-09-08 11:49:43 -07002860 mDispatchEnabled = enabled;
2861 mDispatchFrozen = frozen;
2862 changed = true;
2863 } else {
2864 changed = false;
2865 }
2866
2867#if DEBUG_FOCUS
Jeff Brownb6997262010-10-08 22:31:17 -07002868 //logDispatchStateLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -07002869#endif
2870 } // release lock
2871
2872 if (changed) {
2873 // Wake up poll loop since it may need to make new input dispatching choices.
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07002874 mLooper->wake();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002875 }
2876}
2877
Jeff Brown0029c662011-03-30 02:25:18 -07002878void InputDispatcher::setInputFilterEnabled(bool enabled) {
2879#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002880 ALOGD("setInputFilterEnabled: enabled=%d", enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07002881#endif
2882
2883 { // acquire lock
2884 AutoMutex _l(mLock);
2885
2886 if (mInputFilterEnabled == enabled) {
2887 return;
2888 }
2889
2890 mInputFilterEnabled = enabled;
2891 resetAndDropEverythingLocked("input filter is being enabled or disabled");
2892 } // release lock
2893
2894 // Wake up poll loop since there might be work to do to drop everything.
2895 mLooper->wake();
2896}
2897
Jeff Browne6504122010-09-27 14:52:15 -07002898bool InputDispatcher::transferTouchFocus(const sp<InputChannel>& fromChannel,
2899 const sp<InputChannel>& toChannel) {
2900#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002901 ALOGD("transferTouchFocus: fromChannel=%s, toChannel=%s",
Jeff Browne6504122010-09-27 14:52:15 -07002902 fromChannel->getName().string(), toChannel->getName().string());
2903#endif
2904 { // acquire lock
2905 AutoMutex _l(mLock);
2906
Jeff Brown9302c872011-07-13 22:51:29 -07002907 sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromChannel);
2908 sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toChannel);
2909 if (fromWindowHandle == NULL || toWindowHandle == NULL) {
Jeff Browne6504122010-09-27 14:52:15 -07002910#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002911 ALOGD("Cannot transfer focus because from or to window not found.");
Jeff Browne6504122010-09-27 14:52:15 -07002912#endif
2913 return false;
2914 }
Jeff Brown9302c872011-07-13 22:51:29 -07002915 if (fromWindowHandle == toWindowHandle) {
Jeff Browne6504122010-09-27 14:52:15 -07002916#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002917 ALOGD("Trivial transfer to same window.");
Jeff Browne6504122010-09-27 14:52:15 -07002918#endif
2919 return true;
2920 }
2921
2922 bool found = false;
2923 for (size_t i = 0; i < mTouchState.windows.size(); i++) {
2924 const TouchedWindow& touchedWindow = mTouchState.windows[i];
Jeff Brown9302c872011-07-13 22:51:29 -07002925 if (touchedWindow.windowHandle == fromWindowHandle) {
Jeff Browne6504122010-09-27 14:52:15 -07002926 int32_t oldTargetFlags = touchedWindow.targetFlags;
2927 BitSet32 pointerIds = touchedWindow.pointerIds;
2928
2929 mTouchState.windows.removeAt(i);
2930
Jeff Brown46e75292010-11-10 16:53:45 -08002931 int32_t newTargetFlags = oldTargetFlags
Jeff Browna032cc02011-03-07 16:56:21 -08002932 & (InputTarget::FLAG_FOREGROUND
2933 | InputTarget::FLAG_SPLIT | InputTarget::FLAG_DISPATCH_AS_IS);
Jeff Brown9302c872011-07-13 22:51:29 -07002934 mTouchState.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds);
Jeff Browne6504122010-09-27 14:52:15 -07002935
2936 found = true;
2937 break;
2938 }
2939 }
2940
2941 if (! found) {
2942#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002943 ALOGD("Focus transfer failed because from window did not have focus.");
Jeff Browne6504122010-09-27 14:52:15 -07002944#endif
2945 return false;
2946 }
2947
Jeff Brown9c9f1a32010-10-11 18:32:20 -07002948 ssize_t fromConnectionIndex = getConnectionIndexLocked(fromChannel);
2949 ssize_t toConnectionIndex = getConnectionIndexLocked(toChannel);
2950 if (fromConnectionIndex >= 0 && toConnectionIndex >= 0) {
Jeff Browncbee6d62012-02-03 20:11:27 -08002951 sp<Connection> fromConnection = mConnectionsByFd.valueAt(fromConnectionIndex);
2952 sp<Connection> toConnection = mConnectionsByFd.valueAt(toConnectionIndex);
Jeff Brown9c9f1a32010-10-11 18:32:20 -07002953
2954 fromConnection->inputState.copyPointerStateTo(toConnection->inputState);
Jeff Brownda3d5a92011-03-29 15:11:34 -07002955 CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS,
Jeff Brown9c9f1a32010-10-11 18:32:20 -07002956 "transferring touch focus from this window to another window");
Jeff Brownda3d5a92011-03-29 15:11:34 -07002957 synthesizeCancelationEventsForConnectionLocked(fromConnection, options);
Jeff Brown9c9f1a32010-10-11 18:32:20 -07002958 }
2959
Jeff Browne6504122010-09-27 14:52:15 -07002960#if DEBUG_FOCUS
2961 logDispatchStateLocked();
2962#endif
2963 } // release lock
2964
2965 // Wake up poll loop since it may need to make new input dispatching choices.
2966 mLooper->wake();
2967 return true;
2968}
2969
Jeff Brown120a4592010-10-27 18:43:51 -07002970void InputDispatcher::resetAndDropEverythingLocked(const char* reason) {
2971#if DEBUG_FOCUS
Steve Block5baa3a62011-12-20 16:23:08 +00002972 ALOGD("Resetting and dropping all events (%s).", reason);
Jeff Brown120a4592010-10-27 18:43:51 -07002973#endif
2974
Jeff Brownda3d5a92011-03-29 15:11:34 -07002975 CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason);
2976 synthesizeCancelationEventsForAllConnectionsLocked(options);
Jeff Brown120a4592010-10-27 18:43:51 -07002977
2978 resetKeyRepeatLocked();
2979 releasePendingEventLocked();
2980 drainInboundQueueLocked();
2981 resetTargetsLocked();
2982
2983 mTouchState.reset();
Jeff Brown9302c872011-07-13 22:51:29 -07002984 mLastHoverWindowHandle.clear();
Jeff Brown120a4592010-10-27 18:43:51 -07002985}
2986
Jeff Brownb88102f2010-09-08 11:49:43 -07002987void InputDispatcher::logDispatchStateLocked() {
2988 String8 dump;
2989 dumpDispatchStateLocked(dump);
Jeff Brown2a95c2a2010-09-16 12:31:46 -07002990
2991 char* text = dump.lockBuffer(dump.size());
2992 char* start = text;
2993 while (*start != '\0') {
2994 char* end = strchr(start, '\n');
2995 if (*end == '\n') {
2996 *(end++) = '\0';
2997 }
Steve Block5baa3a62011-12-20 16:23:08 +00002998 ALOGD("%s", start);
Jeff Brown2a95c2a2010-09-16 12:31:46 -07002999 start = end;
3000 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003001}
3002
3003void InputDispatcher::dumpDispatchStateLocked(String8& dump) {
Jeff Brownf2f487182010-10-01 17:46:21 -07003004 dump.appendFormat(INDENT "DispatchEnabled: %d\n", mDispatchEnabled);
3005 dump.appendFormat(INDENT "DispatchFrozen: %d\n", mDispatchFrozen);
Jeff Brownb88102f2010-09-08 11:49:43 -07003006
Jeff Brown9302c872011-07-13 22:51:29 -07003007 if (mFocusedApplicationHandle != NULL) {
Jeff Brownf2f487182010-10-01 17:46:21 -07003008 dump.appendFormat(INDENT "FocusedApplication: name='%s', dispatchingTimeout=%0.3fms\n",
Jeff Browncc4f7db2011-08-30 20:34:48 -07003009 mFocusedApplicationHandle->getName().string(),
3010 mFocusedApplicationHandle->getDispatchingTimeout(
3011 DEFAULT_INPUT_DISPATCHING_TIMEOUT) / 1000000.0);
Jeff Brownb88102f2010-09-08 11:49:43 -07003012 } else {
Jeff Brownf2f487182010-10-01 17:46:21 -07003013 dump.append(INDENT "FocusedApplication: <null>\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003014 }
Jeff Brownf2f487182010-10-01 17:46:21 -07003015 dump.appendFormat(INDENT "FocusedWindow: name='%s'\n",
Jeff Browncc4f7db2011-08-30 20:34:48 -07003016 mFocusedWindowHandle != NULL ? mFocusedWindowHandle->getName().string() : "<null>");
Jeff Brownf2f487182010-10-01 17:46:21 -07003017
3018 dump.appendFormat(INDENT "TouchDown: %s\n", toString(mTouchState.down));
3019 dump.appendFormat(INDENT "TouchSplit: %s\n", toString(mTouchState.split));
Jeff Brown95712852011-01-04 19:41:59 -08003020 dump.appendFormat(INDENT "TouchDeviceId: %d\n", mTouchState.deviceId);
Jeff Brown58a2da82011-01-25 16:02:22 -08003021 dump.appendFormat(INDENT "TouchSource: 0x%08x\n", mTouchState.source);
Jeff Brownf2f487182010-10-01 17:46:21 -07003022 if (!mTouchState.windows.isEmpty()) {
3023 dump.append(INDENT "TouchedWindows:\n");
3024 for (size_t i = 0; i < mTouchState.windows.size(); i++) {
3025 const TouchedWindow& touchedWindow = mTouchState.windows[i];
3026 dump.appendFormat(INDENT2 "%d: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n",
Jeff Browncc4f7db2011-08-30 20:34:48 -07003027 i, touchedWindow.windowHandle->getName().string(),
3028 touchedWindow.pointerIds.value,
Jeff Brownf2f487182010-10-01 17:46:21 -07003029 touchedWindow.targetFlags);
3030 }
3031 } else {
3032 dump.append(INDENT "TouchedWindows: <none>\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003033 }
3034
Jeff Brown9302c872011-07-13 22:51:29 -07003035 if (!mWindowHandles.isEmpty()) {
Jeff Brownf2f487182010-10-01 17:46:21 -07003036 dump.append(INDENT "Windows:\n");
Jeff Brown9302c872011-07-13 22:51:29 -07003037 for (size_t i = 0; i < mWindowHandles.size(); i++) {
3038 const sp<InputWindowHandle>& windowHandle = mWindowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -07003039 const InputWindowInfo* windowInfo = windowHandle->getInfo();
3040
Jeff Brownf2f487182010-10-01 17:46:21 -07003041 dump.appendFormat(INDENT2 "%d: name='%s', paused=%s, hasFocus=%s, hasWallpaper=%s, "
3042 "visible=%s, canReceiveKeys=%s, flags=0x%08x, type=0x%08x, layer=%d, "
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003043 "frame=[%d,%d][%d,%d], scale=%f, "
Jeff Brownfbf09772011-01-16 14:06:57 -08003044 "touchableRegion=",
Jeff Browncc4f7db2011-08-30 20:34:48 -07003045 i, windowInfo->name.string(),
3046 toString(windowInfo->paused),
3047 toString(windowInfo->hasFocus),
3048 toString(windowInfo->hasWallpaper),
3049 toString(windowInfo->visible),
3050 toString(windowInfo->canReceiveKeys),
3051 windowInfo->layoutParamsFlags, windowInfo->layoutParamsType,
3052 windowInfo->layer,
3053 windowInfo->frameLeft, windowInfo->frameTop,
3054 windowInfo->frameRight, windowInfo->frameBottom,
3055 windowInfo->scaleFactor);
3056 dumpRegion(dump, windowInfo->touchableRegion);
3057 dump.appendFormat(", inputFeatures=0x%08x", windowInfo->inputFeatures);
Jeff Brownfbf09772011-01-16 14:06:57 -08003058 dump.appendFormat(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%0.3fms\n",
Jeff Browncc4f7db2011-08-30 20:34:48 -07003059 windowInfo->ownerPid, windowInfo->ownerUid,
3060 windowInfo->dispatchingTimeout / 1000000.0);
Jeff Brownf2f487182010-10-01 17:46:21 -07003061 }
3062 } else {
3063 dump.append(INDENT "Windows: <none>\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003064 }
3065
Jeff Brownf2f487182010-10-01 17:46:21 -07003066 if (!mMonitoringChannels.isEmpty()) {
3067 dump.append(INDENT "MonitoringChannels:\n");
3068 for (size_t i = 0; i < mMonitoringChannels.size(); i++) {
3069 const sp<InputChannel>& channel = mMonitoringChannels[i];
3070 dump.appendFormat(INDENT2 "%d: '%s'\n", i, channel->getName().string());
3071 }
3072 } else {
3073 dump.append(INDENT "MonitoringChannels: <none>\n");
3074 }
Jeff Brown519e0242010-09-15 15:18:56 -07003075
Jeff Brownf2f487182010-10-01 17:46:21 -07003076 dump.appendFormat(INDENT "InboundQueue: length=%u\n", mInboundQueue.count());
3077
3078 if (!mActiveConnections.isEmpty()) {
3079 dump.append(INDENT "ActiveConnections:\n");
3080 for (size_t i = 0; i < mActiveConnections.size(); i++) {
3081 const Connection* connection = mActiveConnections[i];
Jeff Brown76860e32010-10-25 17:37:46 -07003082 dump.appendFormat(INDENT2 "%d: '%s', status=%s, outboundQueueLength=%u, "
Jeff Brownb6997262010-10-08 22:31:17 -07003083 "inputState.isNeutral=%s\n",
Jeff Brownf2f487182010-10-01 17:46:21 -07003084 i, connection->getInputChannelName(), connection->getStatusLabel(),
3085 connection->outboundQueue.count(),
Jeff Brownb6997262010-10-08 22:31:17 -07003086 toString(connection->inputState.isNeutral()));
Jeff Brownf2f487182010-10-01 17:46:21 -07003087 }
3088 } else {
3089 dump.append(INDENT "ActiveConnections: <none>\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003090 }
3091
3092 if (isAppSwitchPendingLocked()) {
Jeff Brownf2f487182010-10-01 17:46:21 -07003093 dump.appendFormat(INDENT "AppSwitch: pending, due in %01.1fms\n",
Jeff Brownb88102f2010-09-08 11:49:43 -07003094 (mAppSwitchDueTime - now()) / 1000000.0);
3095 } else {
Jeff Brownf2f487182010-10-01 17:46:21 -07003096 dump.append(INDENT "AppSwitch: not pending\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003097 }
3098}
3099
Jeff Brown928e0542011-01-10 11:17:36 -08003100status_t InputDispatcher::registerInputChannel(const sp<InputChannel>& inputChannel,
3101 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
Jeff Brown9c3cda02010-06-15 01:31:58 -07003102#if DEBUG_REGISTRATION
Steve Block5baa3a62011-12-20 16:23:08 +00003103 ALOGD("channel '%s' ~ registerInputChannel - monitor=%s", inputChannel->getName().string(),
Jeff Brownb88102f2010-09-08 11:49:43 -07003104 toString(monitor));
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 Brown519e0242010-09-15 15:18:56 -07003110 if (getConnectionIndexLocked(inputChannel) >= 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003111 ALOGW("Attempted to register already registered input channel '%s'",
Jeff Brown46b9ac02010-04-22 18:58:52 -07003112 inputChannel->getName().string());
3113 return BAD_VALUE;
3114 }
3115
Jeff Browncc4f7db2011-08-30 20:34:48 -07003116 sp<Connection> connection = new Connection(inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003117
Jeff Browncbee6d62012-02-03 20:11:27 -08003118 int32_t fd = inputChannel->getFd();
3119 mConnectionsByFd.add(fd, connection);
Jeff Brown9c3cda02010-06-15 01:31:58 -07003120
Jeff Brownb88102f2010-09-08 11:49:43 -07003121 if (monitor) {
3122 mMonitoringChannels.push(inputChannel);
3123 }
3124
Jeff Browncbee6d62012-02-03 20:11:27 -08003125 mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this);
Jeff Brown2cbecea2010-08-17 15:59:26 -07003126
Jeff Brown9c3cda02010-06-15 01:31:58 -07003127 runCommandsLockedInterruptible();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003128 } // release lock
Jeff Brown46b9ac02010-04-22 18:58:52 -07003129 return OK;
3130}
3131
3132status_t InputDispatcher::unregisterInputChannel(const sp<InputChannel>& inputChannel) {
Jeff Brown9c3cda02010-06-15 01:31:58 -07003133#if DEBUG_REGISTRATION
Steve Block5baa3a62011-12-20 16:23:08 +00003134 ALOGD("channel '%s' ~ unregisterInputChannel", inputChannel->getName().string());
Jeff Brown9c3cda02010-06-15 01:31:58 -07003135#endif
3136
Jeff Brown46b9ac02010-04-22 18:58:52 -07003137 { // acquire lock
3138 AutoMutex _l(mLock);
3139
Jeff Browncc4f7db2011-08-30 20:34:48 -07003140 status_t status = unregisterInputChannelLocked(inputChannel, false /*notify*/);
3141 if (status) {
3142 return status;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003143 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003144 } // release lock
3145
Jeff Brown46b9ac02010-04-22 18:58:52 -07003146 // Wake the poll loop because removing the connection may have changed the current
3147 // synchronization state.
Jeff Brown4fe6c3e2010-09-13 23:17:30 -07003148 mLooper->wake();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003149 return OK;
3150}
3151
Jeff Browncc4f7db2011-08-30 20:34:48 -07003152status_t InputDispatcher::unregisterInputChannelLocked(const sp<InputChannel>& inputChannel,
3153 bool notify) {
3154 ssize_t connectionIndex = getConnectionIndexLocked(inputChannel);
3155 if (connectionIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00003156 ALOGW("Attempted to unregister already unregistered input channel '%s'",
Jeff Browncc4f7db2011-08-30 20:34:48 -07003157 inputChannel->getName().string());
3158 return BAD_VALUE;
3159 }
3160
Jeff Browncbee6d62012-02-03 20:11:27 -08003161 sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex);
3162 mConnectionsByFd.removeItemsAt(connectionIndex);
Jeff Browncc4f7db2011-08-30 20:34:48 -07003163
3164 if (connection->monitor) {
3165 removeMonitorChannelLocked(inputChannel);
3166 }
3167
Jeff Browncbee6d62012-02-03 20:11:27 -08003168 mLooper->removeFd(inputChannel->getFd());
Jeff Browncc4f7db2011-08-30 20:34:48 -07003169
3170 nsecs_t currentTime = now();
3171 abortBrokenDispatchCycleLocked(currentTime, connection, notify);
3172
3173 runCommandsLockedInterruptible();
3174
3175 connection->status = Connection::STATUS_ZOMBIE;
3176 return OK;
3177}
3178
3179void InputDispatcher::removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) {
3180 for (size_t i = 0; i < mMonitoringChannels.size(); i++) {
3181 if (mMonitoringChannels[i] == inputChannel) {
3182 mMonitoringChannels.removeAt(i);
3183 break;
3184 }
3185 }
3186}
3187
Jeff Brown519e0242010-09-15 15:18:56 -07003188ssize_t InputDispatcher::getConnectionIndexLocked(const sp<InputChannel>& inputChannel) {
Jeff Browncbee6d62012-02-03 20:11:27 -08003189 ssize_t connectionIndex = mConnectionsByFd.indexOfKey(inputChannel->getFd());
Jeff Brown2cbecea2010-08-17 15:59:26 -07003190 if (connectionIndex >= 0) {
Jeff Browncbee6d62012-02-03 20:11:27 -08003191 sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex);
Jeff Brown2cbecea2010-08-17 15:59:26 -07003192 if (connection->inputChannel.get() == inputChannel.get()) {
3193 return connectionIndex;
3194 }
3195 }
3196
3197 return -1;
3198}
3199
Jeff Brown46b9ac02010-04-22 18:58:52 -07003200void InputDispatcher::activateConnectionLocked(Connection* connection) {
3201 for (size_t i = 0; i < mActiveConnections.size(); i++) {
3202 if (mActiveConnections.itemAt(i) == connection) {
3203 return;
3204 }
3205 }
3206 mActiveConnections.add(connection);
3207}
3208
3209void InputDispatcher::deactivateConnectionLocked(Connection* connection) {
3210 for (size_t i = 0; i < mActiveConnections.size(); i++) {
3211 if (mActiveConnections.itemAt(i) == connection) {
3212 mActiveConnections.removeAt(i);
3213 return;
3214 }
3215 }
3216}
3217
Jeff Brown9c3cda02010-06-15 01:31:58 -07003218void InputDispatcher::onDispatchCycleStartedLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07003219 nsecs_t currentTime, const sp<Connection>& connection) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003220}
3221
Jeff Brown9c3cda02010-06-15 01:31:58 -07003222void InputDispatcher::onDispatchCycleFinishedLocked(
Jeff Brown3915bb82010-11-05 15:02:16 -07003223 nsecs_t currentTime, const sp<Connection>& connection, bool handled) {
3224 CommandEntry* commandEntry = postCommandLocked(
3225 & InputDispatcher::doDispatchCycleFinishedLockedInterruptible);
3226 commandEntry->connection = connection;
3227 commandEntry->handled = handled;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003228}
3229
Jeff Brown9c3cda02010-06-15 01:31:58 -07003230void InputDispatcher::onDispatchCycleBrokenLocked(
Jeff Brown7fbdc842010-06-17 20:52:56 -07003231 nsecs_t currentTime, const sp<Connection>& connection) {
Steve Block3762c312012-01-06 19:20:56 +00003232 ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!",
Jeff Brown46b9ac02010-04-22 18:58:52 -07003233 connection->getInputChannelName());
3234
Jeff Brown9c3cda02010-06-15 01:31:58 -07003235 CommandEntry* commandEntry = postCommandLocked(
3236 & InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible);
Jeff Brown7fbdc842010-06-17 20:52:56 -07003237 commandEntry->connection = connection;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003238}
3239
Jeff Brown519e0242010-09-15 15:18:56 -07003240void InputDispatcher::onANRLocked(
Jeff Brown9302c872011-07-13 22:51:29 -07003241 nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
3242 const sp<InputWindowHandle>& windowHandle,
Jeff Brown519e0242010-09-15 15:18:56 -07003243 nsecs_t eventTime, nsecs_t waitStartTime) {
Steve Block6215d3f2012-01-04 20:05:49 +00003244 ALOGI("Application is not responding: %s. "
Jeff Brown519e0242010-09-15 15:18:56 -07003245 "%01.1fms since event, %01.1fms since wait started",
Jeff Brown9302c872011-07-13 22:51:29 -07003246 getApplicationWindowLabelLocked(applicationHandle, windowHandle).string(),
Jeff Brown519e0242010-09-15 15:18:56 -07003247 (currentTime - eventTime) / 1000000.0,
3248 (currentTime - waitStartTime) / 1000000.0);
3249
3250 CommandEntry* commandEntry = postCommandLocked(
3251 & InputDispatcher::doNotifyANRLockedInterruptible);
Jeff Brown9302c872011-07-13 22:51:29 -07003252 commandEntry->inputApplicationHandle = applicationHandle;
3253 commandEntry->inputWindowHandle = windowHandle;
Jeff Brown519e0242010-09-15 15:18:56 -07003254}
3255
Jeff Brownb88102f2010-09-08 11:49:43 -07003256void InputDispatcher::doNotifyConfigurationChangedInterruptible(
3257 CommandEntry* commandEntry) {
3258 mLock.unlock();
3259
3260 mPolicy->notifyConfigurationChanged(commandEntry->eventTime);
3261
3262 mLock.lock();
3263}
3264
Jeff Brown9c3cda02010-06-15 01:31:58 -07003265void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible(
3266 CommandEntry* commandEntry) {
Jeff Brown7fbdc842010-06-17 20:52:56 -07003267 sp<Connection> connection = commandEntry->connection;
Jeff Brown9c3cda02010-06-15 01:31:58 -07003268
Jeff Brown7fbdc842010-06-17 20:52:56 -07003269 if (connection->status != Connection::STATUS_ZOMBIE) {
3270 mLock.unlock();
Jeff Brown9c3cda02010-06-15 01:31:58 -07003271
Jeff Brown928e0542011-01-10 11:17:36 -08003272 mPolicy->notifyInputChannelBroken(connection->inputWindowHandle);
Jeff Brown7fbdc842010-06-17 20:52:56 -07003273
3274 mLock.lock();
3275 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07003276}
3277
Jeff Brown519e0242010-09-15 15:18:56 -07003278void InputDispatcher::doNotifyANRLockedInterruptible(
Jeff Brown9c3cda02010-06-15 01:31:58 -07003279 CommandEntry* commandEntry) {
Jeff Brown519e0242010-09-15 15:18:56 -07003280 mLock.unlock();
Jeff Brown9c3cda02010-06-15 01:31:58 -07003281
Jeff Brown519e0242010-09-15 15:18:56 -07003282 nsecs_t newTimeout = mPolicy->notifyANR(
Jeff Brown928e0542011-01-10 11:17:36 -08003283 commandEntry->inputApplicationHandle, commandEntry->inputWindowHandle);
Jeff Brown9c3cda02010-06-15 01:31:58 -07003284
Jeff Brown519e0242010-09-15 15:18:56 -07003285 mLock.lock();
Jeff Brown7fbdc842010-06-17 20:52:56 -07003286
Jeff Brown9302c872011-07-13 22:51:29 -07003287 resumeAfterTargetsNotReadyTimeoutLocked(newTimeout,
3288 commandEntry->inputWindowHandle != NULL
Jeff Browncc4f7db2011-08-30 20:34:48 -07003289 ? commandEntry->inputWindowHandle->getInputChannel() : NULL);
Jeff Brown9c3cda02010-06-15 01:31:58 -07003290}
3291
Jeff Brownb88102f2010-09-08 11:49:43 -07003292void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible(
3293 CommandEntry* commandEntry) {
3294 KeyEntry* entry = commandEntry->keyEntry;
Jeff Brown1f245102010-11-18 20:53:46 -08003295
3296 KeyEvent event;
3297 initializeKeyEvent(&event, entry);
Jeff Brownb88102f2010-09-08 11:49:43 -07003298
3299 mLock.unlock();
3300
Jeff Brown905805a2011-10-12 13:57:59 -07003301 nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(commandEntry->inputWindowHandle,
Jeff Brown1f245102010-11-18 20:53:46 -08003302 &event, entry->policyFlags);
Jeff Brownb88102f2010-09-08 11:49:43 -07003303
3304 mLock.lock();
3305
Jeff Brown905805a2011-10-12 13:57:59 -07003306 if (delay < 0) {
3307 entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP;
3308 } else if (!delay) {
3309 entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE;
3310 } else {
3311 entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER;
3312 entry->interceptKeyWakeupTime = now() + delay;
3313 }
Jeff Brownac386072011-07-20 15:19:50 -07003314 entry->release();
Jeff Brownb88102f2010-09-08 11:49:43 -07003315}
3316
Jeff Brown3915bb82010-11-05 15:02:16 -07003317void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(
3318 CommandEntry* commandEntry) {
3319 sp<Connection> connection = commandEntry->connection;
3320 bool handled = commandEntry->handled;
3321
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003322 bool skipNext = false;
Jeff Brown49ed71d2010-12-06 17:13:33 -08003323 if (!connection->outboundQueue.isEmpty()) {
Jeff Brownac386072011-07-20 15:19:50 -07003324 DispatchEntry* dispatchEntry = connection->outboundQueue.head;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003325 if (dispatchEntry->inProgress) {
3326 if (dispatchEntry->eventEntry->type == EventEntry::TYPE_KEY) {
3327 KeyEntry* keyEntry = static_cast<KeyEntry*>(dispatchEntry->eventEntry);
3328 skipNext = afterKeyEventLockedInterruptible(connection,
3329 dispatchEntry, keyEntry, handled);
3330 } else if (dispatchEntry->eventEntry->type == EventEntry::TYPE_MOTION) {
3331 MotionEntry* motionEntry = static_cast<MotionEntry*>(dispatchEntry->eventEntry);
3332 skipNext = afterMotionEventLockedInterruptible(connection,
3333 dispatchEntry, motionEntry, handled);
Jeff Brown49ed71d2010-12-06 17:13:33 -08003334 }
Jeff Brown3915bb82010-11-05 15:02:16 -07003335 }
3336 }
3337
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003338 if (!skipNext) {
3339 startNextDispatchCycleLocked(now(), connection);
3340 }
3341}
3342
3343bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection,
3344 DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled) {
3345 if (!(keyEntry->flags & AKEY_EVENT_FLAG_FALLBACK)) {
3346 // Get the fallback key state.
3347 // Clear it out after dispatching the UP.
3348 int32_t originalKeyCode = keyEntry->keyCode;
3349 int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode);
3350 if (keyEntry->action == AKEY_EVENT_ACTION_UP) {
3351 connection->inputState.removeFallbackKey(originalKeyCode);
3352 }
3353
3354 if (handled || !dispatchEntry->hasForegroundTarget()) {
3355 // If the application handles the original key for which we previously
3356 // generated a fallback or if the window is not a foreground window,
3357 // then cancel the associated fallback key, if any.
3358 if (fallbackKeyCode != -1) {
3359 if (fallbackKeyCode != AKEYCODE_UNKNOWN) {
3360 CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS,
3361 "application handled the original non-fallback key "
3362 "or is no longer a foreground target, "
3363 "canceling previously dispatched fallback key");
3364 options.keyCode = fallbackKeyCode;
3365 synthesizeCancelationEventsForConnectionLocked(connection, options);
3366 }
3367 connection->inputState.removeFallbackKey(originalKeyCode);
3368 }
3369 } else {
3370 // If the application did not handle a non-fallback key, first check
3371 // that we are in a good state to perform unhandled key event processing
3372 // Then ask the policy what to do with it.
3373 bool initialDown = keyEntry->action == AKEY_EVENT_ACTION_DOWN
3374 && keyEntry->repeatCount == 0;
3375 if (fallbackKeyCode == -1 && !initialDown) {
3376#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003377 ALOGD("Unhandled key event: Skipping unhandled key event processing "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003378 "since this is not an initial down. "
3379 "keyCode=%d, action=%d, repeatCount=%d",
3380 originalKeyCode, keyEntry->action, keyEntry->repeatCount);
3381#endif
3382 return false;
3383 }
3384
3385 // Dispatch the unhandled key to the policy.
3386#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003387 ALOGD("Unhandled key event: Asking policy to perform fallback action. "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003388 "keyCode=%d, action=%d, repeatCount=%d",
3389 keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount);
3390#endif
3391 KeyEvent event;
3392 initializeKeyEvent(&event, keyEntry);
3393
3394 mLock.unlock();
3395
3396 bool fallback = mPolicy->dispatchUnhandledKey(connection->inputWindowHandle,
3397 &event, keyEntry->policyFlags, &event);
3398
3399 mLock.lock();
3400
3401 if (connection->status != Connection::STATUS_NORMAL) {
3402 connection->inputState.removeFallbackKey(originalKeyCode);
3403 return true; // skip next cycle
3404 }
3405
Steve Blockec193de2012-01-09 18:35:44 +00003406 ALOG_ASSERT(connection->outboundQueue.head == dispatchEntry);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003407
3408 // Latch the fallback keycode for this key on an initial down.
3409 // The fallback keycode cannot change at any other point in the lifecycle.
3410 if (initialDown) {
3411 if (fallback) {
3412 fallbackKeyCode = event.getKeyCode();
3413 } else {
3414 fallbackKeyCode = AKEYCODE_UNKNOWN;
3415 }
3416 connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode);
3417 }
3418
Steve Blockec193de2012-01-09 18:35:44 +00003419 ALOG_ASSERT(fallbackKeyCode != -1);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003420
3421 // Cancel the fallback key if the policy decides not to send it anymore.
3422 // We will continue to dispatch the key to the policy but we will no
3423 // longer dispatch a fallback key to the application.
3424 if (fallbackKeyCode != AKEYCODE_UNKNOWN
3425 && (!fallback || fallbackKeyCode != event.getKeyCode())) {
3426#if DEBUG_OUTBOUND_EVENT_DETAILS
3427 if (fallback) {
Steve Block5baa3a62011-12-20 16:23:08 +00003428 ALOGD("Unhandled key event: Policy requested to send key %d"
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003429 "as a fallback for %d, but on the DOWN it had requested "
3430 "to send %d instead. Fallback canceled.",
3431 event.getKeyCode(), originalKeyCode, fallbackKeyCode);
3432 } else {
Steve Block5baa3a62011-12-20 16:23:08 +00003433 ALOGD("Unhandled key event: Policy did not request fallback for %d,"
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003434 "but on the DOWN it had requested to send %d. "
3435 "Fallback canceled.",
3436 originalKeyCode, fallbackKeyCode);
3437 }
3438#endif
3439
3440 CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS,
3441 "canceling fallback, policy no longer desires it");
3442 options.keyCode = fallbackKeyCode;
3443 synthesizeCancelationEventsForConnectionLocked(connection, options);
3444
3445 fallback = false;
3446 fallbackKeyCode = AKEYCODE_UNKNOWN;
3447 if (keyEntry->action != AKEY_EVENT_ACTION_UP) {
3448 connection->inputState.setFallbackKey(originalKeyCode,
3449 fallbackKeyCode);
3450 }
3451 }
3452
3453#if DEBUG_OUTBOUND_EVENT_DETAILS
3454 {
3455 String8 msg;
3456 const KeyedVector<int32_t, int32_t>& fallbackKeys =
3457 connection->inputState.getFallbackKeys();
3458 for (size_t i = 0; i < fallbackKeys.size(); i++) {
3459 msg.appendFormat(", %d->%d", fallbackKeys.keyAt(i),
3460 fallbackKeys.valueAt(i));
3461 }
Steve Block5baa3a62011-12-20 16:23:08 +00003462 ALOGD("Unhandled key event: %d currently tracked fallback keys%s.",
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003463 fallbackKeys.size(), msg.string());
3464 }
3465#endif
3466
3467 if (fallback) {
3468 // Restart the dispatch cycle using the fallback key.
3469 keyEntry->eventTime = event.getEventTime();
3470 keyEntry->deviceId = event.getDeviceId();
3471 keyEntry->source = event.getSource();
3472 keyEntry->flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK;
3473 keyEntry->keyCode = fallbackKeyCode;
3474 keyEntry->scanCode = event.getScanCode();
3475 keyEntry->metaState = event.getMetaState();
3476 keyEntry->repeatCount = event.getRepeatCount();
3477 keyEntry->downTime = event.getDownTime();
3478 keyEntry->syntheticRepeat = false;
3479
3480#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003481 ALOGD("Unhandled key event: Dispatching fallback key. "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003482 "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x",
3483 originalKeyCode, fallbackKeyCode, keyEntry->metaState);
3484#endif
3485
3486 dispatchEntry->inProgress = false;
3487 startDispatchCycleLocked(now(), connection);
3488 return true; // already started next cycle
3489 } else {
3490#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003491 ALOGD("Unhandled key event: No fallback key.");
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003492#endif
3493 }
3494 }
3495 }
3496 return false;
3497}
3498
3499bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection,
3500 DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled) {
3501 return false;
Jeff Brown3915bb82010-11-05 15:02:16 -07003502}
3503
Jeff Brownb88102f2010-09-08 11:49:43 -07003504void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) {
3505 mLock.unlock();
3506
Jeff Brown01ce2e92010-09-26 22:20:12 -07003507 mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType);
Jeff Brownb88102f2010-09-08 11:49:43 -07003508
3509 mLock.lock();
3510}
3511
Jeff Brown3915bb82010-11-05 15:02:16 -07003512void InputDispatcher::initializeKeyEvent(KeyEvent* event, const KeyEntry* entry) {
3513 event->initialize(entry->deviceId, entry->source, entry->action, entry->flags,
3514 entry->keyCode, entry->scanCode, entry->metaState, entry->repeatCount,
3515 entry->downTime, entry->eventTime);
3516}
3517
Jeff Brown519e0242010-09-15 15:18:56 -07003518void InputDispatcher::updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry,
3519 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication) {
3520 // TODO Write some statistics about how long we spend waiting.
Jeff Brownb88102f2010-09-08 11:49:43 -07003521}
3522
3523void InputDispatcher::dump(String8& dump) {
Jeff Brown89ef0722011-08-10 16:25:21 -07003524 AutoMutex _l(mLock);
3525
Jeff Brownf2f487182010-10-01 17:46:21 -07003526 dump.append("Input Dispatcher State:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003527 dumpDispatchStateLocked(dump);
Jeff Brown214eaf42011-05-26 19:17:02 -07003528
3529 dump.append(INDENT "Configuration:\n");
Jeff Brown214eaf42011-05-26 19:17:02 -07003530 dump.appendFormat(INDENT2 "KeyRepeatDelay: %0.1fms\n", mConfig.keyRepeatDelay * 0.000001f);
3531 dump.appendFormat(INDENT2 "KeyRepeatTimeout: %0.1fms\n", mConfig.keyRepeatTimeout * 0.000001f);
Jeff Brownb88102f2010-09-08 11:49:43 -07003532}
3533
Jeff Brown89ef0722011-08-10 16:25:21 -07003534void InputDispatcher::monitor() {
3535 // Acquire and release the lock to ensure that the dispatcher has not deadlocked.
3536 mLock.lock();
Jeff Brown112b5f52012-01-27 17:32:06 -08003537 mLooper->wake();
3538 mDispatcherIsAliveCondition.wait(mLock);
Jeff Brown89ef0722011-08-10 16:25:21 -07003539 mLock.unlock();
3540}
3541
Jeff Brown9c3cda02010-06-15 01:31:58 -07003542
Jeff Brown519e0242010-09-15 15:18:56 -07003543// --- InputDispatcher::Queue ---
3544
3545template <typename T>
3546uint32_t InputDispatcher::Queue<T>::count() const {
3547 uint32_t result = 0;
Jeff Brownac386072011-07-20 15:19:50 -07003548 for (const T* entry = head; entry; entry = entry->next) {
Jeff Brown519e0242010-09-15 15:18:56 -07003549 result += 1;
3550 }
3551 return result;
3552}
3553
3554
Jeff Brownac386072011-07-20 15:19:50 -07003555// --- InputDispatcher::InjectionState ---
Jeff Brown46b9ac02010-04-22 18:58:52 -07003556
Jeff Brownac386072011-07-20 15:19:50 -07003557InputDispatcher::InjectionState::InjectionState(int32_t injectorPid, int32_t injectorUid) :
3558 refCount(1),
3559 injectorPid(injectorPid), injectorUid(injectorUid),
3560 injectionResult(INPUT_EVENT_INJECTION_PENDING), injectionIsAsync(false),
3561 pendingForegroundDispatches(0) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003562}
3563
Jeff Brownac386072011-07-20 15:19:50 -07003564InputDispatcher::InjectionState::~InjectionState() {
Jeff Brown01ce2e92010-09-26 22:20:12 -07003565}
3566
Jeff Brownac386072011-07-20 15:19:50 -07003567void InputDispatcher::InjectionState::release() {
3568 refCount -= 1;
3569 if (refCount == 0) {
3570 delete this;
3571 } else {
Steve Blockec193de2012-01-09 18:35:44 +00003572 ALOG_ASSERT(refCount > 0);
Jeff Brown01ce2e92010-09-26 22:20:12 -07003573 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07003574}
3575
Jeff Brownac386072011-07-20 15:19:50 -07003576
3577// --- InputDispatcher::EventEntry ---
3578
3579InputDispatcher::EventEntry::EventEntry(int32_t type, nsecs_t eventTime, uint32_t policyFlags) :
3580 refCount(1), type(type), eventTime(eventTime), policyFlags(policyFlags),
3581 injectionState(NULL), dispatchInProgress(false) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003582}
3583
Jeff Brownac386072011-07-20 15:19:50 -07003584InputDispatcher::EventEntry::~EventEntry() {
3585 releaseInjectionState();
3586}
3587
3588void InputDispatcher::EventEntry::release() {
3589 refCount -= 1;
3590 if (refCount == 0) {
3591 delete this;
3592 } else {
Steve Blockec193de2012-01-09 18:35:44 +00003593 ALOG_ASSERT(refCount > 0);
Jeff Brownac386072011-07-20 15:19:50 -07003594 }
3595}
3596
3597void InputDispatcher::EventEntry::releaseInjectionState() {
3598 if (injectionState) {
3599 injectionState->release();
3600 injectionState = NULL;
3601 }
3602}
3603
3604
3605// --- InputDispatcher::ConfigurationChangedEntry ---
3606
3607InputDispatcher::ConfigurationChangedEntry::ConfigurationChangedEntry(nsecs_t eventTime) :
3608 EventEntry(TYPE_CONFIGURATION_CHANGED, eventTime, 0) {
3609}
3610
3611InputDispatcher::ConfigurationChangedEntry::~ConfigurationChangedEntry() {
3612}
3613
3614
Jeff Brown65fd2512011-08-18 11:20:58 -07003615// --- InputDispatcher::DeviceResetEntry ---
3616
3617InputDispatcher::DeviceResetEntry::DeviceResetEntry(nsecs_t eventTime, int32_t deviceId) :
3618 EventEntry(TYPE_DEVICE_RESET, eventTime, 0),
3619 deviceId(deviceId) {
3620}
3621
3622InputDispatcher::DeviceResetEntry::~DeviceResetEntry() {
3623}
3624
3625
Jeff Brownac386072011-07-20 15:19:50 -07003626// --- InputDispatcher::KeyEntry ---
3627
3628InputDispatcher::KeyEntry::KeyEntry(nsecs_t eventTime,
Jeff Brown58a2da82011-01-25 16:02:22 -08003629 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action,
Jeff Brown7fbdc842010-06-17 20:52:56 -07003630 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
Jeff Brownac386072011-07-20 15:19:50 -07003631 int32_t repeatCount, nsecs_t downTime) :
3632 EventEntry(TYPE_KEY, eventTime, policyFlags),
3633 deviceId(deviceId), source(source), action(action), flags(flags),
3634 keyCode(keyCode), scanCode(scanCode), metaState(metaState),
3635 repeatCount(repeatCount), downTime(downTime),
Jeff Brown905805a2011-10-12 13:57:59 -07003636 syntheticRepeat(false), interceptKeyResult(KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN),
3637 interceptKeyWakeupTime(0) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003638}
3639
Jeff Brownac386072011-07-20 15:19:50 -07003640InputDispatcher::KeyEntry::~KeyEntry() {
3641}
Jeff Brown7fbdc842010-06-17 20:52:56 -07003642
Jeff Brownac386072011-07-20 15:19:50 -07003643void InputDispatcher::KeyEntry::recycle() {
3644 releaseInjectionState();
3645
3646 dispatchInProgress = false;
3647 syntheticRepeat = false;
3648 interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN;
Jeff Brown905805a2011-10-12 13:57:59 -07003649 interceptKeyWakeupTime = 0;
Jeff Brownac386072011-07-20 15:19:50 -07003650}
3651
3652
Jeff Brownae9fc032010-08-18 15:51:08 -07003653// --- InputDispatcher::MotionEntry ---
3654
Jeff Brownac386072011-07-20 15:19:50 -07003655InputDispatcher::MotionEntry::MotionEntry(nsecs_t eventTime,
3656 int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action, int32_t flags,
3657 int32_t metaState, int32_t buttonState,
3658 int32_t edgeFlags, float xPrecision, float yPrecision,
3659 nsecs_t downTime, uint32_t pointerCount,
3660 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords) :
3661 EventEntry(TYPE_MOTION, eventTime, policyFlags),
Jeff Brown3241b6b2012-02-03 15:08:02 -08003662 eventTime(eventTime),
Jeff Brownac386072011-07-20 15:19:50 -07003663 deviceId(deviceId), source(source), action(action), flags(flags),
3664 metaState(metaState), buttonState(buttonState), edgeFlags(edgeFlags),
3665 xPrecision(xPrecision), yPrecision(yPrecision),
Jeff Brown3241b6b2012-02-03 15:08:02 -08003666 downTime(downTime), pointerCount(pointerCount) {
Jeff Brownac386072011-07-20 15:19:50 -07003667 for (uint32_t i = 0; i < pointerCount; i++) {
3668 this->pointerProperties[i].copyFrom(pointerProperties[i]);
Jeff Brown3241b6b2012-02-03 15:08:02 -08003669 this->pointerCoords[i].copyFrom(pointerCoords[i]);
Jeff Brownac386072011-07-20 15:19:50 -07003670 }
3671}
3672
3673InputDispatcher::MotionEntry::~MotionEntry() {
Jeff Brownac386072011-07-20 15:19:50 -07003674}
3675
3676
3677// --- InputDispatcher::DispatchEntry ---
3678
3679InputDispatcher::DispatchEntry::DispatchEntry(EventEntry* eventEntry,
3680 int32_t targetFlags, float xOffset, float yOffset, float scaleFactor) :
3681 eventEntry(eventEntry), targetFlags(targetFlags),
3682 xOffset(xOffset), yOffset(yOffset), scaleFactor(scaleFactor),
3683 inProgress(false),
Jeff Brown3241b6b2012-02-03 15:08:02 -08003684 resolvedAction(0), resolvedFlags(0) {
Jeff Brownac386072011-07-20 15:19:50 -07003685 eventEntry->refCount += 1;
3686}
3687
3688InputDispatcher::DispatchEntry::~DispatchEntry() {
3689 eventEntry->release();
3690}
3691
Jeff Brownb88102f2010-09-08 11:49:43 -07003692
3693// --- InputDispatcher::InputState ---
3694
Jeff Brownb6997262010-10-08 22:31:17 -07003695InputDispatcher::InputState::InputState() {
Jeff Brownb88102f2010-09-08 11:49:43 -07003696}
3697
3698InputDispatcher::InputState::~InputState() {
3699}
3700
3701bool InputDispatcher::InputState::isNeutral() const {
3702 return mKeyMementos.isEmpty() && mMotionMementos.isEmpty();
3703}
3704
Jeff Brown81346812011-06-28 20:08:48 -07003705bool InputDispatcher::InputState::isHovering(int32_t deviceId, uint32_t source) const {
3706 for (size_t i = 0; i < mMotionMementos.size(); i++) {
3707 const MotionMemento& memento = mMotionMementos.itemAt(i);
3708 if (memento.deviceId == deviceId
3709 && memento.source == source
3710 && memento.hovering) {
3711 return true;
3712 }
3713 }
3714 return false;
3715}
Jeff Brownb88102f2010-09-08 11:49:43 -07003716
Jeff Brown81346812011-06-28 20:08:48 -07003717bool InputDispatcher::InputState::trackKey(const KeyEntry* entry,
3718 int32_t action, int32_t flags) {
3719 switch (action) {
3720 case AKEY_EVENT_ACTION_UP: {
3721 if (entry->flags & AKEY_EVENT_FLAG_FALLBACK) {
3722 for (size_t i = 0; i < mFallbackKeys.size(); ) {
3723 if (mFallbackKeys.valueAt(i) == entry->keyCode) {
3724 mFallbackKeys.removeItemsAt(i);
3725 } else {
3726 i += 1;
3727 }
3728 }
3729 }
3730 ssize_t index = findKeyMemento(entry);
3731 if (index >= 0) {
3732 mKeyMementos.removeAt(index);
3733 return true;
3734 }
Jeff Brown68b909d2011-12-07 16:36:01 -08003735 /* FIXME: We can't just drop the key up event because that prevents creating
3736 * popup windows that are automatically shown when a key is held and then
3737 * dismissed when the key is released. The problem is that the popup will
3738 * not have received the original key down, so the key up will be considered
3739 * to be inconsistent with its observed state. We could perhaps handle this
3740 * by synthesizing a key down but that will cause other problems.
3741 *
3742 * So for now, allow inconsistent key up events to be dispatched.
3743 *
Jeff Brown81346812011-06-28 20:08:48 -07003744#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003745 ALOGD("Dropping inconsistent key up event: deviceId=%d, source=%08x, "
Jeff Brown81346812011-06-28 20:08:48 -07003746 "keyCode=%d, scanCode=%d",
3747 entry->deviceId, entry->source, entry->keyCode, entry->scanCode);
3748#endif
3749 return false;
Jeff Brown68b909d2011-12-07 16:36:01 -08003750 */
3751 return true;
Jeff Brown81346812011-06-28 20:08:48 -07003752 }
3753
3754 case AKEY_EVENT_ACTION_DOWN: {
3755 ssize_t index = findKeyMemento(entry);
3756 if (index >= 0) {
3757 mKeyMementos.removeAt(index);
3758 }
3759 addKeyMemento(entry, flags);
3760 return true;
3761 }
3762
3763 default:
3764 return true;
Jeff Brownb88102f2010-09-08 11:49:43 -07003765 }
3766}
3767
Jeff Brown81346812011-06-28 20:08:48 -07003768bool InputDispatcher::InputState::trackMotion(const MotionEntry* entry,
3769 int32_t action, int32_t flags) {
3770 int32_t actionMasked = action & AMOTION_EVENT_ACTION_MASK;
3771 switch (actionMasked) {
3772 case AMOTION_EVENT_ACTION_UP:
3773 case AMOTION_EVENT_ACTION_CANCEL: {
3774 ssize_t index = findMotionMemento(entry, false /*hovering*/);
3775 if (index >= 0) {
3776 mMotionMementos.removeAt(index);
3777 return true;
Jeff Brownda3d5a92011-03-29 15:11:34 -07003778 }
Jeff Brown81346812011-06-28 20:08:48 -07003779#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003780 ALOGD("Dropping inconsistent motion up or cancel event: deviceId=%d, source=%08x, "
Jeff Brown81346812011-06-28 20:08:48 -07003781 "actionMasked=%d",
3782 entry->deviceId, entry->source, actionMasked);
3783#endif
3784 return false;
Jeff Brownda3d5a92011-03-29 15:11:34 -07003785 }
3786
Jeff Brown81346812011-06-28 20:08:48 -07003787 case AMOTION_EVENT_ACTION_DOWN: {
3788 ssize_t index = findMotionMemento(entry, false /*hovering*/);
3789 if (index >= 0) {
3790 mMotionMementos.removeAt(index);
3791 }
3792 addMotionMemento(entry, flags, false /*hovering*/);
3793 return true;
3794 }
3795
3796 case AMOTION_EVENT_ACTION_POINTER_UP:
3797 case AMOTION_EVENT_ACTION_POINTER_DOWN:
3798 case AMOTION_EVENT_ACTION_MOVE: {
3799 ssize_t index = findMotionMemento(entry, false /*hovering*/);
3800 if (index >= 0) {
3801 MotionMemento& memento = mMotionMementos.editItemAt(index);
3802 memento.setPointers(entry);
3803 return true;
3804 }
Jeff Brown2e45fb62011-06-29 21:19:05 -07003805 if (actionMasked == AMOTION_EVENT_ACTION_MOVE
3806 && (entry->source & (AINPUT_SOURCE_CLASS_JOYSTICK
3807 | AINPUT_SOURCE_CLASS_NAVIGATION))) {
3808 // Joysticks and trackballs can send MOVE events without corresponding DOWN or UP.
3809 return true;
3810 }
Jeff Brown81346812011-06-28 20:08:48 -07003811#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003812 ALOGD("Dropping inconsistent motion pointer up/down or move event: "
Jeff Brown81346812011-06-28 20:08:48 -07003813 "deviceId=%d, source=%08x, actionMasked=%d",
3814 entry->deviceId, entry->source, actionMasked);
3815#endif
3816 return false;
3817 }
3818
3819 case AMOTION_EVENT_ACTION_HOVER_EXIT: {
3820 ssize_t index = findMotionMemento(entry, true /*hovering*/);
3821 if (index >= 0) {
3822 mMotionMementos.removeAt(index);
3823 return true;
3824 }
3825#if DEBUG_OUTBOUND_EVENT_DETAILS
Steve Block5baa3a62011-12-20 16:23:08 +00003826 ALOGD("Dropping inconsistent motion hover exit event: deviceId=%d, source=%08x",
Jeff Brown81346812011-06-28 20:08:48 -07003827 entry->deviceId, entry->source);
3828#endif
3829 return false;
3830 }
3831
3832 case AMOTION_EVENT_ACTION_HOVER_ENTER:
3833 case AMOTION_EVENT_ACTION_HOVER_MOVE: {
3834 ssize_t index = findMotionMemento(entry, true /*hovering*/);
3835 if (index >= 0) {
3836 mMotionMementos.removeAt(index);
3837 }
3838 addMotionMemento(entry, flags, true /*hovering*/);
3839 return true;
3840 }
3841
3842 default:
3843 return true;
3844 }
3845}
3846
3847ssize_t InputDispatcher::InputState::findKeyMemento(const KeyEntry* entry) const {
Jeff Brownb88102f2010-09-08 11:49:43 -07003848 for (size_t i = 0; i < mKeyMementos.size(); i++) {
Jeff Brown81346812011-06-28 20:08:48 -07003849 const KeyMemento& memento = mKeyMementos.itemAt(i);
Jeff Brownb88102f2010-09-08 11:49:43 -07003850 if (memento.deviceId == entry->deviceId
3851 && memento.source == entry->source
3852 && memento.keyCode == entry->keyCode
3853 && memento.scanCode == entry->scanCode) {
Jeff Brown81346812011-06-28 20:08:48 -07003854 return i;
Jeff Brownb88102f2010-09-08 11:49:43 -07003855 }
3856 }
Jeff Brown81346812011-06-28 20:08:48 -07003857 return -1;
Jeff Brownb88102f2010-09-08 11:49:43 -07003858}
3859
Jeff Brown81346812011-06-28 20:08:48 -07003860ssize_t InputDispatcher::InputState::findMotionMemento(const MotionEntry* entry,
3861 bool hovering) const {
Jeff Brownb88102f2010-09-08 11:49:43 -07003862 for (size_t i = 0; i < mMotionMementos.size(); i++) {
Jeff Brown81346812011-06-28 20:08:48 -07003863 const MotionMemento& memento = mMotionMementos.itemAt(i);
Jeff Brownb88102f2010-09-08 11:49:43 -07003864 if (memento.deviceId == entry->deviceId
Jeff Brown81346812011-06-28 20:08:48 -07003865 && memento.source == entry->source
3866 && memento.hovering == hovering) {
3867 return i;
Jeff Brownb88102f2010-09-08 11:49:43 -07003868 }
3869 }
Jeff Brown81346812011-06-28 20:08:48 -07003870 return -1;
3871}
Jeff Brownb88102f2010-09-08 11:49:43 -07003872
Jeff Brown81346812011-06-28 20:08:48 -07003873void InputDispatcher::InputState::addKeyMemento(const KeyEntry* entry, int32_t flags) {
3874 mKeyMementos.push();
3875 KeyMemento& memento = mKeyMementos.editTop();
3876 memento.deviceId = entry->deviceId;
3877 memento.source = entry->source;
3878 memento.keyCode = entry->keyCode;
3879 memento.scanCode = entry->scanCode;
3880 memento.flags = flags;
3881 memento.downTime = entry->downTime;
3882}
3883
3884void InputDispatcher::InputState::addMotionMemento(const MotionEntry* entry,
3885 int32_t flags, bool hovering) {
3886 mMotionMementos.push();
3887 MotionMemento& memento = mMotionMementos.editTop();
3888 memento.deviceId = entry->deviceId;
3889 memento.source = entry->source;
3890 memento.flags = flags;
3891 memento.xPrecision = entry->xPrecision;
3892 memento.yPrecision = entry->yPrecision;
3893 memento.downTime = entry->downTime;
3894 memento.setPointers(entry);
3895 memento.hovering = hovering;
Jeff Brownb88102f2010-09-08 11:49:43 -07003896}
3897
3898void InputDispatcher::InputState::MotionMemento::setPointers(const MotionEntry* entry) {
3899 pointerCount = entry->pointerCount;
3900 for (uint32_t i = 0; i < entry->pointerCount; i++) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003901 pointerProperties[i].copyFrom(entry->pointerProperties[i]);
Jeff Brown3241b6b2012-02-03 15:08:02 -08003902 pointerCoords[i].copyFrom(entry->pointerCoords[i]);
Jeff Brownb88102f2010-09-08 11:49:43 -07003903 }
3904}
3905
Jeff Brownb6997262010-10-08 22:31:17 -07003906void InputDispatcher::InputState::synthesizeCancelationEvents(nsecs_t currentTime,
Jeff Brownac386072011-07-20 15:19:50 -07003907 Vector<EventEntry*>& outEvents, const CancelationOptions& options) {
Jeff Brown81346812011-06-28 20:08:48 -07003908 for (size_t i = 0; i < mKeyMementos.size(); i++) {
Jeff Brownb88102f2010-09-08 11:49:43 -07003909 const KeyMemento& memento = mKeyMementos.itemAt(i);
Jeff Brown49ed71d2010-12-06 17:13:33 -08003910 if (shouldCancelKey(memento, options)) {
Jeff Brownac386072011-07-20 15:19:50 -07003911 outEvents.push(new KeyEntry(currentTime,
Jeff Brownb6997262010-10-08 22:31:17 -07003912 memento.deviceId, memento.source, 0,
Jeff Brown49ed71d2010-12-06 17:13:33 -08003913 AKEY_EVENT_ACTION_UP, memento.flags | AKEY_EVENT_FLAG_CANCELED,
Jeff Brownb6997262010-10-08 22:31:17 -07003914 memento.keyCode, memento.scanCode, 0, 0, memento.downTime));
Jeff Brownb6997262010-10-08 22:31:17 -07003915 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003916 }
3917
Jeff Brown81346812011-06-28 20:08:48 -07003918 for (size_t i = 0; i < mMotionMementos.size(); i++) {
Jeff Brownb88102f2010-09-08 11:49:43 -07003919 const MotionMemento& memento = mMotionMementos.itemAt(i);
Jeff Brown49ed71d2010-12-06 17:13:33 -08003920 if (shouldCancelMotion(memento, options)) {
Jeff Brownac386072011-07-20 15:19:50 -07003921 outEvents.push(new MotionEntry(currentTime,
Jeff Brownb6997262010-10-08 22:31:17 -07003922 memento.deviceId, memento.source, 0,
Jeff Browna032cc02011-03-07 16:56:21 -08003923 memento.hovering
3924 ? AMOTION_EVENT_ACTION_HOVER_EXIT
3925 : AMOTION_EVENT_ACTION_CANCEL,
Jeff Brown81346812011-06-28 20:08:48 -07003926 memento.flags, 0, 0, 0,
Jeff Brownb6997262010-10-08 22:31:17 -07003927 memento.xPrecision, memento.yPrecision, memento.downTime,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003928 memento.pointerCount, memento.pointerProperties, memento.pointerCoords));
Jeff Brownb6997262010-10-08 22:31:17 -07003929 }
Jeff Brownb88102f2010-09-08 11:49:43 -07003930 }
3931}
3932
3933void InputDispatcher::InputState::clear() {
3934 mKeyMementos.clear();
3935 mMotionMementos.clear();
Jeff Brownda3d5a92011-03-29 15:11:34 -07003936 mFallbackKeys.clear();
Jeff Brownb6997262010-10-08 22:31:17 -07003937}
3938
Jeff Brown9c9f1a32010-10-11 18:32:20 -07003939void InputDispatcher::InputState::copyPointerStateTo(InputState& other) const {
3940 for (size_t i = 0; i < mMotionMementos.size(); i++) {
3941 const MotionMemento& memento = mMotionMementos.itemAt(i);
3942 if (memento.source & AINPUT_SOURCE_CLASS_POINTER) {
3943 for (size_t j = 0; j < other.mMotionMementos.size(); ) {
3944 const MotionMemento& otherMemento = other.mMotionMementos.itemAt(j);
3945 if (memento.deviceId == otherMemento.deviceId
3946 && memento.source == otherMemento.source) {
3947 other.mMotionMementos.removeAt(j);
3948 } else {
3949 j += 1;
3950 }
3951 }
3952 other.mMotionMementos.push(memento);
3953 }
3954 }
3955}
3956
Jeff Brownda3d5a92011-03-29 15:11:34 -07003957int32_t InputDispatcher::InputState::getFallbackKey(int32_t originalKeyCode) {
3958 ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode);
3959 return index >= 0 ? mFallbackKeys.valueAt(index) : -1;
3960}
3961
3962void InputDispatcher::InputState::setFallbackKey(int32_t originalKeyCode,
3963 int32_t fallbackKeyCode) {
3964 ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode);
3965 if (index >= 0) {
3966 mFallbackKeys.replaceValueAt(index, fallbackKeyCode);
3967 } else {
3968 mFallbackKeys.add(originalKeyCode, fallbackKeyCode);
3969 }
3970}
3971
3972void InputDispatcher::InputState::removeFallbackKey(int32_t originalKeyCode) {
3973 mFallbackKeys.removeItem(originalKeyCode);
3974}
3975
Jeff Brown49ed71d2010-12-06 17:13:33 -08003976bool InputDispatcher::InputState::shouldCancelKey(const KeyMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -07003977 const CancelationOptions& options) {
3978 if (options.keyCode != -1 && memento.keyCode != options.keyCode) {
3979 return false;
3980 }
3981
Jeff Brown65fd2512011-08-18 11:20:58 -07003982 if (options.deviceId != -1 && memento.deviceId != options.deviceId) {
3983 return false;
3984 }
3985
Jeff Brownda3d5a92011-03-29 15:11:34 -07003986 switch (options.mode) {
3987 case CancelationOptions::CANCEL_ALL_EVENTS:
3988 case CancelationOptions::CANCEL_NON_POINTER_EVENTS:
Jeff Brownb6997262010-10-08 22:31:17 -07003989 return true;
Jeff Brownda3d5a92011-03-29 15:11:34 -07003990 case CancelationOptions::CANCEL_FALLBACK_EVENTS:
Jeff Brown49ed71d2010-12-06 17:13:33 -08003991 return memento.flags & AKEY_EVENT_FLAG_FALLBACK;
3992 default:
3993 return false;
3994 }
3995}
3996
3997bool InputDispatcher::InputState::shouldCancelMotion(const MotionMemento& memento,
Jeff Brownda3d5a92011-03-29 15:11:34 -07003998 const CancelationOptions& options) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003999 if (options.deviceId != -1 && memento.deviceId != options.deviceId) {
4000 return false;
4001 }
4002
Jeff Brownda3d5a92011-03-29 15:11:34 -07004003 switch (options.mode) {
4004 case CancelationOptions::CANCEL_ALL_EVENTS:
Jeff Brown49ed71d2010-12-06 17:13:33 -08004005 return true;
Jeff Brownda3d5a92011-03-29 15:11:34 -07004006 case CancelationOptions::CANCEL_POINTER_EVENTS:
Jeff Brown49ed71d2010-12-06 17:13:33 -08004007 return memento.source & AINPUT_SOURCE_CLASS_POINTER;
Jeff Brownda3d5a92011-03-29 15:11:34 -07004008 case CancelationOptions::CANCEL_NON_POINTER_EVENTS:
Jeff Brown49ed71d2010-12-06 17:13:33 -08004009 return !(memento.source & AINPUT_SOURCE_CLASS_POINTER);
4010 default:
4011 return false;
Jeff Brownb6997262010-10-08 22:31:17 -07004012 }
Jeff Brownb88102f2010-09-08 11:49:43 -07004013}
4014
4015
Jeff Brown46b9ac02010-04-22 18:58:52 -07004016// --- InputDispatcher::Connection ---
4017
Jeff Brown928e0542011-01-10 11:17:36 -08004018InputDispatcher::Connection::Connection(const sp<InputChannel>& inputChannel,
Jeff Browncc4f7db2011-08-30 20:34:48 -07004019 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) :
Jeff Brown928e0542011-01-10 11:17:36 -08004020 status(STATUS_NORMAL), inputChannel(inputChannel), inputWindowHandle(inputWindowHandle),
Jeff Browncc4f7db2011-08-30 20:34:48 -07004021 monitor(monitor),
Jeff Brown928e0542011-01-10 11:17:36 -08004022 inputPublisher(inputChannel),
Jeff Brownda3d5a92011-03-29 15:11:34 -07004023 lastEventTime(LONG_LONG_MAX), lastDispatchTime(LONG_LONG_MAX) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07004024}
4025
4026InputDispatcher::Connection::~Connection() {
4027}
4028
Jeff Brown9c3cda02010-06-15 01:31:58 -07004029const char* InputDispatcher::Connection::getStatusLabel() const {
4030 switch (status) {
4031 case STATUS_NORMAL:
4032 return "NORMAL";
4033
4034 case STATUS_BROKEN:
4035 return "BROKEN";
4036
Jeff Brown9c3cda02010-06-15 01:31:58 -07004037 case STATUS_ZOMBIE:
4038 return "ZOMBIE";
4039
4040 default:
4041 return "UNKNOWN";
4042 }
4043}
4044
Jeff Brown46b9ac02010-04-22 18:58:52 -07004045InputDispatcher::DispatchEntry* InputDispatcher::Connection::findQueuedDispatchEntryForEvent(
4046 const EventEntry* eventEntry) const {
Jeff Brownac386072011-07-20 15:19:50 -07004047 for (DispatchEntry* dispatchEntry = outboundQueue.tail; dispatchEntry;
4048 dispatchEntry = dispatchEntry->prev) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07004049 if (dispatchEntry->eventEntry == eventEntry) {
4050 return dispatchEntry;
4051 }
4052 }
4053 return NULL;
4054}
4055
Jeff Brownb88102f2010-09-08 11:49:43 -07004056
Jeff Brown9c3cda02010-06-15 01:31:58 -07004057// --- InputDispatcher::CommandEntry ---
4058
Jeff Brownac386072011-07-20 15:19:50 -07004059InputDispatcher::CommandEntry::CommandEntry(Command command) :
4060 command(command), eventTime(0), keyEntry(NULL), userActivityEventType(0), handled(false) {
Jeff Brown9c3cda02010-06-15 01:31:58 -07004061}
4062
4063InputDispatcher::CommandEntry::~CommandEntry() {
4064}
4065
Jeff Brown46b9ac02010-04-22 18:58:52 -07004066
Jeff Brown01ce2e92010-09-26 22:20:12 -07004067// --- InputDispatcher::TouchState ---
4068
4069InputDispatcher::TouchState::TouchState() :
Jeff Brown58a2da82011-01-25 16:02:22 -08004070 down(false), split(false), deviceId(-1), source(0) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07004071}
4072
4073InputDispatcher::TouchState::~TouchState() {
4074}
4075
4076void InputDispatcher::TouchState::reset() {
4077 down = false;
4078 split = false;
Jeff Brown95712852011-01-04 19:41:59 -08004079 deviceId = -1;
Jeff Brown58a2da82011-01-25 16:02:22 -08004080 source = 0;
Jeff Brown01ce2e92010-09-26 22:20:12 -07004081 windows.clear();
4082}
4083
4084void InputDispatcher::TouchState::copyFrom(const TouchState& other) {
4085 down = other.down;
4086 split = other.split;
Jeff Brown95712852011-01-04 19:41:59 -08004087 deviceId = other.deviceId;
Jeff Brown58a2da82011-01-25 16:02:22 -08004088 source = other.source;
Jeff Brown9302c872011-07-13 22:51:29 -07004089 windows = other.windows;
Jeff Brown01ce2e92010-09-26 22:20:12 -07004090}
4091
Jeff Brown9302c872011-07-13 22:51:29 -07004092void InputDispatcher::TouchState::addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle,
Jeff Brown01ce2e92010-09-26 22:20:12 -07004093 int32_t targetFlags, BitSet32 pointerIds) {
4094 if (targetFlags & InputTarget::FLAG_SPLIT) {
4095 split = true;
4096 }
4097
4098 for (size_t i = 0; i < windows.size(); i++) {
4099 TouchedWindow& touchedWindow = windows.editItemAt(i);
Jeff Brown9302c872011-07-13 22:51:29 -07004100 if (touchedWindow.windowHandle == windowHandle) {
Jeff Brown01ce2e92010-09-26 22:20:12 -07004101 touchedWindow.targetFlags |= targetFlags;
Jeff Brown98db5fa2011-06-08 15:37:10 -07004102 if (targetFlags & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) {
4103 touchedWindow.targetFlags &= ~InputTarget::FLAG_DISPATCH_AS_IS;
4104 }
Jeff Brown01ce2e92010-09-26 22:20:12 -07004105 touchedWindow.pointerIds.value |= pointerIds.value;
4106 return;
4107 }
4108 }
4109
4110 windows.push();
4111
4112 TouchedWindow& touchedWindow = windows.editTop();
Jeff Brown9302c872011-07-13 22:51:29 -07004113 touchedWindow.windowHandle = windowHandle;
Jeff Brown01ce2e92010-09-26 22:20:12 -07004114 touchedWindow.targetFlags = targetFlags;
4115 touchedWindow.pointerIds = pointerIds;
Jeff Brown01ce2e92010-09-26 22:20:12 -07004116}
4117
Jeff Browna032cc02011-03-07 16:56:21 -08004118void InputDispatcher::TouchState::filterNonAsIsTouchWindows() {
Jeff Brown01ce2e92010-09-26 22:20:12 -07004119 for (size_t i = 0 ; i < windows.size(); ) {
Jeff Browna032cc02011-03-07 16:56:21 -08004120 TouchedWindow& window = windows.editItemAt(i);
Jeff Brown98db5fa2011-06-08 15:37:10 -07004121 if (window.targetFlags & (InputTarget::FLAG_DISPATCH_AS_IS
4122 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER)) {
Jeff Browna032cc02011-03-07 16:56:21 -08004123 window.targetFlags &= ~InputTarget::FLAG_DISPATCH_MASK;
4124 window.targetFlags |= InputTarget::FLAG_DISPATCH_AS_IS;
Jeff Brown01ce2e92010-09-26 22:20:12 -07004125 i += 1;
Jeff Browna032cc02011-03-07 16:56:21 -08004126 } else {
4127 windows.removeAt(i);
Jeff Brown01ce2e92010-09-26 22:20:12 -07004128 }
4129 }
4130}
4131
Jeff Brown9302c872011-07-13 22:51:29 -07004132sp<InputWindowHandle> InputDispatcher::TouchState::getFirstForegroundWindowHandle() const {
Jeff Brown01ce2e92010-09-26 22:20:12 -07004133 for (size_t i = 0; i < windows.size(); i++) {
Jeff Brown98db5fa2011-06-08 15:37:10 -07004134 const TouchedWindow& window = windows.itemAt(i);
4135 if (window.targetFlags & InputTarget::FLAG_FOREGROUND) {
Jeff Brown9302c872011-07-13 22:51:29 -07004136 return window.windowHandle;
Jeff Brown01ce2e92010-09-26 22:20:12 -07004137 }
4138 }
4139 return NULL;
4140}
4141
Jeff Brown98db5fa2011-06-08 15:37:10 -07004142bool InputDispatcher::TouchState::isSlippery() const {
4143 // Must have exactly one foreground window.
4144 bool haveSlipperyForegroundWindow = false;
4145 for (size_t i = 0; i < windows.size(); i++) {
4146 const TouchedWindow& window = windows.itemAt(i);
4147 if (window.targetFlags & InputTarget::FLAG_FOREGROUND) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07004148 if (haveSlipperyForegroundWindow
4149 || !(window.windowHandle->getInfo()->layoutParamsFlags
4150 & InputWindowInfo::FLAG_SLIPPERY)) {
Jeff Brown98db5fa2011-06-08 15:37:10 -07004151 return false;
4152 }
4153 haveSlipperyForegroundWindow = true;
4154 }
4155 }
4156 return haveSlipperyForegroundWindow;
4157}
4158
Jeff Brown01ce2e92010-09-26 22:20:12 -07004159
Jeff Brown46b9ac02010-04-22 18:58:52 -07004160// --- InputDispatcherThread ---
4161
4162InputDispatcherThread::InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher) :
4163 Thread(/*canCallJava*/ true), mDispatcher(dispatcher) {
4164}
4165
4166InputDispatcherThread::~InputDispatcherThread() {
4167}
4168
4169bool InputDispatcherThread::threadLoop() {
4170 mDispatcher->dispatchOnce();
4171 return true;
4172}
4173
4174} // namespace android