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