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