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