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