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