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