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