1 // 2 // Copyright 2010 The Android Open Source Project 3 // 4 // The input dispatcher. 5 // 6 #define LOG_TAG "InputDispatcher" 7 8 //#define LOG_NDEBUG 0 9 10 // Log detailed debug messages about each inbound event notification to the dispatcher. 11 #define DEBUG_INBOUND_EVENT_DETAILS 0 12 13 // Log detailed debug messages about each outbound event processed by the dispatcher. 14 #define DEBUG_OUTBOUND_EVENT_DETAILS 0 15 16 // Log debug messages about batching. 17 #define DEBUG_BATCHING 0 18 19 // Log debug messages about the dispatch cycle. 20 #define DEBUG_DISPATCH_CYCLE 0 21 22 // Log debug messages about registrations. 23 #define DEBUG_REGISTRATION 0 24 25 // Log debug messages about performance statistics. 26 #define DEBUG_PERFORMANCE_STATISTICS 0 27 28 // Log debug messages about input event injection. 29 #define DEBUG_INJECTION 0 30 31 // Log debug messages about input event throttling. 32 #define DEBUG_THROTTLING 0 33 34 // Log debug messages about input focus tracking. 35 #define DEBUG_FOCUS 0 36 37 // Log debug messages about the app switch latency optimization. 38 #define DEBUG_APP_SWITCH 0 39 40 #include <cutils/log.h> 41 #include <ui/InputDispatcher.h> 42 #include <ui/PowerManager.h> 43 44 #include <stddef.h> 45 #include <unistd.h> 46 #include <errno.h> 47 #include <limits.h> 48 49 #define INDENT " " 50 #define INDENT2 " " 51 52 namespace android { 53 54 // Delay before reporting long touch events to the power manager. 55 const nsecs_t LONG_TOUCH_DELAY = 300 * 1000000LL; // 300 ms 56 57 // Default input dispatching timeout if there is no focused application or paused window 58 // from which to determine an appropriate dispatching timeout. 59 const nsecs_t DEFAULT_INPUT_DISPATCHING_TIMEOUT = 5000 * 1000000LL; // 5 sec 60 61 // Amount of time to allow for all pending events to be processed when an app switch 62 // key is on the way. This is used to preempt input dispatch and drop input events 63 // when an application takes too long to respond and the user has pressed an app switch key. 64 const nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec 65 66 67 static inline nsecs_t now() { 68 return systemTime(SYSTEM_TIME_MONOTONIC); 69 } 70 71 static inline const char* toString(bool value) { 72 return value ? "true" : "false"; 73 } 74 75 static inline int32_t getMotionEventActionPointerIndex(int32_t action) { 76 return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) 77 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; 78 } 79 80 static bool isValidKeyAction(int32_t action) { 81 switch (action) { 82 case AKEY_EVENT_ACTION_DOWN: 83 case AKEY_EVENT_ACTION_UP: 84 return true; 85 default: 86 return false; 87 } 88 } 89 90 static bool validateKeyEvent(int32_t action) { 91 if (! isValidKeyAction(action)) { 92 LOGE("Key event has invalid action code 0x%x", action); 93 return false; 94 } 95 return true; 96 } 97 98 static bool isValidMotionAction(int32_t action, size_t pointerCount) { 99 switch (action & AMOTION_EVENT_ACTION_MASK) { 100 case AMOTION_EVENT_ACTION_DOWN: 101 case AMOTION_EVENT_ACTION_UP: 102 case AMOTION_EVENT_ACTION_CANCEL: 103 case AMOTION_EVENT_ACTION_MOVE: 104 case AMOTION_EVENT_ACTION_OUTSIDE: 105 return true; 106 case AMOTION_EVENT_ACTION_POINTER_DOWN: 107 case AMOTION_EVENT_ACTION_POINTER_UP: { 108 int32_t index = getMotionEventActionPointerIndex(action); 109 return index >= 0 && size_t(index) < pointerCount; 110 } 111 default: 112 return false; 113 } 114 } 115 116 static bool validateMotionEvent(int32_t action, size_t pointerCount, 117 const int32_t* pointerIds) { 118 if (! isValidMotionAction(action, pointerCount)) { 119 LOGE("Motion event has invalid action code 0x%x", action); 120 return false; 121 } 122 if (pointerCount < 1 || pointerCount > MAX_POINTERS) { 123 LOGE("Motion event has invalid pointer count %d; value must be between 1 and %d.", 124 pointerCount, MAX_POINTERS); 125 return false; 126 } 127 BitSet32 pointerIdBits; 128 for (size_t i = 0; i < pointerCount; i++) { 129 int32_t id = pointerIds[i]; 130 if (id < 0 || id > MAX_POINTER_ID) { 131 LOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", 132 id, MAX_POINTER_ID); 133 return false; 134 } 135 if (pointerIdBits.hasBit(id)) { 136 LOGE("Motion event has duplicate pointer id %d", id); 137 return false; 138 } 139 pointerIdBits.markBit(id); 140 } 141 return true; 142 } 143 144 145 // --- InputWindow --- 146 147 bool InputWindow::touchableAreaContainsPoint(int32_t x, int32_t y) const { 148 return x >= touchableAreaLeft && x <= touchableAreaRight 149 && y >= touchableAreaTop && y <= touchableAreaBottom; 150 } 151 152 bool InputWindow::frameContainsPoint(int32_t x, int32_t y) const { 153 return x >= frameLeft && x <= frameRight 154 && y >= frameTop && y <= frameBottom; 155 } 156 157 bool InputWindow::isTrustedOverlay() const { 158 return layoutParamsType == TYPE_INPUT_METHOD 159 || layoutParamsType == TYPE_INPUT_METHOD_DIALOG 160 || layoutParamsType == TYPE_SECURE_SYSTEM_OVERLAY; 161 } 162 163 164 // --- InputDispatcher --- 165 166 InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) : 167 mPolicy(policy), 168 mPendingEvent(NULL), mAppSwitchDueTime(LONG_LONG_MAX), 169 mDispatchEnabled(true), mDispatchFrozen(false), 170 mFocusedWindow(NULL), 171 mFocusedApplication(NULL), 172 mCurrentInputTargetsValid(false), 173 mInputTargetWaitCause(INPUT_TARGET_WAIT_CAUSE_NONE) { 174 mLooper = new Looper(false); 175 176 mInboundQueue.headSentinel.refCount = -1; 177 mInboundQueue.headSentinel.type = EventEntry::TYPE_SENTINEL; 178 mInboundQueue.headSentinel.eventTime = LONG_LONG_MIN; 179 180 mInboundQueue.tailSentinel.refCount = -1; 181 mInboundQueue.tailSentinel.type = EventEntry::TYPE_SENTINEL; 182 mInboundQueue.tailSentinel.eventTime = LONG_LONG_MAX; 183 184 mKeyRepeatState.lastKeyEntry = NULL; 185 186 int32_t maxEventsPerSecond = policy->getMaxEventsPerSecond(); 187 mThrottleState.minTimeBetweenEvents = 1000000000LL / maxEventsPerSecond; 188 mThrottleState.lastDeviceId = -1; 189 190 #if DEBUG_THROTTLING 191 mThrottleState.originalSampleCount = 0; 192 LOGD("Throttling - Max events per second = %d", maxEventsPerSecond); 193 #endif 194 } 195 196 InputDispatcher::~InputDispatcher() { 197 { // acquire lock 198 AutoMutex _l(mLock); 199 200 resetKeyRepeatLocked(); 201 releasePendingEventLocked(); 202 drainInboundQueueLocked(); 203 } 204 205 while (mConnectionsByReceiveFd.size() != 0) { 206 unregisterInputChannel(mConnectionsByReceiveFd.valueAt(0)->inputChannel); 207 } 208 } 209 210 void InputDispatcher::dispatchOnce() { 211 nsecs_t keyRepeatTimeout = mPolicy->getKeyRepeatTimeout(); 212 nsecs_t keyRepeatDelay = mPolicy->getKeyRepeatDelay(); 213 214 nsecs_t nextWakeupTime = LONG_LONG_MAX; 215 { // acquire lock 216 AutoMutex _l(mLock); 217 dispatchOnceInnerLocked(keyRepeatTimeout, keyRepeatDelay, & nextWakeupTime); 218 219 if (runCommandsLockedInterruptible()) { 220 nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately 221 } 222 } // release lock 223 224 // Wait for callback or timeout or wake. (make sure we round up, not down) 225 nsecs_t currentTime = now(); 226 int32_t timeoutMillis; 227 if (nextWakeupTime > currentTime) { 228 uint64_t timeout = uint64_t(nextWakeupTime - currentTime); 229 timeout = (timeout + 999999LL) / 1000000LL; 230 timeoutMillis = timeout > INT_MAX ? -1 : int32_t(timeout); 231 } else { 232 timeoutMillis = 0; 233 } 234 235 mLooper->pollOnce(timeoutMillis); 236 } 237 238 void InputDispatcher::dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, 239 nsecs_t keyRepeatDelay, nsecs_t* nextWakeupTime) { 240 nsecs_t currentTime = now(); 241 242 // Reset the key repeat timer whenever we disallow key events, even if the next event 243 // is not a key. This is to ensure that we abort a key repeat if the device is just coming 244 // out of sleep. 245 if (keyRepeatTimeout < 0) { 246 resetKeyRepeatLocked(); 247 } 248 249 // If dispatching is frozen, do not process timeouts or try to deliver any new events. 250 if (mDispatchFrozen) { 251 #if DEBUG_FOCUS 252 LOGD("Dispatch frozen. Waiting some more."); 253 #endif 254 return; 255 } 256 257 // Optimize latency of app switches. 258 // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has 259 // been pressed. When it expires, we preempt dispatch and drop all other pending events. 260 bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; 261 if (mAppSwitchDueTime < *nextWakeupTime) { 262 *nextWakeupTime = mAppSwitchDueTime; 263 } 264 265 // Ready to start a new event. 266 // If we don't already have a pending event, go grab one. 267 if (! mPendingEvent) { 268 if (mInboundQueue.isEmpty()) { 269 if (isAppSwitchDue) { 270 // The inbound queue is empty so the app switch key we were waiting 271 // for will never arrive. Stop waiting for it. 272 resetPendingAppSwitchLocked(false); 273 isAppSwitchDue = false; 274 } 275 276 // Synthesize a key repeat if appropriate. 277 if (mKeyRepeatState.lastKeyEntry) { 278 if (currentTime >= mKeyRepeatState.nextRepeatTime) { 279 mPendingEvent = synthesizeKeyRepeatLocked(currentTime, keyRepeatDelay); 280 } else { 281 if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { 282 *nextWakeupTime = mKeyRepeatState.nextRepeatTime; 283 } 284 } 285 } 286 if (! mPendingEvent) { 287 return; 288 } 289 } else { 290 // Inbound queue has at least one entry. 291 EventEntry* entry = mInboundQueue.headSentinel.next; 292 293 // Throttle the entry if it is a move event and there are no 294 // other events behind it in the queue. Due to movement batching, additional 295 // samples may be appended to this event by the time the throttling timeout 296 // expires. 297 // TODO Make this smarter and consider throttling per device independently. 298 if (entry->type == EventEntry::TYPE_MOTION 299 && !isAppSwitchDue 300 && mDispatchEnabled 301 && (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) 302 && !entry->isInjected()) { 303 MotionEntry* motionEntry = static_cast<MotionEntry*>(entry); 304 int32_t deviceId = motionEntry->deviceId; 305 uint32_t source = motionEntry->source; 306 if (! isAppSwitchDue 307 && motionEntry->next == & mInboundQueue.tailSentinel // exactly one event 308 && motionEntry->action == AMOTION_EVENT_ACTION_MOVE 309 && deviceId == mThrottleState.lastDeviceId 310 && source == mThrottleState.lastSource) { 311 nsecs_t nextTime = mThrottleState.lastEventTime 312 + mThrottleState.minTimeBetweenEvents; 313 if (currentTime < nextTime) { 314 // Throttle it! 315 #if DEBUG_THROTTLING 316 LOGD("Throttling - Delaying motion event for " 317 "device 0x%x, source 0x%08x by up to %0.3fms.", 318 deviceId, source, (nextTime - currentTime) * 0.000001); 319 #endif 320 if (nextTime < *nextWakeupTime) { 321 *nextWakeupTime = nextTime; 322 } 323 if (mThrottleState.originalSampleCount == 0) { 324 mThrottleState.originalSampleCount = 325 motionEntry->countSamples(); 326 } 327 return; 328 } 329 } 330 331 #if DEBUG_THROTTLING 332 if (mThrottleState.originalSampleCount != 0) { 333 uint32_t count = motionEntry->countSamples(); 334 LOGD("Throttling - Motion event sample count grew by %d from %d to %d.", 335 count - mThrottleState.originalSampleCount, 336 mThrottleState.originalSampleCount, count); 337 mThrottleState.originalSampleCount = 0; 338 } 339 #endif 340 341 mThrottleState.lastEventTime = entry->eventTime < currentTime 342 ? entry->eventTime : currentTime; 343 mThrottleState.lastDeviceId = deviceId; 344 mThrottleState.lastSource = source; 345 } 346 347 mInboundQueue.dequeue(entry); 348 mPendingEvent = entry; 349 } 350 351 // Poke user activity for this event. 352 if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { 353 pokeUserActivityLocked(mPendingEvent); 354 } 355 } 356 357 // Now we have an event to dispatch. 358 assert(mPendingEvent != NULL); 359 bool done = false; 360 DropReason dropReason = DROP_REASON_NOT_DROPPED; 361 if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { 362 dropReason = DROP_REASON_POLICY; 363 } else if (!mDispatchEnabled) { 364 dropReason = DROP_REASON_DISABLED; 365 } 366 switch (mPendingEvent->type) { 367 case EventEntry::TYPE_CONFIGURATION_CHANGED: { 368 ConfigurationChangedEntry* typedEntry = 369 static_cast<ConfigurationChangedEntry*>(mPendingEvent); 370 done = dispatchConfigurationChangedLocked(currentTime, typedEntry); 371 dropReason = DROP_REASON_NOT_DROPPED; // configuration changes are never dropped 372 break; 373 } 374 375 case EventEntry::TYPE_KEY: { 376 KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent); 377 if (isAppSwitchDue) { 378 if (isAppSwitchKeyEventLocked(typedEntry)) { 379 resetPendingAppSwitchLocked(true); 380 isAppSwitchDue = false; 381 } else if (dropReason == DROP_REASON_NOT_DROPPED) { 382 dropReason = DROP_REASON_APP_SWITCH; 383 } 384 } 385 done = dispatchKeyLocked(currentTime, typedEntry, keyRepeatTimeout, 386 &dropReason, nextWakeupTime); 387 break; 388 } 389 390 case EventEntry::TYPE_MOTION: { 391 MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent); 392 if (dropReason == DROP_REASON_NOT_DROPPED && isAppSwitchDue) { 393 dropReason = DROP_REASON_APP_SWITCH; 394 } 395 done = dispatchMotionLocked(currentTime, typedEntry, 396 &dropReason, nextWakeupTime); 397 break; 398 } 399 400 default: 401 assert(false); 402 break; 403 } 404 405 if (done) { 406 if (dropReason != DROP_REASON_NOT_DROPPED) { 407 dropInboundEventLocked(mPendingEvent, dropReason); 408 } 409 410 releasePendingEventLocked(); 411 *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately 412 } 413 } 414 415 bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) { 416 bool needWake = mInboundQueue.isEmpty(); 417 mInboundQueue.enqueueAtTail(entry); 418 419 switch (entry->type) { 420 case EventEntry::TYPE_KEY: { 421 KeyEntry* keyEntry = static_cast<KeyEntry*>(entry); 422 if (isAppSwitchKeyEventLocked(keyEntry)) { 423 if (keyEntry->action == AKEY_EVENT_ACTION_DOWN) { 424 mAppSwitchSawKeyDown = true; 425 } else if (keyEntry->action == AKEY_EVENT_ACTION_UP) { 426 if (mAppSwitchSawKeyDown) { 427 #if DEBUG_APP_SWITCH 428 LOGD("App switch is pending!"); 429 #endif 430 mAppSwitchDueTime = keyEntry->eventTime + APP_SWITCH_TIMEOUT; 431 mAppSwitchSawKeyDown = false; 432 needWake = true; 433 } 434 } 435 } 436 break; 437 } 438 } 439 440 return needWake; 441 } 442 443 void InputDispatcher::dropInboundEventLocked(EventEntry* entry, DropReason dropReason) { 444 const char* reason; 445 switch (dropReason) { 446 case DROP_REASON_POLICY: 447 #if DEBUG_INBOUND_EVENT_DETAILS 448 LOGD("Dropped event because policy consumed it."); 449 #endif 450 reason = "inbound event was dropped because the policy consumed it"; 451 break; 452 case DROP_REASON_DISABLED: 453 LOGI("Dropped event because input dispatch is disabled."); 454 reason = "inbound event was dropped because input dispatch is disabled"; 455 break; 456 case DROP_REASON_APP_SWITCH: 457 LOGI("Dropped event because of pending overdue app switch."); 458 reason = "inbound event was dropped because of pending overdue app switch"; 459 break; 460 default: 461 assert(false); 462 return; 463 } 464 465 switch (entry->type) { 466 case EventEntry::TYPE_KEY: 467 synthesizeCancelationEventsForAllConnectionsLocked( 468 InputState::CANCEL_NON_POINTER_EVENTS, reason); 469 break; 470 case EventEntry::TYPE_MOTION: { 471 MotionEntry* motionEntry = static_cast<MotionEntry*>(entry); 472 if (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) { 473 synthesizeCancelationEventsForAllConnectionsLocked( 474 InputState::CANCEL_POINTER_EVENTS, reason); 475 } else { 476 synthesizeCancelationEventsForAllConnectionsLocked( 477 InputState::CANCEL_NON_POINTER_EVENTS, reason); 478 } 479 break; 480 } 481 } 482 } 483 484 bool InputDispatcher::isAppSwitchKeyCode(int32_t keyCode) { 485 return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL; 486 } 487 488 bool InputDispatcher::isAppSwitchKeyEventLocked(KeyEntry* keyEntry) { 489 return ! (keyEntry->flags & AKEY_EVENT_FLAG_CANCELED) 490 && isAppSwitchKeyCode(keyEntry->keyCode) 491 && (keyEntry->policyFlags & POLICY_FLAG_TRUSTED) 492 && (keyEntry->policyFlags & POLICY_FLAG_PASS_TO_USER); 493 } 494 495 bool InputDispatcher::isAppSwitchPendingLocked() { 496 return mAppSwitchDueTime != LONG_LONG_MAX; 497 } 498 499 void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { 500 mAppSwitchDueTime = LONG_LONG_MAX; 501 502 #if DEBUG_APP_SWITCH 503 if (handled) { 504 LOGD("App switch has arrived."); 505 } else { 506 LOGD("App switch was abandoned."); 507 } 508 #endif 509 } 510 511 bool InputDispatcher::runCommandsLockedInterruptible() { 512 if (mCommandQueue.isEmpty()) { 513 return false; 514 } 515 516 do { 517 CommandEntry* commandEntry = mCommandQueue.dequeueAtHead(); 518 519 Command command = commandEntry->command; 520 (this->*command)(commandEntry); // commands are implicitly 'LockedInterruptible' 521 522 commandEntry->connection.clear(); 523 mAllocator.releaseCommandEntry(commandEntry); 524 } while (! mCommandQueue.isEmpty()); 525 return true; 526 } 527 528 InputDispatcher::CommandEntry* InputDispatcher::postCommandLocked(Command command) { 529 CommandEntry* commandEntry = mAllocator.obtainCommandEntry(command); 530 mCommandQueue.enqueueAtTail(commandEntry); 531 return commandEntry; 532 } 533 534 void InputDispatcher::drainInboundQueueLocked() { 535 while (! mInboundQueue.isEmpty()) { 536 EventEntry* entry = mInboundQueue.dequeueAtHead(); 537 releaseInboundEventLocked(entry); 538 } 539 } 540 541 void InputDispatcher::releasePendingEventLocked() { 542 if (mPendingEvent) { 543 releaseInboundEventLocked(mPendingEvent); 544 mPendingEvent = NULL; 545 } 546 } 547 548 void InputDispatcher::releaseInboundEventLocked(EventEntry* entry) { 549 InjectionState* injectionState = entry->injectionState; 550 if (injectionState && injectionState->injectionResult == INPUT_EVENT_INJECTION_PENDING) { 551 #if DEBUG_DISPATCH_CYCLE 552 LOGD("Injected inbound event was dropped."); 553 #endif 554 setInjectionResultLocked(entry, INPUT_EVENT_INJECTION_FAILED); 555 } 556 mAllocator.releaseEventEntry(entry); 557 } 558 559 void InputDispatcher::resetKeyRepeatLocked() { 560 if (mKeyRepeatState.lastKeyEntry) { 561 mAllocator.releaseKeyEntry(mKeyRepeatState.lastKeyEntry); 562 mKeyRepeatState.lastKeyEntry = NULL; 563 } 564 } 565 566 InputDispatcher::KeyEntry* InputDispatcher::synthesizeKeyRepeatLocked( 567 nsecs_t currentTime, nsecs_t keyRepeatDelay) { 568 KeyEntry* entry = mKeyRepeatState.lastKeyEntry; 569 570 // Reuse the repeated key entry if it is otherwise unreferenced. 571 uint32_t policyFlags = (entry->policyFlags & POLICY_FLAG_RAW_MASK) 572 | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED; 573 if (entry->refCount == 1) { 574 mAllocator.recycleKeyEntry(entry); 575 entry->eventTime = currentTime; 576 entry->policyFlags = policyFlags; 577 entry->repeatCount += 1; 578 } else { 579 KeyEntry* newEntry = mAllocator.obtainKeyEntry(currentTime, 580 entry->deviceId, entry->source, policyFlags, 581 entry->action, entry->flags, entry->keyCode, entry->scanCode, 582 entry->metaState, entry->repeatCount + 1, entry->downTime); 583 584 mKeyRepeatState.lastKeyEntry = newEntry; 585 mAllocator.releaseKeyEntry(entry); 586 587 entry = newEntry; 588 } 589 entry->syntheticRepeat = true; 590 591 // Increment reference count since we keep a reference to the event in 592 // mKeyRepeatState.lastKeyEntry in addition to the one we return. 593 entry->refCount += 1; 594 595 mKeyRepeatState.nextRepeatTime = currentTime + keyRepeatDelay; 596 return entry; 597 } 598 599 bool InputDispatcher::dispatchConfigurationChangedLocked( 600 nsecs_t currentTime, ConfigurationChangedEntry* entry) { 601 #if DEBUG_OUTBOUND_EVENT_DETAILS 602 LOGD("dispatchConfigurationChanged - eventTime=%lld", entry->eventTime); 603 #endif 604 605 // Reset key repeating in case a keyboard device was added or removed or something. 606 resetKeyRepeatLocked(); 607 608 // Enqueue a command to run outside the lock to tell the policy that the configuration changed. 609 CommandEntry* commandEntry = postCommandLocked( 610 & InputDispatcher::doNotifyConfigurationChangedInterruptible); 611 commandEntry->eventTime = entry->eventTime; 612 return true; 613 } 614 615 bool InputDispatcher::dispatchKeyLocked( 616 nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout, 617 DropReason* dropReason, nsecs_t* nextWakeupTime) { 618 // Preprocessing. 619 if (! entry->dispatchInProgress) { 620 if (entry->repeatCount == 0 621 && entry->action == AKEY_EVENT_ACTION_DOWN 622 && (entry->policyFlags & POLICY_FLAG_TRUSTED) 623 && !entry->isInjected()) { 624 if (mKeyRepeatState.lastKeyEntry 625 && mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode) { 626 // We have seen two identical key downs in a row which indicates that the device 627 // driver is automatically generating key repeats itself. We take note of the 628 // repeat here, but we disable our own next key repeat timer since it is clear that 629 // we will not need to synthesize key repeats ourselves. 630 entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; 631 resetKeyRepeatLocked(); 632 mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves 633 } else { 634 // Not a repeat. Save key down state in case we do see a repeat later. 635 resetKeyRepeatLocked(); 636 mKeyRepeatState.nextRepeatTime = entry->eventTime + keyRepeatTimeout; 637 } 638 mKeyRepeatState.lastKeyEntry = entry; 639 entry->refCount += 1; 640 } else if (! entry->syntheticRepeat) { 641 resetKeyRepeatLocked(); 642 } 643 644 if (entry->repeatCount == 1) { 645 entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; 646 } else { 647 entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; 648 } 649 650 entry->dispatchInProgress = true; 651 resetTargetsLocked(); 652 653 logOutboundKeyDetailsLocked("dispatchKey - ", entry); 654 } 655 656 // Give the policy a chance to intercept the key. 657 if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { 658 if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { 659 CommandEntry* commandEntry = postCommandLocked( 660 & InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); 661 if (mFocusedWindow) { 662 commandEntry->inputChannel = mFocusedWindow->inputChannel; 663 } 664 commandEntry->keyEntry = entry; 665 entry->refCount += 1; 666 return false; // wait for the command to run 667 } else { 668 entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; 669 } 670 } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { 671 if (*dropReason == DROP_REASON_NOT_DROPPED) { 672 *dropReason = DROP_REASON_POLICY; 673 } 674 } 675 676 // Clean up if dropping the event. 677 if (*dropReason != DROP_REASON_NOT_DROPPED) { 678 resetTargetsLocked(); 679 setInjectionResultLocked(entry, *dropReason == DROP_REASON_POLICY 680 ? INPUT_EVENT_INJECTION_SUCCEEDED : INPUT_EVENT_INJECTION_FAILED); 681 return true; 682 } 683 684 // Identify targets. 685 if (! mCurrentInputTargetsValid) { 686 int32_t injectionResult = findFocusedWindowTargetsLocked(currentTime, 687 entry, nextWakeupTime); 688 if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { 689 return false; 690 } 691 692 setInjectionResultLocked(entry, injectionResult); 693 if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { 694 return true; 695 } 696 697 addMonitoringTargetsLocked(); 698 commitTargetsLocked(); 699 } 700 701 // Dispatch the key. 702 dispatchEventToCurrentInputTargetsLocked(currentTime, entry, false); 703 return true; 704 } 705 706 void InputDispatcher::logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry) { 707 #if DEBUG_OUTBOUND_EVENT_DETAILS 708 LOGD("%seventTime=%lld, deviceId=0x%x, source=0x%x, policyFlags=0x%x, " 709 "action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, " 710 "repeatCount=%d, downTime=%lld", 711 prefix, 712 entry->eventTime, entry->deviceId, entry->source, entry->policyFlags, 713 entry->action, entry->flags, entry->keyCode, entry->scanCode, entry->metaState, 714 entry->repeatCount, entry->downTime); 715 #endif 716 } 717 718 bool InputDispatcher::dispatchMotionLocked( 719 nsecs_t currentTime, MotionEntry* entry, DropReason* dropReason, nsecs_t* nextWakeupTime) { 720 // Preprocessing. 721 if (! entry->dispatchInProgress) { 722 entry->dispatchInProgress = true; 723 resetTargetsLocked(); 724 725 logOutboundMotionDetailsLocked("dispatchMotion - ", entry); 726 } 727 728 // Clean up if dropping the event. 729 if (*dropReason != DROP_REASON_NOT_DROPPED) { 730 resetTargetsLocked(); 731 setInjectionResultLocked(entry, *dropReason == DROP_REASON_POLICY 732 ? INPUT_EVENT_INJECTION_SUCCEEDED : INPUT_EVENT_INJECTION_FAILED); 733 return true; 734 } 735 736 bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; 737 738 // Identify targets. 739 if (! mCurrentInputTargetsValid) { 740 int32_t injectionResult; 741 if (isPointerEvent) { 742 // Pointer event. (eg. touchscreen) 743 injectionResult = findTouchedWindowTargetsLocked(currentTime, 744 entry, nextWakeupTime); 745 } else { 746 // Non touch event. (eg. trackball) 747 injectionResult = findFocusedWindowTargetsLocked(currentTime, 748 entry, nextWakeupTime); 749 } 750 if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { 751 return false; 752 } 753 754 setInjectionResultLocked(entry, injectionResult); 755 if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { 756 return true; 757 } 758 759 addMonitoringTargetsLocked(); 760 commitTargetsLocked(); 761 } 762 763 // Dispatch the motion. 764 dispatchEventToCurrentInputTargetsLocked(currentTime, entry, false); 765 return true; 766 } 767 768 769 void InputDispatcher::logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry) { 770 #if DEBUG_OUTBOUND_EVENT_DETAILS 771 LOGD("%seventTime=%lld, deviceId=0x%x, source=0x%x, policyFlags=0x%x, " 772 "action=0x%x, flags=0x%x, " 773 "metaState=0x%x, edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%lld", 774 prefix, 775 entry->eventTime, entry->deviceId, entry->source, entry->policyFlags, 776 entry->action, entry->flags, 777 entry->metaState, entry->edgeFlags, entry->xPrecision, entry->yPrecision, 778 entry->downTime); 779 780 // Print the most recent sample that we have available, this may change due to batching. 781 size_t sampleCount = 1; 782 const MotionSample* sample = & entry->firstSample; 783 for (; sample->next != NULL; sample = sample->next) { 784 sampleCount += 1; 785 } 786 for (uint32_t i = 0; i < entry->pointerCount; i++) { 787 LOGD(" Pointer %d: id=%d, x=%f, y=%f, pressure=%f, size=%f, " 788 "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " 789 "orientation=%f", 790 i, entry->pointerIds[i], 791 sample->pointerCoords[i].x, sample->pointerCoords[i].y, 792 sample->pointerCoords[i].pressure, sample->pointerCoords[i].size, 793 sample->pointerCoords[i].touchMajor, sample->pointerCoords[i].touchMinor, 794 sample->pointerCoords[i].toolMajor, sample->pointerCoords[i].toolMinor, 795 sample->pointerCoords[i].orientation); 796 } 797 798 // Keep in mind that due to batching, it is possible for the number of samples actually 799 // dispatched to change before the application finally consumed them. 800 if (entry->action == AMOTION_EVENT_ACTION_MOVE) { 801 LOGD(" ... Total movement samples currently batched %d ...", sampleCount); 802 } 803 #endif 804 } 805 806 void InputDispatcher::dispatchEventToCurrentInputTargetsLocked(nsecs_t currentTime, 807 EventEntry* eventEntry, bool resumeWithAppendedMotionSample) { 808 #if DEBUG_DISPATCH_CYCLE 809 LOGD("dispatchEventToCurrentInputTargets - " 810 "resumeWithAppendedMotionSample=%s", 811 toString(resumeWithAppendedMotionSample)); 812 #endif 813 814 assert(eventEntry->dispatchInProgress); // should already have been set to true 815 816 pokeUserActivityLocked(eventEntry); 817 818 for (size_t i = 0; i < mCurrentInputTargets.size(); i++) { 819 const InputTarget& inputTarget = mCurrentInputTargets.itemAt(i); 820 821 ssize_t connectionIndex = getConnectionIndexLocked(inputTarget.inputChannel); 822 if (connectionIndex >= 0) { 823 sp<Connection> connection = mConnectionsByReceiveFd.valueAt(connectionIndex); 824 prepareDispatchCycleLocked(currentTime, connection, eventEntry, & inputTarget, 825 resumeWithAppendedMotionSample); 826 } else { 827 #if DEBUG_FOCUS 828 LOGD("Dropping event delivery to target with channel '%s' because it " 829 "is no longer registered with the input dispatcher.", 830 inputTarget.inputChannel->getName().string()); 831 #endif 832 } 833 } 834 } 835 836 void InputDispatcher::resetTargetsLocked() { 837 mCurrentInputTargetsValid = false; 838 mCurrentInputTargets.clear(); 839 mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_NONE; 840 } 841 842 void InputDispatcher::commitTargetsLocked() { 843 mCurrentInputTargetsValid = true; 844 } 845 846 int32_t InputDispatcher::handleTargetsNotReadyLocked(nsecs_t currentTime, 847 const EventEntry* entry, const InputApplication* application, const InputWindow* window, 848 nsecs_t* nextWakeupTime) { 849 if (application == NULL && window == NULL) { 850 if (mInputTargetWaitCause != INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY) { 851 #if DEBUG_FOCUS 852 LOGD("Waiting for system to become ready for input."); 853 #endif 854 mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY; 855 mInputTargetWaitStartTime = currentTime; 856 mInputTargetWaitTimeoutTime = LONG_LONG_MAX; 857 mInputTargetWaitTimeoutExpired = false; 858 } 859 } else { 860 if (mInputTargetWaitCause != INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) { 861 #if DEBUG_FOCUS 862 LOGD("Waiting for application to become ready for input: %s", 863 getApplicationWindowLabelLocked(application, window).string()); 864 #endif 865 nsecs_t timeout = window ? window->dispatchingTimeout : 866 application ? application->dispatchingTimeout : DEFAULT_INPUT_DISPATCHING_TIMEOUT; 867 868 mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY; 869 mInputTargetWaitStartTime = currentTime; 870 mInputTargetWaitTimeoutTime = currentTime + timeout; 871 mInputTargetWaitTimeoutExpired = false; 872 } 873 } 874 875 if (mInputTargetWaitTimeoutExpired) { 876 return INPUT_EVENT_INJECTION_TIMED_OUT; 877 } 878 879 if (currentTime >= mInputTargetWaitTimeoutTime) { 880 onANRLocked(currentTime, application, window, entry->eventTime, mInputTargetWaitStartTime); 881 882 // Force poll loop to wake up immediately on next iteration once we get the 883 // ANR response back from the policy. 884 *nextWakeupTime = LONG_LONG_MIN; 885 return INPUT_EVENT_INJECTION_PENDING; 886 } else { 887 // Force poll loop to wake up when timeout is due. 888 if (mInputTargetWaitTimeoutTime < *nextWakeupTime) { 889 *nextWakeupTime = mInputTargetWaitTimeoutTime; 890 } 891 return INPUT_EVENT_INJECTION_PENDING; 892 } 893 } 894 895 void InputDispatcher::resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout, 896 const sp<InputChannel>& inputChannel) { 897 if (newTimeout > 0) { 898 // Extend the timeout. 899 mInputTargetWaitTimeoutTime = now() + newTimeout; 900 } else { 901 // Give up. 902 mInputTargetWaitTimeoutExpired = true; 903 904 // Release the touch targets. 905 mTouchState.reset(); 906 907 // Input state will not be realistic. Mark it out of sync. 908 if (inputChannel.get()) { 909 ssize_t connectionIndex = getConnectionIndexLocked(inputChannel); 910 if (connectionIndex >= 0) { 911 sp<Connection> connection = mConnectionsByReceiveFd.valueAt(connectionIndex); 912 synthesizeCancelationEventsForConnectionLocked( 913 connection, InputState::CANCEL_ALL_EVENTS, 914 "application not responding"); 915 } 916 } 917 } 918 } 919 920 nsecs_t InputDispatcher::getTimeSpentWaitingForApplicationLocked( 921 nsecs_t currentTime) { 922 if (mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) { 923 return currentTime - mInputTargetWaitStartTime; 924 } 925 return 0; 926 } 927 928 void InputDispatcher::resetANRTimeoutsLocked() { 929 #if DEBUG_FOCUS 930 LOGD("Resetting ANR timeouts."); 931 #endif 932 933 // Reset input target wait timeout. 934 mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_NONE; 935 } 936 937 int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime, 938 const EventEntry* entry, nsecs_t* nextWakeupTime) { 939 mCurrentInputTargets.clear(); 940 941 int32_t injectionResult; 942 943 // If there is no currently focused window and no focused application 944 // then drop the event. 945 if (! mFocusedWindow) { 946 if (mFocusedApplication) { 947 #if DEBUG_FOCUS 948 LOGD("Waiting because there is no focused window but there is a " 949 "focused application that may eventually add a window: %s.", 950 getApplicationWindowLabelLocked(mFocusedApplication, NULL).string()); 951 #endif 952 injectionResult = handleTargetsNotReadyLocked(currentTime, entry, 953 mFocusedApplication, NULL, nextWakeupTime); 954 goto Unresponsive; 955 } 956 957 LOGI("Dropping event because there is no focused window or focused application."); 958 injectionResult = INPUT_EVENT_INJECTION_FAILED; 959 goto Failed; 960 } 961 962 // Check permissions. 963 if (! checkInjectionPermission(mFocusedWindow, entry->injectionState)) { 964 injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; 965 goto Failed; 966 } 967 968 // If the currently focused window is paused then keep waiting. 969 if (mFocusedWindow->paused) { 970 #if DEBUG_FOCUS 971 LOGD("Waiting because focused window is paused."); 972 #endif 973 injectionResult = handleTargetsNotReadyLocked(currentTime, entry, 974 mFocusedApplication, mFocusedWindow, nextWakeupTime); 975 goto Unresponsive; 976 } 977 978 // If the currently focused window is still working on previous events then keep waiting. 979 if (! isWindowFinishedWithPreviousInputLocked(mFocusedWindow)) { 980 #if DEBUG_FOCUS 981 LOGD("Waiting because focused window still processing previous input."); 982 #endif 983 injectionResult = handleTargetsNotReadyLocked(currentTime, entry, 984 mFocusedApplication, mFocusedWindow, nextWakeupTime); 985 goto Unresponsive; 986 } 987 988 // Success! Output targets. 989 injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; 990 addWindowTargetLocked(mFocusedWindow, InputTarget::FLAG_FOREGROUND, BitSet32(0)); 991 992 // Done. 993 Failed: 994 Unresponsive: 995 nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime); 996 updateDispatchStatisticsLocked(currentTime, entry, 997 injectionResult, timeSpentWaitingForApplication); 998 #if DEBUG_FOCUS 999 LOGD("findFocusedWindow finished: injectionResult=%d, " 1000 "timeSpendWaitingForApplication=%0.1fms", 1001 injectionResult, timeSpentWaitingForApplication / 1000000.0); 1002 #endif 1003 return injectionResult; 1004 } 1005 1006 int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, 1007 const MotionEntry* entry, nsecs_t* nextWakeupTime) { 1008 enum InjectionPermission { 1009 INJECTION_PERMISSION_UNKNOWN, 1010 INJECTION_PERMISSION_GRANTED, 1011 INJECTION_PERMISSION_DENIED 1012 }; 1013 1014 mCurrentInputTargets.clear(); 1015 1016 nsecs_t startTime = now(); 1017 1018 // For security reasons, we defer updating the touch state until we are sure that 1019 // event injection will be allowed. 1020 // 1021 // FIXME In the original code, screenWasOff could never be set to true. 1022 // The reason is that the POLICY_FLAG_WOKE_HERE 1023 // and POLICY_FLAG_BRIGHT_HERE flags were set only when preprocessing raw 1024 // EV_KEY, EV_REL and EV_ABS events. As it happens, the touch event was 1025 // actually enqueued using the policyFlags that appeared in the final EV_SYN 1026 // events upon which no preprocessing took place. So policyFlags was always 0. 1027 // In the new native input dispatcher we're a bit more careful about event 1028 // preprocessing so the touches we receive can actually have non-zero policyFlags. 1029 // Unfortunately we obtain undesirable behavior. 1030 // 1031 // Here's what happens: 1032 // 1033 // When the device dims in anticipation of going to sleep, touches 1034 // in windows which have FLAG_TOUCHABLE_WHEN_WAKING cause 1035 // the device to brighten and reset the user activity timer. 1036 // Touches on other windows (such as the launcher window) 1037 // are dropped. Then after a moment, the device goes to sleep. Oops. 1038 // 1039 // Also notice how screenWasOff was being initialized using POLICY_FLAG_BRIGHT_HERE 1040 // instead of POLICY_FLAG_WOKE_HERE... 1041 // 1042 bool screenWasOff = false; // original policy: policyFlags & POLICY_FLAG_BRIGHT_HERE; 1043 1044 int32_t action = entry->action; 1045 int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; 1046 1047 // Update the touch state as needed based on the properties of the touch event. 1048 int32_t injectionResult = INPUT_EVENT_INJECTION_PENDING; 1049 InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; 1050 if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { 1051 mTempTouchState.reset(); 1052 mTempTouchState.down = true; 1053 } else { 1054 mTempTouchState.copyFrom(mTouchState); 1055 } 1056 1057 bool isSplit = mTempTouchState.split && mTempTouchState.down; 1058 if (maskedAction == AMOTION_EVENT_ACTION_DOWN 1059 || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { 1060 /* Case 1: New splittable pointer going down. */ 1061 1062 int32_t pointerIndex = getMotionEventActionPointerIndex(action); 1063 int32_t x = int32_t(entry->firstSample.pointerCoords[pointerIndex].x); 1064 int32_t y = int32_t(entry->firstSample.pointerCoords[pointerIndex].y); 1065 const InputWindow* newTouchedWindow = NULL; 1066 const InputWindow* topErrorWindow = NULL; 1067 1068 // Traverse windows from front to back to find touched window and outside targets. 1069 size_t numWindows = mWindows.size(); 1070 for (size_t i = 0; i < numWindows; i++) { 1071 const InputWindow* window = & mWindows.editItemAt(i); 1072 int32_t flags = window->layoutParamsFlags; 1073 1074 if (flags & InputWindow::FLAG_SYSTEM_ERROR) { 1075 if (! topErrorWindow) { 1076 topErrorWindow = window; 1077 } 1078 } 1079 1080 if (window->visible) { 1081 if (! (flags & InputWindow::FLAG_NOT_TOUCHABLE)) { 1082 bool isTouchModal = (flags & (InputWindow::FLAG_NOT_FOCUSABLE 1083 | InputWindow::FLAG_NOT_TOUCH_MODAL)) == 0; 1084 if (isTouchModal || window->touchableAreaContainsPoint(x, y)) { 1085 if (! screenWasOff || flags & InputWindow::FLAG_TOUCHABLE_WHEN_WAKING) { 1086 newTouchedWindow = window; 1087 } 1088 break; // found touched window, exit window loop 1089 } 1090 } 1091 1092 if (maskedAction == AMOTION_EVENT_ACTION_DOWN 1093 && (flags & InputWindow::FLAG_WATCH_OUTSIDE_TOUCH)) { 1094 int32_t outsideTargetFlags = InputTarget::FLAG_OUTSIDE; 1095 if (isWindowObscuredAtPointLocked(window, x, y)) { 1096 outsideTargetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; 1097 } 1098 1099 mTempTouchState.addOrUpdateWindow(window, outsideTargetFlags, BitSet32(0)); 1100 } 1101 } 1102 } 1103 1104 // If there is an error window but it is not taking focus (typically because 1105 // it is invisible) then wait for it. Any other focused window may in 1106 // fact be in ANR state. 1107 if (topErrorWindow && newTouchedWindow != topErrorWindow) { 1108 #if DEBUG_FOCUS 1109 LOGD("Waiting because system error window is pending."); 1110 #endif 1111 injectionResult = handleTargetsNotReadyLocked(currentTime, entry, 1112 NULL, NULL, nextWakeupTime); 1113 injectionPermission = INJECTION_PERMISSION_UNKNOWN; 1114 goto Unresponsive; 1115 } 1116 1117 // Figure out whether splitting will be allowed for this window. 1118 if (newTouchedWindow 1119 && (newTouchedWindow->layoutParamsFlags & InputWindow::FLAG_SPLIT_TOUCH)) { 1120 // New window supports splitting. 1121 isSplit = true; 1122 } else if (isSplit) { 1123 // New window does not support splitting but we have already split events. 1124 // Assign the pointer to the first foreground window we find. 1125 // (May be NULL which is why we put this code block before the next check.) 1126 newTouchedWindow = mTempTouchState.getFirstForegroundWindow(); 1127 } 1128 1129 // If we did not find a touched window then fail. 1130 if (! newTouchedWindow) { 1131 if (mFocusedApplication) { 1132 #if DEBUG_FOCUS 1133 LOGD("Waiting because there is no touched window but there is a " 1134 "focused application that may eventually add a new window: %s.", 1135 getApplicationWindowLabelLocked(mFocusedApplication, NULL).string()); 1136 #endif 1137 injectionResult = handleTargetsNotReadyLocked(currentTime, entry, 1138 mFocusedApplication, NULL, nextWakeupTime); 1139 goto Unresponsive; 1140 } 1141 1142 LOGI("Dropping event because there is no touched window or focused application."); 1143 injectionResult = INPUT_EVENT_INJECTION_FAILED; 1144 goto Failed; 1145 } 1146 1147 // Set target flags. 1148 int32_t targetFlags = InputTarget::FLAG_FOREGROUND; 1149 if (isSplit) { 1150 targetFlags |= InputTarget::FLAG_SPLIT; 1151 } 1152 if (isWindowObscuredAtPointLocked(newTouchedWindow, x, y)) { 1153 targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; 1154 } 1155 1156 // Update the temporary touch state. 1157 BitSet32 pointerIds; 1158 if (isSplit) { 1159 uint32_t pointerId = entry->pointerIds[pointerIndex]; 1160 pointerIds.markBit(pointerId); 1161 } 1162 mTempTouchState.addOrUpdateWindow(newTouchedWindow, targetFlags, pointerIds); 1163 } else { 1164 /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ 1165 1166 // If the pointer is not currently down, then ignore the event. 1167 if (! mTempTouchState.down) { 1168 LOGI("Dropping event because the pointer is not down."); 1169 injectionResult = INPUT_EVENT_INJECTION_FAILED; 1170 goto Failed; 1171 } 1172 } 1173 1174 // Check permission to inject into all touched foreground windows and ensure there 1175 // is at least one touched foreground window. 1176 { 1177 bool haveForegroundWindow = false; 1178 for (size_t i = 0; i < mTempTouchState.windows.size(); i++) { 1179 const TouchedWindow& touchedWindow = mTempTouchState.windows[i]; 1180 if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { 1181 haveForegroundWindow = true; 1182 if (! checkInjectionPermission(touchedWindow.window, entry->injectionState)) { 1183 injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; 1184 injectionPermission = INJECTION_PERMISSION_DENIED; 1185 goto Failed; 1186 } 1187 } 1188 } 1189 if (! haveForegroundWindow) { 1190 #if DEBUG_INPUT_DISPATCHER_POLICY 1191 LOGD("Dropping event because there is no touched foreground window to receive it."); 1192 #endif 1193 injectionResult = INPUT_EVENT_INJECTION_FAILED; 1194 goto Failed; 1195 } 1196 1197 // Permission granted to injection into all touched foreground windows. 1198 injectionPermission = INJECTION_PERMISSION_GRANTED; 1199 } 1200 1201 // Ensure all touched foreground windows are ready for new input. 1202 for (size_t i = 0; i < mTempTouchState.windows.size(); i++) { 1203 const TouchedWindow& touchedWindow = mTempTouchState.windows[i]; 1204 if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { 1205 // If the touched window is paused then keep waiting. 1206 if (touchedWindow.window->paused) { 1207 #if DEBUG_INPUT_DISPATCHER_POLICY 1208 LOGD("Waiting because touched window is paused."); 1209 #endif 1210 injectionResult = handleTargetsNotReadyLocked(currentTime, entry, 1211 NULL, touchedWindow.window, nextWakeupTime); 1212 goto Unresponsive; 1213 } 1214 1215 // If the touched window is still working on previous events then keep waiting. 1216 if (! isWindowFinishedWithPreviousInputLocked(touchedWindow.window)) { 1217 #if DEBUG_FOCUS 1218 LOGD("Waiting because touched window still processing previous input."); 1219 #endif 1220 injectionResult = handleTargetsNotReadyLocked(currentTime, entry, 1221 NULL, touchedWindow.window, nextWakeupTime); 1222 goto Unresponsive; 1223 } 1224 } 1225 } 1226 1227 // If this is the first pointer going down and the touched window has a wallpaper 1228 // then also add the touched wallpaper windows so they are locked in for the duration 1229 // of the touch gesture. 1230 if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { 1231 const InputWindow* foregroundWindow = mTempTouchState.getFirstForegroundWindow(); 1232 if (foregroundWindow->hasWallpaper) { 1233 for (size_t i = 0; i < mWindows.size(); i++) { 1234 const InputWindow* window = & mWindows[i]; 1235 if (window->layoutParamsType == InputWindow::TYPE_WALLPAPER) { 1236 mTempTouchState.addOrUpdateWindow(window, 1237 InputTarget::FLAG_WINDOW_IS_OBSCURED, BitSet32(0)); 1238 } 1239 } 1240 } 1241 } 1242 1243 // Success! Output targets. 1244 injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; 1245 1246 for (size_t i = 0; i < mTempTouchState.windows.size(); i++) { 1247 const TouchedWindow& touchedWindow = mTempTouchState.windows.itemAt(i); 1248 addWindowTargetLocked(touchedWindow.window, touchedWindow.targetFlags, 1249 touchedWindow.pointerIds); 1250 } 1251 1252 // Drop the outside touch window since we will not care about them in the next iteration. 1253 mTempTouchState.removeOutsideTouchWindows(); 1254 1255 Failed: 1256 // Check injection permission once and for all. 1257 if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { 1258 if (checkInjectionPermission(NULL, entry->injectionState)) { 1259 injectionPermission = INJECTION_PERMISSION_GRANTED; 1260 } else { 1261 injectionPermission = INJECTION_PERMISSION_DENIED; 1262 } 1263 } 1264 1265 // Update final pieces of touch state if the injector had permission. 1266 if (injectionPermission == INJECTION_PERMISSION_GRANTED) { 1267 if (maskedAction == AMOTION_EVENT_ACTION_UP 1268 || maskedAction == AMOTION_EVENT_ACTION_CANCEL) { 1269 // All pointers up or canceled. 1270 mTempTouchState.reset(); 1271 } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { 1272 // First pointer went down. 1273 if (mTouchState.down) { 1274 #if DEBUG_FOCUS 1275 LOGD("Pointer down received while already down."); 1276 #endif 1277 } 1278 } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { 1279 // One pointer went up. 1280 if (isSplit) { 1281 int32_t pointerIndex = getMotionEventActionPointerIndex(action); 1282 uint32_t pointerId = entry->pointerIds[pointerIndex]; 1283 1284 for (size_t i = 0; i < mTempTouchState.windows.size(); ) { 1285 TouchedWindow& touchedWindow = mTempTouchState.windows.editItemAt(i); 1286 if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { 1287 touchedWindow.pointerIds.clearBit(pointerId); 1288 if (touchedWindow.pointerIds.isEmpty()) { 1289 mTempTouchState.windows.removeAt(i); 1290 continue; 1291 } 1292 } 1293 i += 1; 1294 } 1295 } 1296 } 1297 1298 // Save changes to touch state. 1299 mTouchState.copyFrom(mTempTouchState); 1300 } else { 1301 #if DEBUG_FOCUS 1302 LOGD("Not updating touch focus because injection was denied."); 1303 #endif 1304 } 1305 1306 Unresponsive: 1307 // Reset temporary touch state to ensure we release unnecessary references to input channels. 1308 mTempTouchState.reset(); 1309 1310 nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime); 1311 updateDispatchStatisticsLocked(currentTime, entry, 1312 injectionResult, timeSpentWaitingForApplication); 1313 #if DEBUG_FOCUS 1314 LOGD("findTouchedWindow finished: injectionResult=%d, injectionPermission=%d, " 1315 "timeSpentWaitingForApplication=%0.1fms", 1316 injectionResult, injectionPermission, timeSpentWaitingForApplication / 1000000.0); 1317 #endif 1318 return injectionResult; 1319 } 1320 1321 void InputDispatcher::addWindowTargetLocked(const InputWindow* window, int32_t targetFlags, 1322 BitSet32 pointerIds) { 1323 mCurrentInputTargets.push(); 1324 1325 InputTarget& target = mCurrentInputTargets.editTop(); 1326 target.inputChannel = window->inputChannel; 1327 target.flags = targetFlags; 1328 target.xOffset = - window->frameLeft; 1329 target.yOffset = - window->frameTop; 1330 target.pointerIds = pointerIds; 1331 } 1332 1333 void InputDispatcher::addMonitoringTargetsLocked() { 1334 for (size_t i = 0; i < mMonitoringChannels.size(); i++) { 1335 mCurrentInputTargets.push(); 1336 1337 InputTarget& target = mCurrentInputTargets.editTop(); 1338 target.inputChannel = mMonitoringChannels[i]; 1339 target.flags = 0; 1340 target.xOffset = 0; 1341 target.yOffset = 0; 1342 } 1343 } 1344 1345 bool InputDispatcher::checkInjectionPermission(const InputWindow* window, 1346 const InjectionState* injectionState) { 1347 if (injectionState 1348 && (window == NULL || window->ownerUid != injectionState->injectorUid) 1349 && !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { 1350 if (window) { 1351 LOGW("Permission denied: injecting event from pid %d uid %d to window " 1352 "with input channel %s owned by uid %d", 1353 injectionState->injectorPid, injectionState->injectorUid, 1354 window->inputChannel->getName().string(), 1355 window->ownerUid); 1356 } else { 1357 LOGW("Permission denied: injecting event from pid %d uid %d", 1358 injectionState->injectorPid, injectionState->injectorUid); 1359 } 1360 return false; 1361 } 1362 return true; 1363 } 1364 1365 bool InputDispatcher::isWindowObscuredAtPointLocked( 1366 const InputWindow* window, int32_t x, int32_t y) const { 1367 size_t numWindows = mWindows.size(); 1368 for (size_t i = 0; i < numWindows; i++) { 1369 const InputWindow* other = & mWindows.itemAt(i); 1370 if (other == window) { 1371 break; 1372 } 1373 if (other->visible && ! other->isTrustedOverlay() && other->frameContainsPoint(x, y)) { 1374 return true; 1375 } 1376 } 1377 return false; 1378 } 1379 1380 bool InputDispatcher::isWindowFinishedWithPreviousInputLocked(const InputWindow* window) { 1381 ssize_t connectionIndex = getConnectionIndexLocked(window->inputChannel); 1382 if (connectionIndex >= 0) { 1383 sp<Connection> connection = mConnectionsByReceiveFd.valueAt(connectionIndex); 1384 return connection->outboundQueue.isEmpty(); 1385 } else { 1386 return true; 1387 } 1388 } 1389 1390 String8 InputDispatcher::getApplicationWindowLabelLocked(const InputApplication* application, 1391 const InputWindow* window) { 1392 if (application) { 1393 if (window) { 1394 String8 label(application->name); 1395 label.append(" - "); 1396 label.append(window->name); 1397 return label; 1398 } else { 1399 return application->name; 1400 } 1401 } else if (window) { 1402 return window->name; 1403 } else { 1404 return String8("<unknown application or window>"); 1405 } 1406 } 1407 1408 void InputDispatcher::pokeUserActivityLocked(const EventEntry* eventEntry) { 1409 int32_t eventType = POWER_MANAGER_BUTTON_EVENT; 1410 switch (eventEntry->type) { 1411 case EventEntry::TYPE_MOTION: { 1412 const MotionEntry* motionEntry = static_cast<const MotionEntry*>(eventEntry); 1413 if (motionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { 1414 return; 1415 } 1416 1417 if (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) { 1418 switch (motionEntry->action) { 1419 case AMOTION_EVENT_ACTION_DOWN: 1420 eventType = POWER_MANAGER_TOUCH_EVENT; 1421 break; 1422 case AMOTION_EVENT_ACTION_UP: 1423 eventType = POWER_MANAGER_TOUCH_UP_EVENT; 1424 break; 1425 default: 1426 if (motionEntry->eventTime - motionEntry->downTime < LONG_TOUCH_DELAY) { 1427 eventType = POWER_MANAGER_TOUCH_EVENT; 1428 } else { 1429 eventType = POWER_MANAGER_LONG_TOUCH_EVENT; 1430 } 1431 break; 1432 } 1433 } 1434 break; 1435 } 1436 case EventEntry::TYPE_KEY: { 1437 const KeyEntry* keyEntry = static_cast<const KeyEntry*>(eventEntry); 1438 if (keyEntry->flags & AKEY_EVENT_FLAG_CANCELED) { 1439 return; 1440 } 1441 break; 1442 } 1443 } 1444 1445 CommandEntry* commandEntry = postCommandLocked( 1446 & InputDispatcher::doPokeUserActivityLockedInterruptible); 1447 commandEntry->eventTime = eventEntry->eventTime; 1448 commandEntry->userActivityEventType = eventType; 1449 } 1450 1451 void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, 1452 const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget, 1453 bool resumeWithAppendedMotionSample) { 1454 #if DEBUG_DISPATCH_CYCLE 1455 LOGD("channel '%s' ~ prepareDispatchCycle - flags=%d, " 1456 "xOffset=%f, yOffset=%f, " 1457 "windowType=%d, pointerIds=0x%x, " 1458 "resumeWithAppendedMotionSample=%s", 1459 connection->getInputChannelName(), inputTarget->flags, 1460 inputTarget->xOffset, inputTarget->yOffset, 1461 inputTarget->windowType, inputTarget->pointerIds.value, 1462 toString(resumeWithAppendedMotionSample)); 1463 #endif 1464 1465 // Make sure we are never called for streaming when splitting across multiple windows. 1466 bool isSplit = inputTarget->flags & InputTarget::FLAG_SPLIT; 1467 assert(! (resumeWithAppendedMotionSample && isSplit)); 1468 1469 // Skip this event if the connection status is not normal. 1470 // We don't want to enqueue additional outbound events if the connection is broken. 1471 if (connection->status != Connection::STATUS_NORMAL) { 1472 #if DEBUG_DISPATCH_CYCLE 1473 LOGD("channel '%s' ~ Dropping event because the channel status is %s", 1474 connection->getInputChannelName(), connection->getStatusLabel()); 1475 #endif 1476 return; 1477 } 1478 1479 // Split a motion event if needed. 1480 if (isSplit) { 1481 assert(eventEntry->type == EventEntry::TYPE_MOTION); 1482 1483 MotionEntry* originalMotionEntry = static_cast<MotionEntry*>(eventEntry); 1484 if (inputTarget->pointerIds.count() != originalMotionEntry->pointerCount) { 1485 MotionEntry* splitMotionEntry = splitMotionEvent( 1486 originalMotionEntry, inputTarget->pointerIds); 1487 #if DEBUG_FOCUS 1488 LOGD("channel '%s' ~ Split motion event.", 1489 connection->getInputChannelName()); 1490 logOutboundMotionDetailsLocked(" ", splitMotionEntry); 1491 #endif 1492 eventEntry = splitMotionEntry; 1493 } 1494 } 1495 1496 // Resume the dispatch cycle with a freshly appended motion sample. 1497 // First we check that the last dispatch entry in the outbound queue is for the same 1498 // motion event to which we appended the motion sample. If we find such a dispatch 1499 // entry, and if it is currently in progress then we try to stream the new sample. 1500 bool wasEmpty = connection->outboundQueue.isEmpty(); 1501 1502 if (! wasEmpty && resumeWithAppendedMotionSample) { 1503 DispatchEntry* motionEventDispatchEntry = 1504 connection->findQueuedDispatchEntryForEvent(eventEntry); 1505 if (motionEventDispatchEntry) { 1506 // If the dispatch entry is not in progress, then we must be busy dispatching an 1507 // earlier event. Not a problem, the motion event is on the outbound queue and will 1508 // be dispatched later. 1509 if (! motionEventDispatchEntry->inProgress) { 1510 #if DEBUG_BATCHING 1511 LOGD("channel '%s' ~ Not streaming because the motion event has " 1512 "not yet been dispatched. " 1513 "(Waiting for earlier events to be consumed.)", 1514 connection->getInputChannelName()); 1515 #endif 1516 return; 1517 } 1518 1519 // If the dispatch entry is in progress but it already has a tail of pending 1520 // motion samples, then it must mean that the shared memory buffer filled up. 1521 // Not a problem, when this dispatch cycle is finished, we will eventually start 1522 // a new dispatch cycle to process the tail and that tail includes the newly 1523 // appended motion sample. 1524 if (motionEventDispatchEntry->tailMotionSample) { 1525 #if DEBUG_BATCHING 1526 LOGD("channel '%s' ~ Not streaming because no new samples can " 1527 "be appended to the motion event in this dispatch cycle. " 1528 "(Waiting for next dispatch cycle to start.)", 1529 connection->getInputChannelName()); 1530 #endif 1531 return; 1532 } 1533 1534 // The dispatch entry is in progress and is still potentially open for streaming. 1535 // Try to stream the new motion sample. This might fail if the consumer has already 1536 // consumed the motion event (or if the channel is broken). 1537 MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); 1538 MotionSample* appendedMotionSample = motionEntry->lastSample; 1539 status_t status = connection->inputPublisher.appendMotionSample( 1540 appendedMotionSample->eventTime, appendedMotionSample->pointerCoords); 1541 if (status == OK) { 1542 #if DEBUG_BATCHING 1543 LOGD("channel '%s' ~ Successfully streamed new motion sample.", 1544 connection->getInputChannelName()); 1545 #endif 1546 return; 1547 } 1548 1549 #if DEBUG_BATCHING 1550 if (status == NO_MEMORY) { 1551 LOGD("channel '%s' ~ Could not append motion sample to currently " 1552 "dispatched move event because the shared memory buffer is full. " 1553 "(Waiting for next dispatch cycle to start.)", 1554 connection->getInputChannelName()); 1555 } else if (status == status_t(FAILED_TRANSACTION)) { 1556 LOGD("channel '%s' ~ Could not append motion sample to currently " 1557 "dispatched move event because the event has already been consumed. " 1558 "(Waiting for next dispatch cycle to start.)", 1559 connection->getInputChannelName()); 1560 } else { 1561 LOGD("channel '%s' ~ Could not append motion sample to currently " 1562 "dispatched move event due to an error, status=%d. " 1563 "(Waiting for next dispatch cycle to start.)", 1564 connection->getInputChannelName(), status); 1565 } 1566 #endif 1567 // Failed to stream. Start a new tail of pending motion samples to dispatch 1568 // in the next cycle. 1569 motionEventDispatchEntry->tailMotionSample = appendedMotionSample; 1570 return; 1571 } 1572 } 1573 1574 // This is a new event. 1575 // Enqueue a new dispatch entry onto the outbound queue for this connection. 1576 DispatchEntry* dispatchEntry = mAllocator.obtainDispatchEntry(eventEntry, // increments ref 1577 inputTarget->flags, inputTarget->xOffset, inputTarget->yOffset); 1578 if (dispatchEntry->hasForegroundTarget()) { 1579 incrementPendingForegroundDispatchesLocked(eventEntry); 1580 } 1581 1582 // Handle the case where we could not stream a new motion sample because the consumer has 1583 // already consumed the motion event (otherwise the corresponding dispatch entry would 1584 // still be in the outbound queue for this connection). We set the head motion sample 1585 // to the list starting with the newly appended motion sample. 1586 if (resumeWithAppendedMotionSample) { 1587 #if DEBUG_BATCHING 1588 LOGD("channel '%s' ~ Preparing a new dispatch cycle for additional motion samples " 1589 "that cannot be streamed because the motion event has already been consumed.", 1590 connection->getInputChannelName()); 1591 #endif 1592 MotionSample* appendedMotionSample = static_cast<MotionEntry*>(eventEntry)->lastSample; 1593 dispatchEntry->headMotionSample = appendedMotionSample; 1594 } 1595 1596 // Enqueue the dispatch entry. 1597 connection->outboundQueue.enqueueAtTail(dispatchEntry); 1598 1599 // If the outbound queue was previously empty, start the dispatch cycle going. 1600 if (wasEmpty) { 1601 activateConnectionLocked(connection.get()); 1602 startDispatchCycleLocked(currentTime, connection); 1603 } 1604 } 1605 1606 void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, 1607 const sp<Connection>& connection) { 1608 #if DEBUG_DISPATCH_CYCLE 1609 LOGD("channel '%s' ~ startDispatchCycle", 1610 connection->getInputChannelName()); 1611 #endif 1612 1613 assert(connection->status == Connection::STATUS_NORMAL); 1614 assert(! connection->outboundQueue.isEmpty()); 1615 1616 DispatchEntry* dispatchEntry = connection->outboundQueue.headSentinel.next; 1617 assert(! dispatchEntry->inProgress); 1618 1619 // Mark the dispatch entry as in progress. 1620 dispatchEntry->inProgress = true; 1621 1622 // Update the connection's input state. 1623 EventEntry* eventEntry = dispatchEntry->eventEntry; 1624 InputState::Consistency consistency = connection->inputState.trackEvent(eventEntry); 1625 1626 #if FILTER_INPUT_EVENTS 1627 // Filter out inconsistent sequences of input events. 1628 // The input system may drop or inject events in a way that could violate implicit 1629 // invariants on input state and potentially cause an application to crash 1630 // or think that a key or pointer is stuck down. Technically we make no guarantees 1631 // of consistency but it would be nice to improve on this where possible. 1632 // XXX: This code is a proof of concept only. Not ready for prime time. 1633 if (consistency == InputState::TOLERABLE) { 1634 #if DEBUG_DISPATCH_CYCLE 1635 LOGD("channel '%s' ~ Sending an event that is inconsistent with the connection's " 1636 "current input state but that is likely to be tolerated by the application.", 1637 connection->getInputChannelName()); 1638 #endif 1639 } else if (consistency == InputState::BROKEN) { 1640 LOGI("channel '%s' ~ Dropping an event that is inconsistent with the connection's " 1641 "current input state and that is likely to cause the application to crash.", 1642 connection->getInputChannelName()); 1643 startNextDispatchCycleLocked(currentTime, connection); 1644 return; 1645 } 1646 #endif 1647 1648 // Publish the event. 1649 status_t status; 1650 switch (eventEntry->type) { 1651 case EventEntry::TYPE_KEY: { 1652 KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); 1653 1654 // Apply target flags. 1655 int32_t action = keyEntry->action; 1656 int32_t flags = keyEntry->flags; 1657 1658 // Publish the key event. 1659 status = connection->inputPublisher.publishKeyEvent(keyEntry->deviceId, keyEntry->source, 1660 action, flags, keyEntry->keyCode, keyEntry->scanCode, 1661 keyEntry->metaState, keyEntry->repeatCount, keyEntry->downTime, 1662 keyEntry->eventTime); 1663 1664 if (status) { 1665 LOGE("channel '%s' ~ Could not publish key event, " 1666 "status=%d", connection->getInputChannelName(), status); 1667 abortBrokenDispatchCycleLocked(currentTime, connection); 1668 return; 1669 } 1670 break; 1671 } 1672 1673 case EventEntry::TYPE_MOTION: { 1674 MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); 1675 1676 // Apply target flags. 1677 int32_t action = motionEntry->action; 1678 int32_t flags = motionEntry->flags; 1679 if (dispatchEntry->targetFlags & InputTarget::FLAG_OUTSIDE) { 1680 action = AMOTION_EVENT_ACTION_OUTSIDE; 1681 } 1682 if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { 1683 flags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; 1684 } 1685 1686 // If headMotionSample is non-NULL, then it points to the first new sample that we 1687 // were unable to dispatch during the previous cycle so we resume dispatching from 1688 // that point in the list of motion samples. 1689 // Otherwise, we just start from the first sample of the motion event. 1690 MotionSample* firstMotionSample = dispatchEntry->headMotionSample; 1691 if (! firstMotionSample) { 1692 firstMotionSample = & motionEntry->firstSample; 1693 } 1694 1695 // Set the X and Y offset depending on the input source. 1696 float xOffset, yOffset; 1697 if (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) { 1698 xOffset = dispatchEntry->xOffset; 1699 yOffset = dispatchEntry->yOffset; 1700 } else { 1701 xOffset = 0.0f; 1702 yOffset = 0.0f; 1703 } 1704 1705 // Publish the motion event and the first motion sample. 1706 status = connection->inputPublisher.publishMotionEvent(motionEntry->deviceId, 1707 motionEntry->source, action, flags, motionEntry->edgeFlags, motionEntry->metaState, 1708 xOffset, yOffset, 1709 motionEntry->xPrecision, motionEntry->yPrecision, 1710 motionEntry->downTime, firstMotionSample->eventTime, 1711 motionEntry->pointerCount, motionEntry->pointerIds, 1712 firstMotionSample->pointerCoords); 1713 1714 if (status) { 1715 LOGE("channel '%s' ~ Could not publish motion event, " 1716 "status=%d", connection->getInputChannelName(), status); 1717 abortBrokenDispatchCycleLocked(currentTime, connection); 1718 return; 1719 } 1720 1721 // Append additional motion samples. 1722 MotionSample* nextMotionSample = firstMotionSample->next; 1723 for (; nextMotionSample != NULL; nextMotionSample = nextMotionSample->next) { 1724 status = connection->inputPublisher.appendMotionSample( 1725 nextMotionSample->eventTime, nextMotionSample->pointerCoords); 1726 if (status == NO_MEMORY) { 1727 #if DEBUG_DISPATCH_CYCLE 1728 LOGD("channel '%s' ~ Shared memory buffer full. Some motion samples will " 1729 "be sent in the next dispatch cycle.", 1730 connection->getInputChannelName()); 1731 #endif 1732 break; 1733 } 1734 if (status != OK) { 1735 LOGE("channel '%s' ~ Could not append motion sample " 1736 "for a reason other than out of memory, status=%d", 1737 connection->getInputChannelName(), status); 1738 abortBrokenDispatchCycleLocked(currentTime, connection); 1739 return; 1740 } 1741 } 1742 1743 // Remember the next motion sample that we could not dispatch, in case we ran out 1744 // of space in the shared memory buffer. 1745 dispatchEntry->tailMotionSample = nextMotionSample; 1746 break; 1747 } 1748 1749 default: { 1750 assert(false); 1751 } 1752 } 1753 1754 // Send the dispatch signal. 1755 status = connection->inputPublisher.sendDispatchSignal(); 1756 if (status) { 1757 LOGE("channel '%s' ~ Could not send dispatch signal, status=%d", 1758 connection->getInputChannelName(), status); 1759 abortBrokenDispatchCycleLocked(currentTime, connection); 1760 return; 1761 } 1762 1763 // Record information about the newly started dispatch cycle. 1764 connection->lastEventTime = eventEntry->eventTime; 1765 connection->lastDispatchTime = currentTime; 1766 1767 // Notify other system components. 1768 onDispatchCycleStartedLocked(currentTime, connection); 1769 } 1770 1771 void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, 1772 const sp<Connection>& connection) { 1773 #if DEBUG_DISPATCH_CYCLE 1774 LOGD("channel '%s' ~ finishDispatchCycle - %01.1fms since event, " 1775 "%01.1fms since dispatch", 1776 connection->getInputChannelName(), 1777 connection->getEventLatencyMillis(currentTime), 1778 connection->getDispatchLatencyMillis(currentTime)); 1779 #endif 1780 1781 if (connection->status == Connection::STATUS_BROKEN 1782 || connection->status == Connection::STATUS_ZOMBIE) { 1783 return; 1784 } 1785 1786 // Notify other system components. 1787 onDispatchCycleFinishedLocked(currentTime, connection); 1788 1789 // Reset the publisher since the event has been consumed. 1790 // We do this now so that the publisher can release some of its internal resources 1791 // while waiting for the next dispatch cycle to begin. 1792 status_t status = connection->inputPublisher.reset(); 1793 if (status) { 1794 LOGE("channel '%s' ~ Could not reset publisher, status=%d", 1795 connection->getInputChannelName(), status); 1796 abortBrokenDispatchCycleLocked(currentTime, connection); 1797 return; 1798 } 1799 1800 startNextDispatchCycleLocked(currentTime, connection); 1801 } 1802 1803 void InputDispatcher::startNextDispatchCycleLocked(nsecs_t currentTime, 1804 const sp<Connection>& connection) { 1805 // Start the next dispatch cycle for this connection. 1806 while (! connection->outboundQueue.isEmpty()) { 1807 DispatchEntry* dispatchEntry = connection->outboundQueue.headSentinel.next; 1808 if (dispatchEntry->inProgress) { 1809 // Finish or resume current event in progress. 1810 if (dispatchEntry->tailMotionSample) { 1811 // We have a tail of undispatched motion samples. 1812 // Reuse the same DispatchEntry and start a new cycle. 1813 dispatchEntry->inProgress = false; 1814 dispatchEntry->headMotionSample = dispatchEntry->tailMotionSample; 1815 dispatchEntry->tailMotionSample = NULL; 1816 startDispatchCycleLocked(currentTime, connection); 1817 return; 1818 } 1819 // Finished. 1820 connection->outboundQueue.dequeueAtHead(); 1821 if (dispatchEntry->hasForegroundTarget()) { 1822 decrementPendingForegroundDispatchesLocked(dispatchEntry->eventEntry); 1823 } 1824 mAllocator.releaseDispatchEntry(dispatchEntry); 1825 } else { 1826 // If the head is not in progress, then we must have already dequeued the in 1827 // progress event, which means we actually aborted it. 1828 // So just start the next event for this connection. 1829 startDispatchCycleLocked(currentTime, connection); 1830 return; 1831 } 1832 } 1833 1834 // Outbound queue is empty, deactivate the connection. 1835 deactivateConnectionLocked(connection.get()); 1836 } 1837 1838 void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, 1839 const sp<Connection>& connection) { 1840 #if DEBUG_DISPATCH_CYCLE 1841 LOGD("channel '%s' ~ abortBrokenDispatchCycle - broken=%s", 1842 connection->getInputChannelName(), toString(broken)); 1843 #endif 1844 1845 // Clear the outbound queue. 1846 drainOutboundQueueLocked(connection.get()); 1847 1848 // The connection appears to be unrecoverably broken. 1849 // Ignore already broken or zombie connections. 1850 if (connection->status == Connection::STATUS_NORMAL) { 1851 connection->status = Connection::STATUS_BROKEN; 1852 1853 // Notify other system components. 1854 onDispatchCycleBrokenLocked(currentTime, connection); 1855 } 1856 } 1857 1858 void InputDispatcher::drainOutboundQueueLocked(Connection* connection) { 1859 while (! connection->outboundQueue.isEmpty()) { 1860 DispatchEntry* dispatchEntry = connection->outboundQueue.dequeueAtHead(); 1861 if (dispatchEntry->hasForegroundTarget()) { 1862 decrementPendingForegroundDispatchesLocked(dispatchEntry->eventEntry); 1863 } 1864 mAllocator.releaseDispatchEntry(dispatchEntry); 1865 } 1866 1867 deactivateConnectionLocked(connection); 1868 } 1869 1870 int InputDispatcher::handleReceiveCallback(int receiveFd, int events, void* data) { 1871 InputDispatcher* d = static_cast<InputDispatcher*>(data); 1872 1873 { // acquire lock 1874 AutoMutex _l(d->mLock); 1875 1876 ssize_t connectionIndex = d->mConnectionsByReceiveFd.indexOfKey(receiveFd); 1877 if (connectionIndex < 0) { 1878 LOGE("Received spurious receive callback for unknown input channel. " 1879 "fd=%d, events=0x%x", receiveFd, events); 1880 return 0; // remove the callback 1881 } 1882 1883 nsecs_t currentTime = now(); 1884 1885 sp<Connection> connection = d->mConnectionsByReceiveFd.valueAt(connectionIndex); 1886 if (events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP)) { 1887 LOGE("channel '%s' ~ Consumer closed input channel or an error occurred. " 1888 "events=0x%x", connection->getInputChannelName(), events); 1889 d->abortBrokenDispatchCycleLocked(currentTime, connection); 1890 d->runCommandsLockedInterruptible(); 1891 return 0; // remove the callback 1892 } 1893 1894 if (! (events & ALOOPER_EVENT_INPUT)) { 1895 LOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " 1896 "events=0x%x", connection->getInputChannelName(), events); 1897 return 1; 1898 } 1899 1900 status_t status = connection->inputPublisher.receiveFinishedSignal(); 1901 if (status) { 1902 LOGE("channel '%s' ~ Failed to receive finished signal. status=%d", 1903 connection->getInputChannelName(), status); 1904 d->abortBrokenDispatchCycleLocked(currentTime, connection); 1905 d->runCommandsLockedInterruptible(); 1906 return 0; // remove the callback 1907 } 1908 1909 d->finishDispatchCycleLocked(currentTime, connection); 1910 d->runCommandsLockedInterruptible(); 1911 return 1; 1912 } // release lock 1913 } 1914 1915 void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( 1916 InputState::CancelationOptions options, const char* reason) { 1917 for (size_t i = 0; i < mConnectionsByReceiveFd.size(); i++) { 1918 synthesizeCancelationEventsForConnectionLocked( 1919 mConnectionsByReceiveFd.valueAt(i), options, reason); 1920 } 1921 } 1922 1923 void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( 1924 const sp<InputChannel>& channel, InputState::CancelationOptions options, 1925 const char* reason) { 1926 ssize_t index = getConnectionIndexLocked(channel); 1927 if (index >= 0) { 1928 synthesizeCancelationEventsForConnectionLocked( 1929 mConnectionsByReceiveFd.valueAt(index), options, reason); 1930 } 1931 } 1932 1933 void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( 1934 const sp<Connection>& connection, InputState::CancelationOptions options, 1935 const char* reason) { 1936 nsecs_t currentTime = now(); 1937 1938 mTempCancelationEvents.clear(); 1939 connection->inputState.synthesizeCancelationEvents(currentTime, & mAllocator, 1940 mTempCancelationEvents, options); 1941 1942 if (! mTempCancelationEvents.isEmpty() 1943 && connection->status != Connection::STATUS_BROKEN) { 1944 #if DEBUG_OUTBOUND_EVENT_DETAILS 1945 LOGD("channel '%s' ~ Synthesized %d cancelation events to bring channel back in sync " 1946 "with reality: %s, options=%d.", 1947 connection->getInputChannelName(), mTempCancelationEvents.size(), reason, options); 1948 #endif 1949 for (size_t i = 0; i < mTempCancelationEvents.size(); i++) { 1950 EventEntry* cancelationEventEntry = mTempCancelationEvents.itemAt(i); 1951 switch (cancelationEventEntry->type) { 1952 case EventEntry::TYPE_KEY: 1953 logOutboundKeyDetailsLocked("cancel - ", 1954 static_cast<KeyEntry*>(cancelationEventEntry)); 1955 break; 1956 case EventEntry::TYPE_MOTION: 1957 logOutboundMotionDetailsLocked("cancel - ", 1958 static_cast<MotionEntry*>(cancelationEventEntry)); 1959 break; 1960 } 1961 1962 int32_t xOffset, yOffset; 1963 const InputWindow* window = getWindowLocked(connection->inputChannel); 1964 if (window) { 1965 xOffset = -window->frameLeft; 1966 yOffset = -window->frameTop; 1967 } else { 1968 xOffset = 0; 1969 yOffset = 0; 1970 } 1971 1972 DispatchEntry* cancelationDispatchEntry = 1973 mAllocator.obtainDispatchEntry(cancelationEventEntry, // increments ref 1974 0, xOffset, yOffset); 1975 connection->outboundQueue.enqueueAtTail(cancelationDispatchEntry); 1976 1977 mAllocator.releaseEventEntry(cancelationEventEntry); 1978 } 1979 1980 if (!connection->outboundQueue.headSentinel.next->inProgress) { 1981 startDispatchCycleLocked(currentTime, connection); 1982 } 1983 } 1984 } 1985 1986 InputDispatcher::MotionEntry* 1987 InputDispatcher::splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds) { 1988 assert(pointerIds.value != 0); 1989 1990 uint32_t splitPointerIndexMap[MAX_POINTERS]; 1991 int32_t splitPointerIds[MAX_POINTERS]; 1992 PointerCoords splitPointerCoords[MAX_POINTERS]; 1993 1994 uint32_t originalPointerCount = originalMotionEntry->pointerCount; 1995 uint32_t splitPointerCount = 0; 1996 1997 for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; 1998 originalPointerIndex++) { 1999 int32_t pointerId = uint32_t(originalMotionEntry->pointerIds[originalPointerIndex]); 2000 if (pointerIds.hasBit(pointerId)) { 2001 splitPointerIndexMap[splitPointerCount] = originalPointerIndex; 2002 splitPointerIds[splitPointerCount] = pointerId; 2003 splitPointerCoords[splitPointerCount] = 2004 originalMotionEntry->firstSample.pointerCoords[originalPointerIndex]; 2005 splitPointerCount += 1; 2006 } 2007 } 2008 assert(splitPointerCount == pointerIds.count()); 2009 2010 int32_t action = originalMotionEntry->action; 2011 int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; 2012 if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN 2013 || maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { 2014 int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); 2015 int32_t pointerId = originalMotionEntry->pointerIds[originalPointerIndex]; 2016 if (pointerIds.hasBit(pointerId)) { 2017 if (pointerIds.count() == 1) { 2018 // The first/last pointer went down/up. 2019 action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN 2020 ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP; 2021 } else { 2022 // A secondary pointer went down/up. 2023 uint32_t splitPointerIndex = 0; 2024 while (pointerId != splitPointerIds[splitPointerIndex]) { 2025 splitPointerIndex += 1; 2026 } 2027 action = maskedAction | (splitPointerIndex 2028 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); 2029 } 2030 } else { 2031 // An unrelated pointer changed. 2032 action = AMOTION_EVENT_ACTION_MOVE; 2033 } 2034 } 2035 2036 MotionEntry* splitMotionEntry = mAllocator.obtainMotionEntry( 2037 originalMotionEntry->eventTime, 2038 originalMotionEntry->deviceId, 2039 originalMotionEntry->source, 2040 originalMotionEntry->policyFlags, 2041 action, 2042 originalMotionEntry->flags, 2043 originalMotionEntry->metaState, 2044 originalMotionEntry->edgeFlags, 2045 originalMotionEntry->xPrecision, 2046 originalMotionEntry->yPrecision, 2047 originalMotionEntry->downTime, 2048 splitPointerCount, splitPointerIds, splitPointerCoords); 2049 2050 for (MotionSample* originalMotionSample = originalMotionEntry->firstSample.next; 2051 originalMotionSample != NULL; originalMotionSample = originalMotionSample->next) { 2052 for (uint32_t splitPointerIndex = 0; splitPointerIndex < splitPointerCount; 2053 splitPointerIndex++) { 2054 uint32_t originalPointerIndex = splitPointerIndexMap[splitPointerIndex]; 2055 splitPointerCoords[splitPointerIndex] = 2056 originalMotionSample->pointerCoords[originalPointerIndex]; 2057 } 2058 2059 mAllocator.appendMotionSample(splitMotionEntry, originalMotionSample->eventTime, 2060 splitPointerCoords); 2061 } 2062 2063 return splitMotionEntry; 2064 } 2065 2066 void InputDispatcher::notifyConfigurationChanged(nsecs_t eventTime) { 2067 #if DEBUG_INBOUND_EVENT_DETAILS 2068 LOGD("notifyConfigurationChanged - eventTime=%lld", eventTime); 2069 #endif 2070 2071 bool needWake; 2072 { // acquire lock 2073 AutoMutex _l(mLock); 2074 2075 ConfigurationChangedEntry* newEntry = mAllocator.obtainConfigurationChangedEntry(eventTime); 2076 needWake = enqueueInboundEventLocked(newEntry); 2077 } // release lock 2078 2079 if (needWake) { 2080 mLooper->wake(); 2081 } 2082 } 2083 2084 void InputDispatcher::notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source, 2085 uint32_t policyFlags, int32_t action, int32_t flags, 2086 int32_t keyCode, int32_t scanCode, int32_t metaState, nsecs_t downTime) { 2087 #if DEBUG_INBOUND_EVENT_DETAILS 2088 LOGD("notifyKey - eventTime=%lld, deviceId=0x%x, source=0x%x, policyFlags=0x%x, action=0x%x, " 2089 "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%lld", 2090 eventTime, deviceId, source, policyFlags, action, flags, 2091 keyCode, scanCode, metaState, downTime); 2092 #endif 2093 if (! validateKeyEvent(action)) { 2094 return; 2095 } 2096 2097 policyFlags |= POLICY_FLAG_TRUSTED; 2098 mPolicy->interceptKeyBeforeQueueing(eventTime, deviceId, action, /*byref*/ flags, 2099 keyCode, scanCode, /*byref*/ policyFlags); 2100 2101 bool needWake; 2102 { // acquire lock 2103 AutoMutex _l(mLock); 2104 2105 int32_t repeatCount = 0; 2106 KeyEntry* newEntry = mAllocator.obtainKeyEntry(eventTime, 2107 deviceId, source, policyFlags, action, flags, keyCode, scanCode, 2108 metaState, repeatCount, downTime); 2109 2110 needWake = enqueueInboundEventLocked(newEntry); 2111 } // release lock 2112 2113 if (needWake) { 2114 mLooper->wake(); 2115 } 2116 } 2117 2118 void InputDispatcher::notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source, 2119 uint32_t policyFlags, int32_t action, int32_t flags, int32_t metaState, int32_t edgeFlags, 2120 uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords, 2121 float xPrecision, float yPrecision, nsecs_t downTime) { 2122 #if DEBUG_INBOUND_EVENT_DETAILS 2123 LOGD("notifyMotion - eventTime=%lld, deviceId=0x%x, source=0x%x, policyFlags=0x%x, " 2124 "action=0x%x, flags=0x%x, metaState=0x%x, edgeFlags=0x%x, " 2125 "xPrecision=%f, yPrecision=%f, downTime=%lld", 2126 eventTime, deviceId, source, policyFlags, action, flags, metaState, edgeFlags, 2127 xPrecision, yPrecision, downTime); 2128 for (uint32_t i = 0; i < pointerCount; i++) { 2129 LOGD(" Pointer %d: id=%d, x=%f, y=%f, pressure=%f, size=%f, " 2130 "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " 2131 "orientation=%f", 2132 i, pointerIds[i], pointerCoords[i].x, pointerCoords[i].y, 2133 pointerCoords[i].pressure, pointerCoords[i].size, 2134 pointerCoords[i].touchMajor, pointerCoords[i].touchMinor, 2135 pointerCoords[i].toolMajor, pointerCoords[i].toolMinor, 2136 pointerCoords[i].orientation); 2137 } 2138 #endif 2139 if (! validateMotionEvent(action, pointerCount, pointerIds)) { 2140 return; 2141 } 2142 2143 policyFlags |= POLICY_FLAG_TRUSTED; 2144 mPolicy->interceptGenericBeforeQueueing(eventTime, /*byref*/ policyFlags); 2145 2146 bool needWake; 2147 { // acquire lock 2148 AutoMutex _l(mLock); 2149 2150 // Attempt batching and streaming of move events. 2151 if (action == AMOTION_EVENT_ACTION_MOVE) { 2152 // BATCHING CASE 2153 // 2154 // Try to append a move sample to the tail of the inbound queue for this device. 2155 // Give up if we encounter a non-move motion event for this device since that 2156 // means we cannot append any new samples until a new motion event has started. 2157 for (EventEntry* entry = mInboundQueue.tailSentinel.prev; 2158 entry != & mInboundQueue.headSentinel; entry = entry->prev) { 2159 if (entry->type != EventEntry::TYPE_MOTION) { 2160 // Keep looking for motion events. 2161 continue; 2162 } 2163 2164 MotionEntry* motionEntry = static_cast<MotionEntry*>(entry); 2165 if (motionEntry->deviceId != deviceId) { 2166 // Keep looking for this device. 2167 continue; 2168 } 2169 2170 if (motionEntry->action != AMOTION_EVENT_ACTION_MOVE 2171 || motionEntry->pointerCount != pointerCount 2172 || motionEntry->isInjected()) { 2173 // Last motion event in the queue for this device is not compatible for 2174 // appending new samples. Stop here. 2175 goto NoBatchingOrStreaming; 2176 } 2177 2178 // The last motion event is a move and is compatible for appending. 2179 // Do the batching magic. 2180 mAllocator.appendMotionSample(motionEntry, eventTime, pointerCoords); 2181 #if DEBUG_BATCHING 2182 LOGD("Appended motion sample onto batch for most recent " 2183 "motion event for this device in the inbound queue."); 2184 #endif 2185 return; // done! 2186 } 2187 2188 // STREAMING CASE 2189 // 2190 // There is no pending motion event (of any kind) for this device in the inbound queue. 2191 // Search the outbound queue for the current foreground targets to find a dispatched 2192 // motion event that is still in progress. If found, then, appen the new sample to 2193 // that event and push it out to all current targets. The logic in 2194 // prepareDispatchCycleLocked takes care of the case where some targets may 2195 // already have consumed the motion event by starting a new dispatch cycle if needed. 2196 if (mCurrentInputTargetsValid) { 2197 for (size_t i = 0; i < mCurrentInputTargets.size(); i++) { 2198 const InputTarget& inputTarget = mCurrentInputTargets[i]; 2199 if ((inputTarget.flags & InputTarget::FLAG_FOREGROUND) == 0) { 2200 // Skip non-foreground targets. We only want to stream if there is at 2201 // least one foreground target whose dispatch is still in progress. 2202 continue; 2203 } 2204 2205 ssize_t connectionIndex = getConnectionIndexLocked(inputTarget.inputChannel); 2206 if (connectionIndex < 0) { 2207 // Connection must no longer be valid. 2208 continue; 2209 } 2210 2211 sp<Connection> connection = mConnectionsByReceiveFd.valueAt(connectionIndex); 2212 if (connection->outboundQueue.isEmpty()) { 2213 // This foreground target has an empty outbound queue. 2214 continue; 2215 } 2216 2217 DispatchEntry* dispatchEntry = connection->outboundQueue.headSentinel.next; 2218 if (! dispatchEntry->inProgress 2219 || dispatchEntry->eventEntry->type != EventEntry::TYPE_MOTION 2220 || dispatchEntry->isSplit()) { 2221 // No motion event is being dispatched, or it is being split across 2222 // windows in which case we cannot stream. 2223 continue; 2224 } 2225 2226 MotionEntry* motionEntry = static_cast<MotionEntry*>( 2227 dispatchEntry->eventEntry); 2228 if (motionEntry->action != AMOTION_EVENT_ACTION_MOVE 2229 || motionEntry->deviceId != deviceId 2230 || motionEntry->pointerCount != pointerCount 2231 || motionEntry->isInjected()) { 2232 // The motion event is not compatible with this move. 2233 continue; 2234 } 2235 2236 // Hurray! This foreground target is currently dispatching a move event 2237 // that we can stream onto. Append the motion sample and resume dispatch. 2238 mAllocator.appendMotionSample(motionEntry, eventTime, pointerCoords); 2239 #if DEBUG_BATCHING 2240 LOGD("Appended motion sample onto batch for most recently dispatched " 2241 "motion event for this device in the outbound queues. " 2242 "Attempting to stream the motion sample."); 2243 #endif 2244 nsecs_t currentTime = now(); 2245 dispatchEventToCurrentInputTargetsLocked(currentTime, motionEntry, 2246 true /*resumeWithAppendedMotionSample*/); 2247 2248 runCommandsLockedInterruptible(); 2249 return; // done! 2250 } 2251 } 2252 2253 NoBatchingOrStreaming:; 2254 } 2255 2256 // Just enqueue a new motion event. 2257 MotionEntry* newEntry = mAllocator.obtainMotionEntry(eventTime, 2258 deviceId, source, policyFlags, action, flags, metaState, edgeFlags, 2259 xPrecision, yPrecision, downTime, 2260 pointerCount, pointerIds, pointerCoords); 2261 2262 needWake = enqueueInboundEventLocked(newEntry); 2263 } // release lock 2264 2265 if (needWake) { 2266 mLooper->wake(); 2267 } 2268 } 2269 2270 void InputDispatcher::notifySwitch(nsecs_t when, int32_t switchCode, int32_t switchValue, 2271 uint32_t policyFlags) { 2272 #if DEBUG_INBOUND_EVENT_DETAILS 2273 LOGD("notifySwitch - switchCode=%d, switchValue=%d, policyFlags=0x%x", 2274 switchCode, switchValue, policyFlags); 2275 #endif 2276 2277 policyFlags |= POLICY_FLAG_TRUSTED; 2278 mPolicy->notifySwitch(when, switchCode, switchValue, policyFlags); 2279 } 2280 2281 int32_t InputDispatcher::injectInputEvent(const InputEvent* event, 2282 int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis) { 2283 #if DEBUG_INBOUND_EVENT_DETAILS 2284 LOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " 2285 "syncMode=%d, timeoutMillis=%d", 2286 event->getType(), injectorPid, injectorUid, syncMode, timeoutMillis); 2287 #endif 2288 2289 nsecs_t endTime = now() + milliseconds_to_nanoseconds(timeoutMillis); 2290 2291 uint32_t policyFlags = POLICY_FLAG_INJECTED; 2292 if (hasInjectionPermission(injectorPid, injectorUid)) { 2293 policyFlags |= POLICY_FLAG_TRUSTED; 2294 } 2295 2296 EventEntry* injectedEntry; 2297 switch (event->getType()) { 2298 case AINPUT_EVENT_TYPE_KEY: { 2299 const KeyEvent* keyEvent = static_cast<const KeyEvent*>(event); 2300 int32_t action = keyEvent->getAction(); 2301 if (! validateKeyEvent(action)) { 2302 return INPUT_EVENT_INJECTION_FAILED; 2303 } 2304 2305 nsecs_t eventTime = keyEvent->getEventTime(); 2306 int32_t deviceId = keyEvent->getDeviceId(); 2307 int32_t flags = keyEvent->getFlags(); 2308 int32_t keyCode = keyEvent->getKeyCode(); 2309 int32_t scanCode = keyEvent->getScanCode(); 2310 mPolicy->interceptKeyBeforeQueueing(eventTime, deviceId, action, /*byref*/ flags, 2311 keyCode, scanCode, /*byref*/ policyFlags); 2312 2313 mLock.lock(); 2314 injectedEntry = mAllocator.obtainKeyEntry(eventTime, deviceId, keyEvent->getSource(), 2315 policyFlags, action, flags, keyCode, scanCode, keyEvent->getMetaState(), 2316 keyEvent->getRepeatCount(), keyEvent->getDownTime()); 2317 break; 2318 } 2319 2320 case AINPUT_EVENT_TYPE_MOTION: { 2321 const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); 2322 int32_t action = motionEvent->getAction(); 2323 size_t pointerCount = motionEvent->getPointerCount(); 2324 const int32_t* pointerIds = motionEvent->getPointerIds(); 2325 if (! validateMotionEvent(action, pointerCount, pointerIds)) { 2326 return INPUT_EVENT_INJECTION_FAILED; 2327 } 2328 2329 nsecs_t eventTime = motionEvent->getEventTime(); 2330 mPolicy->interceptGenericBeforeQueueing(eventTime, /*byref*/ policyFlags); 2331 2332 mLock.lock(); 2333 const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); 2334 const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); 2335 MotionEntry* motionEntry = mAllocator.obtainMotionEntry(*sampleEventTimes, 2336 motionEvent->getDeviceId(), motionEvent->getSource(), policyFlags, 2337 action, motionEvent->getFlags(), 2338 motionEvent->getMetaState(), motionEvent->getEdgeFlags(), 2339 motionEvent->getXPrecision(), motionEvent->getYPrecision(), 2340 motionEvent->getDownTime(), uint32_t(pointerCount), 2341 pointerIds, samplePointerCoords); 2342 for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { 2343 sampleEventTimes += 1; 2344 samplePointerCoords += pointerCount; 2345 mAllocator.appendMotionSample(motionEntry, *sampleEventTimes, samplePointerCoords); 2346 } 2347 injectedEntry = motionEntry; 2348 break; 2349 } 2350 2351 default: 2352 LOGW("Cannot inject event of type %d", event->getType()); 2353 return INPUT_EVENT_INJECTION_FAILED; 2354 } 2355 2356 InjectionState* injectionState = mAllocator.obtainInjectionState(injectorPid, injectorUid); 2357 if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { 2358 injectionState->injectionIsAsync = true; 2359 } 2360 2361 injectionState->refCount += 1; 2362 injectedEntry->injectionState = injectionState; 2363 2364 bool needWake = enqueueInboundEventLocked(injectedEntry); 2365 mLock.unlock(); 2366 2367 if (needWake) { 2368 mLooper->wake(); 2369 } 2370 2371 int32_t injectionResult; 2372 { // acquire lock 2373 AutoMutex _l(mLock); 2374 2375 if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { 2376 injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; 2377 } else { 2378 for (;;) { 2379 injectionResult = injectionState->injectionResult; 2380 if (injectionResult != INPUT_EVENT_INJECTION_PENDING) { 2381 break; 2382 } 2383 2384 nsecs_t remainingTimeout = endTime - now(); 2385 if (remainingTimeout <= 0) { 2386 #if DEBUG_INJECTION 2387 LOGD("injectInputEvent - Timed out waiting for injection result " 2388 "to become available."); 2389 #endif 2390 injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; 2391 break; 2392 } 2393 2394 mInjectionResultAvailableCondition.waitRelative(mLock, remainingTimeout); 2395 } 2396 2397 if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED 2398 && syncMode == INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED) { 2399 while (injectionState->pendingForegroundDispatches != 0) { 2400 #if DEBUG_INJECTION 2401 LOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", 2402 injectionState->pendingForegroundDispatches); 2403 #endif 2404 nsecs_t remainingTimeout = endTime - now(); 2405 if (remainingTimeout <= 0) { 2406 #if DEBUG_INJECTION 2407 LOGD("injectInputEvent - Timed out waiting for pending foreground " 2408 "dispatches to finish."); 2409 #endif 2410 injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; 2411 break; 2412 } 2413 2414 mInjectionSyncFinishedCondition.waitRelative(mLock, remainingTimeout); 2415 } 2416 } 2417 } 2418 2419 mAllocator.releaseInjectionState(injectionState); 2420 } // release lock 2421 2422 #if DEBUG_INJECTION 2423 LOGD("injectInputEvent - Finished with result %d. " 2424 "injectorPid=%d, injectorUid=%d", 2425 injectionResult, injectorPid, injectorUid); 2426 #endif 2427 2428 return injectionResult; 2429 } 2430 2431 bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { 2432 return injectorUid == 0 2433 || mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); 2434 } 2435 2436 void InputDispatcher::setInjectionResultLocked(EventEntry* entry, int32_t injectionResult) { 2437 InjectionState* injectionState = entry->injectionState; 2438 if (injectionState) { 2439 #if DEBUG_INJECTION 2440 LOGD("Setting input event injection result to %d. " 2441 "injectorPid=%d, injectorUid=%d", 2442 injectionResult, injectionState->injectorPid, injectionState->injectorUid); 2443 #endif 2444 2445 if (injectionState->injectionIsAsync) { 2446 // Log the outcome since the injector did not wait for the injection result. 2447 switch (injectionResult) { 2448 case INPUT_EVENT_INJECTION_SUCCEEDED: 2449 LOGV("Asynchronous input event injection succeeded."); 2450 break; 2451 case INPUT_EVENT_INJECTION_FAILED: 2452 LOGW("Asynchronous input event injection failed."); 2453 break; 2454 case INPUT_EVENT_INJECTION_PERMISSION_DENIED: 2455 LOGW("Asynchronous input event injection permission denied."); 2456 break; 2457 case INPUT_EVENT_INJECTION_TIMED_OUT: 2458 LOGW("Asynchronous input event injection timed out."); 2459 break; 2460 } 2461 } 2462 2463 injectionState->injectionResult = injectionResult; 2464 mInjectionResultAvailableCondition.broadcast(); 2465 } 2466 } 2467 2468 void InputDispatcher::incrementPendingForegroundDispatchesLocked(EventEntry* entry) { 2469 InjectionState* injectionState = entry->injectionState; 2470 if (injectionState) { 2471 injectionState->pendingForegroundDispatches += 1; 2472 } 2473 } 2474 2475 void InputDispatcher::decrementPendingForegroundDispatchesLocked(EventEntry* entry) { 2476 InjectionState* injectionState = entry->injectionState; 2477 if (injectionState) { 2478 injectionState->pendingForegroundDispatches -= 1; 2479 2480 if (injectionState->pendingForegroundDispatches == 0) { 2481 mInjectionSyncFinishedCondition.broadcast(); 2482 } 2483 } 2484 } 2485 2486 const InputWindow* InputDispatcher::getWindowLocked(const sp<InputChannel>& inputChannel) { 2487 for (size_t i = 0; i < mWindows.size(); i++) { 2488 const InputWindow* window = & mWindows[i]; 2489 if (window->inputChannel == inputChannel) { 2490 return window; 2491 } 2492 } 2493 return NULL; 2494 } 2495 2496 void InputDispatcher::setInputWindows(const Vector<InputWindow>& inputWindows) { 2497 #if DEBUG_FOCUS 2498 LOGD("setInputWindows"); 2499 #endif 2500 { // acquire lock 2501 AutoMutex _l(mLock); 2502 2503 // Clear old window pointers. 2504 sp<InputChannel> oldFocusedWindowChannel; 2505 if (mFocusedWindow) { 2506 oldFocusedWindowChannel = mFocusedWindow->inputChannel; 2507 mFocusedWindow = NULL; 2508 } 2509 2510 mWindows.clear(); 2511 2512 // Loop over new windows and rebuild the necessary window pointers for 2513 // tracking focus and touch. 2514 mWindows.appendVector(inputWindows); 2515 2516 size_t numWindows = mWindows.size(); 2517 for (size_t i = 0; i < numWindows; i++) { 2518 const InputWindow* window = & mWindows.itemAt(i); 2519 if (window->hasFocus) { 2520 mFocusedWindow = window; 2521 break; 2522 } 2523 } 2524 2525 if (oldFocusedWindowChannel != NULL) { 2526 if (!mFocusedWindow || oldFocusedWindowChannel != mFocusedWindow->inputChannel) { 2527 #if DEBUG_FOCUS 2528 LOGD("Focus left window: %s", 2529 oldFocusedWindowChannel->getName().string()); 2530 #endif 2531 synthesizeCancelationEventsForInputChannelLocked(oldFocusedWindowChannel, 2532 InputState::CANCEL_NON_POINTER_EVENTS, "focus left window"); 2533 oldFocusedWindowChannel.clear(); 2534 } 2535 } 2536 if (mFocusedWindow && oldFocusedWindowChannel == NULL) { 2537 #if DEBUG_FOCUS 2538 LOGD("Focus entered window: %s", 2539 mFocusedWindow->inputChannel->getName().string()); 2540 #endif 2541 } 2542 2543 for (size_t i = 0; i < mTouchState.windows.size(); ) { 2544 TouchedWindow& touchedWindow = mTouchState.windows.editItemAt(i); 2545 const InputWindow* window = getWindowLocked(touchedWindow.channel); 2546 if (window) { 2547 touchedWindow.window = window; 2548 i += 1; 2549 } else { 2550 #if DEBUG_FOCUS 2551 LOGD("Touched window was removed: %s", touchedWindow.channel->getName().string()); 2552 #endif 2553 synthesizeCancelationEventsForInputChannelLocked(touchedWindow.channel, 2554 InputState::CANCEL_POINTER_EVENTS, "touched window was removed"); 2555 mTouchState.windows.removeAt(i); 2556 } 2557 } 2558 2559 #if DEBUG_FOCUS 2560 //logDispatchStateLocked(); 2561 #endif 2562 } // release lock 2563 2564 // Wake up poll loop since it may need to make new input dispatching choices. 2565 mLooper->wake(); 2566 } 2567 2568 void InputDispatcher::setFocusedApplication(const InputApplication* inputApplication) { 2569 #if DEBUG_FOCUS 2570 LOGD("setFocusedApplication"); 2571 #endif 2572 { // acquire lock 2573 AutoMutex _l(mLock); 2574 2575 releaseFocusedApplicationLocked(); 2576 2577 if (inputApplication) { 2578 mFocusedApplicationStorage = *inputApplication; 2579 mFocusedApplication = & mFocusedApplicationStorage; 2580 } 2581 2582 #if DEBUG_FOCUS 2583 //logDispatchStateLocked(); 2584 #endif 2585 } // release lock 2586 2587 // Wake up poll loop since it may need to make new input dispatching choices. 2588 mLooper->wake(); 2589 } 2590 2591 void InputDispatcher::releaseFocusedApplicationLocked() { 2592 if (mFocusedApplication) { 2593 mFocusedApplication = NULL; 2594 mFocusedApplicationStorage.handle.clear(); 2595 } 2596 } 2597 2598 void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { 2599 #if DEBUG_FOCUS 2600 LOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); 2601 #endif 2602 2603 bool changed; 2604 { // acquire lock 2605 AutoMutex _l(mLock); 2606 2607 if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { 2608 if (mDispatchFrozen && !frozen) { 2609 resetANRTimeoutsLocked(); 2610 } 2611 2612 if (mDispatchEnabled && !enabled) { 2613 resetAndDropEverythingLocked("dispatcher is being disabled"); 2614 } 2615 2616 mDispatchEnabled = enabled; 2617 mDispatchFrozen = frozen; 2618 changed = true; 2619 } else { 2620 changed = false; 2621 } 2622 2623 #if DEBUG_FOCUS 2624 //logDispatchStateLocked(); 2625 #endif 2626 } // release lock 2627 2628 if (changed) { 2629 // Wake up poll loop since it may need to make new input dispatching choices. 2630 mLooper->wake(); 2631 } 2632 } 2633 2634 void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { 2635 #if DEBUG_FOCUS 2636 LOGD("Resetting and dropping all events (%s).", reason); 2637 #endif 2638 2639 synthesizeCancelationEventsForAllConnectionsLocked(InputState::CANCEL_ALL_EVENTS, reason); 2640 2641 resetKeyRepeatLocked(); 2642 releasePendingEventLocked(); 2643 drainInboundQueueLocked(); 2644 resetTargetsLocked(); 2645 2646 mTouchState.reset(); 2647 } 2648 2649 void InputDispatcher::logDispatchStateLocked() { 2650 String8 dump; 2651 dumpDispatchStateLocked(dump); 2652 2653 char* text = dump.lockBuffer(dump.size()); 2654 char* start = text; 2655 while (*start != '\0') { 2656 char* end = strchr(start, '\n'); 2657 if (*end == '\n') { 2658 *(end++) = '\0'; 2659 } 2660 LOGD("%s", start); 2661 start = end; 2662 } 2663 } 2664 2665 void InputDispatcher::dumpDispatchStateLocked(String8& dump) { 2666 dump.appendFormat(INDENT "DispatchEnabled: %d\n", mDispatchEnabled); 2667 dump.appendFormat(INDENT "DispatchFrozen: %d\n", mDispatchFrozen); 2668 2669 if (mFocusedApplication) { 2670 dump.appendFormat(INDENT "FocusedApplication: name='%s', dispatchingTimeout=%0.3fms\n", 2671 mFocusedApplication->name.string(), 2672 mFocusedApplication->dispatchingTimeout / 1000000.0); 2673 } else { 2674 dump.append(INDENT "FocusedApplication: <null>\n"); 2675 } 2676 dump.appendFormat(INDENT "FocusedWindow: name='%s'\n", 2677 mFocusedWindow != NULL ? mFocusedWindow->name.string() : "<null>"); 2678 2679 dump.appendFormat(INDENT "TouchDown: %s\n", toString(mTouchState.down)); 2680 dump.appendFormat(INDENT "TouchSplit: %s\n", toString(mTouchState.split)); 2681 if (!mTouchState.windows.isEmpty()) { 2682 dump.append(INDENT "TouchedWindows:\n"); 2683 for (size_t i = 0; i < mTouchState.windows.size(); i++) { 2684 const TouchedWindow& touchedWindow = mTouchState.windows[i]; 2685 dump.appendFormat(INDENT2 "%d: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", 2686 i, touchedWindow.window->name.string(), touchedWindow.pointerIds.value, 2687 touchedWindow.targetFlags); 2688 } 2689 } else { 2690 dump.append(INDENT "TouchedWindows: <none>\n"); 2691 } 2692 2693 if (!mWindows.isEmpty()) { 2694 dump.append(INDENT "Windows:\n"); 2695 for (size_t i = 0; i < mWindows.size(); i++) { 2696 const InputWindow& window = mWindows[i]; 2697 dump.appendFormat(INDENT2 "%d: name='%s', paused=%s, hasFocus=%s, hasWallpaper=%s, " 2698 "visible=%s, canReceiveKeys=%s, flags=0x%08x, type=0x%08x, layer=%d, " 2699 "frame=[%d,%d][%d,%d], " 2700 "visibleFrame=[%d,%d][%d,%d], " 2701 "touchableArea=[%d,%d][%d,%d], " 2702 "ownerPid=%d, ownerUid=%d, dispatchingTimeout=%0.3fms\n", 2703 i, window.name.string(), 2704 toString(window.paused), 2705 toString(window.hasFocus), 2706 toString(window.hasWallpaper), 2707 toString(window.visible), 2708 toString(window.canReceiveKeys), 2709 window.layoutParamsFlags, window.layoutParamsType, 2710 window.layer, 2711 window.frameLeft, window.frameTop, 2712 window.frameRight, window.frameBottom, 2713 window.visibleFrameLeft, window.visibleFrameTop, 2714 window.visibleFrameRight, window.visibleFrameBottom, 2715 window.touchableAreaLeft, window.touchableAreaTop, 2716 window.touchableAreaRight, window.touchableAreaBottom, 2717 window.ownerPid, window.ownerUid, 2718 window.dispatchingTimeout / 1000000.0); 2719 } 2720 } else { 2721 dump.append(INDENT "Windows: <none>\n"); 2722 } 2723 2724 if (!mMonitoringChannels.isEmpty()) { 2725 dump.append(INDENT "MonitoringChannels:\n"); 2726 for (size_t i = 0; i < mMonitoringChannels.size(); i++) { 2727 const sp<InputChannel>& channel = mMonitoringChannels[i]; 2728 dump.appendFormat(INDENT2 "%d: '%s'\n", i, channel->getName().string()); 2729 } 2730 } else { 2731 dump.append(INDENT "MonitoringChannels: <none>\n"); 2732 } 2733 2734 dump.appendFormat(INDENT "InboundQueue: length=%u\n", mInboundQueue.count()); 2735 2736 if (!mActiveConnections.isEmpty()) { 2737 dump.append(INDENT "ActiveConnections:\n"); 2738 for (size_t i = 0; i < mActiveConnections.size(); i++) { 2739 const Connection* connection = mActiveConnections[i]; 2740 dump.appendFormat(INDENT2 "%d: '%s', status=%s, outboundQueueLength=%u" 2741 "inputState.isNeutral=%s\n", 2742 i, connection->getInputChannelName(), connection->getStatusLabel(), 2743 connection->outboundQueue.count(), 2744 toString(connection->inputState.isNeutral())); 2745 } 2746 } else { 2747 dump.append(INDENT "ActiveConnections: <none>\n"); 2748 } 2749 2750 if (isAppSwitchPendingLocked()) { 2751 dump.appendFormat(INDENT "AppSwitch: pending, due in %01.1fms\n", 2752 (mAppSwitchDueTime - now()) / 1000000.0); 2753 } else { 2754 dump.append(INDENT "AppSwitch: not pending\n"); 2755 } 2756 } 2757 2758 status_t InputDispatcher::registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor) { 2759 #if DEBUG_REGISTRATION 2760 LOGD("channel '%s' ~ registerInputChannel - monitor=%s", inputChannel->getName().string(), 2761 toString(monitor)); 2762 #endif 2763 2764 { // acquire lock 2765 AutoMutex _l(mLock); 2766 2767 if (getConnectionIndexLocked(inputChannel) >= 0) { 2768 LOGW("Attempted to register already registered input channel '%s'", 2769 inputChannel->getName().string()); 2770 return BAD_VALUE; 2771 } 2772 2773 sp<Connection> connection = new Connection(inputChannel); 2774 status_t status = connection->initialize(); 2775 if (status) { 2776 LOGE("Failed to initialize input publisher for input channel '%s', status=%d", 2777 inputChannel->getName().string(), status); 2778 return status; 2779 } 2780 2781 int32_t receiveFd = inputChannel->getReceivePipeFd(); 2782 mConnectionsByReceiveFd.add(receiveFd, connection); 2783 2784 if (monitor) { 2785 mMonitoringChannels.push(inputChannel); 2786 } 2787 2788 mLooper->addFd(receiveFd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); 2789 2790 runCommandsLockedInterruptible(); 2791 } // release lock 2792 return OK; 2793 } 2794 2795 status_t InputDispatcher::unregisterInputChannel(const sp<InputChannel>& inputChannel) { 2796 #if DEBUG_REGISTRATION 2797 LOGD("channel '%s' ~ unregisterInputChannel", inputChannel->getName().string()); 2798 #endif 2799 2800 { // acquire lock 2801 AutoMutex _l(mLock); 2802 2803 ssize_t connectionIndex = getConnectionIndexLocked(inputChannel); 2804 if (connectionIndex < 0) { 2805 LOGW("Attempted to unregister already unregistered input channel '%s'", 2806 inputChannel->getName().string()); 2807 return BAD_VALUE; 2808 } 2809 2810 sp<Connection> connection = mConnectionsByReceiveFd.valueAt(connectionIndex); 2811 mConnectionsByReceiveFd.removeItemsAt(connectionIndex); 2812 2813 connection->status = Connection::STATUS_ZOMBIE; 2814 2815 for (size_t i = 0; i < mMonitoringChannels.size(); i++) { 2816 if (mMonitoringChannels[i] == inputChannel) { 2817 mMonitoringChannels.removeAt(i); 2818 break; 2819 } 2820 } 2821 2822 mLooper->removeFd(inputChannel->getReceivePipeFd()); 2823 2824 nsecs_t currentTime = now(); 2825 abortBrokenDispatchCycleLocked(currentTime, connection); 2826 2827 runCommandsLockedInterruptible(); 2828 } // release lock 2829 2830 // Wake the poll loop because removing the connection may have changed the current 2831 // synchronization state. 2832 mLooper->wake(); 2833 return OK; 2834 } 2835 2836 ssize_t InputDispatcher::getConnectionIndexLocked(const sp<InputChannel>& inputChannel) { 2837 ssize_t connectionIndex = mConnectionsByReceiveFd.indexOfKey(inputChannel->getReceivePipeFd()); 2838 if (connectionIndex >= 0) { 2839 sp<Connection> connection = mConnectionsByReceiveFd.valueAt(connectionIndex); 2840 if (connection->inputChannel.get() == inputChannel.get()) { 2841 return connectionIndex; 2842 } 2843 } 2844 2845 return -1; 2846 } 2847 2848 void InputDispatcher::activateConnectionLocked(Connection* connection) { 2849 for (size_t i = 0; i < mActiveConnections.size(); i++) { 2850 if (mActiveConnections.itemAt(i) == connection) { 2851 return; 2852 } 2853 } 2854 mActiveConnections.add(connection); 2855 } 2856 2857 void InputDispatcher::deactivateConnectionLocked(Connection* connection) { 2858 for (size_t i = 0; i < mActiveConnections.size(); i++) { 2859 if (mActiveConnections.itemAt(i) == connection) { 2860 mActiveConnections.removeAt(i); 2861 return; 2862 } 2863 } 2864 } 2865 2866 void InputDispatcher::onDispatchCycleStartedLocked( 2867 nsecs_t currentTime, const sp<Connection>& connection) { 2868 } 2869 2870 void InputDispatcher::onDispatchCycleFinishedLocked( 2871 nsecs_t currentTime, const sp<Connection>& connection) { 2872 } 2873 2874 void InputDispatcher::onDispatchCycleBrokenLocked( 2875 nsecs_t currentTime, const sp<Connection>& connection) { 2876 LOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", 2877 connection->getInputChannelName()); 2878 2879 CommandEntry* commandEntry = postCommandLocked( 2880 & InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); 2881 commandEntry->connection = connection; 2882 } 2883 2884 void InputDispatcher::onANRLocked( 2885 nsecs_t currentTime, const InputApplication* application, const InputWindow* window, 2886 nsecs_t eventTime, nsecs_t waitStartTime) { 2887 LOGI("Application is not responding: %s. " 2888 "%01.1fms since event, %01.1fms since wait started", 2889 getApplicationWindowLabelLocked(application, window).string(), 2890 (currentTime - eventTime) / 1000000.0, 2891 (currentTime - waitStartTime) / 1000000.0); 2892 2893 CommandEntry* commandEntry = postCommandLocked( 2894 & InputDispatcher::doNotifyANRLockedInterruptible); 2895 if (application) { 2896 commandEntry->inputApplicationHandle = application->handle; 2897 } 2898 if (window) { 2899 commandEntry->inputChannel = window->inputChannel; 2900 } 2901 } 2902 2903 void InputDispatcher::doNotifyConfigurationChangedInterruptible( 2904 CommandEntry* commandEntry) { 2905 mLock.unlock(); 2906 2907 mPolicy->notifyConfigurationChanged(commandEntry->eventTime); 2908 2909 mLock.lock(); 2910 } 2911 2912 void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible( 2913 CommandEntry* commandEntry) { 2914 sp<Connection> connection = commandEntry->connection; 2915 2916 if (connection->status != Connection::STATUS_ZOMBIE) { 2917 mLock.unlock(); 2918 2919 mPolicy->notifyInputChannelBroken(connection->inputChannel); 2920 2921 mLock.lock(); 2922 } 2923 } 2924 2925 void InputDispatcher::doNotifyANRLockedInterruptible( 2926 CommandEntry* commandEntry) { 2927 mLock.unlock(); 2928 2929 nsecs_t newTimeout = mPolicy->notifyANR( 2930 commandEntry->inputApplicationHandle, commandEntry->inputChannel); 2931 2932 mLock.lock(); 2933 2934 resumeAfterTargetsNotReadyTimeoutLocked(newTimeout, commandEntry->inputChannel); 2935 } 2936 2937 void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( 2938 CommandEntry* commandEntry) { 2939 KeyEntry* entry = commandEntry->keyEntry; 2940 mReusableKeyEvent.initialize(entry->deviceId, entry->source, entry->action, entry->flags, 2941 entry->keyCode, entry->scanCode, entry->metaState, entry->repeatCount, 2942 entry->downTime, entry->eventTime); 2943 2944 mLock.unlock(); 2945 2946 bool consumed = mPolicy->interceptKeyBeforeDispatching(commandEntry->inputChannel, 2947 & mReusableKeyEvent, entry->policyFlags); 2948 2949 mLock.lock(); 2950 2951 entry->interceptKeyResult = consumed 2952 ? KeyEntry::INTERCEPT_KEY_RESULT_SKIP 2953 : KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; 2954 mAllocator.releaseKeyEntry(entry); 2955 } 2956 2957 void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { 2958 mLock.unlock(); 2959 2960 mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType); 2961 2962 mLock.lock(); 2963 } 2964 2965 void InputDispatcher::updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry, 2966 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication) { 2967 // TODO Write some statistics about how long we spend waiting. 2968 } 2969 2970 void InputDispatcher::dump(String8& dump) { 2971 dump.append("Input Dispatcher State:\n"); 2972 dumpDispatchStateLocked(dump); 2973 } 2974 2975 2976 // --- InputDispatcher::Queue --- 2977 2978 template <typename T> 2979 uint32_t InputDispatcher::Queue<T>::count() const { 2980 uint32_t result = 0; 2981 for (const T* entry = headSentinel.next; entry != & tailSentinel; entry = entry->next) { 2982 result += 1; 2983 } 2984 return result; 2985 } 2986 2987 2988 // --- InputDispatcher::Allocator --- 2989 2990 InputDispatcher::Allocator::Allocator() { 2991 } 2992 2993 InputDispatcher::InjectionState* 2994 InputDispatcher::Allocator::obtainInjectionState(int32_t injectorPid, int32_t injectorUid) { 2995 InjectionState* injectionState = mInjectionStatePool.alloc(); 2996 injectionState->refCount = 1; 2997 injectionState->injectorPid = injectorPid; 2998 injectionState->injectorUid = injectorUid; 2999 injectionState->injectionIsAsync = false; 3000 injectionState->injectionResult = INPUT_EVENT_INJECTION_PENDING; 3001 injectionState->pendingForegroundDispatches = 0; 3002 return injectionState; 3003 } 3004 3005 void InputDispatcher::Allocator::initializeEventEntry(EventEntry* entry, int32_t type, 3006 nsecs_t eventTime, uint32_t policyFlags) { 3007 entry->type = type; 3008 entry->refCount = 1; 3009 entry->dispatchInProgress = false; 3010 entry->eventTime = eventTime; 3011 entry->policyFlags = policyFlags; 3012 entry->injectionState = NULL; 3013 } 3014 3015 void InputDispatcher::Allocator::releaseEventEntryInjectionState(EventEntry* entry) { 3016 if (entry->injectionState) { 3017 releaseInjectionState(entry->injectionState); 3018 entry->injectionState = NULL; 3019 } 3020 } 3021 3022 InputDispatcher::ConfigurationChangedEntry* 3023 InputDispatcher::Allocator::obtainConfigurationChangedEntry(nsecs_t eventTime) { 3024 ConfigurationChangedEntry* entry = mConfigurationChangeEntryPool.alloc(); 3025 initializeEventEntry(entry, EventEntry::TYPE_CONFIGURATION_CHANGED, eventTime, 0); 3026 return entry; 3027 } 3028 3029 InputDispatcher::KeyEntry* InputDispatcher::Allocator::obtainKeyEntry(nsecs_t eventTime, 3030 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action, 3031 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState, 3032 int32_t repeatCount, nsecs_t downTime) { 3033 KeyEntry* entry = mKeyEntryPool.alloc(); 3034 initializeEventEntry(entry, EventEntry::TYPE_KEY, eventTime, policyFlags); 3035 3036 entry->deviceId = deviceId; 3037 entry->source = source; 3038 entry->action = action; 3039 entry->flags = flags; 3040 entry->keyCode = keyCode; 3041 entry->scanCode = scanCode; 3042 entry->metaState = metaState; 3043 entry->repeatCount = repeatCount; 3044 entry->downTime = downTime; 3045 entry->syntheticRepeat = false; 3046 entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; 3047 return entry; 3048 } 3049 3050 InputDispatcher::MotionEntry* InputDispatcher::Allocator::obtainMotionEntry(nsecs_t eventTime, 3051 int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action, int32_t flags, 3052 int32_t metaState, int32_t edgeFlags, float xPrecision, float yPrecision, 3053 nsecs_t downTime, uint32_t pointerCount, 3054 const int32_t* pointerIds, const PointerCoords* pointerCoords) { 3055 MotionEntry* entry = mMotionEntryPool.alloc(); 3056 initializeEventEntry(entry, EventEntry::TYPE_MOTION, eventTime, policyFlags); 3057 3058 entry->eventTime = eventTime; 3059 entry->deviceId = deviceId; 3060 entry->source = source; 3061 entry->action = action; 3062 entry->flags = flags; 3063 entry->metaState = metaState; 3064 entry->edgeFlags = edgeFlags; 3065 entry->xPrecision = xPrecision; 3066 entry->yPrecision = yPrecision; 3067 entry->downTime = downTime; 3068 entry->pointerCount = pointerCount; 3069 entry->firstSample.eventTime = eventTime; 3070 entry->firstSample.next = NULL; 3071 entry->lastSample = & entry->firstSample; 3072 for (uint32_t i = 0; i < pointerCount; i++) { 3073 entry->pointerIds[i] = pointerIds[i]; 3074 entry->firstSample.pointerCoords[i] = pointerCoords[i]; 3075 } 3076 return entry; 3077 } 3078 3079 InputDispatcher::DispatchEntry* InputDispatcher::Allocator::obtainDispatchEntry( 3080 EventEntry* eventEntry, 3081 int32_t targetFlags, float xOffset, float yOffset) { 3082 DispatchEntry* entry = mDispatchEntryPool.alloc(); 3083 entry->eventEntry = eventEntry; 3084 eventEntry->refCount += 1; 3085 entry->targetFlags = targetFlags; 3086 entry->xOffset = xOffset; 3087 entry->yOffset = yOffset; 3088 entry->inProgress = false; 3089 entry->headMotionSample = NULL; 3090 entry->tailMotionSample = NULL; 3091 return entry; 3092 } 3093 3094 InputDispatcher::CommandEntry* InputDispatcher::Allocator::obtainCommandEntry(Command command) { 3095 CommandEntry* entry = mCommandEntryPool.alloc(); 3096 entry->command = command; 3097 return entry; 3098 } 3099 3100 void InputDispatcher::Allocator::releaseInjectionState(InjectionState* injectionState) { 3101 injectionState->refCount -= 1; 3102 if (injectionState->refCount == 0) { 3103 mInjectionStatePool.free(injectionState); 3104 } else { 3105 assert(injectionState->refCount > 0); 3106 } 3107 } 3108 3109 void InputDispatcher::Allocator::releaseEventEntry(EventEntry* entry) { 3110 switch (entry->type) { 3111 case EventEntry::TYPE_CONFIGURATION_CHANGED: 3112 releaseConfigurationChangedEntry(static_cast<ConfigurationChangedEntry*>(entry)); 3113 break; 3114 case EventEntry::TYPE_KEY: 3115 releaseKeyEntry(static_cast<KeyEntry*>(entry)); 3116 break; 3117 case EventEntry::TYPE_MOTION: 3118 releaseMotionEntry(static_cast<MotionEntry*>(entry)); 3119 break; 3120 default: 3121 assert(false); 3122 break; 3123 } 3124 } 3125 3126 void InputDispatcher::Allocator::releaseConfigurationChangedEntry( 3127 ConfigurationChangedEntry* entry) { 3128 entry->refCount -= 1; 3129 if (entry->refCount == 0) { 3130 releaseEventEntryInjectionState(entry); 3131 mConfigurationChangeEntryPool.free(entry); 3132 } else { 3133 assert(entry->refCount > 0); 3134 } 3135 } 3136 3137 void InputDispatcher::Allocator::releaseKeyEntry(KeyEntry* entry) { 3138 entry->refCount -= 1; 3139 if (entry->refCount == 0) { 3140 releaseEventEntryInjectionState(entry); 3141 mKeyEntryPool.free(entry); 3142 } else { 3143 assert(entry->refCount > 0); 3144 } 3145 } 3146 3147 void InputDispatcher::Allocator::releaseMotionEntry(MotionEntry* entry) { 3148 entry->refCount -= 1; 3149 if (entry->refCount == 0) { 3150 releaseEventEntryInjectionState(entry); 3151 for (MotionSample* sample = entry->firstSample.next; sample != NULL; ) { 3152 MotionSample* next = sample->next; 3153 mMotionSamplePool.free(sample); 3154 sample = next; 3155 } 3156 mMotionEntryPool.free(entry); 3157 } else { 3158 assert(entry->refCount > 0); 3159 } 3160 } 3161 3162 void InputDispatcher::Allocator::releaseDispatchEntry(DispatchEntry* entry) { 3163 releaseEventEntry(entry->eventEntry); 3164 mDispatchEntryPool.free(entry); 3165 } 3166 3167 void InputDispatcher::Allocator::releaseCommandEntry(CommandEntry* entry) { 3168 mCommandEntryPool.free(entry); 3169 } 3170 3171 void InputDispatcher::Allocator::appendMotionSample(MotionEntry* motionEntry, 3172 nsecs_t eventTime, const PointerCoords* pointerCoords) { 3173 MotionSample* sample = mMotionSamplePool.alloc(); 3174 sample->eventTime = eventTime; 3175 uint32_t pointerCount = motionEntry->pointerCount; 3176 for (uint32_t i = 0; i < pointerCount; i++) { 3177 sample->pointerCoords[i] = pointerCoords[i]; 3178 } 3179 3180 sample->next = NULL; 3181 motionEntry->lastSample->next = sample; 3182 motionEntry->lastSample = sample; 3183 } 3184 3185 void InputDispatcher::Allocator::recycleKeyEntry(KeyEntry* keyEntry) { 3186 releaseEventEntryInjectionState(keyEntry); 3187 3188 keyEntry->dispatchInProgress = false; 3189 keyEntry->syntheticRepeat = false; 3190 keyEntry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; 3191 } 3192 3193 3194 // --- InputDispatcher::MotionEntry --- 3195 3196 uint32_t InputDispatcher::MotionEntry::countSamples() const { 3197 uint32_t count = 1; 3198 for (MotionSample* sample = firstSample.next; sample != NULL; sample = sample->next) { 3199 count += 1; 3200 } 3201 return count; 3202 } 3203 3204 3205 // --- InputDispatcher::InputState --- 3206 3207 InputDispatcher::InputState::InputState() { 3208 } 3209 3210 InputDispatcher::InputState::~InputState() { 3211 } 3212 3213 bool InputDispatcher::InputState::isNeutral() const { 3214 return mKeyMementos.isEmpty() && mMotionMementos.isEmpty(); 3215 } 3216 3217 InputDispatcher::InputState::Consistency InputDispatcher::InputState::trackEvent( 3218 const EventEntry* entry) { 3219 switch (entry->type) { 3220 case EventEntry::TYPE_KEY: 3221 return trackKey(static_cast<const KeyEntry*>(entry)); 3222 3223 case EventEntry::TYPE_MOTION: 3224 return trackMotion(static_cast<const MotionEntry*>(entry)); 3225 3226 default: 3227 return CONSISTENT; 3228 } 3229 } 3230 3231 InputDispatcher::InputState::Consistency InputDispatcher::InputState::trackKey( 3232 const KeyEntry* entry) { 3233 int32_t action = entry->action; 3234 for (size_t i = 0; i < mKeyMementos.size(); i++) { 3235 KeyMemento& memento = mKeyMementos.editItemAt(i); 3236 if (memento.deviceId == entry->deviceId 3237 && memento.source == entry->source 3238 && memento.keyCode == entry->keyCode 3239 && memento.scanCode == entry->scanCode) { 3240 switch (action) { 3241 case AKEY_EVENT_ACTION_UP: 3242 mKeyMementos.removeAt(i); 3243 return CONSISTENT; 3244 3245 case AKEY_EVENT_ACTION_DOWN: 3246 return TOLERABLE; 3247 3248 default: 3249 return BROKEN; 3250 } 3251 } 3252 } 3253 3254 switch (action) { 3255 case AKEY_EVENT_ACTION_DOWN: { 3256 mKeyMementos.push(); 3257 KeyMemento& memento = mKeyMementos.editTop(); 3258 memento.deviceId = entry->deviceId; 3259 memento.source = entry->source; 3260 memento.keyCode = entry->keyCode; 3261 memento.scanCode = entry->scanCode; 3262 memento.downTime = entry->downTime; 3263 return CONSISTENT; 3264 } 3265 3266 default: 3267 return BROKEN; 3268 } 3269 } 3270 3271 InputDispatcher::InputState::Consistency InputDispatcher::InputState::trackMotion( 3272 const MotionEntry* entry) { 3273 int32_t action = entry->action & AMOTION_EVENT_ACTION_MASK; 3274 for (size_t i = 0; i < mMotionMementos.size(); i++) { 3275 MotionMemento& memento = mMotionMementos.editItemAt(i); 3276 if (memento.deviceId == entry->deviceId 3277 && memento.source == entry->source) { 3278 switch (action) { 3279 case AMOTION_EVENT_ACTION_UP: 3280 case AMOTION_EVENT_ACTION_CANCEL: 3281 mMotionMementos.removeAt(i); 3282 return CONSISTENT; 3283 3284 case AMOTION_EVENT_ACTION_DOWN: 3285 return TOLERABLE; 3286 3287 case AMOTION_EVENT_ACTION_POINTER_DOWN: 3288 if (entry->pointerCount == memento.pointerCount + 1) { 3289 memento.setPointers(entry); 3290 return CONSISTENT; 3291 } 3292 return BROKEN; 3293 3294 case AMOTION_EVENT_ACTION_POINTER_UP: 3295 if (entry->pointerCount == memento.pointerCount - 1) { 3296 memento.setPointers(entry); 3297 return CONSISTENT; 3298 } 3299 return BROKEN; 3300 3301 case AMOTION_EVENT_ACTION_MOVE: 3302 if (entry->pointerCount == memento.pointerCount) { 3303 return CONSISTENT; 3304 } 3305 return BROKEN; 3306 3307 default: 3308 return BROKEN; 3309 } 3310 } 3311 } 3312 3313 switch (action) { 3314 case AMOTION_EVENT_ACTION_DOWN: { 3315 mMotionMementos.push(); 3316 MotionMemento& memento = mMotionMementos.editTop(); 3317 memento.deviceId = entry->deviceId; 3318 memento.source = entry->source; 3319 memento.xPrecision = entry->xPrecision; 3320 memento.yPrecision = entry->yPrecision; 3321 memento.downTime = entry->downTime; 3322 memento.setPointers(entry); 3323 return CONSISTENT; 3324 } 3325 3326 default: 3327 return BROKEN; 3328 } 3329 } 3330 3331 void InputDispatcher::InputState::MotionMemento::setPointers(const MotionEntry* entry) { 3332 pointerCount = entry->pointerCount; 3333 for (uint32_t i = 0; i < entry->pointerCount; i++) { 3334 pointerIds[i] = entry->pointerIds[i]; 3335 pointerCoords[i] = entry->lastSample->pointerCoords[i]; 3336 } 3337 } 3338 3339 void InputDispatcher::InputState::synthesizeCancelationEvents(nsecs_t currentTime, 3340 Allocator* allocator, Vector<EventEntry*>& outEvents, 3341 CancelationOptions options) { 3342 for (size_t i = 0; i < mKeyMementos.size(); ) { 3343 const KeyMemento& memento = mKeyMementos.itemAt(i); 3344 if (shouldCancelEvent(memento.source, options)) { 3345 outEvents.push(allocator->obtainKeyEntry(currentTime, 3346 memento.deviceId, memento.source, 0, 3347 AKEY_EVENT_ACTION_UP, AKEY_EVENT_FLAG_CANCELED, 3348 memento.keyCode, memento.scanCode, 0, 0, memento.downTime)); 3349 mKeyMementos.removeAt(i); 3350 } else { 3351 i += 1; 3352 } 3353 } 3354 3355 for (size_t i = 0; i < mMotionMementos.size(); ) { 3356 const MotionMemento& memento = mMotionMementos.itemAt(i); 3357 if (shouldCancelEvent(memento.source, options)) { 3358 outEvents.push(allocator->obtainMotionEntry(currentTime, 3359 memento.deviceId, memento.source, 0, 3360 AMOTION_EVENT_ACTION_CANCEL, 0, 0, 0, 3361 memento.xPrecision, memento.yPrecision, memento.downTime, 3362 memento.pointerCount, memento.pointerIds, memento.pointerCoords)); 3363 mMotionMementos.removeAt(i); 3364 } else { 3365 i += 1; 3366 } 3367 } 3368 } 3369 3370 void InputDispatcher::InputState::clear() { 3371 mKeyMementos.clear(); 3372 mMotionMementos.clear(); 3373 } 3374 3375 bool InputDispatcher::InputState::shouldCancelEvent(int32_t eventSource, 3376 CancelationOptions options) { 3377 switch (options) { 3378 case CANCEL_POINTER_EVENTS: 3379 return eventSource & AINPUT_SOURCE_CLASS_POINTER; 3380 case CANCEL_NON_POINTER_EVENTS: 3381 return !(eventSource & AINPUT_SOURCE_CLASS_POINTER); 3382 default: 3383 return true; 3384 } 3385 } 3386 3387 3388 // --- InputDispatcher::Connection --- 3389 3390 InputDispatcher::Connection::Connection(const sp<InputChannel>& inputChannel) : 3391 status(STATUS_NORMAL), inputChannel(inputChannel), inputPublisher(inputChannel), 3392 lastEventTime(LONG_LONG_MAX), lastDispatchTime(LONG_LONG_MAX) { 3393 } 3394 3395 InputDispatcher::Connection::~Connection() { 3396 } 3397 3398 status_t InputDispatcher::Connection::initialize() { 3399 return inputPublisher.initialize(); 3400 } 3401 3402 const char* InputDispatcher::Connection::getStatusLabel() const { 3403 switch (status) { 3404 case STATUS_NORMAL: 3405 return "NORMAL"; 3406 3407 case STATUS_BROKEN: 3408 return "BROKEN"; 3409 3410 case STATUS_ZOMBIE: 3411 return "ZOMBIE"; 3412 3413 default: 3414 return "UNKNOWN"; 3415 } 3416 } 3417 3418 InputDispatcher::DispatchEntry* InputDispatcher::Connection::findQueuedDispatchEntryForEvent( 3419 const EventEntry* eventEntry) const { 3420 for (DispatchEntry* dispatchEntry = outboundQueue.tailSentinel.prev; 3421 dispatchEntry != & outboundQueue.headSentinel; dispatchEntry = dispatchEntry->prev) { 3422 if (dispatchEntry->eventEntry == eventEntry) { 3423 return dispatchEntry; 3424 } 3425 } 3426 return NULL; 3427 } 3428 3429 3430 // --- InputDispatcher::CommandEntry --- 3431 3432 InputDispatcher::CommandEntry::CommandEntry() : 3433 keyEntry(NULL) { 3434 } 3435 3436 InputDispatcher::CommandEntry::~CommandEntry() { 3437 } 3438 3439 3440 // --- InputDispatcher::TouchState --- 3441 3442 InputDispatcher::TouchState::TouchState() : 3443 down(false), split(false) { 3444 } 3445 3446 InputDispatcher::TouchState::~TouchState() { 3447 } 3448 3449 void InputDispatcher::TouchState::reset() { 3450 down = false; 3451 split = false; 3452 windows.clear(); 3453 } 3454 3455 void InputDispatcher::TouchState::copyFrom(const TouchState& other) { 3456 down = other.down; 3457 split = other.split; 3458 windows.clear(); 3459 windows.appendVector(other.windows); 3460 } 3461 3462 void InputDispatcher::TouchState::addOrUpdateWindow(const InputWindow* window, 3463 int32_t targetFlags, BitSet32 pointerIds) { 3464 if (targetFlags & InputTarget::FLAG_SPLIT) { 3465 split = true; 3466 } 3467 3468 for (size_t i = 0; i < windows.size(); i++) { 3469 TouchedWindow& touchedWindow = windows.editItemAt(i); 3470 if (touchedWindow.window == window) { 3471 touchedWindow.targetFlags |= targetFlags; 3472 touchedWindow.pointerIds.value |= pointerIds.value; 3473 return; 3474 } 3475 } 3476 3477 windows.push(); 3478 3479 TouchedWindow& touchedWindow = windows.editTop(); 3480 touchedWindow.window = window; 3481 touchedWindow.targetFlags = targetFlags; 3482 touchedWindow.pointerIds = pointerIds; 3483 touchedWindow.channel = window->inputChannel; 3484 } 3485 3486 void InputDispatcher::TouchState::removeOutsideTouchWindows() { 3487 for (size_t i = 0 ; i < windows.size(); ) { 3488 if (windows[i].targetFlags & InputTarget::FLAG_OUTSIDE) { 3489 windows.removeAt(i); 3490 } else { 3491 i += 1; 3492 } 3493 } 3494 } 3495 3496 const InputWindow* InputDispatcher::TouchState::getFirstForegroundWindow() { 3497 for (size_t i = 0; i < windows.size(); i++) { 3498 if (windows[i].targetFlags & InputTarget::FLAG_FOREGROUND) { 3499 return windows[i].window; 3500 } 3501 } 3502 return NULL; 3503 } 3504 3505 3506 // --- InputDispatcherThread --- 3507 3508 InputDispatcherThread::InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher) : 3509 Thread(/*canCallJava*/ true), mDispatcher(dispatcher) { 3510 } 3511 3512 InputDispatcherThread::~InputDispatcherThread() { 3513 } 3514 3515 bool InputDispatcherThread::threadLoop() { 3516 mDispatcher->dispatchOnce(); 3517 return true; 3518 } 3519 3520 } // namespace android 3521