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