1d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright/*
2d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * Copyright (C) 2010 The Android Open Source Project
3d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright *
4d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * Licensed under the Apache License, Version 2.0 (the "License");
5d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * you may not use this file except in compliance with the License.
6d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * You may obtain a copy of the License at
7d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright *
8d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright *      http://www.apache.org/licenses/LICENSE-2.0
9d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright *
10d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * Unless required by applicable law or agreed to in writing, software
11d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * distributed under the License is distributed on an "AS IS" BASIS,
12d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * See the License for the specific language governing permissions and
14d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * limitations under the License.
15d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright */
16d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
17d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#define LOG_TAG "InputDispatcher"
18d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#define ATRACE_TAG ATRACE_TAG_INPUT
19d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
20d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright//#define LOG_NDEBUG 0
21d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
22d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Log detailed debug messages about each inbound event notification to the dispatcher.
23d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#define DEBUG_INBOUND_EVENT_DETAILS 0
24d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
25d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Log detailed debug messages about each outbound event processed by the dispatcher.
26d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#define DEBUG_OUTBOUND_EVENT_DETAILS 0
27d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
28d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Log debug messages about the dispatch cycle.
29d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#define DEBUG_DISPATCH_CYCLE 0
30d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
31d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Log debug messages about registrations.
32d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#define DEBUG_REGISTRATION 0
33d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
34d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Log debug messages about input event injection.
35d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#define DEBUG_INJECTION 0
36d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
37d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Log debug messages about input focus tracking.
38d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#define DEBUG_FOCUS 0
39d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
40d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Log debug messages about the app switch latency optimization.
41d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#define DEBUG_APP_SWITCH 0
42d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
43d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Log debug messages about hover events.
44d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#define DEBUG_HOVER 0
45d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
46d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include "InputDispatcher.h"
47d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
48d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <utils/Trace.h>
49d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <cutils/log.h>
50d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <powermanager/PowerManager.h>
51d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <ui/Region.h>
52d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
53d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <stddef.h>
54d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <unistd.h>
55d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <errno.h>
56d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <limits.h>
57d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <time.h>
58d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
59d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#define INDENT "  "
60d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#define INDENT2 "    "
61d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#define INDENT3 "      "
62d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#define INDENT4 "        "
63d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
64d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightnamespace android {
65d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
66d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Default input dispatching timeout if there is no focused application or paused window
67d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// from which to determine an appropriate dispatching timeout.
68d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightconst nsecs_t DEFAULT_INPUT_DISPATCHING_TIMEOUT = 5000 * 1000000LL; // 5 sec
69d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
70d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Amount of time to allow for all pending events to be processed when an app switch
71d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// key is on the way.  This is used to preempt input dispatch and drop input events
72d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// when an application takes too long to respond and the user has pressed an app switch key.
73d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightconst nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec
74d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
75d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Amount of time to allow for an event to be dispatched (measured since its eventTime)
76d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// before considering it stale and dropping it.
77d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightconst nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec
78d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
79d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Amount of time to allow touch events to be streamed out to a connection before requiring
80d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// that the first event be finished.  This value extends the ANR timeout by the specified
81d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// amount.  For example, if streaming is allowed to get ahead by one second relative to the
82d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// queue of waiting unfinished events, then ANRs will similarly be delayed by one second.
83d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightconst nsecs_t STREAM_AHEAD_EVENT_TIMEOUT = 500 * 1000000LL; // 0.5sec
84d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
85d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Log a warning when an event takes longer than this to process, even if an ANR does not occur.
86d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightconst nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec
87d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
88d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Number of recent events to keep for debugging purposes.
89d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightconst size_t RECENT_QUEUE_MAX_SIZE = 10;
90d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
91d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightstatic inline nsecs_t now() {
92d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return systemTime(SYSTEM_TIME_MONOTONIC);
93d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
94d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
95d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightstatic inline const char* toString(bool value) {
96d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return value ? "true" : "false";
97d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
98d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
99d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightstatic inline int32_t getMotionEventActionPointerIndex(int32_t action) {
100d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
101d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
102d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
103d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
104d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightstatic bool isValidKeyAction(int32_t action) {
105d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    switch (action) {
106d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case AKEY_EVENT_ACTION_DOWN:
107d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case AKEY_EVENT_ACTION_UP:
108d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return true;
109d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    default:
110d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return false;
111d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
112d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
113d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
114d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightstatic bool validateKeyEvent(int32_t action) {
115d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (! isValidKeyAction(action)) {
116d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ALOGE("Key event has invalid action code 0x%x", action);
117d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return false;
118d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
119d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return true;
120d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
121d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
122d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightstatic bool isValidMotionAction(int32_t action, size_t pointerCount) {
123d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    switch (action & AMOTION_EVENT_ACTION_MASK) {
124d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case AMOTION_EVENT_ACTION_DOWN:
125d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case AMOTION_EVENT_ACTION_UP:
126d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case AMOTION_EVENT_ACTION_CANCEL:
127d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case AMOTION_EVENT_ACTION_MOVE:
128d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case AMOTION_EVENT_ACTION_OUTSIDE:
129d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case AMOTION_EVENT_ACTION_HOVER_ENTER:
130d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case AMOTION_EVENT_ACTION_HOVER_MOVE:
131d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case AMOTION_EVENT_ACTION_HOVER_EXIT:
132d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case AMOTION_EVENT_ACTION_SCROLL:
133d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return true;
134d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case AMOTION_EVENT_ACTION_POINTER_DOWN:
135d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case AMOTION_EVENT_ACTION_POINTER_UP: {
136d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        int32_t index = getMotionEventActionPointerIndex(action);
137d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return index >= 0 && size_t(index) < pointerCount;
138d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
139d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    default:
140d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return false;
141d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
142d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
143d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
144d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightstatic bool validateMotionEvent(int32_t action, size_t pointerCount,
145d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        const PointerProperties* pointerProperties) {
146d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (! isValidMotionAction(action, pointerCount)) {
147d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ALOGE("Motion event has invalid action code 0x%x", action);
148d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return false;
149d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
150d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (pointerCount < 1 || pointerCount > MAX_POINTERS) {
15137764c71a0ac79142f90bb112b6cabffb940b955Narayan Kamath        ALOGE("Motion event has invalid pointer count %zu; value must be between 1 and %d.",
152d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                pointerCount, MAX_POINTERS);
153d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return false;
154d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
155d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    BitSet32 pointerIdBits;
156d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    for (size_t i = 0; i < pointerCount; i++) {
157d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        int32_t id = pointerProperties[i].id;
158d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (id < 0 || id > MAX_POINTER_ID) {
159d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d",
160d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    id, MAX_POINTER_ID);
161d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            return false;
162d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
163d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (pointerIdBits.hasBit(id)) {
164d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            ALOGE("Motion event has duplicate pointer id %d", id);
165d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            return false;
166d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
167d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        pointerIdBits.markBit(id);
168d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
169d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return true;
170d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
171d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
172d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightstatic bool isMainDisplay(int32_t displayId) {
173d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return displayId == ADISPLAY_ID_DEFAULT || displayId == ADISPLAY_ID_NONE;
174d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
175d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
176d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightstatic void dumpRegion(String8& dump, const Region& region) {
177d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (region.isEmpty()) {
178d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        dump.append("<empty>");
179d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return;
180d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
181d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
182d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool first = true;
183d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    Region::const_iterator cur = region.begin();
184d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    Region::const_iterator const tail = region.end();
185d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    while (cur != tail) {
186d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (first) {
187d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            first = false;
188d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        } else {
189d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            dump.append("|");
190d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
191d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        dump.appendFormat("[%d,%d][%d,%d]", cur->left, cur->top, cur->right, cur->bottom);
192d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        cur++;
193d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
194d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
195d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
196d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
197d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// --- InputDispatcher ---
198d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
199d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael WrightInputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) :
200d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    mPolicy(policy),
201d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    mPendingEvent(NULL), mAppSwitchSawKeyDown(false), mAppSwitchDueTime(LONG_LONG_MAX),
202d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    mNextUnblockedEvent(NULL),
203d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    mDispatchEnabled(false), mDispatchFrozen(false), mInputFilterEnabled(false),
204d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    mInputTargetWaitCause(INPUT_TARGET_WAIT_CAUSE_NONE) {
205d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    mLooper = new Looper(false);
206d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
207d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    mKeyRepeatState.lastKeyEntry = NULL;
208d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
209d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    policy->getDispatcherConfiguration(&mConfig);
210d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
211d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
212d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael WrightInputDispatcher::~InputDispatcher() {
213d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    { // acquire lock
214d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        AutoMutex _l(mLock);
215d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
216d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        resetKeyRepeatLocked();
217d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        releasePendingEventLocked();
218d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        drainInboundQueueLocked();
219d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
220d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
221d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    while (mConnectionsByFd.size() != 0) {
222d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        unregisterInputChannel(mConnectionsByFd.valueAt(0)->inputChannel);
223d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
224d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
225d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
226d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightvoid InputDispatcher::dispatchOnce() {
227d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    nsecs_t nextWakeupTime = LONG_LONG_MAX;
228d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    { // acquire lock
229d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        AutoMutex _l(mLock);
230d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mDispatcherIsAliveCondition.broadcast();
231d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
232d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // Run a dispatch loop if there are no pending commands.
233d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // The dispatch loop might enqueue commands to run afterwards.
234d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (!haveCommandsLocked()) {
235d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            dispatchOnceInnerLocked(&nextWakeupTime);
236d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
237d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
238d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // Run all pending commands if there are any.
239d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // If any commands were run then force the next poll to wake up immediately.
240d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (runCommandsLockedInterruptible()) {
241d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            nextWakeupTime = LONG_LONG_MIN;
242d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
243d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    } // release lock
244d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
245d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Wait for callback or timeout or wake.  (make sure we round up, not down)
246d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    nsecs_t currentTime = now();
247d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime);
248d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    mLooper->pollOnce(timeoutMillis);
249d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
250d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
251d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightvoid InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) {
252d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    nsecs_t currentTime = now();
253d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
254dc5992e48ca88d882313d206f8174efcc5e01455Jeff Brown    // Reset the key repeat timer whenever normal dispatch is suspended while the
255dc5992e48ca88d882313d206f8174efcc5e01455Jeff Brown    // device is in a non-interactive state.  This is to ensure that we abort a key
256dc5992e48ca88d882313d206f8174efcc5e01455Jeff Brown    // repeat if the device is just coming out of sleep.
257dc5992e48ca88d882313d206f8174efcc5e01455Jeff Brown    if (!mDispatchEnabled) {
258d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        resetKeyRepeatLocked();
259d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
260d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
261d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // If dispatching is frozen, do not process timeouts or try to deliver any new events.
262d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (mDispatchFrozen) {
263d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#if DEBUG_FOCUS
264d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ALOGD("Dispatch frozen.  Waiting some more.");
265d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#endif
266d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return;
267d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
268d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
269d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Optimize latency of app switches.
270d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has
271d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // been pressed.  When it expires, we preempt dispatch and drop all other pending events.
272d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool isAppSwitchDue = mAppSwitchDueTime <= currentTime;
273d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (mAppSwitchDueTime < *nextWakeupTime) {
274d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        *nextWakeupTime = mAppSwitchDueTime;
275d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
276d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
277d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Ready to start a new event.
278d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // If we don't already have a pending event, go grab one.
279d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (! mPendingEvent) {
280d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (mInboundQueue.isEmpty()) {
281d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            if (isAppSwitchDue) {
282d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                // The inbound queue is empty so the app switch key we were waiting
283d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                // for will never arrive.  Stop waiting for it.
284d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                resetPendingAppSwitchLocked(false);
285d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                isAppSwitchDue = false;
286d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            }
287d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
288d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            // Synthesize a key repeat if appropriate.
289d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            if (mKeyRepeatState.lastKeyEntry) {
290d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                if (currentTime >= mKeyRepeatState.nextRepeatTime) {
291d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    mPendingEvent = synthesizeKeyRepeatLocked(currentTime);
292d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                } else {
293d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) {
294d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                        *nextWakeupTime = mKeyRepeatState.nextRepeatTime;
295d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    }
296d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                }
297d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            }
298d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
299d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            // Nothing to do if there is no pending event.
300d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            if (!mPendingEvent) {
301d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                return;
302d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            }
303d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        } else {
304d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            // Inbound queue has at least one entry.
305d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            mPendingEvent = mInboundQueue.dequeueAtHead();
306d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            traceInboundQueueLengthLocked();
307d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
308d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
309d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // Poke user activity for this event.
310d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) {
311d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            pokeUserActivityLocked(mPendingEvent);
312d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
313d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
314d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // Get ready to dispatch the event.
315d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        resetANRTimeoutsLocked();
316d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
317d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
318d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Now we have an event to dispatch.
319d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // All events are eventually dequeued and processed this way, even if we intend to drop them.
320d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    ALOG_ASSERT(mPendingEvent != NULL);
321d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool done = false;
322d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    DropReason dropReason = DROP_REASON_NOT_DROPPED;
323d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) {
324d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        dropReason = DROP_REASON_POLICY;
325d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    } else if (!mDispatchEnabled) {
326d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        dropReason = DROP_REASON_DISABLED;
327d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
328d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
329d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (mNextUnblockedEvent == mPendingEvent) {
330d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mNextUnblockedEvent = NULL;
331d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
332d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
333d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    switch (mPendingEvent->type) {
334d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case EventEntry::TYPE_CONFIGURATION_CHANGED: {
335d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ConfigurationChangedEntry* typedEntry =
336d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                static_cast<ConfigurationChangedEntry*>(mPendingEvent);
337d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        done = dispatchConfigurationChangedLocked(currentTime, typedEntry);
338d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        dropReason = DROP_REASON_NOT_DROPPED; // configuration changes are never dropped
339d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        break;
340d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
341d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
342d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case EventEntry::TYPE_DEVICE_RESET: {
343d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        DeviceResetEntry* typedEntry =
344d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                static_cast<DeviceResetEntry*>(mPendingEvent);
345d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        done = dispatchDeviceResetLocked(currentTime, typedEntry);
346d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        dropReason = DROP_REASON_NOT_DROPPED; // device resets are never dropped
347d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        break;
348d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
349d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
350d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case EventEntry::TYPE_KEY: {
351d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent);
352d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (isAppSwitchDue) {
353d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            if (isAppSwitchKeyEventLocked(typedEntry)) {
354d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                resetPendingAppSwitchLocked(true);
355d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                isAppSwitchDue = false;
356d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            } else if (dropReason == DROP_REASON_NOT_DROPPED) {
357d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                dropReason = DROP_REASON_APP_SWITCH;
358d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            }
359d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
360d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (dropReason == DROP_REASON_NOT_DROPPED
361d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && isStaleEventLocked(currentTime, typedEntry)) {
362d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            dropReason = DROP_REASON_STALE;
363d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
364d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (dropReason == DROP_REASON_NOT_DROPPED && mNextUnblockedEvent) {
365d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            dropReason = DROP_REASON_BLOCKED;
366d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
367d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime);
368d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        break;
369d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
370d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
371d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case EventEntry::TYPE_MOTION: {
372d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent);
373d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (dropReason == DROP_REASON_NOT_DROPPED && isAppSwitchDue) {
374d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            dropReason = DROP_REASON_APP_SWITCH;
375d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
376d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (dropReason == DROP_REASON_NOT_DROPPED
377d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && isStaleEventLocked(currentTime, typedEntry)) {
378d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            dropReason = DROP_REASON_STALE;
379d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
380d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (dropReason == DROP_REASON_NOT_DROPPED && mNextUnblockedEvent) {
381d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            dropReason = DROP_REASON_BLOCKED;
382d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
383d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        done = dispatchMotionLocked(currentTime, typedEntry,
384d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                &dropReason, nextWakeupTime);
385d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        break;
386d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
387d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
388d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    default:
389d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ALOG_ASSERT(false);
390d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        break;
391d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
392d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
393d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (done) {
394d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (dropReason != DROP_REASON_NOT_DROPPED) {
395d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            dropInboundEventLocked(mPendingEvent, dropReason);
396d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
397d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
398d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        releasePendingEventLocked();
399d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        *nextWakeupTime = LONG_LONG_MIN;  // force next poll to wake up immediately
400d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
401d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
402d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
403d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightbool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) {
404d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool needWake = mInboundQueue.isEmpty();
405d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    mInboundQueue.enqueueAtTail(entry);
406d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    traceInboundQueueLengthLocked();
407d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
408d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    switch (entry->type) {
409d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case EventEntry::TYPE_KEY: {
410d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // Optimize app switch latency.
411d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // If the application takes too long to catch up then we drop all events preceding
412d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // the app switch key.
413d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        KeyEntry* keyEntry = static_cast<KeyEntry*>(entry);
414d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (isAppSwitchKeyEventLocked(keyEntry)) {
415d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            if (keyEntry->action == AKEY_EVENT_ACTION_DOWN) {
416d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                mAppSwitchSawKeyDown = true;
417d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            } else if (keyEntry->action == AKEY_EVENT_ACTION_UP) {
418d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                if (mAppSwitchSawKeyDown) {
419d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#if DEBUG_APP_SWITCH
420d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    ALOGD("App switch is pending!");
421d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#endif
422d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    mAppSwitchDueTime = keyEntry->eventTime + APP_SWITCH_TIMEOUT;
423d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    mAppSwitchSawKeyDown = false;
424d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    needWake = true;
425d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                }
426d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            }
427d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
428d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        break;
429d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
430d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
431d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case EventEntry::TYPE_MOTION: {
432d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // Optimize case where the current application is unresponsive and the user
433d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // decides to touch a window in a different application.
434d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // If the application takes too long to catch up then we drop all events preceding
435d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // the touch into the other window.
436d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        MotionEntry* motionEntry = static_cast<MotionEntry*>(entry);
437d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (motionEntry->action == AMOTION_EVENT_ACTION_DOWN
438d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER)
439d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY
440d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && mInputTargetWaitApplicationHandle != NULL) {
441d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            int32_t displayId = motionEntry->displayId;
442d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            int32_t x = int32_t(motionEntry->pointerCoords[0].
443d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    getAxisValue(AMOTION_EVENT_AXIS_X));
444d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            int32_t y = int32_t(motionEntry->pointerCoords[0].
445d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    getAxisValue(AMOTION_EVENT_AXIS_Y));
446d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            sp<InputWindowHandle> touchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y);
447d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            if (touchedWindowHandle != NULL
448d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    && touchedWindowHandle->inputApplicationHandle
449d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                            != mInputTargetWaitApplicationHandle) {
450d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                // User touched a different application than the one we are waiting on.
451d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                // Flag the event, and start pruning the input queue.
452d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                mNextUnblockedEvent = motionEntry;
453d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                needWake = true;
454d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            }
455d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
456d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        break;
457d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
458d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
459d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
460d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return needWake;
461d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
462d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
463d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightvoid InputDispatcher::addRecentEventLocked(EventEntry* entry) {
464d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    entry->refCount += 1;
465d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    mRecentQueue.enqueueAtTail(entry);
466d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (mRecentQueue.count() > RECENT_QUEUE_MAX_SIZE) {
467d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mRecentQueue.dequeueAtHead()->release();
468d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
469d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
470d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
471d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightsp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId,
472d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        int32_t x, int32_t y) {
473d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Traverse windows from front to back to find touched window.
474d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    size_t numWindows = mWindowHandles.size();
475d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    for (size_t i = 0; i < numWindows; i++) {
476d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        sp<InputWindowHandle> windowHandle = mWindowHandles.itemAt(i);
477d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        const InputWindowInfo* windowInfo = windowHandle->getInfo();
478d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (windowInfo->displayId == displayId) {
479d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            int32_t flags = windowInfo->layoutParamsFlags;
480d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
481d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            if (windowInfo->visible) {
482d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                if (!(flags & InputWindowInfo::FLAG_NOT_TOUCHABLE)) {
483d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    bool isTouchModal = (flags & (InputWindowInfo::FLAG_NOT_FOCUSABLE
484d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                            | InputWindowInfo::FLAG_NOT_TOUCH_MODAL)) == 0;
485d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) {
486d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                        // Found window.
487d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                        return windowHandle;
488d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    }
489d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                }
490d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            }
491d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
492d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
493d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return NULL;
494d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
495d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
496d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightvoid InputDispatcher::dropInboundEventLocked(EventEntry* entry, DropReason dropReason) {
497d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    const char* reason;
498d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    switch (dropReason) {
499d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case DROP_REASON_POLICY:
500d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#if DEBUG_INBOUND_EVENT_DETAILS
501d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ALOGD("Dropped event because policy consumed it.");
502d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#endif
503d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        reason = "inbound event was dropped because the policy consumed it";
504d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        break;
505d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case DROP_REASON_DISABLED:
506d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ALOGI("Dropped event because input dispatch is disabled.");
507d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        reason = "inbound event was dropped because input dispatch is disabled";
508d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        break;
509d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case DROP_REASON_APP_SWITCH:
510d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ALOGI("Dropped event because of pending overdue app switch.");
511d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        reason = "inbound event was dropped because of pending overdue app switch";
512d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        break;
513d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case DROP_REASON_BLOCKED:
514d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ALOGI("Dropped event because the current application is not responding and the user "
515d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                "has started interacting with a different application.");
516d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        reason = "inbound event was dropped because the current application is not responding "
517d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                "and the user has started interacting with a different application";
518d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        break;
519d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case DROP_REASON_STALE:
520d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ALOGI("Dropped event because it is stale.");
521d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        reason = "inbound event was dropped because it is stale";
522d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        break;
523d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    default:
524d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ALOG_ASSERT(false);
525d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return;
526d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
527d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
528d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    switch (entry->type) {
529d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case EventEntry::TYPE_KEY: {
530d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason);
531d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        synthesizeCancelationEventsForAllConnectionsLocked(options);
532d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        break;
533d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
534d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    case EventEntry::TYPE_MOTION: {
535d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        MotionEntry* motionEntry = static_cast<MotionEntry*>(entry);
536d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) {
537d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason);
538d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            synthesizeCancelationEventsForAllConnectionsLocked(options);
539d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        } else {
540d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason);
541d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            synthesizeCancelationEventsForAllConnectionsLocked(options);
542d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        }
543d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        break;
544d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
545d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
546d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
547d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
548d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightbool InputDispatcher::isAppSwitchKeyCode(int32_t keyCode) {
549d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return keyCode == AKEYCODE_HOME
550d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            || keyCode == AKEYCODE_ENDCALL
551d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            || keyCode == AKEYCODE_APP_SWITCH;
552d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
553d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
554d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightbool InputDispatcher::isAppSwitchKeyEventLocked(KeyEntry* keyEntry) {
555d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return ! (keyEntry->flags & AKEY_EVENT_FLAG_CANCELED)
556d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            && isAppSwitchKeyCode(keyEntry->keyCode)
557d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            && (keyEntry->policyFlags & POLICY_FLAG_TRUSTED)
558d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            && (keyEntry->policyFlags & POLICY_FLAG_PASS_TO_USER);
559d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
560d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
561d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightbool InputDispatcher::isAppSwitchPendingLocked() {
562d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return mAppSwitchDueTime != LONG_LONG_MAX;
563d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
564d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
565d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightvoid InputDispatcher::resetPendingAppSwitchLocked(bool handled) {
566d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    mAppSwitchDueTime = LONG_LONG_MAX;
567d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
568d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#if DEBUG_APP_SWITCH
569d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (handled) {
570d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ALOGD("App switch has arrived.");
571d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    } else {
572d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ALOGD("App switch was abandoned.");
573d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
574d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#endif
575d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
576d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
577d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightbool InputDispatcher::isStaleEventLocked(nsecs_t currentTime, EventEntry* entry) {
578d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return currentTime - entry->eventTime >= STALE_EVENT_TIMEOUT;
579d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
580d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
581d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightbool InputDispatcher::haveCommandsLocked() const {
582d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return !mCommandQueue.isEmpty();
583d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
584d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
585d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightbool InputDispatcher::runCommandsLockedInterruptible() {
586d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (mCommandQueue.isEmpty()) {
587d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return false;
588d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
589d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
590d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    do {
591d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        CommandEntry* commandEntry = mCommandQueue.dequeueAtHead();
592d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
593d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        Command command = commandEntry->command;
594d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        (this->*command)(commandEntry); // commands are implicitly 'LockedInterruptible'
595d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
596d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        commandEntry->connection.clear();
597d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        delete commandEntry;
598d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    } while (! mCommandQueue.isEmpty());
599d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return true;
600d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
601d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
602d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael WrightInputDispatcher::CommandEntry* InputDispatcher::postCommandLocked(Command command) {
603d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    CommandEntry* commandEntry = new CommandEntry(command);
604d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    mCommandQueue.enqueueAtTail(commandEntry);
605d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return commandEntry;
606d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
607d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
608d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightvoid InputDispatcher::drainInboundQueueLocked() {
609d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    while (! mInboundQueue.isEmpty()) {
610d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        EventEntry* entry = mInboundQueue.dequeueAtHead();
611d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        releaseInboundEventLocked(entry);
612d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
613d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    traceInboundQueueLengthLocked();
614d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
615d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
616d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightvoid InputDispatcher::releasePendingEventLocked() {
617d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (mPendingEvent) {
618d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        resetANRTimeoutsLocked();
619d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        releaseInboundEventLocked(mPendingEvent);
620d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mPendingEvent = NULL;
621d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
622d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
623d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
624d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightvoid InputDispatcher::releaseInboundEventLocked(EventEntry* entry) {
625d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    InjectionState* injectionState = entry->injectionState;
626d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (injectionState && injectionState->injectionResult == INPUT_EVENT_INJECTION_PENDING) {
627d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#if DEBUG_DISPATCH_CYCLE
628d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ALOGD("Injected inbound event was dropped.");
629d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#endif
630d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        setInjectionResultLocked(entry, INPUT_EVENT_INJECTION_FAILED);
631d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
632d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (entry == mNextUnblockedEvent) {
633d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mNextUnblockedEvent = NULL;
634d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
635d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    addRecentEventLocked(entry);
636d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    entry->release();
637d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
638d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
639d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightvoid InputDispatcher::resetKeyRepeatLocked() {
640d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (mKeyRepeatState.lastKeyEntry) {
641d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mKeyRepeatState.lastKeyEntry->release();
642d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mKeyRepeatState.lastKeyEntry = NULL;
643d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
644d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
645d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
646d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael WrightInputDispatcher::KeyEntry* InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) {
647d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    KeyEntry* entry = mKeyRepeatState.lastKeyEntry;
648d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
649d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Reuse the repeated key entry if it is otherwise unreferenced.
6502e732956adffbdf37374115d52646ddae9498a96Michael Wright    uint32_t policyFlags = entry->policyFlags &
6512e732956adffbdf37374115d52646ddae9498a96Michael Wright            (POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED);
652d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (entry->refCount == 1) {
653d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        entry->recycle();
654d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        entry->eventTime = currentTime;
655d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        entry->policyFlags = policyFlags;
656d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        entry->repeatCount += 1;
657d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    } else {
658d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        KeyEntry* newEntry = new KeyEntry(currentTime,
659d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                entry->deviceId, entry->source, policyFlags,
660d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                entry->action, entry->flags, entry->keyCode, entry->scanCode,
661d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                entry->metaState, entry->repeatCount + 1, entry->downTime);
662d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
663d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        mKeyRepeatState.lastKeyEntry = newEntry;
664d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        entry->release();
665d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
666d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        entry = newEntry;
667d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
668d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    entry->syntheticRepeat = true;
669d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
670d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Increment reference count since we keep a reference to the event in
671d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // mKeyRepeatState.lastKeyEntry in addition to the one we return.
672d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    entry->refCount += 1;
673d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
674d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay;
675d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return entry;
676d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
677d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
678d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightbool InputDispatcher::dispatchConfigurationChangedLocked(
679d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        nsecs_t currentTime, ConfigurationChangedEntry* entry) {
680d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#if DEBUG_OUTBOUND_EVENT_DETAILS
681d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    ALOGD("dispatchConfigurationChanged - eventTime=%lld", entry->eventTime);
682d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#endif
683d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
684d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Reset key repeating in case a keyboard device was added or removed or something.
685d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    resetKeyRepeatLocked();
686d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
687d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Enqueue a command to run outside the lock to tell the policy that the configuration changed.
688d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    CommandEntry* commandEntry = postCommandLocked(
689d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            & InputDispatcher::doNotifyConfigurationChangedInterruptible);
690d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    commandEntry->eventTime = entry->eventTime;
691d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return true;
692d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
693d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
694d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightbool InputDispatcher::dispatchDeviceResetLocked(
695d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        nsecs_t currentTime, DeviceResetEntry* entry) {
696d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#if DEBUG_OUTBOUND_EVENT_DETAILS
697d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    ALOGD("dispatchDeviceReset - eventTime=%lld, deviceId=%d", entry->eventTime, entry->deviceId);
698d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#endif
699d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
700d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS,
701d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            "device was reset");
702d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    options.deviceId = entry->deviceId;
703d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    synthesizeCancelationEventsForAllConnectionsLocked(options);
704d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    return true;
705d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright}
706d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
707d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightbool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry,
708d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        DropReason* dropReason, nsecs_t* nextWakeupTime) {
709d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Preprocessing.
710d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    if (! entry->dispatchInProgress) {
711d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        if (entry->repeatCount == 0
712d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && entry->action == AKEY_EVENT_ACTION_DOWN
713d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && (entry->policyFlags & POLICY_FLAG_TRUSTED)
714d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) {
715d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            if (mKeyRepeatState.lastKeyEntry
716d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                    && mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode) {
717d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                // We have seen two identical key downs in a row which indicates that the device
718d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                // driver is automatically generating key repeats itself.  We take note of the
719d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                // repeat here, but we disable our own next key repeat timer since it is clear that
720d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                // we will not need to synthesize key repeats ourselves.
721d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1;
722d02c5b6aace05d9fd938e2d03705ac4f60f8da19