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