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