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