Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2010 The Android Open Source Project |
| 3 | // |
| 4 | // The input dispatcher. |
| 5 | // |
| 6 | #define LOG_TAG "InputDispatcher" |
| 7 | |
| 8 | //#define LOG_NDEBUG 0 |
| 9 | |
| 10 | // Log detailed debug messages about each inbound event notification to the dispatcher. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 11 | #define DEBUG_INBOUND_EVENT_DETAILS 0 |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 12 | |
| 13 | // Log detailed debug messages about each outbound event processed by the dispatcher. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 14 | #define DEBUG_OUTBOUND_EVENT_DETAILS 0 |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 15 | |
| 16 | // Log debug messages about batching. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 17 | #define DEBUG_BATCHING 0 |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 18 | |
| 19 | // Log debug messages about the dispatch cycle. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 20 | #define DEBUG_DISPATCH_CYCLE 0 |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 21 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 22 | // Log debug messages about registrations. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 23 | #define DEBUG_REGISTRATION 0 |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 24 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 25 | // Log debug messages about performance statistics. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 26 | #define DEBUG_PERFORMANCE_STATISTICS 0 |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 27 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 28 | // Log debug messages about input event injection. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 29 | #define DEBUG_INJECTION 0 |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 30 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 31 | #include <cutils/log.h> |
| 32 | #include <ui/InputDispatcher.h> |
| 33 | |
| 34 | #include <stddef.h> |
| 35 | #include <unistd.h> |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 36 | #include <errno.h> |
| 37 | #include <limits.h> |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 38 | |
| 39 | namespace android { |
| 40 | |
| 41 | // TODO, this needs to be somewhere else, perhaps in the policy |
| 42 | static inline bool isMovementKey(int32_t keyCode) { |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame^] | 43 | return keyCode == AKEYCODE_DPAD_UP |
| 44 | || keyCode == AKEYCODE_DPAD_DOWN |
| 45 | || keyCode == AKEYCODE_DPAD_LEFT |
| 46 | || keyCode == AKEYCODE_DPAD_RIGHT; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 49 | static inline nsecs_t now() { |
| 50 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 51 | } |
| 52 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 53 | // --- InputDispatcher --- |
| 54 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 55 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) : |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 56 | mPolicy(policy) { |
Dianne Hackborn | 85448bb | 2010-07-07 14:27:31 -0700 | [diff] [blame] | 57 | mPollLoop = new PollLoop(false); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 58 | |
| 59 | mInboundQueue.head.refCount = -1; |
| 60 | mInboundQueue.head.type = EventEntry::TYPE_SENTINEL; |
| 61 | mInboundQueue.head.eventTime = LONG_LONG_MIN; |
| 62 | |
| 63 | mInboundQueue.tail.refCount = -1; |
| 64 | mInboundQueue.tail.type = EventEntry::TYPE_SENTINEL; |
| 65 | mInboundQueue.tail.eventTime = LONG_LONG_MAX; |
| 66 | |
| 67 | mKeyRepeatState.lastKeyEntry = NULL; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 68 | |
| 69 | mCurrentInputTargetsValid = false; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | InputDispatcher::~InputDispatcher() { |
| 73 | resetKeyRepeatLocked(); |
| 74 | |
| 75 | while (mConnectionsByReceiveFd.size() != 0) { |
| 76 | unregisterInputChannel(mConnectionsByReceiveFd.valueAt(0)->inputChannel); |
| 77 | } |
| 78 | |
| 79 | for (EventEntry* entry = mInboundQueue.head.next; entry != & mInboundQueue.tail; ) { |
| 80 | EventEntry* next = entry->next; |
| 81 | mAllocator.releaseEventEntry(next); |
| 82 | entry = next; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void InputDispatcher::dispatchOnce() { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 87 | nsecs_t keyRepeatTimeout = mPolicy->getKeyRepeatTimeout(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 88 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 89 | bool skipPoll = false; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 90 | nsecs_t currentTime; |
| 91 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 92 | { // acquire lock |
| 93 | AutoMutex _l(mLock); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 94 | currentTime = now(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 95 | |
| 96 | // Reset the key repeat timer whenever we disallow key events, even if the next event |
| 97 | // is not a key. This is to ensure that we abort a key repeat if the device is just coming |
| 98 | // out of sleep. |
| 99 | // XXX we should handle resetting input state coming out of sleep more generally elsewhere |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 100 | if (keyRepeatTimeout < 0) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 101 | resetKeyRepeatLocked(); |
| 102 | } |
| 103 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 104 | // Detect and process timeouts for all connections and determine if there are any |
| 105 | // synchronous event dispatches pending. This step is entirely non-interruptible. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 106 | bool hasPendingSyncTarget = false; |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 107 | size_t activeConnectionCount = mActiveConnections.size(); |
| 108 | for (size_t i = 0; i < activeConnectionCount; i++) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 109 | Connection* connection = mActiveConnections.itemAt(i); |
| 110 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 111 | if (connection->hasPendingSyncTarget()) { |
| 112 | hasPendingSyncTarget = true; |
| 113 | } |
| 114 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 115 | nsecs_t connectionTimeoutTime = connection->nextTimeoutTime; |
| 116 | if (connectionTimeoutTime <= currentTime) { |
| 117 | mTimedOutConnections.add(connection); |
| 118 | } else if (connectionTimeoutTime < nextWakeupTime) { |
| 119 | nextWakeupTime = connectionTimeoutTime; |
| 120 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 123 | size_t timedOutConnectionCount = mTimedOutConnections.size(); |
| 124 | for (size_t i = 0; i < timedOutConnectionCount; i++) { |
| 125 | Connection* connection = mTimedOutConnections.itemAt(i); |
| 126 | timeoutDispatchCycleLocked(currentTime, connection); |
| 127 | skipPoll = true; |
| 128 | } |
| 129 | mTimedOutConnections.clear(); |
| 130 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 131 | // If we don't have a pending sync target, then we can begin delivering a new event. |
| 132 | // (Otherwise we wait for dispatch to complete for that target.) |
| 133 | if (! hasPendingSyncTarget) { |
| 134 | if (mInboundQueue.isEmpty()) { |
| 135 | if (mKeyRepeatState.lastKeyEntry) { |
| 136 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 137 | processKeyRepeatLockedInterruptible(currentTime, keyRepeatTimeout); |
| 138 | skipPoll = true; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 139 | } else { |
| 140 | if (mKeyRepeatState.nextRepeatTime < nextWakeupTime) { |
| 141 | nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } else { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 146 | // Inbound queue has at least one entry. |
| 147 | // Start processing it but leave it on the queue until later so that the |
| 148 | // input reader can keep appending samples onto a motion event between the |
| 149 | // time we started processing it and the time we finally enqueue dispatch |
| 150 | // entries for it. |
| 151 | EventEntry* entry = mInboundQueue.head.next; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 152 | |
| 153 | switch (entry->type) { |
| 154 | case EventEntry::TYPE_CONFIGURATION_CHANGED: { |
| 155 | ConfigurationChangedEntry* typedEntry = |
| 156 | static_cast<ConfigurationChangedEntry*>(entry); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 157 | processConfigurationChangedLockedInterruptible(currentTime, typedEntry); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 158 | break; |
| 159 | } |
| 160 | |
| 161 | case EventEntry::TYPE_KEY: { |
| 162 | KeyEntry* typedEntry = static_cast<KeyEntry*>(entry); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 163 | processKeyLockedInterruptible(currentTime, typedEntry, keyRepeatTimeout); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 164 | break; |
| 165 | } |
| 166 | |
| 167 | case EventEntry::TYPE_MOTION: { |
| 168 | MotionEntry* typedEntry = static_cast<MotionEntry*>(entry); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 169 | processMotionLockedInterruptible(currentTime, typedEntry); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 170 | break; |
| 171 | } |
| 172 | |
| 173 | default: |
| 174 | assert(false); |
| 175 | break; |
| 176 | } |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 177 | |
| 178 | // Dequeue and release the event entry that we just processed. |
| 179 | mInboundQueue.dequeue(entry); |
| 180 | mAllocator.releaseEventEntry(entry); |
| 181 | skipPoll = true; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 184 | |
| 185 | // Run any deferred commands. |
| 186 | skipPoll |= runCommandsLockedInterruptible(); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 187 | |
| 188 | // Wake up synchronization waiters, if needed. |
| 189 | if (isFullySynchronizedLocked()) { |
| 190 | mFullySynchronizedCondition.broadcast(); |
| 191 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 192 | } // release lock |
| 193 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 194 | // If we dispatched anything, don't poll just now. Wait for the next iteration. |
| 195 | // Contents may have shifted during flight. |
| 196 | if (skipPoll) { |
| 197 | return; |
| 198 | } |
| 199 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 200 | // Wait for callback or timeout or wake. |
| 201 | nsecs_t timeout = nanoseconds_to_milliseconds(nextWakeupTime - currentTime); |
| 202 | int32_t timeoutMillis = timeout > INT_MAX ? -1 : timeout > 0 ? int32_t(timeout) : 0; |
| 203 | mPollLoop->pollOnce(timeoutMillis); |
| 204 | } |
| 205 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 206 | bool InputDispatcher::runCommandsLockedInterruptible() { |
| 207 | if (mCommandQueue.isEmpty()) { |
| 208 | return false; |
| 209 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 210 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 211 | do { |
| 212 | CommandEntry* commandEntry = mCommandQueue.dequeueAtHead(); |
| 213 | |
| 214 | Command command = commandEntry->command; |
| 215 | (this->*command)(commandEntry); // commands are implicitly 'LockedInterruptible' |
| 216 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 217 | commandEntry->connection.clear(); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 218 | mAllocator.releaseCommandEntry(commandEntry); |
| 219 | } while (! mCommandQueue.isEmpty()); |
| 220 | return true; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 223 | InputDispatcher::CommandEntry* InputDispatcher::postCommandLocked(Command command) { |
| 224 | CommandEntry* commandEntry = mAllocator.obtainCommandEntry(command); |
| 225 | mCommandQueue.enqueueAtTail(commandEntry); |
| 226 | return commandEntry; |
| 227 | } |
| 228 | |
| 229 | void InputDispatcher::processConfigurationChangedLockedInterruptible( |
| 230 | nsecs_t currentTime, ConfigurationChangedEntry* entry) { |
| 231 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 232 | LOGD("processConfigurationChanged - eventTime=%lld", entry->eventTime); |
| 233 | #endif |
| 234 | |
Jeff Brown | 0b72e82 | 2010-06-29 16:52:21 -0700 | [diff] [blame] | 235 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 236 | resetKeyRepeatLocked(); |
| 237 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 238 | mLock.unlock(); |
| 239 | |
| 240 | mPolicy->notifyConfigurationChanged(entry->eventTime); |
| 241 | |
| 242 | mLock.lock(); |
| 243 | } |
| 244 | |
| 245 | void InputDispatcher::processKeyLockedInterruptible( |
| 246 | nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 247 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 248 | LOGD("processKey - eventTime=%lld, deviceId=0x%x, nature=0x%x, policyFlags=0x%x, action=0x%x, " |
| 249 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%lld", |
| 250 | entry->eventTime, entry->deviceId, entry->nature, entry->policyFlags, entry->action, |
| 251 | entry->flags, entry->keyCode, entry->scanCode, entry->metaState, |
| 252 | entry->downTime); |
| 253 | #endif |
| 254 | |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 255 | if (entry->action == KEY_EVENT_ACTION_DOWN && ! entry->isInjected()) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 256 | if (mKeyRepeatState.lastKeyEntry |
| 257 | && mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode) { |
| 258 | // We have seen two identical key downs in a row which indicates that the device |
| 259 | // driver is automatically generating key repeats itself. We take note of the |
| 260 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 261 | // we will not need to synthesize key repeats ourselves. |
| 262 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 263 | resetKeyRepeatLocked(); |
| 264 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 265 | } else { |
| 266 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 267 | resetKeyRepeatLocked(); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 268 | mKeyRepeatState.nextRepeatTime = entry->eventTime + keyRepeatTimeout; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 269 | } |
| 270 | mKeyRepeatState.lastKeyEntry = entry; |
| 271 | entry->refCount += 1; |
| 272 | } else { |
| 273 | resetKeyRepeatLocked(); |
| 274 | } |
| 275 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 276 | identifyInputTargetsAndDispatchKeyLockedInterruptible(currentTime, entry); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 279 | void InputDispatcher::processKeyRepeatLockedInterruptible( |
| 280 | nsecs_t currentTime, nsecs_t keyRepeatTimeout) { |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 281 | KeyEntry* entry = mKeyRepeatState.lastKeyEntry; |
| 282 | |
| 283 | // Search the inbound queue for a key up corresponding to this device. |
| 284 | // It doesn't make sense to generate a key repeat event if the key is already up. |
| 285 | for (EventEntry* queuedEntry = mInboundQueue.head.next; |
| 286 | queuedEntry != & mInboundQueue.tail; queuedEntry = entry->next) { |
| 287 | if (queuedEntry->type == EventEntry::TYPE_KEY) { |
| 288 | KeyEntry* queuedKeyEntry = static_cast<KeyEntry*>(queuedEntry); |
| 289 | if (queuedKeyEntry->deviceId == entry->deviceId |
| 290 | && entry->action == KEY_EVENT_ACTION_UP) { |
| 291 | resetKeyRepeatLocked(); |
| 292 | return; |
| 293 | } |
| 294 | } |
| 295 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 296 | |
| 297 | // Synthesize a key repeat after the repeat timeout expired. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 298 | // Reuse the repeated key entry if it is otherwise unreferenced. |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 299 | uint32_t policyFlags = entry->policyFlags & POLICY_FLAG_RAW_MASK; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 300 | if (entry->refCount == 1) { |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 301 | entry->eventTime = currentTime; |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 302 | entry->policyFlags = policyFlags; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 303 | entry->repeatCount += 1; |
| 304 | } else { |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 305 | KeyEntry* newEntry = mAllocator.obtainKeyEntry(currentTime, |
| 306 | entry->deviceId, entry->nature, policyFlags, |
| 307 | entry->action, entry->flags, entry->keyCode, entry->scanCode, |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 308 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 309 | |
| 310 | mKeyRepeatState.lastKeyEntry = newEntry; |
| 311 | mAllocator.releaseKeyEntry(entry); |
| 312 | |
| 313 | entry = newEntry; |
| 314 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 315 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 316 | if (entry->repeatCount == 1) { |
| 317 | entry->flags |= KEY_EVENT_FLAG_LONG_PRESS; |
| 318 | } |
| 319 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 320 | mKeyRepeatState.nextRepeatTime = currentTime + keyRepeatTimeout; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 321 | |
| 322 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 323 | LOGD("processKeyRepeat - eventTime=%lld, deviceId=0x%x, nature=0x%x, policyFlags=0x%x, " |
| 324 | "action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, " |
| 325 | "repeatCount=%d, downTime=%lld", |
| 326 | entry->eventTime, entry->deviceId, entry->nature, entry->policyFlags, |
| 327 | entry->action, entry->flags, entry->keyCode, entry->scanCode, entry->metaState, |
| 328 | entry->repeatCount, entry->downTime); |
| 329 | #endif |
| 330 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 331 | identifyInputTargetsAndDispatchKeyLockedInterruptible(currentTime, entry); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 334 | void InputDispatcher::processMotionLockedInterruptible( |
| 335 | nsecs_t currentTime, MotionEntry* entry) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 336 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 337 | LOGD("processMotion - eventTime=%lld, deviceId=0x%x, nature=0x%x, policyFlags=0x%x, action=0x%x, " |
| 338 | "metaState=0x%x, edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%lld", |
| 339 | entry->eventTime, entry->deviceId, entry->nature, entry->policyFlags, entry->action, |
| 340 | entry->metaState, entry->edgeFlags, entry->xPrecision, entry->yPrecision, |
| 341 | entry->downTime); |
| 342 | |
| 343 | // Print the most recent sample that we have available, this may change due to batching. |
| 344 | size_t sampleCount = 1; |
| 345 | MotionSample* sample = & entry->firstSample; |
| 346 | for (; sample->next != NULL; sample = sample->next) { |
| 347 | sampleCount += 1; |
| 348 | } |
| 349 | for (uint32_t i = 0; i < entry->pointerCount; i++) { |
| 350 | LOGD(" Pointer %d: id=%d, x=%f, y=%f, pressure=%f, size=%f", |
| 351 | i, entry->pointerIds[i], |
| 352 | sample->pointerCoords[i].x, |
| 353 | sample->pointerCoords[i].y, |
| 354 | sample->pointerCoords[i].pressure, |
| 355 | sample->pointerCoords[i].size); |
| 356 | } |
| 357 | |
| 358 | // Keep in mind that due to batching, it is possible for the number of samples actually |
| 359 | // dispatched to change before the application finally consumed them. |
| 360 | if (entry->action == MOTION_EVENT_ACTION_MOVE) { |
| 361 | LOGD(" ... Total movement samples currently batched %d ...", sampleCount); |
| 362 | } |
| 363 | #endif |
| 364 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 365 | identifyInputTargetsAndDispatchMotionLockedInterruptible(currentTime, entry); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 368 | void InputDispatcher::identifyInputTargetsAndDispatchKeyLockedInterruptible( |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 369 | nsecs_t currentTime, KeyEntry* entry) { |
| 370 | #if DEBUG_DISPATCH_CYCLE |
| 371 | LOGD("identifyInputTargetsAndDispatchKey"); |
| 372 | #endif |
| 373 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 374 | entry->dispatchInProgress = true; |
| 375 | mCurrentInputTargetsValid = false; |
| 376 | mLock.unlock(); |
| 377 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 378 | mReusableKeyEvent.initialize(entry->deviceId, entry->nature, entry->action, entry->flags, |
| 379 | entry->keyCode, entry->scanCode, entry->metaState, entry->repeatCount, |
| 380 | entry->downTime, entry->eventTime); |
| 381 | |
| 382 | mCurrentInputTargets.clear(); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 383 | int32_t injectionResult = mPolicy->waitForKeyEventTargets(& mReusableKeyEvent, |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 384 | entry->policyFlags, entry->injectorPid, entry->injectorUid, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 385 | mCurrentInputTargets); |
| 386 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 387 | mLock.lock(); |
| 388 | mCurrentInputTargetsValid = true; |
| 389 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 390 | setInjectionResultLocked(entry, injectionResult); |
| 391 | |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 392 | if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED) { |
| 393 | dispatchEventToCurrentInputTargetsLocked(currentTime, entry, false); |
| 394 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 397 | void InputDispatcher::identifyInputTargetsAndDispatchMotionLockedInterruptible( |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 398 | nsecs_t currentTime, MotionEntry* entry) { |
| 399 | #if DEBUG_DISPATCH_CYCLE |
| 400 | LOGD("identifyInputTargetsAndDispatchMotion"); |
| 401 | #endif |
| 402 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 403 | entry->dispatchInProgress = true; |
| 404 | mCurrentInputTargetsValid = false; |
| 405 | mLock.unlock(); |
| 406 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 407 | mReusableMotionEvent.initialize(entry->deviceId, entry->nature, entry->action, |
| 408 | entry->edgeFlags, entry->metaState, |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 409 | 0, 0, entry->xPrecision, entry->yPrecision, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 410 | entry->downTime, entry->eventTime, entry->pointerCount, entry->pointerIds, |
| 411 | entry->firstSample.pointerCoords); |
| 412 | |
| 413 | mCurrentInputTargets.clear(); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 414 | int32_t injectionResult = mPolicy->waitForMotionEventTargets(& mReusableMotionEvent, |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 415 | entry->policyFlags, entry->injectorPid, entry->injectorUid, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 416 | mCurrentInputTargets); |
| 417 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 418 | mLock.lock(); |
| 419 | mCurrentInputTargetsValid = true; |
| 420 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 421 | setInjectionResultLocked(entry, injectionResult); |
| 422 | |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 423 | if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED) { |
| 424 | dispatchEventToCurrentInputTargetsLocked(currentTime, entry, false); |
| 425 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | void InputDispatcher::dispatchEventToCurrentInputTargetsLocked(nsecs_t currentTime, |
| 429 | EventEntry* eventEntry, bool resumeWithAppendedMotionSample) { |
| 430 | #if DEBUG_DISPATCH_CYCLE |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 431 | LOGD("dispatchEventToCurrentInputTargets - " |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 432 | "resumeWithAppendedMotionSample=%s", |
| 433 | resumeWithAppendedMotionSample ? "true" : "false"); |
| 434 | #endif |
| 435 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 436 | assert(eventEntry->dispatchInProgress); // should already have been set to true |
| 437 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 438 | for (size_t i = 0; i < mCurrentInputTargets.size(); i++) { |
| 439 | const InputTarget& inputTarget = mCurrentInputTargets.itemAt(i); |
| 440 | |
| 441 | ssize_t connectionIndex = mConnectionsByReceiveFd.indexOfKey( |
| 442 | inputTarget.inputChannel->getReceivePipeFd()); |
| 443 | if (connectionIndex >= 0) { |
| 444 | sp<Connection> connection = mConnectionsByReceiveFd.valueAt(connectionIndex); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 445 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, & inputTarget, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 446 | resumeWithAppendedMotionSample); |
| 447 | } else { |
| 448 | LOGW("Framework requested delivery of an input event to channel '%s' but it " |
| 449 | "is not registered with the input dispatcher.", |
| 450 | inputTarget.inputChannel->getName().string()); |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 455 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
| 456 | const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 457 | bool resumeWithAppendedMotionSample) { |
| 458 | #if DEBUG_DISPATCH_CYCLE |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 459 | LOGD("channel '%s' ~ prepareDispatchCycle - flags=%d, timeout=%lldns, " |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 460 | "xOffset=%f, yOffset=%f, resumeWithAppendedMotionSample=%s", |
| 461 | connection->getInputChannelName(), inputTarget->flags, inputTarget->timeout, |
| 462 | inputTarget->xOffset, inputTarget->yOffset, |
| 463 | resumeWithAppendedMotionSample ? "true" : "false"); |
| 464 | #endif |
| 465 | |
| 466 | // Skip this event if the connection status is not normal. |
| 467 | // We don't want to queue outbound events at all if the connection is broken or |
| 468 | // not responding. |
| 469 | if (connection->status != Connection::STATUS_NORMAL) { |
| 470 | LOGV("channel '%s' ~ Dropping event because the channel status is %s", |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 471 | connection->getStatusLabel()); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 472 | return; |
| 473 | } |
| 474 | |
| 475 | // Resume the dispatch cycle with a freshly appended motion sample. |
| 476 | // First we check that the last dispatch entry in the outbound queue is for the same |
| 477 | // motion event to which we appended the motion sample. If we find such a dispatch |
| 478 | // entry, and if it is currently in progress then we try to stream the new sample. |
| 479 | bool wasEmpty = connection->outboundQueue.isEmpty(); |
| 480 | |
| 481 | if (! wasEmpty && resumeWithAppendedMotionSample) { |
| 482 | DispatchEntry* motionEventDispatchEntry = |
| 483 | connection->findQueuedDispatchEntryForEvent(eventEntry); |
| 484 | if (motionEventDispatchEntry) { |
| 485 | // If the dispatch entry is not in progress, then we must be busy dispatching an |
| 486 | // earlier event. Not a problem, the motion event is on the outbound queue and will |
| 487 | // be dispatched later. |
| 488 | if (! motionEventDispatchEntry->inProgress) { |
| 489 | #if DEBUG_BATCHING |
| 490 | LOGD("channel '%s' ~ Not streaming because the motion event has " |
| 491 | "not yet been dispatched. " |
| 492 | "(Waiting for earlier events to be consumed.)", |
| 493 | connection->getInputChannelName()); |
| 494 | #endif |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | // If the dispatch entry is in progress but it already has a tail of pending |
| 499 | // motion samples, then it must mean that the shared memory buffer filled up. |
| 500 | // Not a problem, when this dispatch cycle is finished, we will eventually start |
| 501 | // a new dispatch cycle to process the tail and that tail includes the newly |
| 502 | // appended motion sample. |
| 503 | if (motionEventDispatchEntry->tailMotionSample) { |
| 504 | #if DEBUG_BATCHING |
| 505 | LOGD("channel '%s' ~ Not streaming because no new samples can " |
| 506 | "be appended to the motion event in this dispatch cycle. " |
| 507 | "(Waiting for next dispatch cycle to start.)", |
| 508 | connection->getInputChannelName()); |
| 509 | #endif |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | // The dispatch entry is in progress and is still potentially open for streaming. |
| 514 | // Try to stream the new motion sample. This might fail if the consumer has already |
| 515 | // consumed the motion event (or if the channel is broken). |
| 516 | MotionSample* appendedMotionSample = static_cast<MotionEntry*>(eventEntry)->lastSample; |
| 517 | status_t status = connection->inputPublisher.appendMotionSample( |
| 518 | appendedMotionSample->eventTime, appendedMotionSample->pointerCoords); |
| 519 | if (status == OK) { |
| 520 | #if DEBUG_BATCHING |
| 521 | LOGD("channel '%s' ~ Successfully streamed new motion sample.", |
| 522 | connection->getInputChannelName()); |
| 523 | #endif |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | #if DEBUG_BATCHING |
| 528 | if (status == NO_MEMORY) { |
| 529 | LOGD("channel '%s' ~ Could not append motion sample to currently " |
| 530 | "dispatched move event because the shared memory buffer is full. " |
| 531 | "(Waiting for next dispatch cycle to start.)", |
| 532 | connection->getInputChannelName()); |
| 533 | } else if (status == status_t(FAILED_TRANSACTION)) { |
| 534 | LOGD("channel '%s' ~ Could not append motion sample to currently " |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 535 | "dispatched move event because the event has already been consumed. " |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 536 | "(Waiting for next dispatch cycle to start.)", |
| 537 | connection->getInputChannelName()); |
| 538 | } else { |
| 539 | LOGD("channel '%s' ~ Could not append motion sample to currently " |
| 540 | "dispatched move event due to an error, status=%d. " |
| 541 | "(Waiting for next dispatch cycle to start.)", |
| 542 | connection->getInputChannelName(), status); |
| 543 | } |
| 544 | #endif |
| 545 | // Failed to stream. Start a new tail of pending motion samples to dispatch |
| 546 | // in the next cycle. |
| 547 | motionEventDispatchEntry->tailMotionSample = appendedMotionSample; |
| 548 | return; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | // This is a new event. |
| 553 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
| 554 | DispatchEntry* dispatchEntry = mAllocator.obtainDispatchEntry(eventEntry); // increments ref |
| 555 | dispatchEntry->targetFlags = inputTarget->flags; |
| 556 | dispatchEntry->xOffset = inputTarget->xOffset; |
| 557 | dispatchEntry->yOffset = inputTarget->yOffset; |
| 558 | dispatchEntry->timeout = inputTarget->timeout; |
| 559 | dispatchEntry->inProgress = false; |
| 560 | dispatchEntry->headMotionSample = NULL; |
| 561 | dispatchEntry->tailMotionSample = NULL; |
| 562 | |
| 563 | // Handle the case where we could not stream a new motion sample because the consumer has |
| 564 | // already consumed the motion event (otherwise the corresponding dispatch entry would |
| 565 | // still be in the outbound queue for this connection). We set the head motion sample |
| 566 | // to the list starting with the newly appended motion sample. |
| 567 | if (resumeWithAppendedMotionSample) { |
| 568 | #if DEBUG_BATCHING |
| 569 | LOGD("channel '%s' ~ Preparing a new dispatch cycle for additional motion samples " |
| 570 | "that cannot be streamed because the motion event has already been consumed.", |
| 571 | connection->getInputChannelName()); |
| 572 | #endif |
| 573 | MotionSample* appendedMotionSample = static_cast<MotionEntry*>(eventEntry)->lastSample; |
| 574 | dispatchEntry->headMotionSample = appendedMotionSample; |
| 575 | } |
| 576 | |
| 577 | // Enqueue the dispatch entry. |
| 578 | connection->outboundQueue.enqueueAtTail(dispatchEntry); |
| 579 | |
| 580 | // If the outbound queue was previously empty, start the dispatch cycle going. |
| 581 | if (wasEmpty) { |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 582 | activateConnectionLocked(connection.get()); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 583 | startDispatchCycleLocked(currentTime, connection); |
| 584 | } |
| 585 | } |
| 586 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 587 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
| 588 | const sp<Connection>& connection) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 589 | #if DEBUG_DISPATCH_CYCLE |
| 590 | LOGD("channel '%s' ~ startDispatchCycle", |
| 591 | connection->getInputChannelName()); |
| 592 | #endif |
| 593 | |
| 594 | assert(connection->status == Connection::STATUS_NORMAL); |
| 595 | assert(! connection->outboundQueue.isEmpty()); |
| 596 | |
| 597 | DispatchEntry* dispatchEntry = connection->outboundQueue.head.next; |
| 598 | assert(! dispatchEntry->inProgress); |
| 599 | |
| 600 | // TODO throttle successive ACTION_MOVE motion events for the same device |
| 601 | // possible implementation could set a brief poll timeout here and resume starting the |
| 602 | // dispatch cycle when elapsed |
| 603 | |
| 604 | // Publish the event. |
| 605 | status_t status; |
| 606 | switch (dispatchEntry->eventEntry->type) { |
| 607 | case EventEntry::TYPE_KEY: { |
| 608 | KeyEntry* keyEntry = static_cast<KeyEntry*>(dispatchEntry->eventEntry); |
| 609 | |
| 610 | // Apply target flags. |
| 611 | int32_t action = keyEntry->action; |
| 612 | int32_t flags = keyEntry->flags; |
| 613 | if (dispatchEntry->targetFlags & InputTarget::FLAG_CANCEL) { |
| 614 | flags |= KEY_EVENT_FLAG_CANCELED; |
| 615 | } |
| 616 | |
| 617 | // Publish the key event. |
| 618 | status = connection->inputPublisher.publishKeyEvent(keyEntry->deviceId, keyEntry->nature, |
| 619 | action, flags, keyEntry->keyCode, keyEntry->scanCode, |
| 620 | keyEntry->metaState, keyEntry->repeatCount, keyEntry->downTime, |
| 621 | keyEntry->eventTime); |
| 622 | |
| 623 | if (status) { |
| 624 | LOGE("channel '%s' ~ Could not publish key event, " |
| 625 | "status=%d", connection->getInputChannelName(), status); |
| 626 | abortDispatchCycleLocked(currentTime, connection, true /*broken*/); |
| 627 | return; |
| 628 | } |
| 629 | break; |
| 630 | } |
| 631 | |
| 632 | case EventEntry::TYPE_MOTION: { |
| 633 | MotionEntry* motionEntry = static_cast<MotionEntry*>(dispatchEntry->eventEntry); |
| 634 | |
| 635 | // Apply target flags. |
| 636 | int32_t action = motionEntry->action; |
| 637 | if (dispatchEntry->targetFlags & InputTarget::FLAG_OUTSIDE) { |
| 638 | action = MOTION_EVENT_ACTION_OUTSIDE; |
| 639 | } |
| 640 | if (dispatchEntry->targetFlags & InputTarget::FLAG_CANCEL) { |
| 641 | action = MOTION_EVENT_ACTION_CANCEL; |
| 642 | } |
| 643 | |
| 644 | // If headMotionSample is non-NULL, then it points to the first new sample that we |
| 645 | // were unable to dispatch during the previous cycle so we resume dispatching from |
| 646 | // that point in the list of motion samples. |
| 647 | // Otherwise, we just start from the first sample of the motion event. |
| 648 | MotionSample* firstMotionSample = dispatchEntry->headMotionSample; |
| 649 | if (! firstMotionSample) { |
| 650 | firstMotionSample = & motionEntry->firstSample; |
| 651 | } |
| 652 | |
| 653 | // Publish the motion event and the first motion sample. |
| 654 | status = connection->inputPublisher.publishMotionEvent(motionEntry->deviceId, |
| 655 | motionEntry->nature, action, motionEntry->edgeFlags, motionEntry->metaState, |
| 656 | dispatchEntry->xOffset, dispatchEntry->yOffset, |
| 657 | motionEntry->xPrecision, motionEntry->yPrecision, |
| 658 | motionEntry->downTime, firstMotionSample->eventTime, |
| 659 | motionEntry->pointerCount, motionEntry->pointerIds, |
| 660 | firstMotionSample->pointerCoords); |
| 661 | |
| 662 | if (status) { |
| 663 | LOGE("channel '%s' ~ Could not publish motion event, " |
| 664 | "status=%d", connection->getInputChannelName(), status); |
| 665 | abortDispatchCycleLocked(currentTime, connection, true /*broken*/); |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | // Append additional motion samples. |
| 670 | MotionSample* nextMotionSample = firstMotionSample->next; |
| 671 | for (; nextMotionSample != NULL; nextMotionSample = nextMotionSample->next) { |
| 672 | status = connection->inputPublisher.appendMotionSample( |
| 673 | nextMotionSample->eventTime, nextMotionSample->pointerCoords); |
| 674 | if (status == NO_MEMORY) { |
| 675 | #if DEBUG_DISPATCH_CYCLE |
| 676 | LOGD("channel '%s' ~ Shared memory buffer full. Some motion samples will " |
| 677 | "be sent in the next dispatch cycle.", |
| 678 | connection->getInputChannelName()); |
| 679 | #endif |
| 680 | break; |
| 681 | } |
| 682 | if (status != OK) { |
| 683 | LOGE("channel '%s' ~ Could not append motion sample " |
| 684 | "for a reason other than out of memory, status=%d", |
| 685 | connection->getInputChannelName(), status); |
| 686 | abortDispatchCycleLocked(currentTime, connection, true /*broken*/); |
| 687 | return; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | // Remember the next motion sample that we could not dispatch, in case we ran out |
| 692 | // of space in the shared memory buffer. |
| 693 | dispatchEntry->tailMotionSample = nextMotionSample; |
| 694 | break; |
| 695 | } |
| 696 | |
| 697 | default: { |
| 698 | assert(false); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | // Send the dispatch signal. |
| 703 | status = connection->inputPublisher.sendDispatchSignal(); |
| 704 | if (status) { |
| 705 | LOGE("channel '%s' ~ Could not send dispatch signal, status=%d", |
| 706 | connection->getInputChannelName(), status); |
| 707 | abortDispatchCycleLocked(currentTime, connection, true /*broken*/); |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | // Record information about the newly started dispatch cycle. |
| 712 | dispatchEntry->inProgress = true; |
| 713 | |
| 714 | connection->lastEventTime = dispatchEntry->eventEntry->eventTime; |
| 715 | connection->lastDispatchTime = currentTime; |
| 716 | |
| 717 | nsecs_t timeout = dispatchEntry->timeout; |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 718 | connection->setNextTimeoutTime(currentTime, timeout); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 719 | |
| 720 | // Notify other system components. |
| 721 | onDispatchCycleStartedLocked(currentTime, connection); |
| 722 | } |
| 723 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 724 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
| 725 | const sp<Connection>& connection) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 726 | #if DEBUG_DISPATCH_CYCLE |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 727 | LOGD("channel '%s' ~ finishDispatchCycle - %01.1fms since event, " |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 728 | "%01.1fms since dispatch", |
| 729 | connection->getInputChannelName(), |
| 730 | connection->getEventLatencyMillis(currentTime), |
| 731 | connection->getDispatchLatencyMillis(currentTime)); |
| 732 | #endif |
| 733 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 734 | if (connection->status == Connection::STATUS_BROKEN |
| 735 | || connection->status == Connection::STATUS_ZOMBIE) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 736 | return; |
| 737 | } |
| 738 | |
| 739 | // Clear the pending timeout. |
| 740 | connection->nextTimeoutTime = LONG_LONG_MAX; |
| 741 | |
| 742 | if (connection->status == Connection::STATUS_NOT_RESPONDING) { |
| 743 | // Recovering from an ANR. |
| 744 | connection->status = Connection::STATUS_NORMAL; |
| 745 | |
| 746 | // Notify other system components. |
| 747 | onDispatchCycleFinishedLocked(currentTime, connection, true /*recoveredFromANR*/); |
| 748 | } else { |
| 749 | // Normal finish. Not much to do here. |
| 750 | |
| 751 | // Notify other system components. |
| 752 | onDispatchCycleFinishedLocked(currentTime, connection, false /*recoveredFromANR*/); |
| 753 | } |
| 754 | |
| 755 | // Reset the publisher since the event has been consumed. |
| 756 | // We do this now so that the publisher can release some of its internal resources |
| 757 | // while waiting for the next dispatch cycle to begin. |
| 758 | status_t status = connection->inputPublisher.reset(); |
| 759 | if (status) { |
| 760 | LOGE("channel '%s' ~ Could not reset publisher, status=%d", |
| 761 | connection->getInputChannelName(), status); |
| 762 | abortDispatchCycleLocked(currentTime, connection, true /*broken*/); |
| 763 | return; |
| 764 | } |
| 765 | |
| 766 | // Start the next dispatch cycle for this connection. |
| 767 | while (! connection->outboundQueue.isEmpty()) { |
| 768 | DispatchEntry* dispatchEntry = connection->outboundQueue.head.next; |
| 769 | if (dispatchEntry->inProgress) { |
| 770 | // Finish or resume current event in progress. |
| 771 | if (dispatchEntry->tailMotionSample) { |
| 772 | // We have a tail of undispatched motion samples. |
| 773 | // Reuse the same DispatchEntry and start a new cycle. |
| 774 | dispatchEntry->inProgress = false; |
| 775 | dispatchEntry->headMotionSample = dispatchEntry->tailMotionSample; |
| 776 | dispatchEntry->tailMotionSample = NULL; |
| 777 | startDispatchCycleLocked(currentTime, connection); |
| 778 | return; |
| 779 | } |
| 780 | // Finished. |
| 781 | connection->outboundQueue.dequeueAtHead(); |
| 782 | mAllocator.releaseDispatchEntry(dispatchEntry); |
| 783 | } else { |
| 784 | // If the head is not in progress, then we must have already dequeued the in |
| 785 | // progress event, which means we actually aborted it (due to ANR). |
| 786 | // So just start the next event for this connection. |
| 787 | startDispatchCycleLocked(currentTime, connection); |
| 788 | return; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | // Outbound queue is empty, deactivate the connection. |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 793 | deactivateConnectionLocked(connection.get()); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 796 | void InputDispatcher::timeoutDispatchCycleLocked(nsecs_t currentTime, |
| 797 | const sp<Connection>& connection) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 798 | #if DEBUG_DISPATCH_CYCLE |
| 799 | LOGD("channel '%s' ~ timeoutDispatchCycle", |
| 800 | connection->getInputChannelName()); |
| 801 | #endif |
| 802 | |
| 803 | if (connection->status != Connection::STATUS_NORMAL) { |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 804 | return; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | // Enter the not responding state. |
| 808 | connection->status = Connection::STATUS_NOT_RESPONDING; |
| 809 | connection->lastANRTime = currentTime; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 810 | |
| 811 | // Notify other system components. |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 812 | // This enqueues a command which will eventually either call |
| 813 | // resumeAfterTimeoutDispatchCycleLocked or abortDispatchCycleLocked. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 814 | onDispatchCycleANRLocked(currentTime, connection); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 817 | void InputDispatcher::resumeAfterTimeoutDispatchCycleLocked(nsecs_t currentTime, |
| 818 | const sp<Connection>& connection, nsecs_t newTimeout) { |
| 819 | #if DEBUG_DISPATCH_CYCLE |
| 820 | LOGD("channel '%s' ~ resumeAfterTimeoutDispatchCycleLocked", |
| 821 | connection->getInputChannelName()); |
| 822 | #endif |
| 823 | |
| 824 | if (connection->status != Connection::STATUS_NOT_RESPONDING) { |
| 825 | return; |
| 826 | } |
| 827 | |
| 828 | // Resume normal dispatch. |
| 829 | connection->status = Connection::STATUS_NORMAL; |
| 830 | connection->setNextTimeoutTime(currentTime, newTimeout); |
| 831 | } |
| 832 | |
| 833 | void InputDispatcher::abortDispatchCycleLocked(nsecs_t currentTime, |
| 834 | const sp<Connection>& connection, bool broken) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 835 | #if DEBUG_DISPATCH_CYCLE |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 836 | LOGD("channel '%s' ~ abortDispatchCycle - broken=%s", |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 837 | connection->getInputChannelName(), broken ? "true" : "false"); |
| 838 | #endif |
| 839 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 840 | // Clear the pending timeout. |
| 841 | connection->nextTimeoutTime = LONG_LONG_MAX; |
| 842 | |
| 843 | // Clear the outbound queue. |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 844 | if (! connection->outboundQueue.isEmpty()) { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 845 | do { |
| 846 | DispatchEntry* dispatchEntry = connection->outboundQueue.dequeueAtHead(); |
| 847 | mAllocator.releaseDispatchEntry(dispatchEntry); |
| 848 | } while (! connection->outboundQueue.isEmpty()); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 849 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 850 | deactivateConnectionLocked(connection.get()); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 851 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 852 | |
| 853 | // Handle the case where the connection appears to be unrecoverably broken. |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 854 | // Ignore already broken or zombie connections. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 855 | if (broken) { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 856 | if (connection->status == Connection::STATUS_NORMAL |
| 857 | || connection->status == Connection::STATUS_NOT_RESPONDING) { |
| 858 | connection->status = Connection::STATUS_BROKEN; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 859 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 860 | // Notify other system components. |
| 861 | onDispatchCycleBrokenLocked(currentTime, connection); |
| 862 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 863 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | bool InputDispatcher::handleReceiveCallback(int receiveFd, int events, void* data) { |
| 867 | InputDispatcher* d = static_cast<InputDispatcher*>(data); |
| 868 | |
| 869 | { // acquire lock |
| 870 | AutoMutex _l(d->mLock); |
| 871 | |
| 872 | ssize_t connectionIndex = d->mConnectionsByReceiveFd.indexOfKey(receiveFd); |
| 873 | if (connectionIndex < 0) { |
| 874 | LOGE("Received spurious receive callback for unknown input channel. " |
| 875 | "fd=%d, events=0x%x", receiveFd, events); |
| 876 | return false; // remove the callback |
| 877 | } |
| 878 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 879 | nsecs_t currentTime = now(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 880 | |
| 881 | sp<Connection> connection = d->mConnectionsByReceiveFd.valueAt(connectionIndex); |
| 882 | if (events & (POLLERR | POLLHUP | POLLNVAL)) { |
| 883 | LOGE("channel '%s' ~ Consumer closed input channel or an error occurred. " |
| 884 | "events=0x%x", connection->getInputChannelName(), events); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 885 | d->abortDispatchCycleLocked(currentTime, connection, true /*broken*/); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 886 | d->runCommandsLockedInterruptible(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 887 | return false; // remove the callback |
| 888 | } |
| 889 | |
| 890 | if (! (events & POLLIN)) { |
| 891 | LOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 892 | "events=0x%x", connection->getInputChannelName(), events); |
| 893 | return true; |
| 894 | } |
| 895 | |
| 896 | status_t status = connection->inputPublisher.receiveFinishedSignal(); |
| 897 | if (status) { |
| 898 | LOGE("channel '%s' ~ Failed to receive finished signal. status=%d", |
| 899 | connection->getInputChannelName(), status); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 900 | d->abortDispatchCycleLocked(currentTime, connection, true /*broken*/); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 901 | d->runCommandsLockedInterruptible(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 902 | return false; // remove the callback |
| 903 | } |
| 904 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 905 | d->finishDispatchCycleLocked(currentTime, connection); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 906 | d->runCommandsLockedInterruptible(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 907 | return true; |
| 908 | } // release lock |
| 909 | } |
| 910 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 911 | void InputDispatcher::notifyConfigurationChanged(nsecs_t eventTime) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 912 | #if DEBUG_INBOUND_EVENT_DETAILS |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 913 | LOGD("notifyConfigurationChanged - eventTime=%lld", eventTime); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 914 | #endif |
| 915 | |
| 916 | bool wasEmpty; |
| 917 | { // acquire lock |
| 918 | AutoMutex _l(mLock); |
| 919 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 920 | ConfigurationChangedEntry* newEntry = mAllocator.obtainConfigurationChangedEntry(eventTime); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 921 | |
| 922 | wasEmpty = mInboundQueue.isEmpty(); |
| 923 | mInboundQueue.enqueueAtTail(newEntry); |
| 924 | } // release lock |
| 925 | |
| 926 | if (wasEmpty) { |
| 927 | mPollLoop->wake(); |
| 928 | } |
| 929 | } |
| 930 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 931 | void InputDispatcher::notifyAppSwitchComing(nsecs_t eventTime) { |
| 932 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 933 | LOGD("notifyAppSwitchComing - eventTime=%lld", eventTime); |
| 934 | #endif |
| 935 | |
| 936 | // Remove movement keys from the queue from most recent to least recent, stopping at the |
| 937 | // first non-movement key. |
| 938 | // TODO: Include a detailed description of why we do this... |
| 939 | |
| 940 | { // acquire lock |
| 941 | AutoMutex _l(mLock); |
| 942 | |
| 943 | for (EventEntry* entry = mInboundQueue.tail.prev; entry != & mInboundQueue.head; ) { |
| 944 | EventEntry* prev = entry->prev; |
| 945 | |
| 946 | if (entry->type == EventEntry::TYPE_KEY) { |
| 947 | KeyEntry* keyEntry = static_cast<KeyEntry*>(entry); |
| 948 | if (isMovementKey(keyEntry->keyCode)) { |
| 949 | LOGV("Dropping movement key during app switch: keyCode=%d, action=%d", |
| 950 | keyEntry->keyCode, keyEntry->action); |
| 951 | mInboundQueue.dequeue(keyEntry); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 952 | |
| 953 | setInjectionResultLocked(entry, INPUT_EVENT_INJECTION_FAILED); |
| 954 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 955 | mAllocator.releaseKeyEntry(keyEntry); |
| 956 | } else { |
| 957 | // stop at last non-movement key |
| 958 | break; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | entry = prev; |
| 963 | } |
| 964 | } // release lock |
| 965 | } |
| 966 | |
| 967 | void InputDispatcher::notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t nature, |
| 968 | uint32_t policyFlags, int32_t action, int32_t flags, |
| 969 | int32_t keyCode, int32_t scanCode, int32_t metaState, nsecs_t downTime) { |
| 970 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 971 | LOGD("notifyKey - eventTime=%lld, deviceId=0x%x, nature=0x%x, policyFlags=0x%x, action=0x%x, " |
| 972 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%lld", |
| 973 | eventTime, deviceId, nature, policyFlags, action, flags, |
| 974 | keyCode, scanCode, metaState, downTime); |
| 975 | #endif |
| 976 | |
| 977 | bool wasEmpty; |
| 978 | { // acquire lock |
| 979 | AutoMutex _l(mLock); |
| 980 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 981 | int32_t repeatCount = 0; |
| 982 | KeyEntry* newEntry = mAllocator.obtainKeyEntry(eventTime, |
| 983 | deviceId, nature, policyFlags, action, flags, keyCode, scanCode, |
| 984 | metaState, repeatCount, downTime); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 985 | |
| 986 | wasEmpty = mInboundQueue.isEmpty(); |
| 987 | mInboundQueue.enqueueAtTail(newEntry); |
| 988 | } // release lock |
| 989 | |
| 990 | if (wasEmpty) { |
| 991 | mPollLoop->wake(); |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | void InputDispatcher::notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t nature, |
| 996 | uint32_t policyFlags, int32_t action, int32_t metaState, int32_t edgeFlags, |
| 997 | uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords, |
| 998 | float xPrecision, float yPrecision, nsecs_t downTime) { |
| 999 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 1000 | LOGD("notifyMotion - eventTime=%lld, deviceId=0x%x, nature=0x%x, policyFlags=0x%x, " |
| 1001 | "action=0x%x, metaState=0x%x, edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, " |
| 1002 | "downTime=%lld", |
| 1003 | eventTime, deviceId, nature, policyFlags, action, metaState, edgeFlags, |
| 1004 | xPrecision, yPrecision, downTime); |
| 1005 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 1006 | LOGD(" Pointer %d: id=%d, x=%f, y=%f, pressure=%f, size=%f", |
| 1007 | i, pointerIds[i], pointerCoords[i].x, pointerCoords[i].y, |
| 1008 | pointerCoords[i].pressure, pointerCoords[i].size); |
| 1009 | } |
| 1010 | #endif |
| 1011 | |
| 1012 | bool wasEmpty; |
| 1013 | { // acquire lock |
| 1014 | AutoMutex _l(mLock); |
| 1015 | |
| 1016 | // Attempt batching and streaming of move events. |
| 1017 | if (action == MOTION_EVENT_ACTION_MOVE) { |
| 1018 | // BATCHING CASE |
| 1019 | // |
| 1020 | // Try to append a move sample to the tail of the inbound queue for this device. |
| 1021 | // Give up if we encounter a non-move motion event for this device since that |
| 1022 | // means we cannot append any new samples until a new motion event has started. |
| 1023 | for (EventEntry* entry = mInboundQueue.tail.prev; |
| 1024 | entry != & mInboundQueue.head; entry = entry->prev) { |
| 1025 | if (entry->type != EventEntry::TYPE_MOTION) { |
| 1026 | // Keep looking for motion events. |
| 1027 | continue; |
| 1028 | } |
| 1029 | |
| 1030 | MotionEntry* motionEntry = static_cast<MotionEntry*>(entry); |
| 1031 | if (motionEntry->deviceId != deviceId) { |
| 1032 | // Keep looking for this device. |
| 1033 | continue; |
| 1034 | } |
| 1035 | |
| 1036 | if (motionEntry->action != MOTION_EVENT_ACTION_MOVE |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1037 | || motionEntry->pointerCount != pointerCount |
| 1038 | || motionEntry->isInjected()) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1039 | // Last motion event in the queue for this device is not compatible for |
| 1040 | // appending new samples. Stop here. |
| 1041 | goto NoBatchingOrStreaming; |
| 1042 | } |
| 1043 | |
| 1044 | // The last motion event is a move and is compatible for appending. |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1045 | // Do the batching magic. |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1046 | mAllocator.appendMotionSample(motionEntry, eventTime, pointerCoords); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1047 | #if DEBUG_BATCHING |
| 1048 | LOGD("Appended motion sample onto batch for most recent " |
| 1049 | "motion event for this device in the inbound queue."); |
| 1050 | #endif |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1051 | |
| 1052 | // Sanity check for special case because dispatch is interruptible. |
| 1053 | // The dispatch logic is partially interruptible and releases its lock while |
| 1054 | // identifying targets. However, as soon as the targets have been identified, |
| 1055 | // the dispatcher proceeds to write a dispatch entry into all relevant outbound |
| 1056 | // queues and then promptly removes the motion entry from the queue. |
| 1057 | // |
| 1058 | // Consequently, we should never observe the case where the inbound queue contains |
| 1059 | // an in-progress motion entry unless the current input targets are invalid |
| 1060 | // (currently being computed). Check for this! |
| 1061 | assert(! (motionEntry->dispatchInProgress && mCurrentInputTargetsValid)); |
| 1062 | |
| 1063 | return; // done! |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | // STREAMING CASE |
| 1067 | // |
| 1068 | // There is no pending motion event (of any kind) for this device in the inbound queue. |
| 1069 | // Search the outbound queues for a synchronously dispatched motion event for this |
| 1070 | // device. If found, then we append the new sample to that event and then try to |
| 1071 | // push it out to all current targets. It is possible that some targets will already |
| 1072 | // have consumed the motion event. This case is automatically handled by the |
| 1073 | // logic in prepareDispatchCycleLocked by tracking where resumption takes place. |
| 1074 | // |
| 1075 | // The reason we look for a synchronously dispatched motion event is because we |
| 1076 | // want to be sure that no other motion events have been dispatched since the move. |
| 1077 | // It's also convenient because it means that the input targets are still valid. |
| 1078 | // This code could be improved to support streaming of asynchronously dispatched |
| 1079 | // motion events (which might be significantly more efficient) but it may become |
| 1080 | // a little more complicated as a result. |
| 1081 | // |
| 1082 | // Note: This code crucially depends on the invariant that an outbound queue always |
| 1083 | // contains at most one synchronous event and it is always last (but it might |
| 1084 | // not be first!). |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1085 | if (mCurrentInputTargetsValid) { |
| 1086 | for (size_t i = 0; i < mActiveConnections.size(); i++) { |
| 1087 | Connection* connection = mActiveConnections.itemAt(i); |
| 1088 | if (! connection->outboundQueue.isEmpty()) { |
| 1089 | DispatchEntry* dispatchEntry = connection->outboundQueue.tail.prev; |
| 1090 | if (dispatchEntry->targetFlags & InputTarget::FLAG_SYNC) { |
| 1091 | if (dispatchEntry->eventEntry->type != EventEntry::TYPE_MOTION) { |
| 1092 | goto NoBatchingOrStreaming; |
| 1093 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1094 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1095 | MotionEntry* syncedMotionEntry = static_cast<MotionEntry*>( |
| 1096 | dispatchEntry->eventEntry); |
| 1097 | if (syncedMotionEntry->action != MOTION_EVENT_ACTION_MOVE |
| 1098 | || syncedMotionEntry->deviceId != deviceId |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1099 | || syncedMotionEntry->pointerCount != pointerCount |
| 1100 | || syncedMotionEntry->isInjected()) { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1101 | goto NoBatchingOrStreaming; |
| 1102 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1103 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1104 | // Found synced move entry. Append sample and resume dispatch. |
| 1105 | mAllocator.appendMotionSample(syncedMotionEntry, eventTime, |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1106 | pointerCoords); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1107 | #if DEBUG_BATCHING |
| 1108 | LOGD("Appended motion sample onto batch for most recent synchronously " |
| 1109 | "dispatched motion event for this device in the outbound queues."); |
| 1110 | #endif |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1111 | nsecs_t currentTime = now(); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1112 | dispatchEventToCurrentInputTargetsLocked(currentTime, syncedMotionEntry, |
| 1113 | true /*resumeWithAppendedMotionSample*/); |
| 1114 | |
| 1115 | runCommandsLockedInterruptible(); |
| 1116 | return; // done! |
| 1117 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | NoBatchingOrStreaming:; |
| 1123 | } |
| 1124 | |
| 1125 | // Just enqueue a new motion event. |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1126 | MotionEntry* newEntry = mAllocator.obtainMotionEntry(eventTime, |
| 1127 | deviceId, nature, policyFlags, action, metaState, edgeFlags, |
| 1128 | xPrecision, yPrecision, downTime, |
| 1129 | pointerCount, pointerIds, pointerCoords); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1130 | |
| 1131 | wasEmpty = mInboundQueue.isEmpty(); |
| 1132 | mInboundQueue.enqueueAtTail(newEntry); |
| 1133 | } // release lock |
| 1134 | |
| 1135 | if (wasEmpty) { |
| 1136 | mPollLoop->wake(); |
| 1137 | } |
| 1138 | } |
| 1139 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1140 | int32_t InputDispatcher::injectInputEvent(const InputEvent* event, |
| 1141 | int32_t injectorPid, int32_t injectorUid, bool sync, int32_t timeoutMillis) { |
| 1142 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 1143 | LOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
| 1144 | "sync=%d, timeoutMillis=%d", |
| 1145 | event->getType(), injectorPid, injectorUid, sync, timeoutMillis); |
| 1146 | #endif |
| 1147 | |
| 1148 | nsecs_t endTime = now() + milliseconds_to_nanoseconds(timeoutMillis); |
| 1149 | |
| 1150 | EventEntry* injectedEntry; |
| 1151 | bool wasEmpty; |
| 1152 | { // acquire lock |
| 1153 | AutoMutex _l(mLock); |
| 1154 | |
| 1155 | injectedEntry = createEntryFromInputEventLocked(event); |
| 1156 | injectedEntry->refCount += 1; |
| 1157 | injectedEntry->injectorPid = injectorPid; |
| 1158 | injectedEntry->injectorUid = injectorUid; |
| 1159 | |
| 1160 | wasEmpty = mInboundQueue.isEmpty(); |
| 1161 | mInboundQueue.enqueueAtTail(injectedEntry); |
| 1162 | |
| 1163 | } // release lock |
| 1164 | |
| 1165 | if (wasEmpty) { |
| 1166 | mPollLoop->wake(); |
| 1167 | } |
| 1168 | |
| 1169 | int32_t injectionResult; |
| 1170 | { // acquire lock |
| 1171 | AutoMutex _l(mLock); |
| 1172 | |
| 1173 | for (;;) { |
| 1174 | injectionResult = injectedEntry->injectionResult; |
| 1175 | if (injectionResult != INPUT_EVENT_INJECTION_PENDING) { |
| 1176 | break; |
| 1177 | } |
| 1178 | |
| 1179 | nsecs_t remainingTimeout = endTime - now(); |
| 1180 | if (remainingTimeout <= 0) { |
| 1181 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
| 1182 | sync = false; |
| 1183 | break; |
| 1184 | } |
| 1185 | |
| 1186 | mInjectionResultAvailableCondition.waitRelative(mLock, remainingTimeout); |
| 1187 | } |
| 1188 | |
| 1189 | if (sync) { |
| 1190 | while (! isFullySynchronizedLocked()) { |
| 1191 | nsecs_t remainingTimeout = endTime - now(); |
| 1192 | if (remainingTimeout <= 0) { |
| 1193 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
| 1194 | break; |
| 1195 | } |
| 1196 | |
| 1197 | mFullySynchronizedCondition.waitRelative(mLock, remainingTimeout); |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | mAllocator.releaseEventEntry(injectedEntry); |
| 1202 | } // release lock |
| 1203 | |
| 1204 | return injectionResult; |
| 1205 | } |
| 1206 | |
| 1207 | void InputDispatcher::setInjectionResultLocked(EventEntry* entry, int32_t injectionResult) { |
| 1208 | if (entry->isInjected()) { |
| 1209 | #if DEBUG_INJECTION |
| 1210 | LOGD("Setting input event injection result to %d. " |
| 1211 | "injectorPid=%d, injectorUid=%d", |
| 1212 | injectionResult, entry->injectorPid, entry->injectorUid); |
| 1213 | #endif |
| 1214 | |
| 1215 | entry->injectionResult = injectionResult; |
| 1216 | mInjectionResultAvailableCondition.broadcast(); |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | bool InputDispatcher::isFullySynchronizedLocked() { |
| 1221 | return mInboundQueue.isEmpty() && mActiveConnections.isEmpty(); |
| 1222 | } |
| 1223 | |
| 1224 | InputDispatcher::EventEntry* InputDispatcher::createEntryFromInputEventLocked( |
| 1225 | const InputEvent* event) { |
| 1226 | switch (event->getType()) { |
| 1227 | case INPUT_EVENT_TYPE_KEY: { |
| 1228 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(event); |
| 1229 | uint32_t policyFlags = 0; // XXX consider adding a policy flag to track injected events |
| 1230 | |
| 1231 | KeyEntry* keyEntry = mAllocator.obtainKeyEntry(keyEvent->getEventTime(), |
| 1232 | keyEvent->getDeviceId(), keyEvent->getNature(), policyFlags, |
| 1233 | keyEvent->getAction(), keyEvent->getFlags(), |
| 1234 | keyEvent->getKeyCode(), keyEvent->getScanCode(), keyEvent->getMetaState(), |
| 1235 | keyEvent->getRepeatCount(), keyEvent->getDownTime()); |
| 1236 | return keyEntry; |
| 1237 | } |
| 1238 | |
| 1239 | case INPUT_EVENT_TYPE_MOTION: { |
| 1240 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); |
| 1241 | uint32_t policyFlags = 0; // XXX consider adding a policy flag to track injected events |
| 1242 | |
| 1243 | const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); |
| 1244 | const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); |
| 1245 | size_t pointerCount = motionEvent->getPointerCount(); |
| 1246 | |
| 1247 | MotionEntry* motionEntry = mAllocator.obtainMotionEntry(*sampleEventTimes, |
| 1248 | motionEvent->getDeviceId(), motionEvent->getNature(), policyFlags, |
| 1249 | motionEvent->getAction(), motionEvent->getMetaState(), motionEvent->getEdgeFlags(), |
| 1250 | motionEvent->getXPrecision(), motionEvent->getYPrecision(), |
| 1251 | motionEvent->getDownTime(), uint32_t(pointerCount), |
| 1252 | motionEvent->getPointerIds(), samplePointerCoords); |
| 1253 | for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { |
| 1254 | sampleEventTimes += 1; |
| 1255 | samplePointerCoords += pointerCount; |
| 1256 | mAllocator.appendMotionSample(motionEntry, *sampleEventTimes, samplePointerCoords); |
| 1257 | } |
| 1258 | return motionEntry; |
| 1259 | } |
| 1260 | |
| 1261 | default: |
| 1262 | assert(false); |
| 1263 | return NULL; |
| 1264 | } |
| 1265 | } |
| 1266 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1267 | void InputDispatcher::resetKeyRepeatLocked() { |
| 1268 | if (mKeyRepeatState.lastKeyEntry) { |
| 1269 | mAllocator.releaseKeyEntry(mKeyRepeatState.lastKeyEntry); |
| 1270 | mKeyRepeatState.lastKeyEntry = NULL; |
| 1271 | } |
| 1272 | } |
| 1273 | |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1274 | void InputDispatcher::preemptInputDispatch() { |
| 1275 | #if DEBUG_DISPATCH_CYCLE |
| 1276 | LOGD("preemptInputDispatch"); |
| 1277 | #endif |
| 1278 | |
| 1279 | bool preemptedOne = false; |
| 1280 | { // acquire lock |
| 1281 | AutoMutex _l(mLock); |
| 1282 | |
| 1283 | for (size_t i = 0; i < mActiveConnections.size(); i++) { |
| 1284 | Connection* connection = mActiveConnections[i]; |
| 1285 | if (connection->hasPendingSyncTarget()) { |
| 1286 | #if DEBUG_DISPATCH_CYCLE |
| 1287 | LOGD("channel '%s' ~ Preempted pending synchronous dispatch", |
| 1288 | connection->getInputChannelName()); |
| 1289 | #endif |
| 1290 | connection->outboundQueue.tail.prev->targetFlags &= ~ InputTarget::FLAG_SYNC; |
| 1291 | preemptedOne = true; |
| 1292 | } |
| 1293 | } |
| 1294 | } // release lock |
| 1295 | |
| 1296 | if (preemptedOne) { |
| 1297 | // Wake up the poll loop so it can get a head start dispatching the next event. |
| 1298 | mPollLoop->wake(); |
| 1299 | } |
| 1300 | } |
| 1301 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1302 | status_t InputDispatcher::registerInputChannel(const sp<InputChannel>& inputChannel) { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1303 | #if DEBUG_REGISTRATION |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1304 | LOGD("channel '%s' ~ registerInputChannel", inputChannel->getName().string()); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1305 | #endif |
| 1306 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1307 | int receiveFd; |
| 1308 | { // acquire lock |
| 1309 | AutoMutex _l(mLock); |
| 1310 | |
| 1311 | receiveFd = inputChannel->getReceivePipeFd(); |
| 1312 | if (mConnectionsByReceiveFd.indexOfKey(receiveFd) >= 0) { |
| 1313 | LOGW("Attempted to register already registered input channel '%s'", |
| 1314 | inputChannel->getName().string()); |
| 1315 | return BAD_VALUE; |
| 1316 | } |
| 1317 | |
| 1318 | sp<Connection> connection = new Connection(inputChannel); |
| 1319 | status_t status = connection->initialize(); |
| 1320 | if (status) { |
| 1321 | LOGE("Failed to initialize input publisher for input channel '%s', status=%d", |
| 1322 | inputChannel->getName().string(), status); |
| 1323 | return status; |
| 1324 | } |
| 1325 | |
| 1326 | mConnectionsByReceiveFd.add(receiveFd, connection); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1327 | |
| 1328 | runCommandsLockedInterruptible(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1329 | } // release lock |
| 1330 | |
| 1331 | mPollLoop->setCallback(receiveFd, POLLIN, handleReceiveCallback, this); |
| 1332 | return OK; |
| 1333 | } |
| 1334 | |
| 1335 | status_t InputDispatcher::unregisterInputChannel(const sp<InputChannel>& inputChannel) { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1336 | #if DEBUG_REGISTRATION |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1337 | LOGD("channel '%s' ~ unregisterInputChannel", inputChannel->getName().string()); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1338 | #endif |
| 1339 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1340 | int32_t receiveFd; |
| 1341 | { // acquire lock |
| 1342 | AutoMutex _l(mLock); |
| 1343 | |
| 1344 | receiveFd = inputChannel->getReceivePipeFd(); |
| 1345 | ssize_t connectionIndex = mConnectionsByReceiveFd.indexOfKey(receiveFd); |
| 1346 | if (connectionIndex < 0) { |
| 1347 | LOGW("Attempted to unregister already unregistered input channel '%s'", |
| 1348 | inputChannel->getName().string()); |
| 1349 | return BAD_VALUE; |
| 1350 | } |
| 1351 | |
| 1352 | sp<Connection> connection = mConnectionsByReceiveFd.valueAt(connectionIndex); |
| 1353 | mConnectionsByReceiveFd.removeItemsAt(connectionIndex); |
| 1354 | |
| 1355 | connection->status = Connection::STATUS_ZOMBIE; |
| 1356 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1357 | nsecs_t currentTime = now(); |
| 1358 | abortDispatchCycleLocked(currentTime, connection, true /*broken*/); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1359 | |
| 1360 | runCommandsLockedInterruptible(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1361 | } // release lock |
| 1362 | |
| 1363 | mPollLoop->removeCallback(receiveFd); |
| 1364 | |
| 1365 | // Wake the poll loop because removing the connection may have changed the current |
| 1366 | // synchronization state. |
| 1367 | mPollLoop->wake(); |
| 1368 | return OK; |
| 1369 | } |
| 1370 | |
| 1371 | void InputDispatcher::activateConnectionLocked(Connection* connection) { |
| 1372 | for (size_t i = 0; i < mActiveConnections.size(); i++) { |
| 1373 | if (mActiveConnections.itemAt(i) == connection) { |
| 1374 | return; |
| 1375 | } |
| 1376 | } |
| 1377 | mActiveConnections.add(connection); |
| 1378 | } |
| 1379 | |
| 1380 | void InputDispatcher::deactivateConnectionLocked(Connection* connection) { |
| 1381 | for (size_t i = 0; i < mActiveConnections.size(); i++) { |
| 1382 | if (mActiveConnections.itemAt(i) == connection) { |
| 1383 | mActiveConnections.removeAt(i); |
| 1384 | return; |
| 1385 | } |
| 1386 | } |
| 1387 | } |
| 1388 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1389 | void InputDispatcher::onDispatchCycleStartedLocked( |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1390 | nsecs_t currentTime, const sp<Connection>& connection) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1393 | void InputDispatcher::onDispatchCycleFinishedLocked( |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1394 | nsecs_t currentTime, const sp<Connection>& connection, bool recoveredFromANR) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1395 | if (recoveredFromANR) { |
| 1396 | LOGI("channel '%s' ~ Recovered from ANR. %01.1fms since event, " |
| 1397 | "%01.1fms since dispatch, %01.1fms since ANR", |
| 1398 | connection->getInputChannelName(), |
| 1399 | connection->getEventLatencyMillis(currentTime), |
| 1400 | connection->getDispatchLatencyMillis(currentTime), |
| 1401 | connection->getANRLatencyMillis(currentTime)); |
| 1402 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1403 | CommandEntry* commandEntry = postCommandLocked( |
| 1404 | & InputDispatcher::doNotifyInputChannelRecoveredFromANRLockedInterruptible); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1405 | commandEntry->connection = connection; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1406 | } |
| 1407 | } |
| 1408 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1409 | void InputDispatcher::onDispatchCycleANRLocked( |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1410 | nsecs_t currentTime, const sp<Connection>& connection) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1411 | LOGI("channel '%s' ~ Not responding! %01.1fms since event, %01.1fms since dispatch", |
| 1412 | connection->getInputChannelName(), |
| 1413 | connection->getEventLatencyMillis(currentTime), |
| 1414 | connection->getDispatchLatencyMillis(currentTime)); |
| 1415 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1416 | CommandEntry* commandEntry = postCommandLocked( |
| 1417 | & InputDispatcher::doNotifyInputChannelANRLockedInterruptible); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1418 | commandEntry->connection = connection; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1419 | } |
| 1420 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1421 | void InputDispatcher::onDispatchCycleBrokenLocked( |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1422 | nsecs_t currentTime, const sp<Connection>& connection) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1423 | LOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 1424 | connection->getInputChannelName()); |
| 1425 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1426 | CommandEntry* commandEntry = postCommandLocked( |
| 1427 | & InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1428 | commandEntry->connection = connection; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1431 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible( |
| 1432 | CommandEntry* commandEntry) { |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1433 | sp<Connection> connection = commandEntry->connection; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1434 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1435 | if (connection->status != Connection::STATUS_ZOMBIE) { |
| 1436 | mLock.unlock(); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1437 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1438 | mPolicy->notifyInputChannelBroken(connection->inputChannel); |
| 1439 | |
| 1440 | mLock.lock(); |
| 1441 | } |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | void InputDispatcher::doNotifyInputChannelANRLockedInterruptible( |
| 1445 | CommandEntry* commandEntry) { |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1446 | sp<Connection> connection = commandEntry->connection; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1447 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1448 | if (connection->status != Connection::STATUS_ZOMBIE) { |
| 1449 | mLock.unlock(); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1450 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1451 | nsecs_t newTimeout; |
| 1452 | bool resume = mPolicy->notifyInputChannelANR(connection->inputChannel, newTimeout); |
| 1453 | |
| 1454 | mLock.lock(); |
| 1455 | |
| 1456 | nsecs_t currentTime = now(); |
| 1457 | if (resume) { |
| 1458 | resumeAfterTimeoutDispatchCycleLocked(currentTime, connection, newTimeout); |
| 1459 | } else { |
| 1460 | abortDispatchCycleLocked(currentTime, connection, false /*(not) broken*/); |
| 1461 | } |
| 1462 | } |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | void InputDispatcher::doNotifyInputChannelRecoveredFromANRLockedInterruptible( |
| 1466 | CommandEntry* commandEntry) { |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1467 | sp<Connection> connection = commandEntry->connection; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1468 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1469 | if (connection->status != Connection::STATUS_ZOMBIE) { |
| 1470 | mLock.unlock(); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1471 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1472 | mPolicy->notifyInputChannelRecoveredFromANR(connection->inputChannel); |
| 1473 | |
| 1474 | mLock.lock(); |
| 1475 | } |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1479 | // --- InputDispatcher::Allocator --- |
| 1480 | |
| 1481 | InputDispatcher::Allocator::Allocator() { |
| 1482 | } |
| 1483 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1484 | void InputDispatcher::Allocator::initializeEventEntry(EventEntry* entry, int32_t type, |
| 1485 | nsecs_t eventTime) { |
| 1486 | entry->type = type; |
| 1487 | entry->refCount = 1; |
| 1488 | entry->dispatchInProgress = false; |
Christopher Tate | e91a5db | 2010-06-23 16:50:30 -0700 | [diff] [blame] | 1489 | entry->eventTime = eventTime; |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1490 | entry->injectionResult = INPUT_EVENT_INJECTION_PENDING; |
| 1491 | entry->injectorPid = -1; |
| 1492 | entry->injectorUid = -1; |
| 1493 | } |
| 1494 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1495 | InputDispatcher::ConfigurationChangedEntry* |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1496 | InputDispatcher::Allocator::obtainConfigurationChangedEntry(nsecs_t eventTime) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1497 | ConfigurationChangedEntry* entry = mConfigurationChangeEntryPool.alloc(); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1498 | initializeEventEntry(entry, EventEntry::TYPE_CONFIGURATION_CHANGED, eventTime); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1499 | return entry; |
| 1500 | } |
| 1501 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1502 | InputDispatcher::KeyEntry* InputDispatcher::Allocator::obtainKeyEntry(nsecs_t eventTime, |
| 1503 | int32_t deviceId, int32_t nature, uint32_t policyFlags, int32_t action, |
| 1504 | int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState, |
| 1505 | int32_t repeatCount, nsecs_t downTime) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1506 | KeyEntry* entry = mKeyEntryPool.alloc(); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1507 | initializeEventEntry(entry, EventEntry::TYPE_KEY, eventTime); |
| 1508 | |
| 1509 | entry->deviceId = deviceId; |
| 1510 | entry->nature = nature; |
| 1511 | entry->policyFlags = policyFlags; |
| 1512 | entry->action = action; |
| 1513 | entry->flags = flags; |
| 1514 | entry->keyCode = keyCode; |
| 1515 | entry->scanCode = scanCode; |
| 1516 | entry->metaState = metaState; |
| 1517 | entry->repeatCount = repeatCount; |
| 1518 | entry->downTime = downTime; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1519 | return entry; |
| 1520 | } |
| 1521 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1522 | InputDispatcher::MotionEntry* InputDispatcher::Allocator::obtainMotionEntry(nsecs_t eventTime, |
| 1523 | int32_t deviceId, int32_t nature, uint32_t policyFlags, int32_t action, |
| 1524 | int32_t metaState, int32_t edgeFlags, float xPrecision, float yPrecision, |
| 1525 | nsecs_t downTime, uint32_t pointerCount, |
| 1526 | const int32_t* pointerIds, const PointerCoords* pointerCoords) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1527 | MotionEntry* entry = mMotionEntryPool.alloc(); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1528 | initializeEventEntry(entry, EventEntry::TYPE_MOTION, eventTime); |
| 1529 | |
| 1530 | entry->eventTime = eventTime; |
| 1531 | entry->deviceId = deviceId; |
| 1532 | entry->nature = nature; |
| 1533 | entry->policyFlags = policyFlags; |
| 1534 | entry->action = action; |
| 1535 | entry->metaState = metaState; |
| 1536 | entry->edgeFlags = edgeFlags; |
| 1537 | entry->xPrecision = xPrecision; |
| 1538 | entry->yPrecision = yPrecision; |
| 1539 | entry->downTime = downTime; |
| 1540 | entry->pointerCount = pointerCount; |
| 1541 | entry->firstSample.eventTime = eventTime; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1542 | entry->firstSample.next = NULL; |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1543 | entry->lastSample = & entry->firstSample; |
| 1544 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 1545 | entry->pointerIds[i] = pointerIds[i]; |
| 1546 | entry->firstSample.pointerCoords[i] = pointerCoords[i]; |
| 1547 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1548 | return entry; |
| 1549 | } |
| 1550 | |
| 1551 | InputDispatcher::DispatchEntry* InputDispatcher::Allocator::obtainDispatchEntry( |
| 1552 | EventEntry* eventEntry) { |
| 1553 | DispatchEntry* entry = mDispatchEntryPool.alloc(); |
| 1554 | entry->eventEntry = eventEntry; |
| 1555 | eventEntry->refCount += 1; |
| 1556 | return entry; |
| 1557 | } |
| 1558 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1559 | InputDispatcher::CommandEntry* InputDispatcher::Allocator::obtainCommandEntry(Command command) { |
| 1560 | CommandEntry* entry = mCommandEntryPool.alloc(); |
| 1561 | entry->command = command; |
| 1562 | return entry; |
| 1563 | } |
| 1564 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1565 | void InputDispatcher::Allocator::releaseEventEntry(EventEntry* entry) { |
| 1566 | switch (entry->type) { |
| 1567 | case EventEntry::TYPE_CONFIGURATION_CHANGED: |
| 1568 | releaseConfigurationChangedEntry(static_cast<ConfigurationChangedEntry*>(entry)); |
| 1569 | break; |
| 1570 | case EventEntry::TYPE_KEY: |
| 1571 | releaseKeyEntry(static_cast<KeyEntry*>(entry)); |
| 1572 | break; |
| 1573 | case EventEntry::TYPE_MOTION: |
| 1574 | releaseMotionEntry(static_cast<MotionEntry*>(entry)); |
| 1575 | break; |
| 1576 | default: |
| 1577 | assert(false); |
| 1578 | break; |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | void InputDispatcher::Allocator::releaseConfigurationChangedEntry( |
| 1583 | ConfigurationChangedEntry* entry) { |
| 1584 | entry->refCount -= 1; |
| 1585 | if (entry->refCount == 0) { |
| 1586 | mConfigurationChangeEntryPool.free(entry); |
| 1587 | } else { |
| 1588 | assert(entry->refCount > 0); |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | void InputDispatcher::Allocator::releaseKeyEntry(KeyEntry* entry) { |
| 1593 | entry->refCount -= 1; |
| 1594 | if (entry->refCount == 0) { |
| 1595 | mKeyEntryPool.free(entry); |
| 1596 | } else { |
| 1597 | assert(entry->refCount > 0); |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | void InputDispatcher::Allocator::releaseMotionEntry(MotionEntry* entry) { |
| 1602 | entry->refCount -= 1; |
| 1603 | if (entry->refCount == 0) { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1604 | for (MotionSample* sample = entry->firstSample.next; sample != NULL; ) { |
| 1605 | MotionSample* next = sample->next; |
| 1606 | mMotionSamplePool.free(sample); |
| 1607 | sample = next; |
| 1608 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1609 | mMotionEntryPool.free(entry); |
| 1610 | } else { |
| 1611 | assert(entry->refCount > 0); |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | void InputDispatcher::Allocator::releaseDispatchEntry(DispatchEntry* entry) { |
| 1616 | releaseEventEntry(entry->eventEntry); |
| 1617 | mDispatchEntryPool.free(entry); |
| 1618 | } |
| 1619 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1620 | void InputDispatcher::Allocator::releaseCommandEntry(CommandEntry* entry) { |
| 1621 | mCommandEntryPool.free(entry); |
| 1622 | } |
| 1623 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1624 | void InputDispatcher::Allocator::appendMotionSample(MotionEntry* motionEntry, |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1625 | nsecs_t eventTime, const PointerCoords* pointerCoords) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1626 | MotionSample* sample = mMotionSamplePool.alloc(); |
| 1627 | sample->eventTime = eventTime; |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1628 | uint32_t pointerCount = motionEntry->pointerCount; |
| 1629 | for (uint32_t i = 0; i < pointerCount; i++) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1630 | sample->pointerCoords[i] = pointerCoords[i]; |
| 1631 | } |
| 1632 | |
| 1633 | sample->next = NULL; |
| 1634 | motionEntry->lastSample->next = sample; |
| 1635 | motionEntry->lastSample = sample; |
| 1636 | } |
| 1637 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1638 | // --- InputDispatcher::Connection --- |
| 1639 | |
| 1640 | InputDispatcher::Connection::Connection(const sp<InputChannel>& inputChannel) : |
| 1641 | status(STATUS_NORMAL), inputChannel(inputChannel), inputPublisher(inputChannel), |
| 1642 | nextTimeoutTime(LONG_LONG_MAX), |
| 1643 | lastEventTime(LONG_LONG_MAX), lastDispatchTime(LONG_LONG_MAX), |
| 1644 | lastANRTime(LONG_LONG_MAX) { |
| 1645 | } |
| 1646 | |
| 1647 | InputDispatcher::Connection::~Connection() { |
| 1648 | } |
| 1649 | |
| 1650 | status_t InputDispatcher::Connection::initialize() { |
| 1651 | return inputPublisher.initialize(); |
| 1652 | } |
| 1653 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1654 | void InputDispatcher::Connection::setNextTimeoutTime(nsecs_t currentTime, nsecs_t timeout) { |
| 1655 | nextTimeoutTime = (timeout >= 0) ? currentTime + timeout : LONG_LONG_MAX; |
| 1656 | } |
| 1657 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1658 | const char* InputDispatcher::Connection::getStatusLabel() const { |
| 1659 | switch (status) { |
| 1660 | case STATUS_NORMAL: |
| 1661 | return "NORMAL"; |
| 1662 | |
| 1663 | case STATUS_BROKEN: |
| 1664 | return "BROKEN"; |
| 1665 | |
| 1666 | case STATUS_NOT_RESPONDING: |
| 1667 | return "NOT_RESPONDING"; |
| 1668 | |
| 1669 | case STATUS_ZOMBIE: |
| 1670 | return "ZOMBIE"; |
| 1671 | |
| 1672 | default: |
| 1673 | return "UNKNOWN"; |
| 1674 | } |
| 1675 | } |
| 1676 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1677 | InputDispatcher::DispatchEntry* InputDispatcher::Connection::findQueuedDispatchEntryForEvent( |
| 1678 | const EventEntry* eventEntry) const { |
| 1679 | for (DispatchEntry* dispatchEntry = outboundQueue.tail.prev; |
| 1680 | dispatchEntry != & outboundQueue.head; dispatchEntry = dispatchEntry->prev) { |
| 1681 | if (dispatchEntry->eventEntry == eventEntry) { |
| 1682 | return dispatchEntry; |
| 1683 | } |
| 1684 | } |
| 1685 | return NULL; |
| 1686 | } |
| 1687 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1688 | // --- InputDispatcher::CommandEntry --- |
| 1689 | |
| 1690 | InputDispatcher::CommandEntry::CommandEntry() { |
| 1691 | } |
| 1692 | |
| 1693 | InputDispatcher::CommandEntry::~CommandEntry() { |
| 1694 | } |
| 1695 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1696 | |
| 1697 | // --- InputDispatcherThread --- |
| 1698 | |
| 1699 | InputDispatcherThread::InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher) : |
| 1700 | Thread(/*canCallJava*/ true), mDispatcher(dispatcher) { |
| 1701 | } |
| 1702 | |
| 1703 | InputDispatcherThread::~InputDispatcherThread() { |
| 1704 | } |
| 1705 | |
| 1706 | bool InputDispatcherThread::threadLoop() { |
| 1707 | mDispatcher->dispatchOnce(); |
| 1708 | return true; |
| 1709 | } |
| 1710 | |
| 1711 | } // namespace android |