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