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