Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2010 The Android Open Source Project |
michael@0 | 3 | * |
michael@0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 5 | * you may not use this file except in compliance with the License. |
michael@0 | 6 | * You may obtain a copy of the License at |
michael@0 | 7 | * |
michael@0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 9 | * |
michael@0 | 10 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 13 | * See the License for the specific language governing permissions and |
michael@0 | 14 | * limitations under the License. |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | #define LOG_TAG "InputDispatcher" |
michael@0 | 18 | #define ATRACE_TAG ATRACE_TAG_INPUT |
michael@0 | 19 | |
michael@0 | 20 | //#define LOG_NDEBUG 0 |
michael@0 | 21 | #include "cutils_log.h" |
michael@0 | 22 | |
michael@0 | 23 | // Log detailed debug messages about each inbound event notification to the dispatcher. |
michael@0 | 24 | #define DEBUG_INBOUND_EVENT_DETAILS 0 |
michael@0 | 25 | |
michael@0 | 26 | // Log detailed debug messages about each outbound event processed by the dispatcher. |
michael@0 | 27 | #define DEBUG_OUTBOUND_EVENT_DETAILS 0 |
michael@0 | 28 | |
michael@0 | 29 | // Log debug messages about the dispatch cycle. |
michael@0 | 30 | #define DEBUG_DISPATCH_CYCLE 0 |
michael@0 | 31 | |
michael@0 | 32 | // Log debug messages about registrations. |
michael@0 | 33 | #define DEBUG_REGISTRATION 0 |
michael@0 | 34 | |
michael@0 | 35 | // Log debug messages about input event injection. |
michael@0 | 36 | #define DEBUG_INJECTION 0 |
michael@0 | 37 | |
michael@0 | 38 | // Log debug messages about input focus tracking. |
michael@0 | 39 | #define DEBUG_FOCUS 0 |
michael@0 | 40 | |
michael@0 | 41 | // Log debug messages about the app switch latency optimization. |
michael@0 | 42 | #define DEBUG_APP_SWITCH 0 |
michael@0 | 43 | |
michael@0 | 44 | // Log debug messages about hover events. |
michael@0 | 45 | #define DEBUG_HOVER 0 |
michael@0 | 46 | |
michael@0 | 47 | #include "InputDispatcher.h" |
michael@0 | 48 | |
michael@0 | 49 | #include "Trace.h" |
michael@0 | 50 | #include "PowerManager.h" |
michael@0 | 51 | |
michael@0 | 52 | #include <stddef.h> |
michael@0 | 53 | #include <unistd.h> |
michael@0 | 54 | #include <errno.h> |
michael@0 | 55 | #include <limits.h> |
michael@0 | 56 | #include <time.h> |
michael@0 | 57 | |
michael@0 | 58 | #define INDENT " " |
michael@0 | 59 | #define INDENT2 " " |
michael@0 | 60 | #define INDENT3 " " |
michael@0 | 61 | #define INDENT4 " " |
michael@0 | 62 | |
michael@0 | 63 | namespace android { |
michael@0 | 64 | |
michael@0 | 65 | // Default input dispatching timeout if there is no focused application or paused window |
michael@0 | 66 | // from which to determine an appropriate dispatching timeout. |
michael@0 | 67 | const nsecs_t DEFAULT_INPUT_DISPATCHING_TIMEOUT = 5000 * 1000000LL; // 5 sec |
michael@0 | 68 | |
michael@0 | 69 | // Amount of time to allow for all pending events to be processed when an app switch |
michael@0 | 70 | // key is on the way. This is used to preempt input dispatch and drop input events |
michael@0 | 71 | // when an application takes too long to respond and the user has pressed an app switch key. |
michael@0 | 72 | const nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
michael@0 | 73 | |
michael@0 | 74 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
michael@0 | 75 | // before considering it stale and dropping it. |
michael@0 | 76 | const nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
michael@0 | 77 | |
michael@0 | 78 | // Amount of time to allow touch events to be streamed out to a connection before requiring |
michael@0 | 79 | // that the first event be finished. This value extends the ANR timeout by the specified |
michael@0 | 80 | // amount. For example, if streaming is allowed to get ahead by one second relative to the |
michael@0 | 81 | // queue of waiting unfinished events, then ANRs will similarly be delayed by one second. |
michael@0 | 82 | const nsecs_t STREAM_AHEAD_EVENT_TIMEOUT = 500 * 1000000LL; // 0.5sec |
michael@0 | 83 | |
michael@0 | 84 | // Log a warning when an event takes longer than this to process, even if an ANR does not occur. |
michael@0 | 85 | const nsecs_t SLOW_EVENT_PROCESSING_WARNING_TIMEOUT = 2000 * 1000000LL; // 2sec |
michael@0 | 86 | |
michael@0 | 87 | |
michael@0 | 88 | static inline nsecs_t now() { |
michael@0 | 89 | return systemTime(SYSTEM_TIME_MONOTONIC); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | static inline const char* toString(bool value) { |
michael@0 | 93 | return value ? "true" : "false"; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
michael@0 | 97 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) |
michael@0 | 98 | >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | static bool isValidKeyAction(int32_t action) { |
michael@0 | 102 | switch (action) { |
michael@0 | 103 | case AKEY_EVENT_ACTION_DOWN: |
michael@0 | 104 | case AKEY_EVENT_ACTION_UP: |
michael@0 | 105 | return true; |
michael@0 | 106 | default: |
michael@0 | 107 | return false; |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | static bool validateKeyEvent(int32_t action) { |
michael@0 | 112 | if (! isValidKeyAction(action)) { |
michael@0 | 113 | ALOGE("Key event has invalid action code 0x%x", action); |
michael@0 | 114 | return false; |
michael@0 | 115 | } |
michael@0 | 116 | return true; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | static bool isValidMotionAction(int32_t action, size_t pointerCount) { |
michael@0 | 120 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
michael@0 | 121 | case AMOTION_EVENT_ACTION_DOWN: |
michael@0 | 122 | case AMOTION_EVENT_ACTION_UP: |
michael@0 | 123 | case AMOTION_EVENT_ACTION_CANCEL: |
michael@0 | 124 | case AMOTION_EVENT_ACTION_MOVE: |
michael@0 | 125 | case AMOTION_EVENT_ACTION_OUTSIDE: |
michael@0 | 126 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
michael@0 | 127 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
michael@0 | 128 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
michael@0 | 129 | case AMOTION_EVENT_ACTION_SCROLL: |
michael@0 | 130 | return true; |
michael@0 | 131 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
michael@0 | 132 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
michael@0 | 133 | int32_t index = getMotionEventActionPointerIndex(action); |
michael@0 | 134 | return index >= 0 && size_t(index) < pointerCount; |
michael@0 | 135 | } |
michael@0 | 136 | default: |
michael@0 | 137 | return false; |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | static bool validateMotionEvent(int32_t action, size_t pointerCount, |
michael@0 | 142 | const PointerProperties* pointerProperties) { |
michael@0 | 143 | if (! isValidMotionAction(action, pointerCount)) { |
michael@0 | 144 | ALOGE("Motion event has invalid action code 0x%x", action); |
michael@0 | 145 | return false; |
michael@0 | 146 | } |
michael@0 | 147 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
michael@0 | 148 | ALOGE("Motion event has invalid pointer count %d; value must be between 1 and %d.", |
michael@0 | 149 | pointerCount, MAX_POINTERS); |
michael@0 | 150 | return false; |
michael@0 | 151 | } |
michael@0 | 152 | BitSet32 pointerIdBits; |
michael@0 | 153 | for (size_t i = 0; i < pointerCount; i++) { |
michael@0 | 154 | int32_t id = pointerProperties[i].id; |
michael@0 | 155 | if (id < 0 || id > MAX_POINTER_ID) { |
michael@0 | 156 | ALOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", |
michael@0 | 157 | id, MAX_POINTER_ID); |
michael@0 | 158 | return false; |
michael@0 | 159 | } |
michael@0 | 160 | if (pointerIdBits.hasBit(id)) { |
michael@0 | 161 | ALOGE("Motion event has duplicate pointer id %d", id); |
michael@0 | 162 | return false; |
michael@0 | 163 | } |
michael@0 | 164 | pointerIdBits.markBit(id); |
michael@0 | 165 | } |
michael@0 | 166 | return true; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | static bool isMainDisplay(int32_t displayId) { |
michael@0 | 170 | return displayId == ADISPLAY_ID_DEFAULT || displayId == ADISPLAY_ID_NONE; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | static void dumpRegion(String8& dump, const SkRegion& region) { |
michael@0 | 174 | if (region.isEmpty()) { |
michael@0 | 175 | dump.append("<empty>"); |
michael@0 | 176 | return; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | bool first = true; |
michael@0 | 180 | for (SkRegion::Iterator it(region); !it.done(); it.next()) { |
michael@0 | 181 | if (first) { |
michael@0 | 182 | first = false; |
michael@0 | 183 | } else { |
michael@0 | 184 | dump.append("|"); |
michael@0 | 185 | } |
michael@0 | 186 | const SkIRect& rect = it.rect(); |
michael@0 | 187 | dump.appendFormat("[%d,%d][%d,%d]", rect.fLeft, rect.fTop, rect.fRight, rect.fBottom); |
michael@0 | 188 | } |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | |
michael@0 | 192 | // --- InputDispatcher --- |
michael@0 | 193 | |
michael@0 | 194 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) : |
michael@0 | 195 | mPolicy(policy), |
michael@0 | 196 | mPendingEvent(NULL), mAppSwitchSawKeyDown(false), mAppSwitchDueTime(LONG_LONG_MAX), |
michael@0 | 197 | mNextUnblockedEvent(NULL), |
michael@0 | 198 | mDispatchEnabled(false), mDispatchFrozen(false), mInputFilterEnabled(false), |
michael@0 | 199 | mInputTargetWaitCause(INPUT_TARGET_WAIT_CAUSE_NONE) { |
michael@0 | 200 | mLooper = new Looper(false); |
michael@0 | 201 | |
michael@0 | 202 | mKeyRepeatState.lastKeyEntry = NULL; |
michael@0 | 203 | |
michael@0 | 204 | policy->getDispatcherConfiguration(&mConfig); |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | InputDispatcher::~InputDispatcher() { |
michael@0 | 208 | { // acquire lock |
michael@0 | 209 | AutoMutex _l(mLock); |
michael@0 | 210 | |
michael@0 | 211 | resetKeyRepeatLocked(); |
michael@0 | 212 | releasePendingEventLocked(); |
michael@0 | 213 | drainInboundQueueLocked(); |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | while (mConnectionsByFd.size() != 0) { |
michael@0 | 217 | unregisterInputChannel(mConnectionsByFd.valueAt(0)->inputChannel); |
michael@0 | 218 | } |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | void InputDispatcher::dispatchOnce() { |
michael@0 | 222 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
michael@0 | 223 | { // acquire lock |
michael@0 | 224 | AutoMutex _l(mLock); |
michael@0 | 225 | mDispatcherIsAliveCondition.broadcast(); |
michael@0 | 226 | |
michael@0 | 227 | // Run a dispatch loop if there are no pending commands. |
michael@0 | 228 | // The dispatch loop might enqueue commands to run afterwards. |
michael@0 | 229 | if (!haveCommandsLocked()) { |
michael@0 | 230 | dispatchOnceInnerLocked(&nextWakeupTime); |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | // Run all pending commands if there are any. |
michael@0 | 234 | // If any commands were run then force the next poll to wake up immediately. |
michael@0 | 235 | if (runCommandsLockedInterruptible()) { |
michael@0 | 236 | nextWakeupTime = LONG_LONG_MIN; |
michael@0 | 237 | } |
michael@0 | 238 | } // release lock |
michael@0 | 239 | |
michael@0 | 240 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
michael@0 | 241 | nsecs_t currentTime = now(); |
michael@0 | 242 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
michael@0 | 243 | mLooper->pollOnce(timeoutMillis); |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
michael@0 | 247 | nsecs_t currentTime = now(); |
michael@0 | 248 | |
michael@0 | 249 | // Reset the key repeat timer whenever we disallow key events, even if the next event |
michael@0 | 250 | // is not a key. This is to ensure that we abort a key repeat if the device is just coming |
michael@0 | 251 | // out of sleep. |
michael@0 | 252 | if (!mPolicy->isKeyRepeatEnabled()) { |
michael@0 | 253 | resetKeyRepeatLocked(); |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
michael@0 | 257 | if (mDispatchFrozen) { |
michael@0 | 258 | #if DEBUG_FOCUS |
michael@0 | 259 | ALOGD("Dispatch frozen. Waiting some more."); |
michael@0 | 260 | #endif |
michael@0 | 261 | return; |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | // Optimize latency of app switches. |
michael@0 | 265 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
michael@0 | 266 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
michael@0 | 267 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
michael@0 | 268 | if (mAppSwitchDueTime < *nextWakeupTime) { |
michael@0 | 269 | *nextWakeupTime = mAppSwitchDueTime; |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | // Ready to start a new event. |
michael@0 | 273 | // If we don't already have a pending event, go grab one. |
michael@0 | 274 | if (! mPendingEvent) { |
michael@0 | 275 | if (mInboundQueue.isEmpty()) { |
michael@0 | 276 | if (isAppSwitchDue) { |
michael@0 | 277 | // The inbound queue is empty so the app switch key we were waiting |
michael@0 | 278 | // for will never arrive. Stop waiting for it. |
michael@0 | 279 | resetPendingAppSwitchLocked(false); |
michael@0 | 280 | isAppSwitchDue = false; |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | // Synthesize a key repeat if appropriate. |
michael@0 | 284 | if (mKeyRepeatState.lastKeyEntry) { |
michael@0 | 285 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
michael@0 | 286 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
michael@0 | 287 | } else { |
michael@0 | 288 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
michael@0 | 289 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
michael@0 | 290 | } |
michael@0 | 291 | } |
michael@0 | 292 | } |
michael@0 | 293 | |
michael@0 | 294 | // Nothing to do if there is no pending event. |
michael@0 | 295 | if (!mPendingEvent) { |
michael@0 | 296 | return; |
michael@0 | 297 | } |
michael@0 | 298 | } else { |
michael@0 | 299 | // Inbound queue has at least one entry. |
michael@0 | 300 | mPendingEvent = mInboundQueue.dequeueAtHead(); |
michael@0 | 301 | traceInboundQueueLengthLocked(); |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | // Poke user activity for this event. |
michael@0 | 305 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
michael@0 | 306 | pokeUserActivityLocked(mPendingEvent); |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | // Get ready to dispatch the event. |
michael@0 | 310 | resetANRTimeoutsLocked(); |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | // Now we have an event to dispatch. |
michael@0 | 314 | // All events are eventually dequeued and processed this way, even if we intend to drop them. |
michael@0 | 315 | ALOG_ASSERT(mPendingEvent != NULL); |
michael@0 | 316 | bool done = false; |
michael@0 | 317 | DropReason dropReason = DROP_REASON_NOT_DROPPED; |
michael@0 | 318 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
michael@0 | 319 | dropReason = DROP_REASON_POLICY; |
michael@0 | 320 | } else if (!mDispatchEnabled) { |
michael@0 | 321 | dropReason = DROP_REASON_DISABLED; |
michael@0 | 322 | } |
michael@0 | 323 | |
michael@0 | 324 | if (mNextUnblockedEvent == mPendingEvent) { |
michael@0 | 325 | mNextUnblockedEvent = NULL; |
michael@0 | 326 | } |
michael@0 | 327 | |
michael@0 | 328 | switch (mPendingEvent->type) { |
michael@0 | 329 | case EventEntry::TYPE_CONFIGURATION_CHANGED: { |
michael@0 | 330 | ConfigurationChangedEntry* typedEntry = |
michael@0 | 331 | static_cast<ConfigurationChangedEntry*>(mPendingEvent); |
michael@0 | 332 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
michael@0 | 333 | dropReason = DROP_REASON_NOT_DROPPED; // configuration changes are never dropped |
michael@0 | 334 | break; |
michael@0 | 335 | } |
michael@0 | 336 | |
michael@0 | 337 | case EventEntry::TYPE_DEVICE_RESET: { |
michael@0 | 338 | DeviceResetEntry* typedEntry = |
michael@0 | 339 | static_cast<DeviceResetEntry*>(mPendingEvent); |
michael@0 | 340 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
michael@0 | 341 | dropReason = DROP_REASON_NOT_DROPPED; // device resets are never dropped |
michael@0 | 342 | break; |
michael@0 | 343 | } |
michael@0 | 344 | |
michael@0 | 345 | case EventEntry::TYPE_KEY: { |
michael@0 | 346 | KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent); |
michael@0 | 347 | if (isAppSwitchDue) { |
michael@0 | 348 | if (isAppSwitchKeyEventLocked(typedEntry)) { |
michael@0 | 349 | resetPendingAppSwitchLocked(true); |
michael@0 | 350 | isAppSwitchDue = false; |
michael@0 | 351 | } else if (dropReason == DROP_REASON_NOT_DROPPED) { |
michael@0 | 352 | dropReason = DROP_REASON_APP_SWITCH; |
michael@0 | 353 | } |
michael@0 | 354 | } |
michael@0 | 355 | if (dropReason == DROP_REASON_NOT_DROPPED |
michael@0 | 356 | && isStaleEventLocked(currentTime, typedEntry)) { |
michael@0 | 357 | dropReason = DROP_REASON_STALE; |
michael@0 | 358 | } |
michael@0 | 359 | if (dropReason == DROP_REASON_NOT_DROPPED && mNextUnblockedEvent) { |
michael@0 | 360 | dropReason = DROP_REASON_BLOCKED; |
michael@0 | 361 | } |
michael@0 | 362 | done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); |
michael@0 | 363 | break; |
michael@0 | 364 | } |
michael@0 | 365 | |
michael@0 | 366 | case EventEntry::TYPE_MOTION: { |
michael@0 | 367 | MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent); |
michael@0 | 368 | if (dropReason == DROP_REASON_NOT_DROPPED && isAppSwitchDue) { |
michael@0 | 369 | dropReason = DROP_REASON_APP_SWITCH; |
michael@0 | 370 | } |
michael@0 | 371 | if (dropReason == DROP_REASON_NOT_DROPPED |
michael@0 | 372 | && isStaleEventLocked(currentTime, typedEntry)) { |
michael@0 | 373 | dropReason = DROP_REASON_STALE; |
michael@0 | 374 | } |
michael@0 | 375 | if (dropReason == DROP_REASON_NOT_DROPPED && mNextUnblockedEvent) { |
michael@0 | 376 | dropReason = DROP_REASON_BLOCKED; |
michael@0 | 377 | } |
michael@0 | 378 | done = dispatchMotionLocked(currentTime, typedEntry, |
michael@0 | 379 | &dropReason, nextWakeupTime); |
michael@0 | 380 | break; |
michael@0 | 381 | } |
michael@0 | 382 | |
michael@0 | 383 | default: |
michael@0 | 384 | ALOG_ASSERT(false); |
michael@0 | 385 | break; |
michael@0 | 386 | } |
michael@0 | 387 | |
michael@0 | 388 | if (done) { |
michael@0 | 389 | if (dropReason != DROP_REASON_NOT_DROPPED) { |
michael@0 | 390 | dropInboundEventLocked(mPendingEvent, dropReason); |
michael@0 | 391 | } |
michael@0 | 392 | |
michael@0 | 393 | releasePendingEventLocked(); |
michael@0 | 394 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
michael@0 | 395 | } |
michael@0 | 396 | } |
michael@0 | 397 | |
michael@0 | 398 | bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) { |
michael@0 | 399 | bool needWake = mInboundQueue.isEmpty(); |
michael@0 | 400 | mInboundQueue.enqueueAtTail(entry); |
michael@0 | 401 | traceInboundQueueLengthLocked(); |
michael@0 | 402 | |
michael@0 | 403 | switch (entry->type) { |
michael@0 | 404 | case EventEntry::TYPE_KEY: { |
michael@0 | 405 | // Optimize app switch latency. |
michael@0 | 406 | // If the application takes too long to catch up then we drop all events preceding |
michael@0 | 407 | // the app switch key. |
michael@0 | 408 | KeyEntry* keyEntry = static_cast<KeyEntry*>(entry); |
michael@0 | 409 | if (isAppSwitchKeyEventLocked(keyEntry)) { |
michael@0 | 410 | if (keyEntry->action == AKEY_EVENT_ACTION_DOWN) { |
michael@0 | 411 | mAppSwitchSawKeyDown = true; |
michael@0 | 412 | } else if (keyEntry->action == AKEY_EVENT_ACTION_UP) { |
michael@0 | 413 | if (mAppSwitchSawKeyDown) { |
michael@0 | 414 | #if DEBUG_APP_SWITCH |
michael@0 | 415 | ALOGD("App switch is pending!"); |
michael@0 | 416 | #endif |
michael@0 | 417 | mAppSwitchDueTime = keyEntry->eventTime + APP_SWITCH_TIMEOUT; |
michael@0 | 418 | mAppSwitchSawKeyDown = false; |
michael@0 | 419 | needWake = true; |
michael@0 | 420 | } |
michael@0 | 421 | } |
michael@0 | 422 | } |
michael@0 | 423 | break; |
michael@0 | 424 | } |
michael@0 | 425 | |
michael@0 | 426 | case EventEntry::TYPE_MOTION: { |
michael@0 | 427 | // Optimize case where the current application is unresponsive and the user |
michael@0 | 428 | // decides to touch a window in a different application. |
michael@0 | 429 | // If the application takes too long to catch up then we drop all events preceding |
michael@0 | 430 | // the touch into the other window. |
michael@0 | 431 | MotionEntry* motionEntry = static_cast<MotionEntry*>(entry); |
michael@0 | 432 | if (motionEntry->action == AMOTION_EVENT_ACTION_DOWN |
michael@0 | 433 | && (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) |
michael@0 | 434 | && mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY |
michael@0 | 435 | && mInputTargetWaitApplicationHandle != NULL) { |
michael@0 | 436 | int32_t displayId = motionEntry->displayId; |
michael@0 | 437 | int32_t x = int32_t(motionEntry->pointerCoords[0]. |
michael@0 | 438 | getAxisValue(AMOTION_EVENT_AXIS_X)); |
michael@0 | 439 | int32_t y = int32_t(motionEntry->pointerCoords[0]. |
michael@0 | 440 | getAxisValue(AMOTION_EVENT_AXIS_Y)); |
michael@0 | 441 | sp<InputWindowHandle> touchedWindowHandle = findTouchedWindowAtLocked(displayId, x, y); |
michael@0 | 442 | if (touchedWindowHandle != NULL |
michael@0 | 443 | && touchedWindowHandle->inputApplicationHandle |
michael@0 | 444 | != mInputTargetWaitApplicationHandle) { |
michael@0 | 445 | // User touched a different application than the one we are waiting on. |
michael@0 | 446 | // Flag the event, and start pruning the input queue. |
michael@0 | 447 | mNextUnblockedEvent = motionEntry; |
michael@0 | 448 | needWake = true; |
michael@0 | 449 | } |
michael@0 | 450 | } |
michael@0 | 451 | break; |
michael@0 | 452 | } |
michael@0 | 453 | } |
michael@0 | 454 | |
michael@0 | 455 | return needWake; |
michael@0 | 456 | } |
michael@0 | 457 | |
michael@0 | 458 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t displayId, |
michael@0 | 459 | int32_t x, int32_t y) { |
michael@0 | 460 | // Traverse windows from front to back to find touched window. |
michael@0 | 461 | size_t numWindows = mWindowHandles.size(); |
michael@0 | 462 | for (size_t i = 0; i < numWindows; i++) { |
michael@0 | 463 | sp<InputWindowHandle> windowHandle = mWindowHandles.itemAt(i); |
michael@0 | 464 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
michael@0 | 465 | if (windowInfo->displayId == displayId) { |
michael@0 | 466 | int32_t flags = windowInfo->layoutParamsFlags; |
michael@0 | 467 | |
michael@0 | 468 | if (windowInfo->visible) { |
michael@0 | 469 | if (!(flags & InputWindowInfo::FLAG_NOT_TOUCHABLE)) { |
michael@0 | 470 | bool isTouchModal = (flags & (InputWindowInfo::FLAG_NOT_FOCUSABLE |
michael@0 | 471 | | InputWindowInfo::FLAG_NOT_TOUCH_MODAL)) == 0; |
michael@0 | 472 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
michael@0 | 473 | // Found window. |
michael@0 | 474 | return windowHandle; |
michael@0 | 475 | } |
michael@0 | 476 | } |
michael@0 | 477 | } |
michael@0 | 478 | |
michael@0 | 479 | if (flags & InputWindowInfo::FLAG_SYSTEM_ERROR) { |
michael@0 | 480 | // Error window is on top but not visible, so touch is dropped. |
michael@0 | 481 | return NULL; |
michael@0 | 482 | } |
michael@0 | 483 | } |
michael@0 | 484 | } |
michael@0 | 485 | return NULL; |
michael@0 | 486 | } |
michael@0 | 487 | |
michael@0 | 488 | void InputDispatcher::dropInboundEventLocked(EventEntry* entry, DropReason dropReason) { |
michael@0 | 489 | const char* reason; |
michael@0 | 490 | switch (dropReason) { |
michael@0 | 491 | case DROP_REASON_POLICY: |
michael@0 | 492 | #if DEBUG_INBOUND_EVENT_DETAILS |
michael@0 | 493 | ALOGD("Dropped event because policy consumed it."); |
michael@0 | 494 | #endif |
michael@0 | 495 | reason = "inbound event was dropped because the policy consumed it"; |
michael@0 | 496 | break; |
michael@0 | 497 | case DROP_REASON_DISABLED: |
michael@0 | 498 | ALOGI("Dropped event because input dispatch is disabled."); |
michael@0 | 499 | reason = "inbound event was dropped because input dispatch is disabled"; |
michael@0 | 500 | break; |
michael@0 | 501 | case DROP_REASON_APP_SWITCH: |
michael@0 | 502 | ALOGI("Dropped event because of pending overdue app switch."); |
michael@0 | 503 | reason = "inbound event was dropped because of pending overdue app switch"; |
michael@0 | 504 | break; |
michael@0 | 505 | case DROP_REASON_BLOCKED: |
michael@0 | 506 | ALOGI("Dropped event because the current application is not responding and the user " |
michael@0 | 507 | "has started interacting with a different application."); |
michael@0 | 508 | reason = "inbound event was dropped because the current application is not responding " |
michael@0 | 509 | "and the user has started interacting with a different application"; |
michael@0 | 510 | break; |
michael@0 | 511 | case DROP_REASON_STALE: |
michael@0 | 512 | ALOGI("Dropped event because it is stale."); |
michael@0 | 513 | reason = "inbound event was dropped because it is stale"; |
michael@0 | 514 | break; |
michael@0 | 515 | default: |
michael@0 | 516 | ALOG_ASSERT(false); |
michael@0 | 517 | return; |
michael@0 | 518 | } |
michael@0 | 519 | |
michael@0 | 520 | switch (entry->type) { |
michael@0 | 521 | case EventEntry::TYPE_KEY: { |
michael@0 | 522 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
michael@0 | 523 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
michael@0 | 524 | break; |
michael@0 | 525 | } |
michael@0 | 526 | case EventEntry::TYPE_MOTION: { |
michael@0 | 527 | MotionEntry* motionEntry = static_cast<MotionEntry*>(entry); |
michael@0 | 528 | if (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) { |
michael@0 | 529 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
michael@0 | 530 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
michael@0 | 531 | } else { |
michael@0 | 532 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
michael@0 | 533 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
michael@0 | 534 | } |
michael@0 | 535 | break; |
michael@0 | 536 | } |
michael@0 | 537 | } |
michael@0 | 538 | } |
michael@0 | 539 | |
michael@0 | 540 | bool InputDispatcher::isAppSwitchKeyCode(int32_t keyCode) { |
michael@0 | 541 | return keyCode == AKEYCODE_HOME |
michael@0 | 542 | || keyCode == AKEYCODE_ENDCALL |
michael@0 | 543 | || keyCode == AKEYCODE_APP_SWITCH; |
michael@0 | 544 | } |
michael@0 | 545 | |
michael@0 | 546 | bool InputDispatcher::isAppSwitchKeyEventLocked(KeyEntry* keyEntry) { |
michael@0 | 547 | return ! (keyEntry->flags & AKEY_EVENT_FLAG_CANCELED) |
michael@0 | 548 | && isAppSwitchKeyCode(keyEntry->keyCode) |
michael@0 | 549 | && (keyEntry->policyFlags & POLICY_FLAG_TRUSTED) |
michael@0 | 550 | && (keyEntry->policyFlags & POLICY_FLAG_PASS_TO_USER); |
michael@0 | 551 | } |
michael@0 | 552 | |
michael@0 | 553 | bool InputDispatcher::isAppSwitchPendingLocked() { |
michael@0 | 554 | return mAppSwitchDueTime != LONG_LONG_MAX; |
michael@0 | 555 | } |
michael@0 | 556 | |
michael@0 | 557 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
michael@0 | 558 | mAppSwitchDueTime = LONG_LONG_MAX; |
michael@0 | 559 | |
michael@0 | 560 | #if DEBUG_APP_SWITCH |
michael@0 | 561 | if (handled) { |
michael@0 | 562 | ALOGD("App switch has arrived."); |
michael@0 | 563 | } else { |
michael@0 | 564 | ALOGD("App switch was abandoned."); |
michael@0 | 565 | } |
michael@0 | 566 | #endif |
michael@0 | 567 | } |
michael@0 | 568 | |
michael@0 | 569 | bool InputDispatcher::isStaleEventLocked(nsecs_t currentTime, EventEntry* entry) { |
michael@0 | 570 | return currentTime - entry->eventTime >= STALE_EVENT_TIMEOUT; |
michael@0 | 571 | } |
michael@0 | 572 | |
michael@0 | 573 | bool InputDispatcher::haveCommandsLocked() const { |
michael@0 | 574 | return !mCommandQueue.isEmpty(); |
michael@0 | 575 | } |
michael@0 | 576 | |
michael@0 | 577 | bool InputDispatcher::runCommandsLockedInterruptible() { |
michael@0 | 578 | if (mCommandQueue.isEmpty()) { |
michael@0 | 579 | return false; |
michael@0 | 580 | } |
michael@0 | 581 | |
michael@0 | 582 | do { |
michael@0 | 583 | CommandEntry* commandEntry = mCommandQueue.dequeueAtHead(); |
michael@0 | 584 | |
michael@0 | 585 | Command command = commandEntry->command; |
michael@0 | 586 | (this->*command)(commandEntry); // commands are implicitly 'LockedInterruptible' |
michael@0 | 587 | |
michael@0 | 588 | commandEntry->connection.clear(); |
michael@0 | 589 | delete commandEntry; |
michael@0 | 590 | } while (! mCommandQueue.isEmpty()); |
michael@0 | 591 | return true; |
michael@0 | 592 | } |
michael@0 | 593 | |
michael@0 | 594 | InputDispatcher::CommandEntry* InputDispatcher::postCommandLocked(Command command) { |
michael@0 | 595 | CommandEntry* commandEntry = new CommandEntry(command); |
michael@0 | 596 | mCommandQueue.enqueueAtTail(commandEntry); |
michael@0 | 597 | return commandEntry; |
michael@0 | 598 | } |
michael@0 | 599 | |
michael@0 | 600 | void InputDispatcher::drainInboundQueueLocked() { |
michael@0 | 601 | while (! mInboundQueue.isEmpty()) { |
michael@0 | 602 | EventEntry* entry = mInboundQueue.dequeueAtHead(); |
michael@0 | 603 | releaseInboundEventLocked(entry); |
michael@0 | 604 | } |
michael@0 | 605 | traceInboundQueueLengthLocked(); |
michael@0 | 606 | } |
michael@0 | 607 | |
michael@0 | 608 | void InputDispatcher::releasePendingEventLocked() { |
michael@0 | 609 | if (mPendingEvent) { |
michael@0 | 610 | resetANRTimeoutsLocked(); |
michael@0 | 611 | releaseInboundEventLocked(mPendingEvent); |
michael@0 | 612 | mPendingEvent = NULL; |
michael@0 | 613 | } |
michael@0 | 614 | } |
michael@0 | 615 | |
michael@0 | 616 | void InputDispatcher::releaseInboundEventLocked(EventEntry* entry) { |
michael@0 | 617 | InjectionState* injectionState = entry->injectionState; |
michael@0 | 618 | if (injectionState && injectionState->injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
michael@0 | 619 | #if DEBUG_DISPATCH_CYCLE |
michael@0 | 620 | ALOGD("Injected inbound event was dropped."); |
michael@0 | 621 | #endif |
michael@0 | 622 | setInjectionResultLocked(entry, INPUT_EVENT_INJECTION_FAILED); |
michael@0 | 623 | } |
michael@0 | 624 | if (entry == mNextUnblockedEvent) { |
michael@0 | 625 | mNextUnblockedEvent = NULL; |
michael@0 | 626 | } |
michael@0 | 627 | entry->release(); |
michael@0 | 628 | } |
michael@0 | 629 | |
michael@0 | 630 | void InputDispatcher::resetKeyRepeatLocked() { |
michael@0 | 631 | if (mKeyRepeatState.lastKeyEntry) { |
michael@0 | 632 | mKeyRepeatState.lastKeyEntry->release(); |
michael@0 | 633 | mKeyRepeatState.lastKeyEntry = NULL; |
michael@0 | 634 | } |
michael@0 | 635 | } |
michael@0 | 636 | |
michael@0 | 637 | InputDispatcher::KeyEntry* InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
michael@0 | 638 | KeyEntry* entry = mKeyRepeatState.lastKeyEntry; |
michael@0 | 639 | |
michael@0 | 640 | // Reuse the repeated key entry if it is otherwise unreferenced. |
michael@0 | 641 | uint32_t policyFlags = (entry->policyFlags & POLICY_FLAG_RAW_MASK) |
michael@0 | 642 | | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED; |
michael@0 | 643 | if (entry->refCount == 1) { |
michael@0 | 644 | entry->recycle(); |
michael@0 | 645 | entry->eventTime = currentTime; |
michael@0 | 646 | entry->policyFlags = policyFlags; |
michael@0 | 647 | entry->repeatCount += 1; |
michael@0 | 648 | } else { |
michael@0 | 649 | KeyEntry* newEntry = new KeyEntry(currentTime, |
michael@0 | 650 | entry->deviceId, entry->source, policyFlags, |
michael@0 | 651 | entry->action, entry->flags, entry->keyCode, entry->scanCode, |
michael@0 | 652 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
michael@0 | 653 | |
michael@0 | 654 | mKeyRepeatState.lastKeyEntry = newEntry; |
michael@0 | 655 | entry->release(); |
michael@0 | 656 | |
michael@0 | 657 | entry = newEntry; |
michael@0 | 658 | } |
michael@0 | 659 | entry->syntheticRepeat = true; |
michael@0 | 660 | |
michael@0 | 661 | // Increment reference count since we keep a reference to the event in |
michael@0 | 662 | // mKeyRepeatState.lastKeyEntry in addition to the one we return. |
michael@0 | 663 | entry->refCount += 1; |
michael@0 | 664 | |
michael@0 | 665 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
michael@0 | 666 | return entry; |
michael@0 | 667 | } |
michael@0 | 668 | |
michael@0 | 669 | bool InputDispatcher::dispatchConfigurationChangedLocked( |
michael@0 | 670 | nsecs_t currentTime, ConfigurationChangedEntry* entry) { |
michael@0 | 671 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
michael@0 | 672 | ALOGD("dispatchConfigurationChanged - eventTime=%lld", entry->eventTime); |
michael@0 | 673 | #endif |
michael@0 | 674 | |
michael@0 | 675 | // Reset key repeating in case a keyboard device was added or removed or something. |
michael@0 | 676 | resetKeyRepeatLocked(); |
michael@0 | 677 | |
michael@0 | 678 | // Enqueue a command to run outside the lock to tell the policy that the configuration changed. |
michael@0 | 679 | CommandEntry* commandEntry = postCommandLocked( |
michael@0 | 680 | & InputDispatcher::doNotifyConfigurationChangedInterruptible); |
michael@0 | 681 | commandEntry->eventTime = entry->eventTime; |
michael@0 | 682 | return true; |
michael@0 | 683 | } |
michael@0 | 684 | |
michael@0 | 685 | bool InputDispatcher::dispatchDeviceResetLocked( |
michael@0 | 686 | nsecs_t currentTime, DeviceResetEntry* entry) { |
michael@0 | 687 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
michael@0 | 688 | ALOGD("dispatchDeviceReset - eventTime=%lld, deviceId=%d", entry->eventTime, entry->deviceId); |
michael@0 | 689 | #endif |
michael@0 | 690 | |
michael@0 | 691 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
michael@0 | 692 | "device was reset"); |
michael@0 | 693 | options.deviceId = entry->deviceId; |
michael@0 | 694 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
michael@0 | 695 | return true; |
michael@0 | 696 | } |
michael@0 | 697 | |
michael@0 | 698 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, |
michael@0 | 699 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
michael@0 | 700 | // Preprocessing. |
michael@0 | 701 | if (! entry->dispatchInProgress) { |
michael@0 | 702 | if (entry->repeatCount == 0 |
michael@0 | 703 | && entry->action == AKEY_EVENT_ACTION_DOWN |
michael@0 | 704 | && (entry->policyFlags & POLICY_FLAG_TRUSTED) |
michael@0 | 705 | && (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
michael@0 | 706 | if (mKeyRepeatState.lastKeyEntry |
michael@0 | 707 | && mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode) { |
michael@0 | 708 | // We have seen two identical key downs in a row which indicates that the device |
michael@0 | 709 | // driver is automatically generating key repeats itself. We take note of the |
michael@0 | 710 | // repeat here, but we disable our own next key repeat timer since it is clear that |
michael@0 | 711 | // we will not need to synthesize key repeats ourselves. |
michael@0 | 712 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
michael@0 | 713 | resetKeyRepeatLocked(); |
michael@0 | 714 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
michael@0 | 715 | } else { |
michael@0 | 716 | // Not a repeat. Save key down state in case we do see a repeat later. |
michael@0 | 717 | resetKeyRepeatLocked(); |
michael@0 | 718 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
michael@0 | 719 | } |
michael@0 | 720 | mKeyRepeatState.lastKeyEntry = entry; |
michael@0 | 721 | entry->refCount += 1; |
michael@0 | 722 | } else if (! entry->syntheticRepeat) { |
michael@0 | 723 | resetKeyRepeatLocked(); |
michael@0 | 724 | } |
michael@0 | 725 | |
michael@0 | 726 | if (entry->repeatCount == 1) { |
michael@0 | 727 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
michael@0 | 728 | } else { |
michael@0 | 729 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
michael@0 | 730 | } |
michael@0 | 731 | |
michael@0 | 732 | entry->dispatchInProgress = true; |
michael@0 | 733 | |
michael@0 | 734 | logOutboundKeyDetailsLocked("dispatchKey - ", entry); |
michael@0 | 735 | } |
michael@0 | 736 | |
michael@0 | 737 | // Handle case where the policy asked us to try again later last time. |
michael@0 | 738 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
michael@0 | 739 | if (currentTime < entry->interceptKeyWakeupTime) { |
michael@0 | 740 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
michael@0 | 741 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
michael@0 | 742 | } |
michael@0 | 743 | return false; // wait until next wakeup |
michael@0 | 744 | } |
michael@0 | 745 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
michael@0 | 746 | entry->interceptKeyWakeupTime = 0; |
michael@0 | 747 | } |
michael@0 | 748 | |
michael@0 | 749 | // Give the policy a chance to intercept the key. |
michael@0 | 750 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
michael@0 | 751 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
michael@0 | 752 | CommandEntry* commandEntry = postCommandLocked( |
michael@0 | 753 | & InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); |
michael@0 | 754 | if (mFocusedWindowHandle != NULL) { |
michael@0 | 755 | commandEntry->inputWindowHandle = mFocusedWindowHandle; |
michael@0 | 756 | } |
michael@0 | 757 | commandEntry->keyEntry = entry; |
michael@0 | 758 | entry->refCount += 1; |
michael@0 | 759 | return false; // wait for the command to run |
michael@0 | 760 | } else { |
michael@0 | 761 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
michael@0 | 762 | } |
michael@0 | 763 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
michael@0 | 764 | if (*dropReason == DROP_REASON_NOT_DROPPED) { |
michael@0 | 765 | *dropReason = DROP_REASON_POLICY; |
michael@0 | 766 | } |
michael@0 | 767 | } |
michael@0 | 768 | |
michael@0 | 769 | // Clean up if dropping the event. |
michael@0 | 770 | if (*dropReason != DROP_REASON_NOT_DROPPED) { |
michael@0 | 771 | setInjectionResultLocked(entry, *dropReason == DROP_REASON_POLICY |
michael@0 | 772 | ? INPUT_EVENT_INJECTION_SUCCEEDED : INPUT_EVENT_INJECTION_FAILED); |
michael@0 | 773 | return true; |
michael@0 | 774 | } |
michael@0 | 775 | |
michael@0 | 776 | // Identify targets. |
michael@0 | 777 | Vector<InputTarget> inputTargets; |
michael@0 | 778 | int32_t injectionResult = findFocusedWindowTargetsLocked(currentTime, |
michael@0 | 779 | entry, inputTargets, nextWakeupTime); |
michael@0 | 780 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
michael@0 | 781 | return false; |
michael@0 | 782 | } |
michael@0 | 783 | |
michael@0 | 784 | setInjectionResultLocked(entry, injectionResult); |
michael@0 | 785 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
michael@0 | 786 | return true; |
michael@0 | 787 | } |
michael@0 | 788 | |
michael@0 | 789 | addMonitoringTargetsLocked(inputTargets); |
michael@0 | 790 | |
michael@0 | 791 | // Dispatch the key. |
michael@0 | 792 | dispatchEventLocked(currentTime, entry, inputTargets); |
michael@0 | 793 | return true; |
michael@0 | 794 | } |
michael@0 | 795 | |
michael@0 | 796 | void InputDispatcher::logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry) { |
michael@0 | 797 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
michael@0 | 798 | ALOGD("%seventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, " |
michael@0 | 799 | "action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, " |
michael@0 | 800 | "repeatCount=%d, downTime=%lld", |
michael@0 | 801 | prefix, |
michael@0 | 802 | entry->eventTime, entry->deviceId, entry->source, entry->policyFlags, |
michael@0 | 803 | entry->action, entry->flags, entry->keyCode, entry->scanCode, entry->metaState, |
michael@0 | 804 | entry->repeatCount, entry->downTime); |
michael@0 | 805 | #endif |
michael@0 | 806 | } |
michael@0 | 807 | |
michael@0 | 808 | bool InputDispatcher::dispatchMotionLocked( |
michael@0 | 809 | nsecs_t currentTime, MotionEntry* entry, DropReason* dropReason, nsecs_t* nextWakeupTime) { |
michael@0 | 810 | // Preprocessing. |
michael@0 | 811 | if (! entry->dispatchInProgress) { |
michael@0 | 812 | entry->dispatchInProgress = true; |
michael@0 | 813 | |
michael@0 | 814 | logOutboundMotionDetailsLocked("dispatchMotion - ", entry); |
michael@0 | 815 | } |
michael@0 | 816 | |
michael@0 | 817 | // Clean up if dropping the event. |
michael@0 | 818 | if (*dropReason != DROP_REASON_NOT_DROPPED) { |
michael@0 | 819 | setInjectionResultLocked(entry, *dropReason == DROP_REASON_POLICY |
michael@0 | 820 | ? INPUT_EVENT_INJECTION_SUCCEEDED : INPUT_EVENT_INJECTION_FAILED); |
michael@0 | 821 | return true; |
michael@0 | 822 | } |
michael@0 | 823 | |
michael@0 | 824 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
michael@0 | 825 | |
michael@0 | 826 | // Identify targets. |
michael@0 | 827 | Vector<InputTarget> inputTargets; |
michael@0 | 828 | |
michael@0 | 829 | bool conflictingPointerActions = false; |
michael@0 | 830 | int32_t injectionResult; |
michael@0 | 831 | if (isPointerEvent) { |
michael@0 | 832 | // Pointer event. (eg. touchscreen) |
michael@0 | 833 | injectionResult = findTouchedWindowTargetsLocked(currentTime, |
michael@0 | 834 | entry, inputTargets, nextWakeupTime, &conflictingPointerActions); |
michael@0 | 835 | } else { |
michael@0 | 836 | // Non touch event. (eg. trackball) |
michael@0 | 837 | injectionResult = findFocusedWindowTargetsLocked(currentTime, |
michael@0 | 838 | entry, inputTargets, nextWakeupTime); |
michael@0 | 839 | } |
michael@0 | 840 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
michael@0 | 841 | return false; |
michael@0 | 842 | } |
michael@0 | 843 | |
michael@0 | 844 | setInjectionResultLocked(entry, injectionResult); |
michael@0 | 845 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
michael@0 | 846 | return true; |
michael@0 | 847 | } |
michael@0 | 848 | |
michael@0 | 849 | // TODO: support sending secondary display events to input monitors |
michael@0 | 850 | if (isMainDisplay(entry->displayId)) { |
michael@0 | 851 | addMonitoringTargetsLocked(inputTargets); |
michael@0 | 852 | } |
michael@0 | 853 | |
michael@0 | 854 | // Dispatch the motion. |
michael@0 | 855 | if (conflictingPointerActions) { |
michael@0 | 856 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
michael@0 | 857 | "conflicting pointer actions"); |
michael@0 | 858 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
michael@0 | 859 | } |
michael@0 | 860 | dispatchEventLocked(currentTime, entry, inputTargets); |
michael@0 | 861 | return true; |
michael@0 | 862 | } |
michael@0 | 863 | |
michael@0 | 864 | |
michael@0 | 865 | void InputDispatcher::logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry) { |
michael@0 | 866 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
michael@0 | 867 | ALOGD("%seventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, " |
michael@0 | 868 | "action=0x%x, flags=0x%x, " |
michael@0 | 869 | "metaState=0x%x, buttonState=0x%x, " |
michael@0 | 870 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%lld", |
michael@0 | 871 | prefix, |
michael@0 | 872 | entry->eventTime, entry->deviceId, entry->source, entry->policyFlags, |
michael@0 | 873 | entry->action, entry->flags, |
michael@0 | 874 | entry->metaState, entry->buttonState, |
michael@0 | 875 | entry->edgeFlags, entry->xPrecision, entry->yPrecision, |
michael@0 | 876 | entry->downTime); |
michael@0 | 877 | |
michael@0 | 878 | for (uint32_t i = 0; i < entry->pointerCount; i++) { |
michael@0 | 879 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
michael@0 | 880 | "x=%f, y=%f, pressure=%f, size=%f, " |
michael@0 | 881 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
michael@0 | 882 | "orientation=%f", |
michael@0 | 883 | i, entry->pointerProperties[i].id, |
michael@0 | 884 | entry->pointerProperties[i].toolType, |
michael@0 | 885 | entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
michael@0 | 886 | entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
michael@0 | 887 | entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
michael@0 | 888 | entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
michael@0 | 889 | entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
michael@0 | 890 | entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
michael@0 | 891 | entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
michael@0 | 892 | entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
michael@0 | 893 | entry->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
michael@0 | 894 | } |
michael@0 | 895 | #endif |
michael@0 | 896 | } |
michael@0 | 897 | |
michael@0 | 898 | void InputDispatcher::dispatchEventLocked(nsecs_t currentTime, |
michael@0 | 899 | EventEntry* eventEntry, const Vector<InputTarget>& inputTargets) { |
michael@0 | 900 | #if DEBUG_DISPATCH_CYCLE |
michael@0 | 901 | ALOGD("dispatchEventToCurrentInputTargets"); |
michael@0 | 902 | #endif |
michael@0 | 903 | |
michael@0 | 904 | ALOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
michael@0 | 905 | |
michael@0 | 906 | pokeUserActivityLocked(eventEntry); |
michael@0 | 907 | |
michael@0 | 908 | for (size_t i = 0; i < inputTargets.size(); i++) { |
michael@0 | 909 | const InputTarget& inputTarget = inputTargets.itemAt(i); |
michael@0 | 910 | |
michael@0 | 911 | ssize_t connectionIndex = getConnectionIndexLocked(inputTarget.inputChannel); |
michael@0 | 912 | if (connectionIndex >= 0) { |
michael@0 | 913 | sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex); |
michael@0 | 914 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, &inputTarget); |
michael@0 | 915 | } else { |
michael@0 | 916 | #if DEBUG_FOCUS |
michael@0 | 917 | ALOGD("Dropping event delivery to target with channel '%s' because it " |
michael@0 | 918 | "is no longer registered with the input dispatcher.", |
michael@0 | 919 | inputTarget.inputChannel->getName().string()); |
michael@0 | 920 | #endif |
michael@0 | 921 | } |
michael@0 | 922 | } |
michael@0 | 923 | } |
michael@0 | 924 | |
michael@0 | 925 | int32_t InputDispatcher::handleTargetsNotReadyLocked(nsecs_t currentTime, |
michael@0 | 926 | const EventEntry* entry, |
michael@0 | 927 | const sp<InputApplicationHandle>& applicationHandle, |
michael@0 | 928 | const sp<InputWindowHandle>& windowHandle, |
michael@0 | 929 | nsecs_t* nextWakeupTime, const char* reason) { |
michael@0 | 930 | if (applicationHandle == NULL && windowHandle == NULL) { |
michael@0 | 931 | if (mInputTargetWaitCause != INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY) { |
michael@0 | 932 | #if DEBUG_FOCUS |
michael@0 | 933 | ALOGD("Waiting for system to become ready for input. Reason: %s", reason); |
michael@0 | 934 | #endif |
michael@0 | 935 | mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY; |
michael@0 | 936 | mInputTargetWaitStartTime = currentTime; |
michael@0 | 937 | mInputTargetWaitTimeoutTime = LONG_LONG_MAX; |
michael@0 | 938 | mInputTargetWaitTimeoutExpired = false; |
michael@0 | 939 | mInputTargetWaitApplicationHandle.clear(); |
michael@0 | 940 | } |
michael@0 | 941 | } else { |
michael@0 | 942 | if (mInputTargetWaitCause != INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) { |
michael@0 | 943 | #if DEBUG_FOCUS |
michael@0 | 944 | ALOGD("Waiting for application to become ready for input: %s. Reason: %s", |
michael@0 | 945 | getApplicationWindowLabelLocked(applicationHandle, windowHandle).string(), |
michael@0 | 946 | reason); |
michael@0 | 947 | #endif |
michael@0 | 948 | nsecs_t timeout; |
michael@0 | 949 | if (windowHandle != NULL) { |
michael@0 | 950 | timeout = windowHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
michael@0 | 951 | } else if (applicationHandle != NULL) { |
michael@0 | 952 | timeout = applicationHandle->getDispatchingTimeout( |
michael@0 | 953 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
michael@0 | 954 | } else { |
michael@0 | 955 | timeout = DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
michael@0 | 956 | } |
michael@0 | 957 | |
michael@0 | 958 | mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY; |
michael@0 | 959 | mInputTargetWaitStartTime = currentTime; |
michael@0 | 960 | mInputTargetWaitTimeoutTime = currentTime + timeout; |
michael@0 | 961 | mInputTargetWaitTimeoutExpired = false; |
michael@0 | 962 | mInputTargetWaitApplicationHandle.clear(); |
michael@0 | 963 | |
michael@0 | 964 | if (windowHandle != NULL) { |
michael@0 | 965 | mInputTargetWaitApplicationHandle = windowHandle->inputApplicationHandle; |
michael@0 | 966 | } |
michael@0 | 967 | if (mInputTargetWaitApplicationHandle == NULL && applicationHandle != NULL) { |
michael@0 | 968 | mInputTargetWaitApplicationHandle = applicationHandle; |
michael@0 | 969 | } |
michael@0 | 970 | } |
michael@0 | 971 | } |
michael@0 | 972 | |
michael@0 | 973 | if (mInputTargetWaitTimeoutExpired) { |
michael@0 | 974 | return INPUT_EVENT_INJECTION_TIMED_OUT; |
michael@0 | 975 | } |
michael@0 | 976 | |
michael@0 | 977 | if (currentTime >= mInputTargetWaitTimeoutTime) { |
michael@0 | 978 | onANRLocked(currentTime, applicationHandle, windowHandle, |
michael@0 | 979 | entry->eventTime, mInputTargetWaitStartTime, reason); |
michael@0 | 980 | |
michael@0 | 981 | // Force poll loop to wake up immediately on next iteration once we get the |
michael@0 | 982 | // ANR response back from the policy. |
michael@0 | 983 | *nextWakeupTime = LONG_LONG_MIN; |
michael@0 | 984 | return INPUT_EVENT_INJECTION_PENDING; |
michael@0 | 985 | } else { |
michael@0 | 986 | // Force poll loop to wake up when timeout is due. |
michael@0 | 987 | if (mInputTargetWaitTimeoutTime < *nextWakeupTime) { |
michael@0 | 988 | *nextWakeupTime = mInputTargetWaitTimeoutTime; |
michael@0 | 989 | } |
michael@0 | 990 | return INPUT_EVENT_INJECTION_PENDING; |
michael@0 | 991 | } |
michael@0 | 992 | } |
michael@0 | 993 | |
michael@0 | 994 | void InputDispatcher::resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout, |
michael@0 | 995 | const sp<InputChannel>& inputChannel) { |
michael@0 | 996 | if (newTimeout > 0) { |
michael@0 | 997 | // Extend the timeout. |
michael@0 | 998 | mInputTargetWaitTimeoutTime = now() + newTimeout; |
michael@0 | 999 | } else { |
michael@0 | 1000 | // Give up. |
michael@0 | 1001 | mInputTargetWaitTimeoutExpired = true; |
michael@0 | 1002 | |
michael@0 | 1003 | // Input state will not be realistic. Mark it out of sync. |
michael@0 | 1004 | if (inputChannel.get()) { |
michael@0 | 1005 | ssize_t connectionIndex = getConnectionIndexLocked(inputChannel); |
michael@0 | 1006 | if (connectionIndex >= 0) { |
michael@0 | 1007 | sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex); |
michael@0 | 1008 | sp<InputWindowHandle> windowHandle = connection->inputWindowHandle; |
michael@0 | 1009 | |
michael@0 | 1010 | if (windowHandle != NULL) { |
michael@0 | 1011 | mTouchState.removeWindow(windowHandle); |
michael@0 | 1012 | } |
michael@0 | 1013 | |
michael@0 | 1014 | if (connection->status == Connection::STATUS_NORMAL) { |
michael@0 | 1015 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
michael@0 | 1016 | "application not responding"); |
michael@0 | 1017 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
michael@0 | 1018 | } |
michael@0 | 1019 | } |
michael@0 | 1020 | } |
michael@0 | 1021 | } |
michael@0 | 1022 | } |
michael@0 | 1023 | |
michael@0 | 1024 | nsecs_t InputDispatcher::getTimeSpentWaitingForApplicationLocked( |
michael@0 | 1025 | nsecs_t currentTime) { |
michael@0 | 1026 | if (mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) { |
michael@0 | 1027 | return currentTime - mInputTargetWaitStartTime; |
michael@0 | 1028 | } |
michael@0 | 1029 | return 0; |
michael@0 | 1030 | } |
michael@0 | 1031 | |
michael@0 | 1032 | void InputDispatcher::resetANRTimeoutsLocked() { |
michael@0 | 1033 | #if DEBUG_FOCUS |
michael@0 | 1034 | ALOGD("Resetting ANR timeouts."); |
michael@0 | 1035 | #endif |
michael@0 | 1036 | |
michael@0 | 1037 | // Reset input target wait timeout. |
michael@0 | 1038 | mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_NONE; |
michael@0 | 1039 | mInputTargetWaitApplicationHandle.clear(); |
michael@0 | 1040 | } |
michael@0 | 1041 | |
michael@0 | 1042 | int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime, |
michael@0 | 1043 | const EventEntry* entry, Vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime) { |
michael@0 | 1044 | int32_t injectionResult; |
michael@0 | 1045 | |
michael@0 | 1046 | // If there is no currently focused window and no focused application |
michael@0 | 1047 | // then drop the event. |
michael@0 | 1048 | if (mFocusedWindowHandle == NULL) { |
michael@0 | 1049 | if (mFocusedApplicationHandle != NULL) { |
michael@0 | 1050 | injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
michael@0 | 1051 | mFocusedApplicationHandle, NULL, nextWakeupTime, |
michael@0 | 1052 | "Waiting because no window has focus but there is a " |
michael@0 | 1053 | "focused application that may eventually add a window " |
michael@0 | 1054 | "when it finishes starting up."); |
michael@0 | 1055 | goto Unresponsive; |
michael@0 | 1056 | } |
michael@0 | 1057 | |
michael@0 | 1058 | ALOGI("Dropping event because there is no focused window or focused application."); |
michael@0 | 1059 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
michael@0 | 1060 | goto Failed; |
michael@0 | 1061 | } |
michael@0 | 1062 | |
michael@0 | 1063 | // Check permissions. |
michael@0 | 1064 | if (! checkInjectionPermission(mFocusedWindowHandle, entry->injectionState)) { |
michael@0 | 1065 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
michael@0 | 1066 | goto Failed; |
michael@0 | 1067 | } |
michael@0 | 1068 | |
michael@0 | 1069 | // If the currently focused window is paused then keep waiting. |
michael@0 | 1070 | if (mFocusedWindowHandle->getInfo()->paused) { |
michael@0 | 1071 | injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
michael@0 | 1072 | mFocusedApplicationHandle, mFocusedWindowHandle, nextWakeupTime, |
michael@0 | 1073 | "Waiting because the focused window is paused."); |
michael@0 | 1074 | goto Unresponsive; |
michael@0 | 1075 | } |
michael@0 | 1076 | |
michael@0 | 1077 | // If the currently focused window is still working on previous events then keep waiting. |
michael@0 | 1078 | if (!isWindowReadyForMoreInputLocked(currentTime, mFocusedWindowHandle, entry)) { |
michael@0 | 1079 | injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
michael@0 | 1080 | mFocusedApplicationHandle, mFocusedWindowHandle, nextWakeupTime, |
michael@0 | 1081 | "Waiting because the focused window has not finished " |
michael@0 | 1082 | "processing the input events that were previously delivered to it."); |
michael@0 | 1083 | goto Unresponsive; |
michael@0 | 1084 | } |
michael@0 | 1085 | |
michael@0 | 1086 | // Success! Output targets. |
michael@0 | 1087 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
michael@0 | 1088 | addWindowTargetLocked(mFocusedWindowHandle, |
michael@0 | 1089 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, BitSet32(0), |
michael@0 | 1090 | inputTargets); |
michael@0 | 1091 | |
michael@0 | 1092 | // Done. |
michael@0 | 1093 | Failed: |
michael@0 | 1094 | Unresponsive: |
michael@0 | 1095 | nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime); |
michael@0 | 1096 | updateDispatchStatisticsLocked(currentTime, entry, |
michael@0 | 1097 | injectionResult, timeSpentWaitingForApplication); |
michael@0 | 1098 | #if DEBUG_FOCUS |
michael@0 | 1099 | ALOGD("findFocusedWindow finished: injectionResult=%d, " |
michael@0 | 1100 | "timeSpentWaitingForApplication=%0.1fms", |
michael@0 | 1101 | injectionResult, timeSpentWaitingForApplication / 1000000.0); |
michael@0 | 1102 | #endif |
michael@0 | 1103 | return injectionResult; |
michael@0 | 1104 | } |
michael@0 | 1105 | |
michael@0 | 1106 | int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, |
michael@0 | 1107 | const MotionEntry* entry, Vector<InputTarget>& inputTargets, nsecs_t* nextWakeupTime, |
michael@0 | 1108 | bool* outConflictingPointerActions) { |
michael@0 | 1109 | enum InjectionPermission { |
michael@0 | 1110 | INJECTION_PERMISSION_UNKNOWN, |
michael@0 | 1111 | INJECTION_PERMISSION_GRANTED, |
michael@0 | 1112 | INJECTION_PERMISSION_DENIED |
michael@0 | 1113 | }; |
michael@0 | 1114 | |
michael@0 | 1115 | nsecs_t startTime = now(); |
michael@0 | 1116 | |
michael@0 | 1117 | // For security reasons, we defer updating the touch state until we are sure that |
michael@0 | 1118 | // event injection will be allowed. |
michael@0 | 1119 | // |
michael@0 | 1120 | // FIXME In the original code, screenWasOff could never be set to true. |
michael@0 | 1121 | // The reason is that the POLICY_FLAG_WOKE_HERE |
michael@0 | 1122 | // and POLICY_FLAG_BRIGHT_HERE flags were set only when preprocessing raw |
michael@0 | 1123 | // EV_KEY, EV_REL and EV_ABS events. As it happens, the touch event was |
michael@0 | 1124 | // actually enqueued using the policyFlags that appeared in the final EV_SYN |
michael@0 | 1125 | // events upon which no preprocessing took place. So policyFlags was always 0. |
michael@0 | 1126 | // In the new native input dispatcher we're a bit more careful about event |
michael@0 | 1127 | // preprocessing so the touches we receive can actually have non-zero policyFlags. |
michael@0 | 1128 | // Unfortunately we obtain undesirable behavior. |
michael@0 | 1129 | // |
michael@0 | 1130 | // Here's what happens: |
michael@0 | 1131 | // |
michael@0 | 1132 | // When the device dims in anticipation of going to sleep, touches |
michael@0 | 1133 | // in windows which have FLAG_TOUCHABLE_WHEN_WAKING cause |
michael@0 | 1134 | // the device to brighten and reset the user activity timer. |
michael@0 | 1135 | // Touches on other windows (such as the launcher window) |
michael@0 | 1136 | // are dropped. Then after a moment, the device goes to sleep. Oops. |
michael@0 | 1137 | // |
michael@0 | 1138 | // Also notice how screenWasOff was being initialized using POLICY_FLAG_BRIGHT_HERE |
michael@0 | 1139 | // instead of POLICY_FLAG_WOKE_HERE... |
michael@0 | 1140 | // |
michael@0 | 1141 | bool screenWasOff = false; // original policy: policyFlags & POLICY_FLAG_BRIGHT_HERE; |
michael@0 | 1142 | |
michael@0 | 1143 | int32_t displayId = entry->displayId; |
michael@0 | 1144 | int32_t action = entry->action; |
michael@0 | 1145 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
michael@0 | 1146 | |
michael@0 | 1147 | // Update the touch state as needed based on the properties of the touch event. |
michael@0 | 1148 | int32_t injectionResult = INPUT_EVENT_INJECTION_PENDING; |
michael@0 | 1149 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
michael@0 | 1150 | sp<InputWindowHandle> newHoverWindowHandle; |
michael@0 | 1151 | |
michael@0 | 1152 | bool isSplit = mTouchState.split; |
michael@0 | 1153 | bool switchedDevice = mTouchState.deviceId >= 0 && mTouchState.displayId >= 0 |
michael@0 | 1154 | && (mTouchState.deviceId != entry->deviceId |
michael@0 | 1155 | || mTouchState.source != entry->source |
michael@0 | 1156 | || mTouchState.displayId != displayId); |
michael@0 | 1157 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE |
michael@0 | 1158 | || maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER |
michael@0 | 1159 | || maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
michael@0 | 1160 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN |
michael@0 | 1161 | || maskedAction == AMOTION_EVENT_ACTION_SCROLL |
michael@0 | 1162 | || isHoverAction); |
michael@0 | 1163 | bool wrongDevice = false; |
michael@0 | 1164 | if (newGesture) { |
michael@0 | 1165 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
michael@0 | 1166 | if (switchedDevice && mTouchState.down && !down) { |
michael@0 | 1167 | #if DEBUG_FOCUS |
michael@0 | 1168 | ALOGD("Dropping event because a pointer for a different device is already down."); |
michael@0 | 1169 | #endif |
michael@0 | 1170 | mTempTouchState.copyFrom(mTouchState); |
michael@0 | 1171 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
michael@0 | 1172 | switchedDevice = false; |
michael@0 | 1173 | wrongDevice = true; |
michael@0 | 1174 | goto Failed; |
michael@0 | 1175 | } |
michael@0 | 1176 | mTempTouchState.reset(); |
michael@0 | 1177 | mTempTouchState.down = down; |
michael@0 | 1178 | mTempTouchState.deviceId = entry->deviceId; |
michael@0 | 1179 | mTempTouchState.source = entry->source; |
michael@0 | 1180 | mTempTouchState.displayId = displayId; |
michael@0 | 1181 | isSplit = false; |
michael@0 | 1182 | } else { |
michael@0 | 1183 | mTempTouchState.copyFrom(mTouchState); |
michael@0 | 1184 | } |
michael@0 | 1185 | |
michael@0 | 1186 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
michael@0 | 1187 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
michael@0 | 1188 | |
michael@0 | 1189 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
michael@0 | 1190 | int32_t x = int32_t(entry->pointerCoords[pointerIndex]. |
michael@0 | 1191 | getAxisValue(AMOTION_EVENT_AXIS_X)); |
michael@0 | 1192 | int32_t y = int32_t(entry->pointerCoords[pointerIndex]. |
michael@0 | 1193 | getAxisValue(AMOTION_EVENT_AXIS_Y)); |
michael@0 | 1194 | sp<InputWindowHandle> newTouchedWindowHandle; |
michael@0 | 1195 | sp<InputWindowHandle> topErrorWindowHandle; |
michael@0 | 1196 | bool isTouchModal = false; |
michael@0 | 1197 | |
michael@0 | 1198 | // Traverse windows from front to back to find touched window and outside targets. |
michael@0 | 1199 | size_t numWindows = mWindowHandles.size(); |
michael@0 | 1200 | for (size_t i = 0; i < numWindows; i++) { |
michael@0 | 1201 | sp<InputWindowHandle> windowHandle = mWindowHandles.itemAt(i); |
michael@0 | 1202 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
michael@0 | 1203 | if (windowInfo->displayId != displayId) { |
michael@0 | 1204 | continue; // wrong display |
michael@0 | 1205 | } |
michael@0 | 1206 | |
michael@0 | 1207 | int32_t flags = windowInfo->layoutParamsFlags; |
michael@0 | 1208 | if (flags & InputWindowInfo::FLAG_SYSTEM_ERROR) { |
michael@0 | 1209 | if (topErrorWindowHandle == NULL) { |
michael@0 | 1210 | topErrorWindowHandle = windowHandle; |
michael@0 | 1211 | } |
michael@0 | 1212 | } |
michael@0 | 1213 | |
michael@0 | 1214 | if (windowInfo->visible) { |
michael@0 | 1215 | if (! (flags & InputWindowInfo::FLAG_NOT_TOUCHABLE)) { |
michael@0 | 1216 | isTouchModal = (flags & (InputWindowInfo::FLAG_NOT_FOCUSABLE |
michael@0 | 1217 | | InputWindowInfo::FLAG_NOT_TOUCH_MODAL)) == 0; |
michael@0 | 1218 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
michael@0 | 1219 | if (! screenWasOff |
michael@0 | 1220 | || (flags & InputWindowInfo::FLAG_TOUCHABLE_WHEN_WAKING)) { |
michael@0 | 1221 | newTouchedWindowHandle = windowHandle; |
michael@0 | 1222 | } |
michael@0 | 1223 | break; // found touched window, exit window loop |
michael@0 | 1224 | } |
michael@0 | 1225 | } |
michael@0 | 1226 | |
michael@0 | 1227 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN |
michael@0 | 1228 | && (flags & InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH)) { |
michael@0 | 1229 | int32_t outsideTargetFlags = InputTarget::FLAG_DISPATCH_AS_OUTSIDE; |
michael@0 | 1230 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
michael@0 | 1231 | outsideTargetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
michael@0 | 1232 | } |
michael@0 | 1233 | |
michael@0 | 1234 | mTempTouchState.addOrUpdateWindow( |
michael@0 | 1235 | windowHandle, outsideTargetFlags, BitSet32(0)); |
michael@0 | 1236 | } |
michael@0 | 1237 | } |
michael@0 | 1238 | } |
michael@0 | 1239 | |
michael@0 | 1240 | // If there is an error window but it is not taking focus (typically because |
michael@0 | 1241 | // it is invisible) then wait for it. Any other focused window may in |
michael@0 | 1242 | // fact be in ANR state. |
michael@0 | 1243 | if (topErrorWindowHandle != NULL && newTouchedWindowHandle != topErrorWindowHandle) { |
michael@0 | 1244 | injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
michael@0 | 1245 | NULL, NULL, nextWakeupTime, |
michael@0 | 1246 | "Waiting because a system error window is about to be displayed."); |
michael@0 | 1247 | injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
michael@0 | 1248 | goto Unresponsive; |
michael@0 | 1249 | } |
michael@0 | 1250 | |
michael@0 | 1251 | // Figure out whether splitting will be allowed for this window. |
michael@0 | 1252 | if (newTouchedWindowHandle != NULL |
michael@0 | 1253 | && newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
michael@0 | 1254 | // New window supports splitting. |
michael@0 | 1255 | isSplit = true; |
michael@0 | 1256 | } else if (isSplit) { |
michael@0 | 1257 | // New window does not support splitting but we have already split events. |
michael@0 | 1258 | // Ignore the new window. |
michael@0 | 1259 | newTouchedWindowHandle = NULL; |
michael@0 | 1260 | } |
michael@0 | 1261 | |
michael@0 | 1262 | // Handle the case where we did not find a window. |
michael@0 | 1263 | if (newTouchedWindowHandle == NULL) { |
michael@0 | 1264 | // Try to assign the pointer to the first foreground window we find, if there is one. |
michael@0 | 1265 | newTouchedWindowHandle = mTempTouchState.getFirstForegroundWindowHandle(); |
michael@0 | 1266 | if (newTouchedWindowHandle == NULL) { |
michael@0 | 1267 | // There is no touched window. If this is an initial down event |
michael@0 | 1268 | // then wait for a window to appear that will handle the touch. This is |
michael@0 | 1269 | // to ensure that we report an ANR in the case where an application has started |
michael@0 | 1270 | // but not yet put up a window and the user is starting to get impatient. |
michael@0 | 1271 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN |
michael@0 | 1272 | && mFocusedApplicationHandle != NULL) { |
michael@0 | 1273 | injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
michael@0 | 1274 | mFocusedApplicationHandle, NULL, nextWakeupTime, |
michael@0 | 1275 | "Waiting because there is no touchable window that can " |
michael@0 | 1276 | "handle the event but there is focused application that may " |
michael@0 | 1277 | "eventually add a new window when it finishes starting up."); |
michael@0 | 1278 | goto Unresponsive; |
michael@0 | 1279 | } |
michael@0 | 1280 | |
michael@0 | 1281 | ALOGI("Dropping event because there is no touched window."); |
michael@0 | 1282 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
michael@0 | 1283 | goto Failed; |
michael@0 | 1284 | } |
michael@0 | 1285 | } |
michael@0 | 1286 | |
michael@0 | 1287 | // Set target flags. |
michael@0 | 1288 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
michael@0 | 1289 | if (isSplit) { |
michael@0 | 1290 | targetFlags |= InputTarget::FLAG_SPLIT; |
michael@0 | 1291 | } |
michael@0 | 1292 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
michael@0 | 1293 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
michael@0 | 1294 | } |
michael@0 | 1295 | |
michael@0 | 1296 | // Update hover state. |
michael@0 | 1297 | if (isHoverAction) { |
michael@0 | 1298 | newHoverWindowHandle = newTouchedWindowHandle; |
michael@0 | 1299 | } else if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) { |
michael@0 | 1300 | newHoverWindowHandle = mLastHoverWindowHandle; |
michael@0 | 1301 | } |
michael@0 | 1302 | |
michael@0 | 1303 | // Update the temporary touch state. |
michael@0 | 1304 | BitSet32 pointerIds; |
michael@0 | 1305 | if (isSplit) { |
michael@0 | 1306 | uint32_t pointerId = entry->pointerProperties[pointerIndex].id; |
michael@0 | 1307 | pointerIds.markBit(pointerId); |
michael@0 | 1308 | } |
michael@0 | 1309 | mTempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
michael@0 | 1310 | } else { |
michael@0 | 1311 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
michael@0 | 1312 | |
michael@0 | 1313 | // If the pointer is not currently down, then ignore the event. |
michael@0 | 1314 | if (! mTempTouchState.down) { |
michael@0 | 1315 | #if DEBUG_FOCUS |
michael@0 | 1316 | ALOGD("Dropping event because the pointer is not down or we previously " |
michael@0 | 1317 | "dropped the pointer down event."); |
michael@0 | 1318 | #endif |
michael@0 | 1319 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
michael@0 | 1320 | goto Failed; |
michael@0 | 1321 | } |
michael@0 | 1322 | |
michael@0 | 1323 | // Check whether touches should slip outside of the current foreground window. |
michael@0 | 1324 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE |
michael@0 | 1325 | && entry->pointerCount == 1 |
michael@0 | 1326 | && mTempTouchState.isSlippery()) { |
michael@0 | 1327 | int32_t x = int32_t(entry->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
michael@0 | 1328 | int32_t y = int32_t(entry->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
michael@0 | 1329 | |
michael@0 | 1330 | sp<InputWindowHandle> oldTouchedWindowHandle = |
michael@0 | 1331 | mTempTouchState.getFirstForegroundWindowHandle(); |
michael@0 | 1332 | sp<InputWindowHandle> newTouchedWindowHandle = |
michael@0 | 1333 | findTouchedWindowAtLocked(displayId, x, y); |
michael@0 | 1334 | if (oldTouchedWindowHandle != newTouchedWindowHandle |
michael@0 | 1335 | && newTouchedWindowHandle != NULL) { |
michael@0 | 1336 | #if DEBUG_FOCUS |
michael@0 | 1337 | ALOGD("Touch is slipping out of window %s into window %s.", |
michael@0 | 1338 | oldTouchedWindowHandle->getName().string(), |
michael@0 | 1339 | newTouchedWindowHandle->getName().string()); |
michael@0 | 1340 | #endif |
michael@0 | 1341 | // Make a slippery exit from the old window. |
michael@0 | 1342 | mTempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
michael@0 | 1343 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, BitSet32(0)); |
michael@0 | 1344 | |
michael@0 | 1345 | // Make a slippery entrance into the new window. |
michael@0 | 1346 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
michael@0 | 1347 | isSplit = true; |
michael@0 | 1348 | } |
michael@0 | 1349 | |
michael@0 | 1350 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND |
michael@0 | 1351 | | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
michael@0 | 1352 | if (isSplit) { |
michael@0 | 1353 | targetFlags |= InputTarget::FLAG_SPLIT; |
michael@0 | 1354 | } |
michael@0 | 1355 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
michael@0 | 1356 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
michael@0 | 1357 | } |
michael@0 | 1358 | |
michael@0 | 1359 | BitSet32 pointerIds; |
michael@0 | 1360 | if (isSplit) { |
michael@0 | 1361 | pointerIds.markBit(entry->pointerProperties[0].id); |
michael@0 | 1362 | } |
michael@0 | 1363 | mTempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
michael@0 | 1364 | } |
michael@0 | 1365 | } |
michael@0 | 1366 | } |
michael@0 | 1367 | |
michael@0 | 1368 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
michael@0 | 1369 | // Let the previous window know that the hover sequence is over. |
michael@0 | 1370 | if (mLastHoverWindowHandle != NULL) { |
michael@0 | 1371 | #if DEBUG_HOVER |
michael@0 | 1372 | ALOGD("Sending hover exit event to window %s.", |
michael@0 | 1373 | mLastHoverWindowHandle->getName().string()); |
michael@0 | 1374 | #endif |
michael@0 | 1375 | mTempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
michael@0 | 1376 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
michael@0 | 1377 | } |
michael@0 | 1378 | |
michael@0 | 1379 | // Let the new window know that the hover sequence is starting. |
michael@0 | 1380 | if (newHoverWindowHandle != NULL) { |
michael@0 | 1381 | #if DEBUG_HOVER |
michael@0 | 1382 | ALOGD("Sending hover enter event to window %s.", |
michael@0 | 1383 | newHoverWindowHandle->getName().string()); |
michael@0 | 1384 | #endif |
michael@0 | 1385 | mTempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
michael@0 | 1386 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, BitSet32(0)); |
michael@0 | 1387 | } |
michael@0 | 1388 | } |
michael@0 | 1389 | |
michael@0 | 1390 | // Check permission to inject into all touched foreground windows and ensure there |
michael@0 | 1391 | // is at least one touched foreground window. |
michael@0 | 1392 | { |
michael@0 | 1393 | bool haveForegroundWindow = false; |
michael@0 | 1394 | for (size_t i = 0; i < mTempTouchState.windows.size(); i++) { |
michael@0 | 1395 | const TouchedWindow& touchedWindow = mTempTouchState.windows[i]; |
michael@0 | 1396 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
michael@0 | 1397 | haveForegroundWindow = true; |
michael@0 | 1398 | if (! checkInjectionPermission(touchedWindow.windowHandle, |
michael@0 | 1399 | entry->injectionState)) { |
michael@0 | 1400 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
michael@0 | 1401 | injectionPermission = INJECTION_PERMISSION_DENIED; |
michael@0 | 1402 | goto Failed; |
michael@0 | 1403 | } |
michael@0 | 1404 | } |
michael@0 | 1405 | } |
michael@0 | 1406 | if (! haveForegroundWindow) { |
michael@0 | 1407 | #if DEBUG_FOCUS |
michael@0 | 1408 | ALOGD("Dropping event because there is no touched foreground window to receive it."); |
michael@0 | 1409 | #endif |
michael@0 | 1410 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
michael@0 | 1411 | goto Failed; |
michael@0 | 1412 | } |
michael@0 | 1413 | |
michael@0 | 1414 | // Permission granted to injection into all touched foreground windows. |
michael@0 | 1415 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
michael@0 | 1416 | } |
michael@0 | 1417 | |
michael@0 | 1418 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
michael@0 | 1419 | // set the policy flag that we will not reveal coordinate information to this window. |
michael@0 | 1420 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
michael@0 | 1421 | sp<InputWindowHandle> foregroundWindowHandle = |
michael@0 | 1422 | mTempTouchState.getFirstForegroundWindowHandle(); |
michael@0 | 1423 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
michael@0 | 1424 | for (size_t i = 0; i < mTempTouchState.windows.size(); i++) { |
michael@0 | 1425 | const TouchedWindow& touchedWindow = mTempTouchState.windows[i]; |
michael@0 | 1426 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
michael@0 | 1427 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; |
michael@0 | 1428 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { |
michael@0 | 1429 | mTempTouchState.addOrUpdateWindow(inputWindowHandle, |
michael@0 | 1430 | InputTarget::FLAG_ZERO_COORDS, BitSet32(0)); |
michael@0 | 1431 | } |
michael@0 | 1432 | } |
michael@0 | 1433 | } |
michael@0 | 1434 | } |
michael@0 | 1435 | |
michael@0 | 1436 | // Ensure all touched foreground windows are ready for new input. |
michael@0 | 1437 | for (size_t i = 0; i < mTempTouchState.windows.size(); i++) { |
michael@0 | 1438 | const TouchedWindow& touchedWindow = mTempTouchState.windows[i]; |
michael@0 | 1439 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
michael@0 | 1440 | // If the touched window is paused then keep waiting. |
michael@0 | 1441 | if (touchedWindow.windowHandle->getInfo()->paused) { |
michael@0 | 1442 | injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
michael@0 | 1443 | NULL, touchedWindow.windowHandle, nextWakeupTime, |
michael@0 | 1444 | "Waiting because the touched window is paused."); |
michael@0 | 1445 | goto Unresponsive; |
michael@0 | 1446 | } |
michael@0 | 1447 | |
michael@0 | 1448 | // If the touched window is still working on previous events then keep waiting. |
michael@0 | 1449 | if (!isWindowReadyForMoreInputLocked(currentTime, touchedWindow.windowHandle, entry)) { |
michael@0 | 1450 | injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
michael@0 | 1451 | NULL, touchedWindow.windowHandle, nextWakeupTime, |
michael@0 | 1452 | "Waiting because the touched window has not finished " |
michael@0 | 1453 | "processing the input events that were previously delivered to it."); |
michael@0 | 1454 | goto Unresponsive; |
michael@0 | 1455 | } |
michael@0 | 1456 | } |
michael@0 | 1457 | } |
michael@0 | 1458 | |
michael@0 | 1459 | // If this is the first pointer going down and the touched window has a wallpaper |
michael@0 | 1460 | // then also add the touched wallpaper windows so they are locked in for the duration |
michael@0 | 1461 | // of the touch gesture. |
michael@0 | 1462 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
michael@0 | 1463 | // engine only supports touch events. We would need to add a mechanism similar |
michael@0 | 1464 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
michael@0 | 1465 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
michael@0 | 1466 | sp<InputWindowHandle> foregroundWindowHandle = |
michael@0 | 1467 | mTempTouchState.getFirstForegroundWindowHandle(); |
michael@0 | 1468 | if (foregroundWindowHandle->getInfo()->hasWallpaper) { |
michael@0 | 1469 | for (size_t i = 0; i < mWindowHandles.size(); i++) { |
michael@0 | 1470 | sp<InputWindowHandle> windowHandle = mWindowHandles.itemAt(i); |
michael@0 | 1471 | const InputWindowInfo* info = windowHandle->getInfo(); |
michael@0 | 1472 | if (info->displayId == displayId |
michael@0 | 1473 | && windowHandle->getInfo()->layoutParamsType |
michael@0 | 1474 | == InputWindowInfo::TYPE_WALLPAPER) { |
michael@0 | 1475 | mTempTouchState.addOrUpdateWindow(windowHandle, |
michael@0 | 1476 | InputTarget::FLAG_WINDOW_IS_OBSCURED |
michael@0 | 1477 | | InputTarget::FLAG_DISPATCH_AS_IS, |
michael@0 | 1478 | BitSet32(0)); |
michael@0 | 1479 | } |
michael@0 | 1480 | } |
michael@0 | 1481 | } |
michael@0 | 1482 | } |
michael@0 | 1483 | |
michael@0 | 1484 | // Success! Output targets. |
michael@0 | 1485 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
michael@0 | 1486 | |
michael@0 | 1487 | for (size_t i = 0; i < mTempTouchState.windows.size(); i++) { |
michael@0 | 1488 | const TouchedWindow& touchedWindow = mTempTouchState.windows.itemAt(i); |
michael@0 | 1489 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
michael@0 | 1490 | touchedWindow.pointerIds, inputTargets); |
michael@0 | 1491 | } |
michael@0 | 1492 | |
michael@0 | 1493 | // Drop the outside or hover touch windows since we will not care about them |
michael@0 | 1494 | // in the next iteration. |
michael@0 | 1495 | mTempTouchState.filterNonAsIsTouchWindows(); |
michael@0 | 1496 | |
michael@0 | 1497 | Failed: |
michael@0 | 1498 | // Check injection permission once and for all. |
michael@0 | 1499 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
michael@0 | 1500 | if (checkInjectionPermission(NULL, entry->injectionState)) { |
michael@0 | 1501 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
michael@0 | 1502 | } else { |
michael@0 | 1503 | injectionPermission = INJECTION_PERMISSION_DENIED; |
michael@0 | 1504 | } |
michael@0 | 1505 | } |
michael@0 | 1506 | |
michael@0 | 1507 | // Update final pieces of touch state if the injector had permission. |
michael@0 | 1508 | if (injectionPermission == INJECTION_PERMISSION_GRANTED) { |
michael@0 | 1509 | if (!wrongDevice) { |
michael@0 | 1510 | if (switchedDevice) { |
michael@0 | 1511 | #if DEBUG_FOCUS |
michael@0 | 1512 | ALOGD("Conflicting pointer actions: Switched to a different device."); |
michael@0 | 1513 | #endif |
michael@0 | 1514 | *outConflictingPointerActions = true; |
michael@0 | 1515 | } |
michael@0 | 1516 | |
michael@0 | 1517 | if (isHoverAction) { |
michael@0 | 1518 | // Started hovering, therefore no longer down. |
michael@0 | 1519 | if (mTouchState.down) { |
michael@0 | 1520 | #if DEBUG_FOCUS |
michael@0 | 1521 | ALOGD("Conflicting pointer actions: Hover received while pointer was down."); |
michael@0 | 1522 | #endif |
michael@0 | 1523 | *outConflictingPointerActions = true; |
michael@0 | 1524 | } |
michael@0 | 1525 | mTouchState.reset(); |
michael@0 | 1526 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER |
michael@0 | 1527 | || maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
michael@0 | 1528 | mTouchState.deviceId = entry->deviceId; |
michael@0 | 1529 | mTouchState.source = entry->source; |
michael@0 | 1530 | mTouchState.displayId = displayId; |
michael@0 | 1531 | } |
michael@0 | 1532 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP |
michael@0 | 1533 | || maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
michael@0 | 1534 | // All pointers up or canceled. |
michael@0 | 1535 | mTouchState.reset(); |
michael@0 | 1536 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
michael@0 | 1537 | // First pointer went down. |
michael@0 | 1538 | if (mTouchState.down) { |
michael@0 | 1539 | #if DEBUG_FOCUS |
michael@0 | 1540 | ALOGD("Conflicting pointer actions: Down received while already down."); |
michael@0 | 1541 | #endif |
michael@0 | 1542 | *outConflictingPointerActions = true; |
michael@0 | 1543 | } |
michael@0 | 1544 | mTouchState.copyFrom(mTempTouchState); |
michael@0 | 1545 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
michael@0 | 1546 | // One pointer went up. |
michael@0 | 1547 | if (isSplit) { |
michael@0 | 1548 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
michael@0 | 1549 | uint32_t pointerId = entry->pointerProperties[pointerIndex].id; |
michael@0 | 1550 | |
michael@0 | 1551 | for (size_t i = 0; i < mTempTouchState.windows.size(); ) { |
michael@0 | 1552 | TouchedWindow& touchedWindow = mTempTouchState.windows.editItemAt(i); |
michael@0 | 1553 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
michael@0 | 1554 | touchedWindow.pointerIds.clearBit(pointerId); |
michael@0 | 1555 | if (touchedWindow.pointerIds.isEmpty()) { |
michael@0 | 1556 | mTempTouchState.windows.removeAt(i); |
michael@0 | 1557 | continue; |
michael@0 | 1558 | } |
michael@0 | 1559 | } |
michael@0 | 1560 | i += 1; |
michael@0 | 1561 | } |
michael@0 | 1562 | } |
michael@0 | 1563 | mTouchState.copyFrom(mTempTouchState); |
michael@0 | 1564 | } else if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) { |
michael@0 | 1565 | // Discard temporary touch state since it was only valid for this action. |
michael@0 | 1566 | } else { |
michael@0 | 1567 | // Save changes to touch state as-is for all other actions. |
michael@0 | 1568 | mTouchState.copyFrom(mTempTouchState); |
michael@0 | 1569 | } |
michael@0 | 1570 | |
michael@0 | 1571 | // Update hover state. |
michael@0 | 1572 | mLastHoverWindowHandle = newHoverWindowHandle; |
michael@0 | 1573 | } |
michael@0 | 1574 | } else { |
michael@0 | 1575 | #if DEBUG_FOCUS |
michael@0 | 1576 | ALOGD("Not updating touch focus because injection was denied."); |
michael@0 | 1577 | #endif |
michael@0 | 1578 | } |
michael@0 | 1579 | |
michael@0 | 1580 | Unresponsive: |
michael@0 | 1581 | // Reset temporary touch state to ensure we release unnecessary references to input channels. |
michael@0 | 1582 | mTempTouchState.reset(); |
michael@0 | 1583 | |
michael@0 | 1584 | nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime); |
michael@0 | 1585 | updateDispatchStatisticsLocked(currentTime, entry, |
michael@0 | 1586 | injectionResult, timeSpentWaitingForApplication); |
michael@0 | 1587 | #if DEBUG_FOCUS |
michael@0 | 1588 | ALOGD("findTouchedWindow finished: injectionResult=%d, injectionPermission=%d, " |
michael@0 | 1589 | "timeSpentWaitingForApplication=%0.1fms", |
michael@0 | 1590 | injectionResult, injectionPermission, timeSpentWaitingForApplication / 1000000.0); |
michael@0 | 1591 | #endif |
michael@0 | 1592 | return injectionResult; |
michael@0 | 1593 | } |
michael@0 | 1594 | |
michael@0 | 1595 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, |
michael@0 | 1596 | int32_t targetFlags, BitSet32 pointerIds, Vector<InputTarget>& inputTargets) { |
michael@0 | 1597 | inputTargets.push(); |
michael@0 | 1598 | |
michael@0 | 1599 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
michael@0 | 1600 | InputTarget& target = inputTargets.editTop(); |
michael@0 | 1601 | target.inputChannel = windowInfo->inputChannel; |
michael@0 | 1602 | target.flags = targetFlags; |
michael@0 | 1603 | target.xOffset = - windowInfo->frameLeft; |
michael@0 | 1604 | target.yOffset = - windowInfo->frameTop; |
michael@0 | 1605 | target.scaleFactor = windowInfo->scaleFactor; |
michael@0 | 1606 | target.pointerIds = pointerIds; |
michael@0 | 1607 | } |
michael@0 | 1608 | |
michael@0 | 1609 | void InputDispatcher::addMonitoringTargetsLocked(Vector<InputTarget>& inputTargets) { |
michael@0 | 1610 | for (size_t i = 0; i < mMonitoringChannels.size(); i++) { |
michael@0 | 1611 | inputTargets.push(); |
michael@0 | 1612 | |
michael@0 | 1613 | InputTarget& target = inputTargets.editTop(); |
michael@0 | 1614 | target.inputChannel = mMonitoringChannels[i]; |
michael@0 | 1615 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
michael@0 | 1616 | target.xOffset = 0; |
michael@0 | 1617 | target.yOffset = 0; |
michael@0 | 1618 | target.pointerIds.clear(); |
michael@0 | 1619 | target.scaleFactor = 1.0f; |
michael@0 | 1620 | } |
michael@0 | 1621 | } |
michael@0 | 1622 | |
michael@0 | 1623 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, |
michael@0 | 1624 | const InjectionState* injectionState) { |
michael@0 | 1625 | if (injectionState |
michael@0 | 1626 | && (windowHandle == NULL |
michael@0 | 1627 | || windowHandle->getInfo()->ownerUid != injectionState->injectorUid) |
michael@0 | 1628 | && !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
michael@0 | 1629 | if (windowHandle != NULL) { |
michael@0 | 1630 | ALOGW("Permission denied: injecting event from pid %d uid %d to window %s " |
michael@0 | 1631 | "owned by uid %d", |
michael@0 | 1632 | injectionState->injectorPid, injectionState->injectorUid, |
michael@0 | 1633 | windowHandle->getName().string(), |
michael@0 | 1634 | windowHandle->getInfo()->ownerUid); |
michael@0 | 1635 | } else { |
michael@0 | 1636 | ALOGW("Permission denied: injecting event from pid %d uid %d", |
michael@0 | 1637 | injectionState->injectorPid, injectionState->injectorUid); |
michael@0 | 1638 | } |
michael@0 | 1639 | return false; |
michael@0 | 1640 | } |
michael@0 | 1641 | return true; |
michael@0 | 1642 | } |
michael@0 | 1643 | |
michael@0 | 1644 | bool InputDispatcher::isWindowObscuredAtPointLocked( |
michael@0 | 1645 | const sp<InputWindowHandle>& windowHandle, int32_t x, int32_t y) const { |
michael@0 | 1646 | int32_t displayId = windowHandle->getInfo()->displayId; |
michael@0 | 1647 | size_t numWindows = mWindowHandles.size(); |
michael@0 | 1648 | for (size_t i = 0; i < numWindows; i++) { |
michael@0 | 1649 | sp<InputWindowHandle> otherHandle = mWindowHandles.itemAt(i); |
michael@0 | 1650 | if (otherHandle == windowHandle) { |
michael@0 | 1651 | break; |
michael@0 | 1652 | } |
michael@0 | 1653 | |
michael@0 | 1654 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
michael@0 | 1655 | if (otherInfo->displayId == displayId |
michael@0 | 1656 | && otherInfo->visible && !otherInfo->isTrustedOverlay() |
michael@0 | 1657 | && otherInfo->frameContainsPoint(x, y)) { |
michael@0 | 1658 | return true; |
michael@0 | 1659 | } |
michael@0 | 1660 | } |
michael@0 | 1661 | return false; |
michael@0 | 1662 | } |
michael@0 | 1663 | |
michael@0 | 1664 | bool InputDispatcher::isWindowReadyForMoreInputLocked(nsecs_t currentTime, |
michael@0 | 1665 | const sp<InputWindowHandle>& windowHandle, const EventEntry* eventEntry) { |
michael@0 | 1666 | ssize_t connectionIndex = getConnectionIndexLocked(windowHandle->getInputChannel()); |
michael@0 | 1667 | if (connectionIndex >= 0) { |
michael@0 | 1668 | sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex); |
michael@0 | 1669 | if (connection->inputPublisherBlocked) { |
michael@0 | 1670 | return false; |
michael@0 | 1671 | } |
michael@0 | 1672 | if (eventEntry->type == EventEntry::TYPE_KEY) { |
michael@0 | 1673 | // If the event is a key event, then we must wait for all previous events to |
michael@0 | 1674 | // complete before delivering it because previous events may have the |
michael@0 | 1675 | // side-effect of transferring focus to a different window and we want to |
michael@0 | 1676 | // ensure that the following keys are sent to the new window. |
michael@0 | 1677 | // |
michael@0 | 1678 | // Suppose the user touches a button in a window then immediately presses "A". |
michael@0 | 1679 | // If the button causes a pop-up window to appear then we want to ensure that |
michael@0 | 1680 | // the "A" key is delivered to the new pop-up window. This is because users |
michael@0 | 1681 | // often anticipate pending UI changes when typing on a keyboard. |
michael@0 | 1682 | // To obtain this behavior, we must serialize key events with respect to all |
michael@0 | 1683 | // prior input events. |
michael@0 | 1684 | return connection->outboundQueue.isEmpty() |
michael@0 | 1685 | && connection->waitQueue.isEmpty(); |
michael@0 | 1686 | } |
michael@0 | 1687 | // Touch events can always be sent to a window immediately because the user intended |
michael@0 | 1688 | // to touch whatever was visible at the time. Even if focus changes or a new |
michael@0 | 1689 | // window appears moments later, the touch event was meant to be delivered to |
michael@0 | 1690 | // whatever window happened to be on screen at the time. |
michael@0 | 1691 | // |
michael@0 | 1692 | // Generic motion events, such as trackball or joystick events are a little trickier. |
michael@0 | 1693 | // Like key events, generic motion events are delivered to the focused window. |
michael@0 | 1694 | // Unlike key events, generic motion events don't tend to transfer focus to other |
michael@0 | 1695 | // windows and it is not important for them to be serialized. So we prefer to deliver |
michael@0 | 1696 | // generic motion events as soon as possible to improve efficiency and reduce lag |
michael@0 | 1697 | // through batching. |
michael@0 | 1698 | // |
michael@0 | 1699 | // The one case where we pause input event delivery is when the wait queue is piling |
michael@0 | 1700 | // up with lots of events because the application is not responding. |
michael@0 | 1701 | // This condition ensures that ANRs are detected reliably. |
michael@0 | 1702 | if (!connection->waitQueue.isEmpty() |
michael@0 | 1703 | && currentTime >= connection->waitQueue.head->eventEntry->eventTime |
michael@0 | 1704 | + STREAM_AHEAD_EVENT_TIMEOUT) { |
michael@0 | 1705 | return false; |
michael@0 | 1706 | } |
michael@0 | 1707 | } |
michael@0 | 1708 | return true; |
michael@0 | 1709 | } |
michael@0 | 1710 | |
michael@0 | 1711 | String8 InputDispatcher::getApplicationWindowLabelLocked( |
michael@0 | 1712 | const sp<InputApplicationHandle>& applicationHandle, |
michael@0 | 1713 | const sp<InputWindowHandle>& windowHandle) { |
michael@0 | 1714 | if (applicationHandle != NULL) { |
michael@0 | 1715 | if (windowHandle != NULL) { |
michael@0 | 1716 | String8 label(applicationHandle->getName()); |
michael@0 | 1717 | label.append(" - "); |
michael@0 | 1718 | label.append(windowHandle->getName()); |
michael@0 | 1719 | return label; |
michael@0 | 1720 | } else { |
michael@0 | 1721 | return applicationHandle->getName(); |
michael@0 | 1722 | } |
michael@0 | 1723 | } else if (windowHandle != NULL) { |
michael@0 | 1724 | return windowHandle->getName(); |
michael@0 | 1725 | } else { |
michael@0 | 1726 | return String8("<unknown application or window>"); |
michael@0 | 1727 | } |
michael@0 | 1728 | } |
michael@0 | 1729 | |
michael@0 | 1730 | void InputDispatcher::pokeUserActivityLocked(const EventEntry* eventEntry) { |
michael@0 | 1731 | if (mFocusedWindowHandle != NULL) { |
michael@0 | 1732 | const InputWindowInfo* info = mFocusedWindowHandle->getInfo(); |
michael@0 | 1733 | if (info->inputFeatures & InputWindowInfo::INPUT_FEATURE_DISABLE_USER_ACTIVITY) { |
michael@0 | 1734 | #if DEBUG_DISPATCH_CYCLE |
michael@0 | 1735 | ALOGD("Not poking user activity: disabled by window '%s'.", info->name.string()); |
michael@0 | 1736 | #endif |
michael@0 | 1737 | return; |
michael@0 | 1738 | } |
michael@0 | 1739 | } |
michael@0 | 1740 | |
michael@0 | 1741 | int32_t eventType = USER_ACTIVITY_EVENT_OTHER; |
michael@0 | 1742 | switch (eventEntry->type) { |
michael@0 | 1743 | case EventEntry::TYPE_MOTION: { |
michael@0 | 1744 | const MotionEntry* motionEntry = static_cast<const MotionEntry*>(eventEntry); |
michael@0 | 1745 | if (motionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
michael@0 | 1746 | return; |
michael@0 | 1747 | } |
michael@0 | 1748 | |
michael@0 | 1749 | if (MotionEvent::isTouchEvent(motionEntry->source, motionEntry->action)) { |
michael@0 | 1750 | eventType = USER_ACTIVITY_EVENT_TOUCH; |
michael@0 | 1751 | } |
michael@0 | 1752 | break; |
michael@0 | 1753 | } |
michael@0 | 1754 | case EventEntry::TYPE_KEY: { |
michael@0 | 1755 | const KeyEntry* keyEntry = static_cast<const KeyEntry*>(eventEntry); |
michael@0 | 1756 | if (keyEntry->flags & AKEY_EVENT_FLAG_CANCELED) { |
michael@0 | 1757 | return; |
michael@0 | 1758 | } |
michael@0 | 1759 | eventType = USER_ACTIVITY_EVENT_BUTTON; |
michael@0 | 1760 | break; |
michael@0 | 1761 | } |
michael@0 | 1762 | } |
michael@0 | 1763 | |
michael@0 | 1764 | CommandEntry* commandEntry = postCommandLocked( |
michael@0 | 1765 | & InputDispatcher::doPokeUserActivityLockedInterruptible); |
michael@0 | 1766 | commandEntry->eventTime = eventEntry->eventTime; |
michael@0 | 1767 | commandEntry->userActivityEventType = eventType; |
michael@0 | 1768 | } |
michael@0 | 1769 | |
michael@0 | 1770 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
michael@0 | 1771 | const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget) { |
michael@0 | 1772 | #if DEBUG_DISPATCH_CYCLE |
michael@0 | 1773 | ALOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
michael@0 | 1774 | "xOffset=%f, yOffset=%f, scaleFactor=%f, " |
michael@0 | 1775 | "pointerIds=0x%x", |
michael@0 | 1776 | connection->getInputChannelName(), inputTarget->flags, |
michael@0 | 1777 | inputTarget->xOffset, inputTarget->yOffset, |
michael@0 | 1778 | inputTarget->scaleFactor, inputTarget->pointerIds.value); |
michael@0 | 1779 | #endif |
michael@0 | 1780 | |
michael@0 | 1781 | // Skip this event if the connection status is not normal. |
michael@0 | 1782 | // We don't want to enqueue additional outbound events if the connection is broken. |
michael@0 | 1783 | if (connection->status != Connection::STATUS_NORMAL) { |
michael@0 | 1784 | #if DEBUG_DISPATCH_CYCLE |
michael@0 | 1785 | ALOGD("channel '%s' ~ Dropping event because the channel status is %s", |
michael@0 | 1786 | connection->getInputChannelName(), connection->getStatusLabel()); |
michael@0 | 1787 | #endif |
michael@0 | 1788 | return; |
michael@0 | 1789 | } |
michael@0 | 1790 | |
michael@0 | 1791 | // Split a motion event if needed. |
michael@0 | 1792 | if (inputTarget->flags & InputTarget::FLAG_SPLIT) { |
michael@0 | 1793 | ALOG_ASSERT(eventEntry->type == EventEntry::TYPE_MOTION); |
michael@0 | 1794 | |
michael@0 | 1795 | MotionEntry* originalMotionEntry = static_cast<MotionEntry*>(eventEntry); |
michael@0 | 1796 | if (inputTarget->pointerIds.count() != originalMotionEntry->pointerCount) { |
michael@0 | 1797 | MotionEntry* splitMotionEntry = splitMotionEvent( |
michael@0 | 1798 | originalMotionEntry, inputTarget->pointerIds); |
michael@0 | 1799 | if (!splitMotionEntry) { |
michael@0 | 1800 | return; // split event was dropped |
michael@0 | 1801 | } |
michael@0 | 1802 | #if DEBUG_FOCUS |
michael@0 | 1803 | ALOGD("channel '%s' ~ Split motion event.", |
michael@0 | 1804 | connection->getInputChannelName()); |
michael@0 | 1805 | logOutboundMotionDetailsLocked(" ", splitMotionEntry); |
michael@0 | 1806 | #endif |
michael@0 | 1807 | enqueueDispatchEntriesLocked(currentTime, connection, |
michael@0 | 1808 | splitMotionEntry, inputTarget); |
michael@0 | 1809 | splitMotionEntry->release(); |
michael@0 | 1810 | return; |
michael@0 | 1811 | } |
michael@0 | 1812 | } |
michael@0 | 1813 | |
michael@0 | 1814 | // Not splitting. Enqueue dispatch entries for the event as is. |
michael@0 | 1815 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget); |
michael@0 | 1816 | } |
michael@0 | 1817 | |
michael@0 | 1818 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
michael@0 | 1819 | const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget) { |
michael@0 | 1820 | bool wasEmpty = connection->outboundQueue.isEmpty(); |
michael@0 | 1821 | |
michael@0 | 1822 | // Enqueue dispatch entries for the requested modes. |
michael@0 | 1823 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
michael@0 | 1824 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
michael@0 | 1825 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
michael@0 | 1826 | InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
michael@0 | 1827 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
michael@0 | 1828 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
michael@0 | 1829 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
michael@0 | 1830 | InputTarget::FLAG_DISPATCH_AS_IS); |
michael@0 | 1831 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
michael@0 | 1832 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
michael@0 | 1833 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
michael@0 | 1834 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
michael@0 | 1835 | |
michael@0 | 1836 | // If the outbound queue was previously empty, start the dispatch cycle going. |
michael@0 | 1837 | if (wasEmpty && !connection->outboundQueue.isEmpty()) { |
michael@0 | 1838 | startDispatchCycleLocked(currentTime, connection); |
michael@0 | 1839 | } |
michael@0 | 1840 | } |
michael@0 | 1841 | |
michael@0 | 1842 | void InputDispatcher::enqueueDispatchEntryLocked( |
michael@0 | 1843 | const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget, |
michael@0 | 1844 | int32_t dispatchMode) { |
michael@0 | 1845 | int32_t inputTargetFlags = inputTarget->flags; |
michael@0 | 1846 | if (!(inputTargetFlags & dispatchMode)) { |
michael@0 | 1847 | return; |
michael@0 | 1848 | } |
michael@0 | 1849 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
michael@0 | 1850 | |
michael@0 | 1851 | // This is a new event. |
michael@0 | 1852 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
michael@0 | 1853 | DispatchEntry* dispatchEntry = new DispatchEntry(eventEntry, // increments ref |
michael@0 | 1854 | inputTargetFlags, inputTarget->xOffset, inputTarget->yOffset, |
michael@0 | 1855 | inputTarget->scaleFactor); |
michael@0 | 1856 | |
michael@0 | 1857 | // Apply target flags and update the connection's input state. |
michael@0 | 1858 | switch (eventEntry->type) { |
michael@0 | 1859 | case EventEntry::TYPE_KEY: { |
michael@0 | 1860 | KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); |
michael@0 | 1861 | dispatchEntry->resolvedAction = keyEntry->action; |
michael@0 | 1862 | dispatchEntry->resolvedFlags = keyEntry->flags; |
michael@0 | 1863 | |
michael@0 | 1864 | if (!connection->inputState.trackKey(keyEntry, |
michael@0 | 1865 | dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags)) { |
michael@0 | 1866 | #if DEBUG_DISPATCH_CYCLE |
michael@0 | 1867 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", |
michael@0 | 1868 | connection->getInputChannelName()); |
michael@0 | 1869 | #endif |
michael@0 | 1870 | delete dispatchEntry; |
michael@0 | 1871 | return; // skip the inconsistent event |
michael@0 | 1872 | } |
michael@0 | 1873 | break; |
michael@0 | 1874 | } |
michael@0 | 1875 | |
michael@0 | 1876 | case EventEntry::TYPE_MOTION: { |
michael@0 | 1877 | MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); |
michael@0 | 1878 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
michael@0 | 1879 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
michael@0 | 1880 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
michael@0 | 1881 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
michael@0 | 1882 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
michael@0 | 1883 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
michael@0 | 1884 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
michael@0 | 1885 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
michael@0 | 1886 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
michael@0 | 1887 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
michael@0 | 1888 | } else { |
michael@0 | 1889 | dispatchEntry->resolvedAction = motionEntry->action; |
michael@0 | 1890 | } |
michael@0 | 1891 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE |
michael@0 | 1892 | && !connection->inputState.isHovering( |
michael@0 | 1893 | motionEntry->deviceId, motionEntry->source, motionEntry->displayId)) { |
michael@0 | 1894 | #if DEBUG_DISPATCH_CYCLE |
michael@0 | 1895 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter event", |
michael@0 | 1896 | connection->getInputChannelName()); |
michael@0 | 1897 | #endif |
michael@0 | 1898 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
michael@0 | 1899 | } |
michael@0 | 1900 | |
michael@0 | 1901 | dispatchEntry->resolvedFlags = motionEntry->flags; |
michael@0 | 1902 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
michael@0 | 1903 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
michael@0 | 1904 | } |
michael@0 | 1905 | |
michael@0 | 1906 | if (!connection->inputState.trackMotion(motionEntry, |
michael@0 | 1907 | dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags)) { |
michael@0 | 1908 | #if DEBUG_DISPATCH_CYCLE |
michael@0 | 1909 | ALOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion event", |
michael@0 | 1910 | connection->getInputChannelName()); |
michael@0 | 1911 | #endif |
michael@0 | 1912 | delete dispatchEntry; |
michael@0 | 1913 | return; // skip the inconsistent event |
michael@0 | 1914 | } |
michael@0 | 1915 | break; |
michael@0 | 1916 | } |
michael@0 | 1917 | } |
michael@0 | 1918 | |
michael@0 | 1919 | // Remember that we are waiting for this dispatch to complete. |
michael@0 | 1920 | if (dispatchEntry->hasForegroundTarget()) { |
michael@0 | 1921 | incrementPendingForegroundDispatchesLocked(eventEntry); |
michael@0 | 1922 | } |
michael@0 | 1923 | |
michael@0 | 1924 | // Enqueue the dispatch entry. |
michael@0 | 1925 | connection->outboundQueue.enqueueAtTail(dispatchEntry); |
michael@0 | 1926 | traceOutboundQueueLengthLocked(connection); |
michael@0 | 1927 | } |
michael@0 | 1928 | |
michael@0 | 1929 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
michael@0 | 1930 | const sp<Connection>& connection) { |
michael@0 | 1931 | #if DEBUG_DISPATCH_CYCLE |
michael@0 | 1932 | ALOGD("channel '%s' ~ startDispatchCycle", |
michael@0 | 1933 | connection->getInputChannelName()); |
michael@0 | 1934 | #endif |
michael@0 | 1935 | |
michael@0 | 1936 | while (connection->status == Connection::STATUS_NORMAL |
michael@0 | 1937 | && !connection->outboundQueue.isEmpty()) { |
michael@0 | 1938 | DispatchEntry* dispatchEntry = connection->outboundQueue.head; |
michael@0 | 1939 | dispatchEntry->deliveryTime = currentTime; |
michael@0 | 1940 | |
michael@0 | 1941 | // Publish the event. |
michael@0 | 1942 | status_t status; |
michael@0 | 1943 | EventEntry* eventEntry = dispatchEntry->eventEntry; |
michael@0 | 1944 | switch (eventEntry->type) { |
michael@0 | 1945 | case EventEntry::TYPE_KEY: { |
michael@0 | 1946 | KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); |
michael@0 | 1947 | |
michael@0 | 1948 | // Publish the key event. |
michael@0 | 1949 | status = connection->inputPublisher.publishKeyEvent(dispatchEntry->seq, |
michael@0 | 1950 | keyEntry->deviceId, keyEntry->source, |
michael@0 | 1951 | dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags, |
michael@0 | 1952 | keyEntry->keyCode, keyEntry->scanCode, |
michael@0 | 1953 | keyEntry->metaState, keyEntry->repeatCount, keyEntry->downTime, |
michael@0 | 1954 | keyEntry->eventTime); |
michael@0 | 1955 | break; |
michael@0 | 1956 | } |
michael@0 | 1957 | |
michael@0 | 1958 | case EventEntry::TYPE_MOTION: { |
michael@0 | 1959 | MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); |
michael@0 | 1960 | |
michael@0 | 1961 | PointerCoords scaledCoords[MAX_POINTERS]; |
michael@0 | 1962 | const PointerCoords* usingCoords = motionEntry->pointerCoords; |
michael@0 | 1963 | |
michael@0 | 1964 | // Set the X and Y offset depending on the input source. |
michael@0 | 1965 | float xOffset, yOffset, scaleFactor; |
michael@0 | 1966 | if ((motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) |
michael@0 | 1967 | && !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
michael@0 | 1968 | scaleFactor = dispatchEntry->scaleFactor; |
michael@0 | 1969 | xOffset = dispatchEntry->xOffset * scaleFactor; |
michael@0 | 1970 | yOffset = dispatchEntry->yOffset * scaleFactor; |
michael@0 | 1971 | if (scaleFactor != 1.0f) { |
michael@0 | 1972 | for (size_t i = 0; i < motionEntry->pointerCount; i++) { |
michael@0 | 1973 | scaledCoords[i] = motionEntry->pointerCoords[i]; |
michael@0 | 1974 | scaledCoords[i].scale(scaleFactor); |
michael@0 | 1975 | } |
michael@0 | 1976 | usingCoords = scaledCoords; |
michael@0 | 1977 | } |
michael@0 | 1978 | } else { |
michael@0 | 1979 | xOffset = 0.0f; |
michael@0 | 1980 | yOffset = 0.0f; |
michael@0 | 1981 | scaleFactor = 1.0f; |
michael@0 | 1982 | |
michael@0 | 1983 | // We don't want the dispatch target to know. |
michael@0 | 1984 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
michael@0 | 1985 | for (size_t i = 0; i < motionEntry->pointerCount; i++) { |
michael@0 | 1986 | scaledCoords[i].clear(); |
michael@0 | 1987 | } |
michael@0 | 1988 | usingCoords = scaledCoords; |
michael@0 | 1989 | } |
michael@0 | 1990 | } |
michael@0 | 1991 | |
michael@0 | 1992 | // Publish the motion event. |
michael@0 | 1993 | status = connection->inputPublisher.publishMotionEvent(dispatchEntry->seq, |
michael@0 | 1994 | motionEntry->deviceId, motionEntry->source, |
michael@0 | 1995 | dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags, |
michael@0 | 1996 | motionEntry->edgeFlags, motionEntry->metaState, motionEntry->buttonState, |
michael@0 | 1997 | xOffset, yOffset, |
michael@0 | 1998 | motionEntry->xPrecision, motionEntry->yPrecision, |
michael@0 | 1999 | motionEntry->downTime, motionEntry->eventTime, |
michael@0 | 2000 | motionEntry->pointerCount, motionEntry->pointerProperties, |
michael@0 | 2001 | usingCoords); |
michael@0 | 2002 | break; |
michael@0 | 2003 | } |
michael@0 | 2004 | |
michael@0 | 2005 | default: |
michael@0 | 2006 | ALOG_ASSERT(false); |
michael@0 | 2007 | return; |
michael@0 | 2008 | } |
michael@0 | 2009 | |
michael@0 | 2010 | // Check the result. |
michael@0 | 2011 | if (status) { |
michael@0 | 2012 | if (status == WOULD_BLOCK) { |
michael@0 | 2013 | if (connection->waitQueue.isEmpty()) { |
michael@0 | 2014 | ALOGE("channel '%s' ~ Could not publish event because the pipe is full. " |
michael@0 | 2015 | "This is unexpected because the wait queue is empty, so the pipe " |
michael@0 | 2016 | "should be empty and we shouldn't have any problems writing an " |
michael@0 | 2017 | "event to it, status=%d", connection->getInputChannelName(), status); |
michael@0 | 2018 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
michael@0 | 2019 | } else { |
michael@0 | 2020 | // Pipe is full and we are waiting for the app to finish process some events |
michael@0 | 2021 | // before sending more events to it. |
michael@0 | 2022 | #if DEBUG_DISPATCH_CYCLE |
michael@0 | 2023 | ALOGD("channel '%s' ~ Could not publish event because the pipe is full, " |
michael@0 | 2024 | "waiting for the application to catch up", |
michael@0 | 2025 | connection->getInputChannelName()); |
michael@0 | 2026 | #endif |
michael@0 | 2027 | connection->inputPublisherBlocked = true; |
michael@0 | 2028 | } |
michael@0 | 2029 | } else { |
michael@0 | 2030 | ALOGE("channel '%s' ~ Could not publish event due to an unexpected error, " |
michael@0 | 2031 | "status=%d", connection->getInputChannelName(), status); |
michael@0 | 2032 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
michael@0 | 2033 | } |
michael@0 | 2034 | return; |
michael@0 | 2035 | } |
michael@0 | 2036 | |
michael@0 | 2037 | // Re-enqueue the event on the wait queue. |
michael@0 | 2038 | connection->outboundQueue.dequeue(dispatchEntry); |
michael@0 | 2039 | traceOutboundQueueLengthLocked(connection); |
michael@0 | 2040 | connection->waitQueue.enqueueAtTail(dispatchEntry); |
michael@0 | 2041 | traceWaitQueueLengthLocked(connection); |
michael@0 | 2042 | } |
michael@0 | 2043 | } |
michael@0 | 2044 | |
michael@0 | 2045 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
michael@0 | 2046 | const sp<Connection>& connection, uint32_t seq, bool handled) { |
michael@0 | 2047 | #if DEBUG_DISPATCH_CYCLE |
michael@0 | 2048 | ALOGD("channel '%s' ~ finishDispatchCycle - seq=%u, handled=%s", |
michael@0 | 2049 | connection->getInputChannelName(), seq, toString(handled)); |
michael@0 | 2050 | #endif |
michael@0 | 2051 | |
michael@0 | 2052 | connection->inputPublisherBlocked = false; |
michael@0 | 2053 | |
michael@0 | 2054 | if (connection->status == Connection::STATUS_BROKEN |
michael@0 | 2055 | || connection->status == Connection::STATUS_ZOMBIE) { |
michael@0 | 2056 | return; |
michael@0 | 2057 | } |
michael@0 | 2058 | |
michael@0 | 2059 | // Notify other system components and prepare to start the next dispatch cycle. |
michael@0 | 2060 | onDispatchCycleFinishedLocked(currentTime, connection, seq, handled); |
michael@0 | 2061 | } |
michael@0 | 2062 | |
michael@0 | 2063 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
michael@0 | 2064 | const sp<Connection>& connection, bool notify) { |
michael@0 | 2065 | #if DEBUG_DISPATCH_CYCLE |
michael@0 | 2066 | ALOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
michael@0 | 2067 | connection->getInputChannelName(), toString(notify)); |
michael@0 | 2068 | #endif |
michael@0 | 2069 | |
michael@0 | 2070 | // Clear the dispatch queues. |
michael@0 | 2071 | drainDispatchQueueLocked(&connection->outboundQueue); |
michael@0 | 2072 | traceOutboundQueueLengthLocked(connection); |
michael@0 | 2073 | drainDispatchQueueLocked(&connection->waitQueue); |
michael@0 | 2074 | traceWaitQueueLengthLocked(connection); |
michael@0 | 2075 | |
michael@0 | 2076 | // The connection appears to be unrecoverably broken. |
michael@0 | 2077 | // Ignore already broken or zombie connections. |
michael@0 | 2078 | if (connection->status == Connection::STATUS_NORMAL) { |
michael@0 | 2079 | connection->status = Connection::STATUS_BROKEN; |
michael@0 | 2080 | |
michael@0 | 2081 | if (notify) { |
michael@0 | 2082 | // Notify other system components. |
michael@0 | 2083 | onDispatchCycleBrokenLocked(currentTime, connection); |
michael@0 | 2084 | } |
michael@0 | 2085 | } |
michael@0 | 2086 | } |
michael@0 | 2087 | |
michael@0 | 2088 | void InputDispatcher::drainDispatchQueueLocked(Queue<DispatchEntry>* queue) { |
michael@0 | 2089 | while (!queue->isEmpty()) { |
michael@0 | 2090 | DispatchEntry* dispatchEntry = queue->dequeueAtHead(); |
michael@0 | 2091 | releaseDispatchEntryLocked(dispatchEntry); |
michael@0 | 2092 | } |
michael@0 | 2093 | } |
michael@0 | 2094 | |
michael@0 | 2095 | void InputDispatcher::releaseDispatchEntryLocked(DispatchEntry* dispatchEntry) { |
michael@0 | 2096 | if (dispatchEntry->hasForegroundTarget()) { |
michael@0 | 2097 | decrementPendingForegroundDispatchesLocked(dispatchEntry->eventEntry); |
michael@0 | 2098 | } |
michael@0 | 2099 | delete dispatchEntry; |
michael@0 | 2100 | } |
michael@0 | 2101 | |
michael@0 | 2102 | int InputDispatcher::handleReceiveCallback(int fd, int events, void* data) { |
michael@0 | 2103 | InputDispatcher* d = static_cast<InputDispatcher*>(data); |
michael@0 | 2104 | |
michael@0 | 2105 | { // acquire lock |
michael@0 | 2106 | AutoMutex _l(d->mLock); |
michael@0 | 2107 | |
michael@0 | 2108 | ssize_t connectionIndex = d->mConnectionsByFd.indexOfKey(fd); |
michael@0 | 2109 | if (connectionIndex < 0) { |
michael@0 | 2110 | ALOGE("Received spurious receive callback for unknown input channel. " |
michael@0 | 2111 | "fd=%d, events=0x%x", fd, events); |
michael@0 | 2112 | return 0; // remove the callback |
michael@0 | 2113 | } |
michael@0 | 2114 | |
michael@0 | 2115 | bool notify; |
michael@0 | 2116 | sp<Connection> connection = d->mConnectionsByFd.valueAt(connectionIndex); |
michael@0 | 2117 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
michael@0 | 2118 | if (!(events & ALOOPER_EVENT_INPUT)) { |
michael@0 | 2119 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
michael@0 | 2120 | "events=0x%x", connection->getInputChannelName(), events); |
michael@0 | 2121 | return 1; |
michael@0 | 2122 | } |
michael@0 | 2123 | |
michael@0 | 2124 | nsecs_t currentTime = now(); |
michael@0 | 2125 | bool gotOne = false; |
michael@0 | 2126 | status_t status; |
michael@0 | 2127 | for (;;) { |
michael@0 | 2128 | uint32_t seq; |
michael@0 | 2129 | bool handled; |
michael@0 | 2130 | status = connection->inputPublisher.receiveFinishedSignal(&seq, &handled); |
michael@0 | 2131 | if (status) { |
michael@0 | 2132 | break; |
michael@0 | 2133 | } |
michael@0 | 2134 | d->finishDispatchCycleLocked(currentTime, connection, seq, handled); |
michael@0 | 2135 | gotOne = true; |
michael@0 | 2136 | } |
michael@0 | 2137 | if (gotOne) { |
michael@0 | 2138 | d->runCommandsLockedInterruptible(); |
michael@0 | 2139 | if (status == WOULD_BLOCK) { |
michael@0 | 2140 | return 1; |
michael@0 | 2141 | } |
michael@0 | 2142 | } |
michael@0 | 2143 | |
michael@0 | 2144 | notify = status != DEAD_OBJECT || !connection->monitor; |
michael@0 | 2145 | if (notify) { |
michael@0 | 2146 | ALOGE("channel '%s' ~ Failed to receive finished signal. status=%d", |
michael@0 | 2147 | connection->getInputChannelName(), status); |
michael@0 | 2148 | } |
michael@0 | 2149 | } else { |
michael@0 | 2150 | // Monitor channels are never explicitly unregistered. |
michael@0 | 2151 | // We do it automatically when the remote endpoint is closed so don't warn |
michael@0 | 2152 | // about them. |
michael@0 | 2153 | notify = !connection->monitor; |
michael@0 | 2154 | if (notify) { |
michael@0 | 2155 | ALOGW("channel '%s' ~ Consumer closed input channel or an error occurred. " |
michael@0 | 2156 | "events=0x%x", connection->getInputChannelName(), events); |
michael@0 | 2157 | } |
michael@0 | 2158 | } |
michael@0 | 2159 | |
michael@0 | 2160 | // Unregister the channel. |
michael@0 | 2161 | d->unregisterInputChannelLocked(connection->inputChannel, notify); |
michael@0 | 2162 | return 0; // remove the callback |
michael@0 | 2163 | } // release lock |
michael@0 | 2164 | } |
michael@0 | 2165 | |
michael@0 | 2166 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
michael@0 | 2167 | const CancelationOptions& options) { |
michael@0 | 2168 | for (size_t i = 0; i < mConnectionsByFd.size(); i++) { |
michael@0 | 2169 | synthesizeCancelationEventsForConnectionLocked( |
michael@0 | 2170 | mConnectionsByFd.valueAt(i), options); |
michael@0 | 2171 | } |
michael@0 | 2172 | } |
michael@0 | 2173 | |
michael@0 | 2174 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
michael@0 | 2175 | const sp<InputChannel>& channel, const CancelationOptions& options) { |
michael@0 | 2176 | ssize_t index = getConnectionIndexLocked(channel); |
michael@0 | 2177 | if (index >= 0) { |
michael@0 | 2178 | synthesizeCancelationEventsForConnectionLocked( |
michael@0 | 2179 | mConnectionsByFd.valueAt(index), options); |
michael@0 | 2180 | } |
michael@0 | 2181 | } |
michael@0 | 2182 | |
michael@0 | 2183 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
michael@0 | 2184 | const sp<Connection>& connection, const CancelationOptions& options) { |
michael@0 | 2185 | if (connection->status == Connection::STATUS_BROKEN) { |
michael@0 | 2186 | return; |
michael@0 | 2187 | } |
michael@0 | 2188 | |
michael@0 | 2189 | nsecs_t currentTime = now(); |
michael@0 | 2190 | |
michael@0 | 2191 | Vector<EventEntry*> cancelationEvents; |
michael@0 | 2192 | connection->inputState.synthesizeCancelationEvents(currentTime, |
michael@0 | 2193 | cancelationEvents, options); |
michael@0 | 2194 | |
michael@0 | 2195 | if (!cancelationEvents.isEmpty()) { |
michael@0 | 2196 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
michael@0 | 2197 | ALOGD("channel '%s' ~ Synthesized %d cancelation events to bring channel back in sync " |
michael@0 | 2198 | "with reality: %s, mode=%d.", |
michael@0 | 2199 | connection->getInputChannelName(), cancelationEvents.size(), |
michael@0 | 2200 | options.reason, options.mode); |
michael@0 | 2201 | #endif |
michael@0 | 2202 | for (size_t i = 0; i < cancelationEvents.size(); i++) { |
michael@0 | 2203 | EventEntry* cancelationEventEntry = cancelationEvents.itemAt(i); |
michael@0 | 2204 | switch (cancelationEventEntry->type) { |
michael@0 | 2205 | case EventEntry::TYPE_KEY: |
michael@0 | 2206 | logOutboundKeyDetailsLocked("cancel - ", |
michael@0 | 2207 | static_cast<KeyEntry*>(cancelationEventEntry)); |
michael@0 | 2208 | break; |
michael@0 | 2209 | case EventEntry::TYPE_MOTION: |
michael@0 | 2210 | logOutboundMotionDetailsLocked("cancel - ", |
michael@0 | 2211 | static_cast<MotionEntry*>(cancelationEventEntry)); |
michael@0 | 2212 | break; |
michael@0 | 2213 | } |
michael@0 | 2214 | |
michael@0 | 2215 | InputTarget target; |
michael@0 | 2216 | sp<InputWindowHandle> windowHandle = getWindowHandleLocked(connection->inputChannel); |
michael@0 | 2217 | if (windowHandle != NULL) { |
michael@0 | 2218 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
michael@0 | 2219 | target.xOffset = -windowInfo->frameLeft; |
michael@0 | 2220 | target.yOffset = -windowInfo->frameTop; |
michael@0 | 2221 | target.scaleFactor = windowInfo->scaleFactor; |
michael@0 | 2222 | } else { |
michael@0 | 2223 | target.xOffset = 0; |
michael@0 | 2224 | target.yOffset = 0; |
michael@0 | 2225 | target.scaleFactor = 1.0f; |
michael@0 | 2226 | } |
michael@0 | 2227 | target.inputChannel = connection->inputChannel; |
michael@0 | 2228 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
michael@0 | 2229 | |
michael@0 | 2230 | enqueueDispatchEntryLocked(connection, cancelationEventEntry, // increments ref |
michael@0 | 2231 | &target, InputTarget::FLAG_DISPATCH_AS_IS); |
michael@0 | 2232 | |
michael@0 | 2233 | cancelationEventEntry->release(); |
michael@0 | 2234 | } |
michael@0 | 2235 | |
michael@0 | 2236 | startDispatchCycleLocked(currentTime, connection); |
michael@0 | 2237 | } |
michael@0 | 2238 | } |
michael@0 | 2239 | |
michael@0 | 2240 | InputDispatcher::MotionEntry* |
michael@0 | 2241 | InputDispatcher::splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds) { |
michael@0 | 2242 | ALOG_ASSERT(pointerIds.value != 0); |
michael@0 | 2243 | |
michael@0 | 2244 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
michael@0 | 2245 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
michael@0 | 2246 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
michael@0 | 2247 | |
michael@0 | 2248 | uint32_t originalPointerCount = originalMotionEntry->pointerCount; |
michael@0 | 2249 | uint32_t splitPointerCount = 0; |
michael@0 | 2250 | |
michael@0 | 2251 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
michael@0 | 2252 | originalPointerIndex++) { |
michael@0 | 2253 | const PointerProperties& pointerProperties = |
michael@0 | 2254 | originalMotionEntry->pointerProperties[originalPointerIndex]; |
michael@0 | 2255 | uint32_t pointerId = uint32_t(pointerProperties.id); |
michael@0 | 2256 | if (pointerIds.hasBit(pointerId)) { |
michael@0 | 2257 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
michael@0 | 2258 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
michael@0 | 2259 | splitPointerCoords[splitPointerCount].copyFrom( |
michael@0 | 2260 | originalMotionEntry->pointerCoords[originalPointerIndex]); |
michael@0 | 2261 | splitPointerCount += 1; |
michael@0 | 2262 | } |
michael@0 | 2263 | } |
michael@0 | 2264 | |
michael@0 | 2265 | if (splitPointerCount != pointerIds.count()) { |
michael@0 | 2266 | // This is bad. We are missing some of the pointers that we expected to deliver. |
michael@0 | 2267 | // Most likely this indicates that we received an ACTION_MOVE events that has |
michael@0 | 2268 | // different pointer ids than we expected based on the previous ACTION_DOWN |
michael@0 | 2269 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
michael@0 | 2270 | // in this way. |
michael@0 | 2271 | ALOGW("Dropping split motion event because the pointer count is %d but " |
michael@0 | 2272 | "we expected there to be %d pointers. This probably means we received " |
michael@0 | 2273 | "a broken sequence of pointer ids from the input device.", |
michael@0 | 2274 | splitPointerCount, pointerIds.count()); |
michael@0 | 2275 | return NULL; |
michael@0 | 2276 | } |
michael@0 | 2277 | |
michael@0 | 2278 | int32_t action = originalMotionEntry->action; |
michael@0 | 2279 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
michael@0 | 2280 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
michael@0 | 2281 | || maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
michael@0 | 2282 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
michael@0 | 2283 | const PointerProperties& pointerProperties = |
michael@0 | 2284 | originalMotionEntry->pointerProperties[originalPointerIndex]; |
michael@0 | 2285 | uint32_t pointerId = uint32_t(pointerProperties.id); |
michael@0 | 2286 | if (pointerIds.hasBit(pointerId)) { |
michael@0 | 2287 | if (pointerIds.count() == 1) { |
michael@0 | 2288 | // The first/last pointer went down/up. |
michael@0 | 2289 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
michael@0 | 2290 | ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP; |
michael@0 | 2291 | } else { |
michael@0 | 2292 | // A secondary pointer went down/up. |
michael@0 | 2293 | uint32_t splitPointerIndex = 0; |
michael@0 | 2294 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
michael@0 | 2295 | splitPointerIndex += 1; |
michael@0 | 2296 | } |
michael@0 | 2297 | action = maskedAction | (splitPointerIndex |
michael@0 | 2298 | << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
michael@0 | 2299 | } |
michael@0 | 2300 | } else { |
michael@0 | 2301 | // An unrelated pointer changed. |
michael@0 | 2302 | action = AMOTION_EVENT_ACTION_MOVE; |
michael@0 | 2303 | } |
michael@0 | 2304 | } |
michael@0 | 2305 | |
michael@0 | 2306 | MotionEntry* splitMotionEntry = new MotionEntry( |
michael@0 | 2307 | originalMotionEntry->eventTime, |
michael@0 | 2308 | originalMotionEntry->deviceId, |
michael@0 | 2309 | originalMotionEntry->source, |
michael@0 | 2310 | originalMotionEntry->policyFlags, |
michael@0 | 2311 | action, |
michael@0 | 2312 | originalMotionEntry->flags, |
michael@0 | 2313 | originalMotionEntry->metaState, |
michael@0 | 2314 | originalMotionEntry->buttonState, |
michael@0 | 2315 | originalMotionEntry->edgeFlags, |
michael@0 | 2316 | originalMotionEntry->xPrecision, |
michael@0 | 2317 | originalMotionEntry->yPrecision, |
michael@0 | 2318 | originalMotionEntry->downTime, |
michael@0 | 2319 | originalMotionEntry->displayId, |
michael@0 | 2320 | splitPointerCount, splitPointerProperties, splitPointerCoords); |
michael@0 | 2321 | |
michael@0 | 2322 | if (originalMotionEntry->injectionState) { |
michael@0 | 2323 | splitMotionEntry->injectionState = originalMotionEntry->injectionState; |
michael@0 | 2324 | splitMotionEntry->injectionState->refCount += 1; |
michael@0 | 2325 | } |
michael@0 | 2326 | |
michael@0 | 2327 | return splitMotionEntry; |
michael@0 | 2328 | } |
michael@0 | 2329 | |
michael@0 | 2330 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
michael@0 | 2331 | #if DEBUG_INBOUND_EVENT_DETAILS |
michael@0 | 2332 | ALOGD("notifyConfigurationChanged - eventTime=%lld", args->eventTime); |
michael@0 | 2333 | #endif |
michael@0 | 2334 | |
michael@0 | 2335 | bool needWake; |
michael@0 | 2336 | { // acquire lock |
michael@0 | 2337 | AutoMutex _l(mLock); |
michael@0 | 2338 | |
michael@0 | 2339 | ConfigurationChangedEntry* newEntry = new ConfigurationChangedEntry(args->eventTime); |
michael@0 | 2340 | needWake = enqueueInboundEventLocked(newEntry); |
michael@0 | 2341 | } // release lock |
michael@0 | 2342 | |
michael@0 | 2343 | if (needWake) { |
michael@0 | 2344 | mLooper->wake(); |
michael@0 | 2345 | } |
michael@0 | 2346 | } |
michael@0 | 2347 | |
michael@0 | 2348 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
michael@0 | 2349 | #if DEBUG_INBOUND_EVENT_DETAILS |
michael@0 | 2350 | ALOGD("notifyKey - eventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, action=0x%x, " |
michael@0 | 2351 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%lld", |
michael@0 | 2352 | args->eventTime, args->deviceId, args->source, args->policyFlags, |
michael@0 | 2353 | args->action, args->flags, args->keyCode, args->scanCode, |
michael@0 | 2354 | args->metaState, args->downTime); |
michael@0 | 2355 | #endif |
michael@0 | 2356 | if (!validateKeyEvent(args->action)) { |
michael@0 | 2357 | return; |
michael@0 | 2358 | } |
michael@0 | 2359 | |
michael@0 | 2360 | uint32_t policyFlags = args->policyFlags; |
michael@0 | 2361 | int32_t flags = args->flags; |
michael@0 | 2362 | int32_t metaState = args->metaState; |
michael@0 | 2363 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
michael@0 | 2364 | policyFlags |= POLICY_FLAG_VIRTUAL; |
michael@0 | 2365 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
michael@0 | 2366 | } |
michael@0 | 2367 | if (policyFlags & POLICY_FLAG_ALT) { |
michael@0 | 2368 | metaState |= AMETA_ALT_ON | AMETA_ALT_LEFT_ON; |
michael@0 | 2369 | } |
michael@0 | 2370 | if (policyFlags & POLICY_FLAG_ALT_GR) { |
michael@0 | 2371 | metaState |= AMETA_ALT_ON | AMETA_ALT_RIGHT_ON; |
michael@0 | 2372 | } |
michael@0 | 2373 | if (policyFlags & POLICY_FLAG_SHIFT) { |
michael@0 | 2374 | metaState |= AMETA_SHIFT_ON | AMETA_SHIFT_LEFT_ON; |
michael@0 | 2375 | } |
michael@0 | 2376 | if (policyFlags & POLICY_FLAG_CAPS_LOCK) { |
michael@0 | 2377 | metaState |= AMETA_CAPS_LOCK_ON; |
michael@0 | 2378 | } |
michael@0 | 2379 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
michael@0 | 2380 | metaState |= AMETA_FUNCTION_ON; |
michael@0 | 2381 | } |
michael@0 | 2382 | |
michael@0 | 2383 | policyFlags |= POLICY_FLAG_TRUSTED; |
michael@0 | 2384 | |
michael@0 | 2385 | KeyEvent event; |
michael@0 | 2386 | event.initialize(args->deviceId, args->source, args->action, |
michael@0 | 2387 | flags, args->keyCode, args->scanCode, metaState, 0, |
michael@0 | 2388 | args->downTime, args->eventTime); |
michael@0 | 2389 | |
michael@0 | 2390 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
michael@0 | 2391 | |
michael@0 | 2392 | if (policyFlags & POLICY_FLAG_WOKE_HERE) { |
michael@0 | 2393 | flags |= AKEY_EVENT_FLAG_WOKE_HERE; |
michael@0 | 2394 | } |
michael@0 | 2395 | |
michael@0 | 2396 | bool needWake; |
michael@0 | 2397 | { // acquire lock |
michael@0 | 2398 | mLock.lock(); |
michael@0 | 2399 | |
michael@0 | 2400 | if (shouldSendKeyToInputFilterLocked(args)) { |
michael@0 | 2401 | mLock.unlock(); |
michael@0 | 2402 | |
michael@0 | 2403 | policyFlags |= POLICY_FLAG_FILTERED; |
michael@0 | 2404 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
michael@0 | 2405 | return; // event was consumed by the filter |
michael@0 | 2406 | } |
michael@0 | 2407 | |
michael@0 | 2408 | mLock.lock(); |
michael@0 | 2409 | } |
michael@0 | 2410 | |
michael@0 | 2411 | int32_t repeatCount = 0; |
michael@0 | 2412 | KeyEntry* newEntry = new KeyEntry(args->eventTime, |
michael@0 | 2413 | args->deviceId, args->source, policyFlags, |
michael@0 | 2414 | args->action, flags, args->keyCode, args->scanCode, |
michael@0 | 2415 | metaState, repeatCount, args->downTime); |
michael@0 | 2416 | |
michael@0 | 2417 | needWake = enqueueInboundEventLocked(newEntry); |
michael@0 | 2418 | mLock.unlock(); |
michael@0 | 2419 | } // release lock |
michael@0 | 2420 | |
michael@0 | 2421 | if (needWake) { |
michael@0 | 2422 | mLooper->wake(); |
michael@0 | 2423 | } |
michael@0 | 2424 | } |
michael@0 | 2425 | |
michael@0 | 2426 | bool InputDispatcher::shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) { |
michael@0 | 2427 | return mInputFilterEnabled; |
michael@0 | 2428 | } |
michael@0 | 2429 | |
michael@0 | 2430 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
michael@0 | 2431 | #if DEBUG_INBOUND_EVENT_DETAILS |
michael@0 | 2432 | ALOGD("notifyMotion - eventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, " |
michael@0 | 2433 | "action=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, edgeFlags=0x%x, " |
michael@0 | 2434 | "xPrecision=%f, yPrecision=%f, downTime=%lld", |
michael@0 | 2435 | args->eventTime, args->deviceId, args->source, args->policyFlags, |
michael@0 | 2436 | args->action, args->flags, args->metaState, args->buttonState, |
michael@0 | 2437 | args->edgeFlags, args->xPrecision, args->yPrecision, args->downTime); |
michael@0 | 2438 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
michael@0 | 2439 | ALOGD(" Pointer %d: id=%d, toolType=%d, " |
michael@0 | 2440 | "x=%f, y=%f, pressure=%f, size=%f, " |
michael@0 | 2441 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
michael@0 | 2442 | "orientation=%f", |
michael@0 | 2443 | i, args->pointerProperties[i].id, |
michael@0 | 2444 | args->pointerProperties[i].toolType, |
michael@0 | 2445 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
michael@0 | 2446 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
michael@0 | 2447 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
michael@0 | 2448 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
michael@0 | 2449 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
michael@0 | 2450 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
michael@0 | 2451 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
michael@0 | 2452 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
michael@0 | 2453 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
michael@0 | 2454 | } |
michael@0 | 2455 | #endif |
michael@0 | 2456 | if (!validateMotionEvent(args->action, args->pointerCount, args->pointerProperties)) { |
michael@0 | 2457 | return; |
michael@0 | 2458 | } |
michael@0 | 2459 | |
michael@0 | 2460 | uint32_t policyFlags = args->policyFlags; |
michael@0 | 2461 | policyFlags |= POLICY_FLAG_TRUSTED; |
michael@0 | 2462 | mPolicy->interceptMotionBeforeQueueing(args->eventTime, /*byref*/ policyFlags); |
michael@0 | 2463 | |
michael@0 | 2464 | bool needWake; |
michael@0 | 2465 | { // acquire lock |
michael@0 | 2466 | mLock.lock(); |
michael@0 | 2467 | |
michael@0 | 2468 | if (shouldSendMotionToInputFilterLocked(args)) { |
michael@0 | 2469 | mLock.unlock(); |
michael@0 | 2470 | |
michael@0 | 2471 | MotionEvent event; |
michael@0 | 2472 | event.initialize(args->deviceId, args->source, args->action, args->flags, |
michael@0 | 2473 | args->edgeFlags, args->metaState, args->buttonState, 0, 0, |
michael@0 | 2474 | args->xPrecision, args->yPrecision, |
michael@0 | 2475 | args->downTime, args->eventTime, |
michael@0 | 2476 | args->pointerCount, args->pointerProperties, args->pointerCoords); |
michael@0 | 2477 | |
michael@0 | 2478 | policyFlags |= POLICY_FLAG_FILTERED; |
michael@0 | 2479 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
michael@0 | 2480 | return; // event was consumed by the filter |
michael@0 | 2481 | } |
michael@0 | 2482 | |
michael@0 | 2483 | mLock.lock(); |
michael@0 | 2484 | } |
michael@0 | 2485 | |
michael@0 | 2486 | // Just enqueue a new motion event. |
michael@0 | 2487 | MotionEntry* newEntry = new MotionEntry(args->eventTime, |
michael@0 | 2488 | args->deviceId, args->source, policyFlags, |
michael@0 | 2489 | args->action, args->flags, args->metaState, args->buttonState, |
michael@0 | 2490 | args->edgeFlags, args->xPrecision, args->yPrecision, args->downTime, |
michael@0 | 2491 | args->displayId, |
michael@0 | 2492 | args->pointerCount, args->pointerProperties, args->pointerCoords); |
michael@0 | 2493 | |
michael@0 | 2494 | needWake = enqueueInboundEventLocked(newEntry); |
michael@0 | 2495 | mLock.unlock(); |
michael@0 | 2496 | } // release lock |
michael@0 | 2497 | |
michael@0 | 2498 | if (needWake) { |
michael@0 | 2499 | mLooper->wake(); |
michael@0 | 2500 | } |
michael@0 | 2501 | } |
michael@0 | 2502 | |
michael@0 | 2503 | bool InputDispatcher::shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) { |
michael@0 | 2504 | // TODO: support sending secondary display events to input filter |
michael@0 | 2505 | return mInputFilterEnabled && isMainDisplay(args->displayId); |
michael@0 | 2506 | } |
michael@0 | 2507 | |
michael@0 | 2508 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
michael@0 | 2509 | #if DEBUG_INBOUND_EVENT_DETAILS |
michael@0 | 2510 | ALOGD("notifySwitch - eventTime=%lld, policyFlags=0x%x, switchValues=0x%08x, switchMask=0x%08x", |
michael@0 | 2511 | args->eventTime, args->policyFlags, |
michael@0 | 2512 | args->switchValues, args->switchMask); |
michael@0 | 2513 | #endif |
michael@0 | 2514 | |
michael@0 | 2515 | uint32_t policyFlags = args->policyFlags; |
michael@0 | 2516 | policyFlags |= POLICY_FLAG_TRUSTED; |
michael@0 | 2517 | mPolicy->notifySwitch(args->eventTime, |
michael@0 | 2518 | args->switchValues, args->switchMask, policyFlags); |
michael@0 | 2519 | } |
michael@0 | 2520 | |
michael@0 | 2521 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
michael@0 | 2522 | #if DEBUG_INBOUND_EVENT_DETAILS |
michael@0 | 2523 | ALOGD("notifyDeviceReset - eventTime=%lld, deviceId=%d", |
michael@0 | 2524 | args->eventTime, args->deviceId); |
michael@0 | 2525 | #endif |
michael@0 | 2526 | |
michael@0 | 2527 | bool needWake; |
michael@0 | 2528 | { // acquire lock |
michael@0 | 2529 | AutoMutex _l(mLock); |
michael@0 | 2530 | |
michael@0 | 2531 | DeviceResetEntry* newEntry = new DeviceResetEntry(args->eventTime, args->deviceId); |
michael@0 | 2532 | needWake = enqueueInboundEventLocked(newEntry); |
michael@0 | 2533 | } // release lock |
michael@0 | 2534 | |
michael@0 | 2535 | if (needWake) { |
michael@0 | 2536 | mLooper->wake(); |
michael@0 | 2537 | } |
michael@0 | 2538 | } |
michael@0 | 2539 | |
michael@0 | 2540 | int32_t InputDispatcher::injectInputEvent(const InputEvent* event, |
michael@0 | 2541 | int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis, |
michael@0 | 2542 | uint32_t policyFlags) { |
michael@0 | 2543 | #if DEBUG_INBOUND_EVENT_DETAILS |
michael@0 | 2544 | ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
michael@0 | 2545 | "syncMode=%d, timeoutMillis=%d, policyFlags=0x%08x", |
michael@0 | 2546 | event->getType(), injectorPid, injectorUid, syncMode, timeoutMillis, policyFlags); |
michael@0 | 2547 | #endif |
michael@0 | 2548 | |
michael@0 | 2549 | nsecs_t endTime = now() + milliseconds_to_nanoseconds(timeoutMillis); |
michael@0 | 2550 | |
michael@0 | 2551 | policyFlags |= POLICY_FLAG_INJECTED; |
michael@0 | 2552 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
michael@0 | 2553 | policyFlags |= POLICY_FLAG_TRUSTED; |
michael@0 | 2554 | } |
michael@0 | 2555 | |
michael@0 | 2556 | EventEntry* firstInjectedEntry; |
michael@0 | 2557 | EventEntry* lastInjectedEntry; |
michael@0 | 2558 | switch (event->getType()) { |
michael@0 | 2559 | case AINPUT_EVENT_TYPE_KEY: { |
michael@0 | 2560 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(event); |
michael@0 | 2561 | int32_t action = keyEvent->getAction(); |
michael@0 | 2562 | if (! validateKeyEvent(action)) { |
michael@0 | 2563 | return INPUT_EVENT_INJECTION_FAILED; |
michael@0 | 2564 | } |
michael@0 | 2565 | |
michael@0 | 2566 | int32_t flags = keyEvent->getFlags(); |
michael@0 | 2567 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
michael@0 | 2568 | policyFlags |= POLICY_FLAG_VIRTUAL; |
michael@0 | 2569 | } |
michael@0 | 2570 | |
michael@0 | 2571 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
michael@0 | 2572 | mPolicy->interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags); |
michael@0 | 2573 | } |
michael@0 | 2574 | |
michael@0 | 2575 | if (policyFlags & POLICY_FLAG_WOKE_HERE) { |
michael@0 | 2576 | flags |= AKEY_EVENT_FLAG_WOKE_HERE; |
michael@0 | 2577 | } |
michael@0 | 2578 | |
michael@0 | 2579 | mLock.lock(); |
michael@0 | 2580 | firstInjectedEntry = new KeyEntry(keyEvent->getEventTime(), |
michael@0 | 2581 | keyEvent->getDeviceId(), keyEvent->getSource(), |
michael@0 | 2582 | policyFlags, action, flags, |
michael@0 | 2583 | keyEvent->getKeyCode(), keyEvent->getScanCode(), keyEvent->getMetaState(), |
michael@0 | 2584 | keyEvent->getRepeatCount(), keyEvent->getDownTime()); |
michael@0 | 2585 | lastInjectedEntry = firstInjectedEntry; |
michael@0 | 2586 | break; |
michael@0 | 2587 | } |
michael@0 | 2588 | |
michael@0 | 2589 | case AINPUT_EVENT_TYPE_MOTION: { |
michael@0 | 2590 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); |
michael@0 | 2591 | int32_t displayId = ADISPLAY_ID_DEFAULT; |
michael@0 | 2592 | int32_t action = motionEvent->getAction(); |
michael@0 | 2593 | size_t pointerCount = motionEvent->getPointerCount(); |
michael@0 | 2594 | const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); |
michael@0 | 2595 | if (! validateMotionEvent(action, pointerCount, pointerProperties)) { |
michael@0 | 2596 | return INPUT_EVENT_INJECTION_FAILED; |
michael@0 | 2597 | } |
michael@0 | 2598 | |
michael@0 | 2599 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
michael@0 | 2600 | nsecs_t eventTime = motionEvent->getEventTime(); |
michael@0 | 2601 | mPolicy->interceptMotionBeforeQueueing(eventTime, /*byref*/ policyFlags); |
michael@0 | 2602 | } |
michael@0 | 2603 | |
michael@0 | 2604 | mLock.lock(); |
michael@0 | 2605 | const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); |
michael@0 | 2606 | const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); |
michael@0 | 2607 | firstInjectedEntry = new MotionEntry(*sampleEventTimes, |
michael@0 | 2608 | motionEvent->getDeviceId(), motionEvent->getSource(), policyFlags, |
michael@0 | 2609 | action, motionEvent->getFlags(), |
michael@0 | 2610 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
michael@0 | 2611 | motionEvent->getEdgeFlags(), |
michael@0 | 2612 | motionEvent->getXPrecision(), motionEvent->getYPrecision(), |
michael@0 | 2613 | motionEvent->getDownTime(), displayId, |
michael@0 | 2614 | uint32_t(pointerCount), pointerProperties, samplePointerCoords); |
michael@0 | 2615 | lastInjectedEntry = firstInjectedEntry; |
michael@0 | 2616 | for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { |
michael@0 | 2617 | sampleEventTimes += 1; |
michael@0 | 2618 | samplePointerCoords += pointerCount; |
michael@0 | 2619 | MotionEntry* nextInjectedEntry = new MotionEntry(*sampleEventTimes, |
michael@0 | 2620 | motionEvent->getDeviceId(), motionEvent->getSource(), policyFlags, |
michael@0 | 2621 | action, motionEvent->getFlags(), |
michael@0 | 2622 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
michael@0 | 2623 | motionEvent->getEdgeFlags(), |
michael@0 | 2624 | motionEvent->getXPrecision(), motionEvent->getYPrecision(), |
michael@0 | 2625 | motionEvent->getDownTime(), displayId, |
michael@0 | 2626 | uint32_t(pointerCount), pointerProperties, samplePointerCoords); |
michael@0 | 2627 | lastInjectedEntry->next = nextInjectedEntry; |
michael@0 | 2628 | lastInjectedEntry = nextInjectedEntry; |
michael@0 | 2629 | } |
michael@0 | 2630 | break; |
michael@0 | 2631 | } |
michael@0 | 2632 | |
michael@0 | 2633 | default: |
michael@0 | 2634 | ALOGW("Cannot inject event of type %d", event->getType()); |
michael@0 | 2635 | return INPUT_EVENT_INJECTION_FAILED; |
michael@0 | 2636 | } |
michael@0 | 2637 | |
michael@0 | 2638 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
michael@0 | 2639 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { |
michael@0 | 2640 | injectionState->injectionIsAsync = true; |
michael@0 | 2641 | } |
michael@0 | 2642 | |
michael@0 | 2643 | injectionState->refCount += 1; |
michael@0 | 2644 | lastInjectedEntry->injectionState = injectionState; |
michael@0 | 2645 | |
michael@0 | 2646 | bool needWake = false; |
michael@0 | 2647 | for (EventEntry* entry = firstInjectedEntry; entry != NULL; ) { |
michael@0 | 2648 | EventEntry* nextEntry = entry->next; |
michael@0 | 2649 | needWake |= enqueueInboundEventLocked(entry); |
michael@0 | 2650 | entry = nextEntry; |
michael@0 | 2651 | } |
michael@0 | 2652 | |
michael@0 | 2653 | mLock.unlock(); |
michael@0 | 2654 | |
michael@0 | 2655 | if (needWake) { |
michael@0 | 2656 | mLooper->wake(); |
michael@0 | 2657 | } |
michael@0 | 2658 | |
michael@0 | 2659 | int32_t injectionResult; |
michael@0 | 2660 | { // acquire lock |
michael@0 | 2661 | AutoMutex _l(mLock); |
michael@0 | 2662 | |
michael@0 | 2663 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { |
michael@0 | 2664 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
michael@0 | 2665 | } else { |
michael@0 | 2666 | for (;;) { |
michael@0 | 2667 | injectionResult = injectionState->injectionResult; |
michael@0 | 2668 | if (injectionResult != INPUT_EVENT_INJECTION_PENDING) { |
michael@0 | 2669 | break; |
michael@0 | 2670 | } |
michael@0 | 2671 | |
michael@0 | 2672 | nsecs_t remainingTimeout = endTime - now(); |
michael@0 | 2673 | if (remainingTimeout <= 0) { |
michael@0 | 2674 | #if DEBUG_INJECTION |
michael@0 | 2675 | ALOGD("injectInputEvent - Timed out waiting for injection result " |
michael@0 | 2676 | "to become available."); |
michael@0 | 2677 | #endif |
michael@0 | 2678 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
michael@0 | 2679 | break; |
michael@0 | 2680 | } |
michael@0 | 2681 | |
michael@0 | 2682 | mInjectionResultAvailableCondition.waitRelative(mLock, remainingTimeout); |
michael@0 | 2683 | } |
michael@0 | 2684 | |
michael@0 | 2685 | if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED |
michael@0 | 2686 | && syncMode == INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED) { |
michael@0 | 2687 | while (injectionState->pendingForegroundDispatches != 0) { |
michael@0 | 2688 | #if DEBUG_INJECTION |
michael@0 | 2689 | ALOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
michael@0 | 2690 | injectionState->pendingForegroundDispatches); |
michael@0 | 2691 | #endif |
michael@0 | 2692 | nsecs_t remainingTimeout = endTime - now(); |
michael@0 | 2693 | if (remainingTimeout <= 0) { |
michael@0 | 2694 | #if DEBUG_INJECTION |
michael@0 | 2695 | ALOGD("injectInputEvent - Timed out waiting for pending foreground " |
michael@0 | 2696 | "dispatches to finish."); |
michael@0 | 2697 | #endif |
michael@0 | 2698 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
michael@0 | 2699 | break; |
michael@0 | 2700 | } |
michael@0 | 2701 | |
michael@0 | 2702 | mInjectionSyncFinishedCondition.waitRelative(mLock, remainingTimeout); |
michael@0 | 2703 | } |
michael@0 | 2704 | } |
michael@0 | 2705 | } |
michael@0 | 2706 | |
michael@0 | 2707 | injectionState->release(); |
michael@0 | 2708 | } // release lock |
michael@0 | 2709 | |
michael@0 | 2710 | #if DEBUG_INJECTION |
michael@0 | 2711 | ALOGD("injectInputEvent - Finished with result %d. " |
michael@0 | 2712 | "injectorPid=%d, injectorUid=%d", |
michael@0 | 2713 | injectionResult, injectorPid, injectorUid); |
michael@0 | 2714 | #endif |
michael@0 | 2715 | |
michael@0 | 2716 | return injectionResult; |
michael@0 | 2717 | } |
michael@0 | 2718 | |
michael@0 | 2719 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
michael@0 | 2720 | return injectorUid == 0 |
michael@0 | 2721 | || mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
michael@0 | 2722 | } |
michael@0 | 2723 | |
michael@0 | 2724 | void InputDispatcher::setInjectionResultLocked(EventEntry* entry, int32_t injectionResult) { |
michael@0 | 2725 | InjectionState* injectionState = entry->injectionState; |
michael@0 | 2726 | if (injectionState) { |
michael@0 | 2727 | #if DEBUG_INJECTION |
michael@0 | 2728 | ALOGD("Setting input event injection result to %d. " |
michael@0 | 2729 | "injectorPid=%d, injectorUid=%d", |
michael@0 | 2730 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
michael@0 | 2731 | #endif |
michael@0 | 2732 | |
michael@0 | 2733 | if (injectionState->injectionIsAsync |
michael@0 | 2734 | && !(entry->policyFlags & POLICY_FLAG_FILTERED)) { |
michael@0 | 2735 | // Log the outcome since the injector did not wait for the injection result. |
michael@0 | 2736 | switch (injectionResult) { |
michael@0 | 2737 | case INPUT_EVENT_INJECTION_SUCCEEDED: |
michael@0 | 2738 | ALOGV("Asynchronous input event injection succeeded."); |
michael@0 | 2739 | break; |
michael@0 | 2740 | case INPUT_EVENT_INJECTION_FAILED: |
michael@0 | 2741 | ALOGW("Asynchronous input event injection failed."); |
michael@0 | 2742 | break; |
michael@0 | 2743 | case INPUT_EVENT_INJECTION_PERMISSION_DENIED: |
michael@0 | 2744 | ALOGW("Asynchronous input event injection permission denied."); |
michael@0 | 2745 | break; |
michael@0 | 2746 | case INPUT_EVENT_INJECTION_TIMED_OUT: |
michael@0 | 2747 | ALOGW("Asynchronous input event injection timed out."); |
michael@0 | 2748 | break; |
michael@0 | 2749 | } |
michael@0 | 2750 | } |
michael@0 | 2751 | |
michael@0 | 2752 | injectionState->injectionResult = injectionResult; |
michael@0 | 2753 | mInjectionResultAvailableCondition.broadcast(); |
michael@0 | 2754 | } |
michael@0 | 2755 | } |
michael@0 | 2756 | |
michael@0 | 2757 | void InputDispatcher::incrementPendingForegroundDispatchesLocked(EventEntry* entry) { |
michael@0 | 2758 | InjectionState* injectionState = entry->injectionState; |
michael@0 | 2759 | if (injectionState) { |
michael@0 | 2760 | injectionState->pendingForegroundDispatches += 1; |
michael@0 | 2761 | } |
michael@0 | 2762 | } |
michael@0 | 2763 | |
michael@0 | 2764 | void InputDispatcher::decrementPendingForegroundDispatchesLocked(EventEntry* entry) { |
michael@0 | 2765 | InjectionState* injectionState = entry->injectionState; |
michael@0 | 2766 | if (injectionState) { |
michael@0 | 2767 | injectionState->pendingForegroundDispatches -= 1; |
michael@0 | 2768 | |
michael@0 | 2769 | if (injectionState->pendingForegroundDispatches == 0) { |
michael@0 | 2770 | mInjectionSyncFinishedCondition.broadcast(); |
michael@0 | 2771 | } |
michael@0 | 2772 | } |
michael@0 | 2773 | } |
michael@0 | 2774 | |
michael@0 | 2775 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
michael@0 | 2776 | const sp<InputChannel>& inputChannel) const { |
michael@0 | 2777 | size_t numWindows = mWindowHandles.size(); |
michael@0 | 2778 | for (size_t i = 0; i < numWindows; i++) { |
michael@0 | 2779 | const sp<InputWindowHandle>& windowHandle = mWindowHandles.itemAt(i); |
michael@0 | 2780 | if (windowHandle->getInputChannel() == inputChannel) { |
michael@0 | 2781 | return windowHandle; |
michael@0 | 2782 | } |
michael@0 | 2783 | } |
michael@0 | 2784 | return NULL; |
michael@0 | 2785 | } |
michael@0 | 2786 | |
michael@0 | 2787 | bool InputDispatcher::hasWindowHandleLocked( |
michael@0 | 2788 | const sp<InputWindowHandle>& windowHandle) const { |
michael@0 | 2789 | size_t numWindows = mWindowHandles.size(); |
michael@0 | 2790 | for (size_t i = 0; i < numWindows; i++) { |
michael@0 | 2791 | if (mWindowHandles.itemAt(i) == windowHandle) { |
michael@0 | 2792 | return true; |
michael@0 | 2793 | } |
michael@0 | 2794 | } |
michael@0 | 2795 | return false; |
michael@0 | 2796 | } |
michael@0 | 2797 | |
michael@0 | 2798 | void InputDispatcher::setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles) { |
michael@0 | 2799 | #if DEBUG_FOCUS |
michael@0 | 2800 | ALOGD("setInputWindows"); |
michael@0 | 2801 | #endif |
michael@0 | 2802 | { // acquire lock |
michael@0 | 2803 | AutoMutex _l(mLock); |
michael@0 | 2804 | |
michael@0 | 2805 | Vector<sp<InputWindowHandle> > oldWindowHandles = mWindowHandles; |
michael@0 | 2806 | mWindowHandles = inputWindowHandles; |
michael@0 | 2807 | |
michael@0 | 2808 | sp<InputWindowHandle> newFocusedWindowHandle; |
michael@0 | 2809 | bool foundHoveredWindow = false; |
michael@0 | 2810 | for (size_t i = 0; i < mWindowHandles.size(); i++) { |
michael@0 | 2811 | const sp<InputWindowHandle>& windowHandle = mWindowHandles.itemAt(i); |
michael@0 | 2812 | if (!windowHandle->updateInfo() || windowHandle->getInputChannel() == NULL) { |
michael@0 | 2813 | mWindowHandles.removeAt(i--); |
michael@0 | 2814 | continue; |
michael@0 | 2815 | } |
michael@0 | 2816 | if (windowHandle->getInfo()->hasFocus) { |
michael@0 | 2817 | newFocusedWindowHandle = windowHandle; |
michael@0 | 2818 | } |
michael@0 | 2819 | if (windowHandle == mLastHoverWindowHandle) { |
michael@0 | 2820 | foundHoveredWindow = true; |
michael@0 | 2821 | } |
michael@0 | 2822 | } |
michael@0 | 2823 | |
michael@0 | 2824 | if (!foundHoveredWindow) { |
michael@0 | 2825 | mLastHoverWindowHandle = NULL; |
michael@0 | 2826 | } |
michael@0 | 2827 | |
michael@0 | 2828 | if (mFocusedWindowHandle != newFocusedWindowHandle) { |
michael@0 | 2829 | if (mFocusedWindowHandle != NULL) { |
michael@0 | 2830 | #if DEBUG_FOCUS |
michael@0 | 2831 | ALOGD("Focus left window: %s", |
michael@0 | 2832 | mFocusedWindowHandle->getName().string()); |
michael@0 | 2833 | #endif |
michael@0 | 2834 | sp<InputChannel> focusedInputChannel = mFocusedWindowHandle->getInputChannel(); |
michael@0 | 2835 | if (focusedInputChannel != NULL) { |
michael@0 | 2836 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
michael@0 | 2837 | "focus left window"); |
michael@0 | 2838 | synthesizeCancelationEventsForInputChannelLocked( |
michael@0 | 2839 | focusedInputChannel, options); |
michael@0 | 2840 | } |
michael@0 | 2841 | } |
michael@0 | 2842 | if (newFocusedWindowHandle != NULL) { |
michael@0 | 2843 | #if DEBUG_FOCUS |
michael@0 | 2844 | ALOGD("Focus entered window: %s", |
michael@0 | 2845 | newFocusedWindowHandle->getName().string()); |
michael@0 | 2846 | #endif |
michael@0 | 2847 | } |
michael@0 | 2848 | mFocusedWindowHandle = newFocusedWindowHandle; |
michael@0 | 2849 | } |
michael@0 | 2850 | |
michael@0 | 2851 | for (size_t i = 0; i < mTouchState.windows.size(); i++) { |
michael@0 | 2852 | TouchedWindow& touchedWindow = mTouchState.windows.editItemAt(i); |
michael@0 | 2853 | if (!hasWindowHandleLocked(touchedWindow.windowHandle)) { |
michael@0 | 2854 | #if DEBUG_FOCUS |
michael@0 | 2855 | ALOGD("Touched window was removed: %s", |
michael@0 | 2856 | touchedWindow.windowHandle->getName().string()); |
michael@0 | 2857 | #endif |
michael@0 | 2858 | sp<InputChannel> touchedInputChannel = |
michael@0 | 2859 | touchedWindow.windowHandle->getInputChannel(); |
michael@0 | 2860 | if (touchedInputChannel != NULL) { |
michael@0 | 2861 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
michael@0 | 2862 | "touched window was removed"); |
michael@0 | 2863 | synthesizeCancelationEventsForInputChannelLocked( |
michael@0 | 2864 | touchedInputChannel, options); |
michael@0 | 2865 | } |
michael@0 | 2866 | mTouchState.windows.removeAt(i--); |
michael@0 | 2867 | } |
michael@0 | 2868 | } |
michael@0 | 2869 | |
michael@0 | 2870 | // Release information for windows that are no longer present. |
michael@0 | 2871 | // This ensures that unused input channels are released promptly. |
michael@0 | 2872 | // Otherwise, they might stick around until the window handle is destroyed |
michael@0 | 2873 | // which might not happen until the next GC. |
michael@0 | 2874 | for (size_t i = 0; i < oldWindowHandles.size(); i++) { |
michael@0 | 2875 | const sp<InputWindowHandle>& oldWindowHandle = oldWindowHandles.itemAt(i); |
michael@0 | 2876 | if (!hasWindowHandleLocked(oldWindowHandle)) { |
michael@0 | 2877 | #if DEBUG_FOCUS |
michael@0 | 2878 | ALOGD("Window went away: %s", oldWindowHandle->getName().string()); |
michael@0 | 2879 | #endif |
michael@0 | 2880 | oldWindowHandle->releaseInfo(); |
michael@0 | 2881 | } |
michael@0 | 2882 | } |
michael@0 | 2883 | } // release lock |
michael@0 | 2884 | |
michael@0 | 2885 | // Wake up poll loop since it may need to make new input dispatching choices. |
michael@0 | 2886 | mLooper->wake(); |
michael@0 | 2887 | } |
michael@0 | 2888 | |
michael@0 | 2889 | void InputDispatcher::setFocusedApplication( |
michael@0 | 2890 | const sp<InputApplicationHandle>& inputApplicationHandle) { |
michael@0 | 2891 | #if DEBUG_FOCUS |
michael@0 | 2892 | ALOGD("setFocusedApplication"); |
michael@0 | 2893 | #endif |
michael@0 | 2894 | { // acquire lock |
michael@0 | 2895 | AutoMutex _l(mLock); |
michael@0 | 2896 | |
michael@0 | 2897 | if (inputApplicationHandle != NULL && inputApplicationHandle->updateInfo()) { |
michael@0 | 2898 | if (mFocusedApplicationHandle != inputApplicationHandle) { |
michael@0 | 2899 | if (mFocusedApplicationHandle != NULL) { |
michael@0 | 2900 | resetANRTimeoutsLocked(); |
michael@0 | 2901 | mFocusedApplicationHandle->releaseInfo(); |
michael@0 | 2902 | } |
michael@0 | 2903 | mFocusedApplicationHandle = inputApplicationHandle; |
michael@0 | 2904 | } |
michael@0 | 2905 | } else if (mFocusedApplicationHandle != NULL) { |
michael@0 | 2906 | resetANRTimeoutsLocked(); |
michael@0 | 2907 | mFocusedApplicationHandle->releaseInfo(); |
michael@0 | 2908 | mFocusedApplicationHandle.clear(); |
michael@0 | 2909 | } |
michael@0 | 2910 | |
michael@0 | 2911 | #if DEBUG_FOCUS |
michael@0 | 2912 | //logDispatchStateLocked(); |
michael@0 | 2913 | #endif |
michael@0 | 2914 | } // release lock |
michael@0 | 2915 | |
michael@0 | 2916 | // Wake up poll loop since it may need to make new input dispatching choices. |
michael@0 | 2917 | mLooper->wake(); |
michael@0 | 2918 | } |
michael@0 | 2919 | |
michael@0 | 2920 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
michael@0 | 2921 | #if DEBUG_FOCUS |
michael@0 | 2922 | ALOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
michael@0 | 2923 | #endif |
michael@0 | 2924 | |
michael@0 | 2925 | bool changed; |
michael@0 | 2926 | { // acquire lock |
michael@0 | 2927 | AutoMutex _l(mLock); |
michael@0 | 2928 | |
michael@0 | 2929 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
michael@0 | 2930 | if (mDispatchFrozen && !frozen) { |
michael@0 | 2931 | resetANRTimeoutsLocked(); |
michael@0 | 2932 | } |
michael@0 | 2933 | |
michael@0 | 2934 | if (mDispatchEnabled && !enabled) { |
michael@0 | 2935 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
michael@0 | 2936 | } |
michael@0 | 2937 | |
michael@0 | 2938 | mDispatchEnabled = enabled; |
michael@0 | 2939 | mDispatchFrozen = frozen; |
michael@0 | 2940 | changed = true; |
michael@0 | 2941 | } else { |
michael@0 | 2942 | changed = false; |
michael@0 | 2943 | } |
michael@0 | 2944 | |
michael@0 | 2945 | #if DEBUG_FOCUS |
michael@0 | 2946 | //logDispatchStateLocked(); |
michael@0 | 2947 | #endif |
michael@0 | 2948 | } // release lock |
michael@0 | 2949 | |
michael@0 | 2950 | if (changed) { |
michael@0 | 2951 | // Wake up poll loop since it may need to make new input dispatching choices. |
michael@0 | 2952 | mLooper->wake(); |
michael@0 | 2953 | } |
michael@0 | 2954 | } |
michael@0 | 2955 | |
michael@0 | 2956 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
michael@0 | 2957 | #if DEBUG_FOCUS |
michael@0 | 2958 | ALOGD("setInputFilterEnabled: enabled=%d", enabled); |
michael@0 | 2959 | #endif |
michael@0 | 2960 | |
michael@0 | 2961 | { // acquire lock |
michael@0 | 2962 | AutoMutex _l(mLock); |
michael@0 | 2963 | |
michael@0 | 2964 | if (mInputFilterEnabled == enabled) { |
michael@0 | 2965 | return; |
michael@0 | 2966 | } |
michael@0 | 2967 | |
michael@0 | 2968 | mInputFilterEnabled = enabled; |
michael@0 | 2969 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
michael@0 | 2970 | } // release lock |
michael@0 | 2971 | |
michael@0 | 2972 | // Wake up poll loop since there might be work to do to drop everything. |
michael@0 | 2973 | mLooper->wake(); |
michael@0 | 2974 | } |
michael@0 | 2975 | |
michael@0 | 2976 | bool InputDispatcher::transferTouchFocus(const sp<InputChannel>& fromChannel, |
michael@0 | 2977 | const sp<InputChannel>& toChannel) { |
michael@0 | 2978 | #if DEBUG_FOCUS |
michael@0 | 2979 | ALOGD("transferTouchFocus: fromChannel=%s, toChannel=%s", |
michael@0 | 2980 | fromChannel->getName().string(), toChannel->getName().string()); |
michael@0 | 2981 | #endif |
michael@0 | 2982 | { // acquire lock |
michael@0 | 2983 | AutoMutex _l(mLock); |
michael@0 | 2984 | |
michael@0 | 2985 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromChannel); |
michael@0 | 2986 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toChannel); |
michael@0 | 2987 | if (fromWindowHandle == NULL || toWindowHandle == NULL) { |
michael@0 | 2988 | #if DEBUG_FOCUS |
michael@0 | 2989 | ALOGD("Cannot transfer focus because from or to window not found."); |
michael@0 | 2990 | #endif |
michael@0 | 2991 | return false; |
michael@0 | 2992 | } |
michael@0 | 2993 | if (fromWindowHandle == toWindowHandle) { |
michael@0 | 2994 | #if DEBUG_FOCUS |
michael@0 | 2995 | ALOGD("Trivial transfer to same window."); |
michael@0 | 2996 | #endif |
michael@0 | 2997 | return true; |
michael@0 | 2998 | } |
michael@0 | 2999 | if (fromWindowHandle->getInfo()->displayId != toWindowHandle->getInfo()->displayId) { |
michael@0 | 3000 | #if DEBUG_FOCUS |
michael@0 | 3001 | ALOGD("Cannot transfer focus because windows are on different displays."); |
michael@0 | 3002 | #endif |
michael@0 | 3003 | return false; |
michael@0 | 3004 | } |
michael@0 | 3005 | |
michael@0 | 3006 | bool found = false; |
michael@0 | 3007 | for (size_t i = 0; i < mTouchState.windows.size(); i++) { |
michael@0 | 3008 | const TouchedWindow& touchedWindow = mTouchState.windows[i]; |
michael@0 | 3009 | if (touchedWindow.windowHandle == fromWindowHandle) { |
michael@0 | 3010 | int32_t oldTargetFlags = touchedWindow.targetFlags; |
michael@0 | 3011 | BitSet32 pointerIds = touchedWindow.pointerIds; |
michael@0 | 3012 | |
michael@0 | 3013 | mTouchState.windows.removeAt(i); |
michael@0 | 3014 | |
michael@0 | 3015 | int32_t newTargetFlags = oldTargetFlags |
michael@0 | 3016 | & (InputTarget::FLAG_FOREGROUND |
michael@0 | 3017 | | InputTarget::FLAG_SPLIT | InputTarget::FLAG_DISPATCH_AS_IS); |
michael@0 | 3018 | mTouchState.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
michael@0 | 3019 | |
michael@0 | 3020 | found = true; |
michael@0 | 3021 | break; |
michael@0 | 3022 | } |
michael@0 | 3023 | } |
michael@0 | 3024 | |
michael@0 | 3025 | if (! found) { |
michael@0 | 3026 | #if DEBUG_FOCUS |
michael@0 | 3027 | ALOGD("Focus transfer failed because from window did not have focus."); |
michael@0 | 3028 | #endif |
michael@0 | 3029 | return false; |
michael@0 | 3030 | } |
michael@0 | 3031 | |
michael@0 | 3032 | ssize_t fromConnectionIndex = getConnectionIndexLocked(fromChannel); |
michael@0 | 3033 | ssize_t toConnectionIndex = getConnectionIndexLocked(toChannel); |
michael@0 | 3034 | if (fromConnectionIndex >= 0 && toConnectionIndex >= 0) { |
michael@0 | 3035 | sp<Connection> fromConnection = mConnectionsByFd.valueAt(fromConnectionIndex); |
michael@0 | 3036 | sp<Connection> toConnection = mConnectionsByFd.valueAt(toConnectionIndex); |
michael@0 | 3037 | |
michael@0 | 3038 | fromConnection->inputState.copyPointerStateTo(toConnection->inputState); |
michael@0 | 3039 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
michael@0 | 3040 | "transferring touch focus from this window to another window"); |
michael@0 | 3041 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
michael@0 | 3042 | } |
michael@0 | 3043 | |
michael@0 | 3044 | #if DEBUG_FOCUS |
michael@0 | 3045 | logDispatchStateLocked(); |
michael@0 | 3046 | #endif |
michael@0 | 3047 | } // release lock |
michael@0 | 3048 | |
michael@0 | 3049 | // Wake up poll loop since it may need to make new input dispatching choices. |
michael@0 | 3050 | mLooper->wake(); |
michael@0 | 3051 | return true; |
michael@0 | 3052 | } |
michael@0 | 3053 | |
michael@0 | 3054 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
michael@0 | 3055 | #if DEBUG_FOCUS |
michael@0 | 3056 | ALOGD("Resetting and dropping all events (%s).", reason); |
michael@0 | 3057 | #endif |
michael@0 | 3058 | |
michael@0 | 3059 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
michael@0 | 3060 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
michael@0 | 3061 | |
michael@0 | 3062 | resetKeyRepeatLocked(); |
michael@0 | 3063 | releasePendingEventLocked(); |
michael@0 | 3064 | drainInboundQueueLocked(); |
michael@0 | 3065 | resetANRTimeoutsLocked(); |
michael@0 | 3066 | |
michael@0 | 3067 | mTouchState.reset(); |
michael@0 | 3068 | mLastHoverWindowHandle.clear(); |
michael@0 | 3069 | } |
michael@0 | 3070 | |
michael@0 | 3071 | void InputDispatcher::logDispatchStateLocked() { |
michael@0 | 3072 | String8 dump; |
michael@0 | 3073 | dumpDispatchStateLocked(dump); |
michael@0 | 3074 | |
michael@0 | 3075 | char* text = dump.lockBuffer(dump.size()); |
michael@0 | 3076 | char* start = text; |
michael@0 | 3077 | while (*start != '\0') { |
michael@0 | 3078 | char* end = strchr(start, '\n'); |
michael@0 | 3079 | if (*end == '\n') { |
michael@0 | 3080 | *(end++) = '\0'; |
michael@0 | 3081 | } |
michael@0 | 3082 | ALOGD("%s", start); |
michael@0 | 3083 | start = end; |
michael@0 | 3084 | } |
michael@0 | 3085 | } |
michael@0 | 3086 | |
michael@0 | 3087 | void InputDispatcher::dumpDispatchStateLocked(String8& dump) { |
michael@0 | 3088 | dump.appendFormat(INDENT "DispatchEnabled: %d\n", mDispatchEnabled); |
michael@0 | 3089 | dump.appendFormat(INDENT "DispatchFrozen: %d\n", mDispatchFrozen); |
michael@0 | 3090 | |
michael@0 | 3091 | if (mFocusedApplicationHandle != NULL) { |
michael@0 | 3092 | dump.appendFormat(INDENT "FocusedApplication: name='%s', dispatchingTimeout=%0.3fms\n", |
michael@0 | 3093 | mFocusedApplicationHandle->getName().string(), |
michael@0 | 3094 | mFocusedApplicationHandle->getDispatchingTimeout( |
michael@0 | 3095 | DEFAULT_INPUT_DISPATCHING_TIMEOUT) / 1000000.0); |
michael@0 | 3096 | } else { |
michael@0 | 3097 | dump.append(INDENT "FocusedApplication: <null>\n"); |
michael@0 | 3098 | } |
michael@0 | 3099 | dump.appendFormat(INDENT "FocusedWindow: name='%s'\n", |
michael@0 | 3100 | mFocusedWindowHandle != NULL ? mFocusedWindowHandle->getName().string() : "<null>"); |
michael@0 | 3101 | |
michael@0 | 3102 | dump.appendFormat(INDENT "TouchDown: %s\n", toString(mTouchState.down)); |
michael@0 | 3103 | dump.appendFormat(INDENT "TouchSplit: %s\n", toString(mTouchState.split)); |
michael@0 | 3104 | dump.appendFormat(INDENT "TouchDeviceId: %d\n", mTouchState.deviceId); |
michael@0 | 3105 | dump.appendFormat(INDENT "TouchSource: 0x%08x\n", mTouchState.source); |
michael@0 | 3106 | dump.appendFormat(INDENT "TouchDisplayId: %d\n", mTouchState.displayId); |
michael@0 | 3107 | if (!mTouchState.windows.isEmpty()) { |
michael@0 | 3108 | dump.append(INDENT "TouchedWindows:\n"); |
michael@0 | 3109 | for (size_t i = 0; i < mTouchState.windows.size(); i++) { |
michael@0 | 3110 | const TouchedWindow& touchedWindow = mTouchState.windows[i]; |
michael@0 | 3111 | dump.appendFormat(INDENT2 "%d: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
michael@0 | 3112 | i, touchedWindow.windowHandle->getName().string(), |
michael@0 | 3113 | touchedWindow.pointerIds.value, |
michael@0 | 3114 | touchedWindow.targetFlags); |
michael@0 | 3115 | } |
michael@0 | 3116 | } else { |
michael@0 | 3117 | dump.append(INDENT "TouchedWindows: <none>\n"); |
michael@0 | 3118 | } |
michael@0 | 3119 | |
michael@0 | 3120 | if (!mWindowHandles.isEmpty()) { |
michael@0 | 3121 | dump.append(INDENT "Windows:\n"); |
michael@0 | 3122 | for (size_t i = 0; i < mWindowHandles.size(); i++) { |
michael@0 | 3123 | const sp<InputWindowHandle>& windowHandle = mWindowHandles.itemAt(i); |
michael@0 | 3124 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
michael@0 | 3125 | |
michael@0 | 3126 | dump.appendFormat(INDENT2 "%d: name='%s', displayId=%d, " |
michael@0 | 3127 | "paused=%s, hasFocus=%s, hasWallpaper=%s, " |
michael@0 | 3128 | "visible=%s, canReceiveKeys=%s, flags=0x%08x, type=0x%08x, layer=%d, " |
michael@0 | 3129 | "frame=[%d,%d][%d,%d], scale=%f, " |
michael@0 | 3130 | "touchableRegion=", |
michael@0 | 3131 | i, windowInfo->name.string(), windowInfo->displayId, |
michael@0 | 3132 | toString(windowInfo->paused), |
michael@0 | 3133 | toString(windowInfo->hasFocus), |
michael@0 | 3134 | toString(windowInfo->hasWallpaper), |
michael@0 | 3135 | toString(windowInfo->visible), |
michael@0 | 3136 | toString(windowInfo->canReceiveKeys), |
michael@0 | 3137 | windowInfo->layoutParamsFlags, windowInfo->layoutParamsType, |
michael@0 | 3138 | windowInfo->layer, |
michael@0 | 3139 | windowInfo->frameLeft, windowInfo->frameTop, |
michael@0 | 3140 | windowInfo->frameRight, windowInfo->frameBottom, |
michael@0 | 3141 | windowInfo->scaleFactor); |
michael@0 | 3142 | dumpRegion(dump, windowInfo->touchableRegion); |
michael@0 | 3143 | dump.appendFormat(", inputFeatures=0x%08x", windowInfo->inputFeatures); |
michael@0 | 3144 | dump.appendFormat(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%0.3fms\n", |
michael@0 | 3145 | windowInfo->ownerPid, windowInfo->ownerUid, |
michael@0 | 3146 | windowInfo->dispatchingTimeout / 1000000.0); |
michael@0 | 3147 | } |
michael@0 | 3148 | } else { |
michael@0 | 3149 | dump.append(INDENT "Windows: <none>\n"); |
michael@0 | 3150 | } |
michael@0 | 3151 | |
michael@0 | 3152 | if (!mMonitoringChannels.isEmpty()) { |
michael@0 | 3153 | dump.append(INDENT "MonitoringChannels:\n"); |
michael@0 | 3154 | for (size_t i = 0; i < mMonitoringChannels.size(); i++) { |
michael@0 | 3155 | const sp<InputChannel>& channel = mMonitoringChannels[i]; |
michael@0 | 3156 | dump.appendFormat(INDENT2 "%d: '%s'\n", i, channel->getName().string()); |
michael@0 | 3157 | } |
michael@0 | 3158 | } else { |
michael@0 | 3159 | dump.append(INDENT "MonitoringChannels: <none>\n"); |
michael@0 | 3160 | } |
michael@0 | 3161 | |
michael@0 | 3162 | nsecs_t currentTime = now(); |
michael@0 | 3163 | |
michael@0 | 3164 | if (!mInboundQueue.isEmpty()) { |
michael@0 | 3165 | dump.appendFormat(INDENT "InboundQueue: length=%u\n", mInboundQueue.count()); |
michael@0 | 3166 | for (EventEntry* entry = mInboundQueue.head; entry; entry = entry->next) { |
michael@0 | 3167 | dump.append(INDENT2); |
michael@0 | 3168 | entry->appendDescription(dump); |
michael@0 | 3169 | dump.appendFormat(", age=%0.1fms\n", |
michael@0 | 3170 | (currentTime - entry->eventTime) * 0.000001f); |
michael@0 | 3171 | } |
michael@0 | 3172 | } else { |
michael@0 | 3173 | dump.append(INDENT "InboundQueue: <empty>\n"); |
michael@0 | 3174 | } |
michael@0 | 3175 | |
michael@0 | 3176 | if (!mConnectionsByFd.isEmpty()) { |
michael@0 | 3177 | dump.append(INDENT "Connections:\n"); |
michael@0 | 3178 | for (size_t i = 0; i < mConnectionsByFd.size(); i++) { |
michael@0 | 3179 | const sp<Connection>& connection = mConnectionsByFd.valueAt(i); |
michael@0 | 3180 | dump.appendFormat(INDENT2 "%d: channelName='%s', windowName='%s', " |
michael@0 | 3181 | "status=%s, monitor=%s, inputPublisherBlocked=%s\n", |
michael@0 | 3182 | i, connection->getInputChannelName(), connection->getWindowName(), |
michael@0 | 3183 | connection->getStatusLabel(), toString(connection->monitor), |
michael@0 | 3184 | toString(connection->inputPublisherBlocked)); |
michael@0 | 3185 | |
michael@0 | 3186 | if (!connection->outboundQueue.isEmpty()) { |
michael@0 | 3187 | dump.appendFormat(INDENT3 "OutboundQueue: length=%u\n", |
michael@0 | 3188 | connection->outboundQueue.count()); |
michael@0 | 3189 | for (DispatchEntry* entry = connection->outboundQueue.head; entry; |
michael@0 | 3190 | entry = entry->next) { |
michael@0 | 3191 | dump.append(INDENT4); |
michael@0 | 3192 | entry->eventEntry->appendDescription(dump); |
michael@0 | 3193 | dump.appendFormat(", targetFlags=0x%08x, resolvedAction=%d, age=%0.1fms\n", |
michael@0 | 3194 | entry->targetFlags, entry->resolvedAction, |
michael@0 | 3195 | (currentTime - entry->eventEntry->eventTime) * 0.000001f); |
michael@0 | 3196 | } |
michael@0 | 3197 | } else { |
michael@0 | 3198 | dump.append(INDENT3 "OutboundQueue: <empty>\n"); |
michael@0 | 3199 | } |
michael@0 | 3200 | |
michael@0 | 3201 | if (!connection->waitQueue.isEmpty()) { |
michael@0 | 3202 | dump.appendFormat(INDENT3 "WaitQueue: length=%u\n", |
michael@0 | 3203 | connection->waitQueue.count()); |
michael@0 | 3204 | for (DispatchEntry* entry = connection->waitQueue.head; entry; |
michael@0 | 3205 | entry = entry->next) { |
michael@0 | 3206 | dump.append(INDENT4); |
michael@0 | 3207 | entry->eventEntry->appendDescription(dump); |
michael@0 | 3208 | dump.appendFormat(", targetFlags=0x%08x, resolvedAction=%d, " |
michael@0 | 3209 | "age=%0.1fms, wait=%0.1fms\n", |
michael@0 | 3210 | entry->targetFlags, entry->resolvedAction, |
michael@0 | 3211 | (currentTime - entry->eventEntry->eventTime) * 0.000001f, |
michael@0 | 3212 | (currentTime - entry->deliveryTime) * 0.000001f); |
michael@0 | 3213 | } |
michael@0 | 3214 | } else { |
michael@0 | 3215 | dump.append(INDENT3 "WaitQueue: <empty>\n"); |
michael@0 | 3216 | } |
michael@0 | 3217 | } |
michael@0 | 3218 | } else { |
michael@0 | 3219 | dump.append(INDENT "Connections: <none>\n"); |
michael@0 | 3220 | } |
michael@0 | 3221 | |
michael@0 | 3222 | if (isAppSwitchPendingLocked()) { |
michael@0 | 3223 | dump.appendFormat(INDENT "AppSwitch: pending, due in %0.1fms\n", |
michael@0 | 3224 | (mAppSwitchDueTime - now()) / 1000000.0); |
michael@0 | 3225 | } else { |
michael@0 | 3226 | dump.append(INDENT "AppSwitch: not pending\n"); |
michael@0 | 3227 | } |
michael@0 | 3228 | |
michael@0 | 3229 | dump.append(INDENT "Configuration:\n"); |
michael@0 | 3230 | dump.appendFormat(INDENT2 "KeyRepeatDelay: %0.1fms\n", |
michael@0 | 3231 | mConfig.keyRepeatDelay * 0.000001f); |
michael@0 | 3232 | dump.appendFormat(INDENT2 "KeyRepeatTimeout: %0.1fms\n", |
michael@0 | 3233 | mConfig.keyRepeatTimeout * 0.000001f); |
michael@0 | 3234 | } |
michael@0 | 3235 | |
michael@0 | 3236 | status_t InputDispatcher::registerInputChannel(const sp<InputChannel>& inputChannel, |
michael@0 | 3237 | const sp<InputWindowHandle>& inputWindowHandle, bool monitor) { |
michael@0 | 3238 | #if DEBUG_REGISTRATION |
michael@0 | 3239 | ALOGD("channel '%s' ~ registerInputChannel - monitor=%s", inputChannel->getName().string(), |
michael@0 | 3240 | toString(monitor)); |
michael@0 | 3241 | #endif |
michael@0 | 3242 | |
michael@0 | 3243 | { // acquire lock |
michael@0 | 3244 | AutoMutex _l(mLock); |
michael@0 | 3245 | |
michael@0 | 3246 | if (getConnectionIndexLocked(inputChannel) >= 0) { |
michael@0 | 3247 | ALOGW("Attempted to register already registered input channel '%s'", |
michael@0 | 3248 | inputChannel->getName().string()); |
michael@0 | 3249 | return BAD_VALUE; |
michael@0 | 3250 | } |
michael@0 | 3251 | |
michael@0 | 3252 | sp<Connection> connection = new Connection(inputChannel, inputWindowHandle, monitor); |
michael@0 | 3253 | |
michael@0 | 3254 | int fd = inputChannel->getFd(); |
michael@0 | 3255 | mConnectionsByFd.add(fd, connection); |
michael@0 | 3256 | |
michael@0 | 3257 | if (monitor) { |
michael@0 | 3258 | mMonitoringChannels.push(inputChannel); |
michael@0 | 3259 | } |
michael@0 | 3260 | |
michael@0 | 3261 | mLooper->addFd(fd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
michael@0 | 3262 | } // release lock |
michael@0 | 3263 | |
michael@0 | 3264 | // Wake the looper because some connections have changed. |
michael@0 | 3265 | mLooper->wake(); |
michael@0 | 3266 | return OK; |
michael@0 | 3267 | } |
michael@0 | 3268 | |
michael@0 | 3269 | status_t InputDispatcher::unregisterInputChannel(const sp<InputChannel>& inputChannel) { |
michael@0 | 3270 | #if DEBUG_REGISTRATION |
michael@0 | 3271 | ALOGD("channel '%s' ~ unregisterInputChannel", inputChannel->getName().string()); |
michael@0 | 3272 | #endif |
michael@0 | 3273 | |
michael@0 | 3274 | { // acquire lock |
michael@0 | 3275 | AutoMutex _l(mLock); |
michael@0 | 3276 | |
michael@0 | 3277 | status_t status = unregisterInputChannelLocked(inputChannel, false /*notify*/); |
michael@0 | 3278 | if (status) { |
michael@0 | 3279 | return status; |
michael@0 | 3280 | } |
michael@0 | 3281 | } // release lock |
michael@0 | 3282 | |
michael@0 | 3283 | // Wake the poll loop because removing the connection may have changed the current |
michael@0 | 3284 | // synchronization state. |
michael@0 | 3285 | mLooper->wake(); |
michael@0 | 3286 | return OK; |
michael@0 | 3287 | } |
michael@0 | 3288 | |
michael@0 | 3289 | status_t InputDispatcher::unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, |
michael@0 | 3290 | bool notify) { |
michael@0 | 3291 | ssize_t connectionIndex = getConnectionIndexLocked(inputChannel); |
michael@0 | 3292 | if (connectionIndex < 0) { |
michael@0 | 3293 | ALOGW("Attempted to unregister already unregistered input channel '%s'", |
michael@0 | 3294 | inputChannel->getName().string()); |
michael@0 | 3295 | return BAD_VALUE; |
michael@0 | 3296 | } |
michael@0 | 3297 | |
michael@0 | 3298 | sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex); |
michael@0 | 3299 | mConnectionsByFd.removeItemsAt(connectionIndex); |
michael@0 | 3300 | |
michael@0 | 3301 | if (connection->monitor) { |
michael@0 | 3302 | removeMonitorChannelLocked(inputChannel); |
michael@0 | 3303 | } |
michael@0 | 3304 | |
michael@0 | 3305 | mLooper->removeFd(inputChannel->getFd()); |
michael@0 | 3306 | |
michael@0 | 3307 | nsecs_t currentTime = now(); |
michael@0 | 3308 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
michael@0 | 3309 | |
michael@0 | 3310 | connection->status = Connection::STATUS_ZOMBIE; |
michael@0 | 3311 | return OK; |
michael@0 | 3312 | } |
michael@0 | 3313 | |
michael@0 | 3314 | void InputDispatcher::removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) { |
michael@0 | 3315 | for (size_t i = 0; i < mMonitoringChannels.size(); i++) { |
michael@0 | 3316 | if (mMonitoringChannels[i] == inputChannel) { |
michael@0 | 3317 | mMonitoringChannels.removeAt(i); |
michael@0 | 3318 | break; |
michael@0 | 3319 | } |
michael@0 | 3320 | } |
michael@0 | 3321 | } |
michael@0 | 3322 | |
michael@0 | 3323 | ssize_t InputDispatcher::getConnectionIndexLocked(const sp<InputChannel>& inputChannel) { |
michael@0 | 3324 | ssize_t connectionIndex = mConnectionsByFd.indexOfKey(inputChannel->getFd()); |
michael@0 | 3325 | if (connectionIndex >= 0) { |
michael@0 | 3326 | sp<Connection> connection = mConnectionsByFd.valueAt(connectionIndex); |
michael@0 | 3327 | if (connection->inputChannel.get() == inputChannel.get()) { |
michael@0 | 3328 | return connectionIndex; |
michael@0 | 3329 | } |
michael@0 | 3330 | } |
michael@0 | 3331 | |
michael@0 | 3332 | return -1; |
michael@0 | 3333 | } |
michael@0 | 3334 | |
michael@0 | 3335 | void InputDispatcher::onDispatchCycleFinishedLocked( |
michael@0 | 3336 | nsecs_t currentTime, const sp<Connection>& connection, uint32_t seq, bool handled) { |
michael@0 | 3337 | CommandEntry* commandEntry = postCommandLocked( |
michael@0 | 3338 | & InputDispatcher::doDispatchCycleFinishedLockedInterruptible); |
michael@0 | 3339 | commandEntry->connection = connection; |
michael@0 | 3340 | commandEntry->eventTime = currentTime; |
michael@0 | 3341 | commandEntry->seq = seq; |
michael@0 | 3342 | commandEntry->handled = handled; |
michael@0 | 3343 | } |
michael@0 | 3344 | |
michael@0 | 3345 | void InputDispatcher::onDispatchCycleBrokenLocked( |
michael@0 | 3346 | nsecs_t currentTime, const sp<Connection>& connection) { |
michael@0 | 3347 | ALOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
michael@0 | 3348 | connection->getInputChannelName()); |
michael@0 | 3349 | |
michael@0 | 3350 | CommandEntry* commandEntry = postCommandLocked( |
michael@0 | 3351 | & InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); |
michael@0 | 3352 | commandEntry->connection = connection; |
michael@0 | 3353 | } |
michael@0 | 3354 | |
michael@0 | 3355 | void InputDispatcher::onANRLocked( |
michael@0 | 3356 | nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle, |
michael@0 | 3357 | const sp<InputWindowHandle>& windowHandle, |
michael@0 | 3358 | nsecs_t eventTime, nsecs_t waitStartTime, const char* reason) { |
michael@0 | 3359 | float dispatchLatency = (currentTime - eventTime) * 0.000001f; |
michael@0 | 3360 | float waitDuration = (currentTime - waitStartTime) * 0.000001f; |
michael@0 | 3361 | ALOGI("Application is not responding: %s. " |
michael@0 | 3362 | "It has been %0.1fms since event, %0.1fms since wait started. Reason: %s", |
michael@0 | 3363 | getApplicationWindowLabelLocked(applicationHandle, windowHandle).string(), |
michael@0 | 3364 | dispatchLatency, waitDuration, reason); |
michael@0 | 3365 | |
michael@0 | 3366 | // Capture a record of the InputDispatcher state at the time of the ANR. |
michael@0 | 3367 | time_t t = time(NULL); |
michael@0 | 3368 | struct tm tm; |
michael@0 | 3369 | localtime_r(&t, &tm); |
michael@0 | 3370 | char timestr[64]; |
michael@0 | 3371 | strftime(timestr, sizeof(timestr), "%F %T", &tm); |
michael@0 | 3372 | mLastANRState.clear(); |
michael@0 | 3373 | mLastANRState.append(INDENT "ANR:\n"); |
michael@0 | 3374 | mLastANRState.appendFormat(INDENT2 "Time: %s\n", timestr); |
michael@0 | 3375 | mLastANRState.appendFormat(INDENT2 "Window: %s\n", |
michael@0 | 3376 | getApplicationWindowLabelLocked(applicationHandle, windowHandle).string()); |
michael@0 | 3377 | mLastANRState.appendFormat(INDENT2 "DispatchLatency: %0.1fms\n", dispatchLatency); |
michael@0 | 3378 | mLastANRState.appendFormat(INDENT2 "WaitDuration: %0.1fms\n", waitDuration); |
michael@0 | 3379 | mLastANRState.appendFormat(INDENT2 "Reason: %s\n", reason); |
michael@0 | 3380 | dumpDispatchStateLocked(mLastANRState); |
michael@0 | 3381 | |
michael@0 | 3382 | CommandEntry* commandEntry = postCommandLocked( |
michael@0 | 3383 | & InputDispatcher::doNotifyANRLockedInterruptible); |
michael@0 | 3384 | commandEntry->inputApplicationHandle = applicationHandle; |
michael@0 | 3385 | commandEntry->inputWindowHandle = windowHandle; |
michael@0 | 3386 | } |
michael@0 | 3387 | |
michael@0 | 3388 | void InputDispatcher::doNotifyConfigurationChangedInterruptible( |
michael@0 | 3389 | CommandEntry* commandEntry) { |
michael@0 | 3390 | mLock.unlock(); |
michael@0 | 3391 | |
michael@0 | 3392 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); |
michael@0 | 3393 | |
michael@0 | 3394 | mLock.lock(); |
michael@0 | 3395 | } |
michael@0 | 3396 | |
michael@0 | 3397 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible( |
michael@0 | 3398 | CommandEntry* commandEntry) { |
michael@0 | 3399 | sp<Connection> connection = commandEntry->connection; |
michael@0 | 3400 | |
michael@0 | 3401 | if (connection->status != Connection::STATUS_ZOMBIE) { |
michael@0 | 3402 | mLock.unlock(); |
michael@0 | 3403 | |
michael@0 | 3404 | mPolicy->notifyInputChannelBroken(connection->inputWindowHandle); |
michael@0 | 3405 | |
michael@0 | 3406 | mLock.lock(); |
michael@0 | 3407 | } |
michael@0 | 3408 | } |
michael@0 | 3409 | |
michael@0 | 3410 | void InputDispatcher::doNotifyANRLockedInterruptible( |
michael@0 | 3411 | CommandEntry* commandEntry) { |
michael@0 | 3412 | mLock.unlock(); |
michael@0 | 3413 | |
michael@0 | 3414 | nsecs_t newTimeout = mPolicy->notifyANR( |
michael@0 | 3415 | commandEntry->inputApplicationHandle, commandEntry->inputWindowHandle); |
michael@0 | 3416 | |
michael@0 | 3417 | mLock.lock(); |
michael@0 | 3418 | |
michael@0 | 3419 | resumeAfterTargetsNotReadyTimeoutLocked(newTimeout, |
michael@0 | 3420 | commandEntry->inputWindowHandle != NULL |
michael@0 | 3421 | ? commandEntry->inputWindowHandle->getInputChannel() : NULL); |
michael@0 | 3422 | } |
michael@0 | 3423 | |
michael@0 | 3424 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( |
michael@0 | 3425 | CommandEntry* commandEntry) { |
michael@0 | 3426 | KeyEntry* entry = commandEntry->keyEntry; |
michael@0 | 3427 | |
michael@0 | 3428 | KeyEvent event; |
michael@0 | 3429 | initializeKeyEvent(&event, entry); |
michael@0 | 3430 | |
michael@0 | 3431 | mLock.unlock(); |
michael@0 | 3432 | |
michael@0 | 3433 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(commandEntry->inputWindowHandle, |
michael@0 | 3434 | &event, entry->policyFlags); |
michael@0 | 3435 | |
michael@0 | 3436 | mLock.lock(); |
michael@0 | 3437 | |
michael@0 | 3438 | if (delay < 0) { |
michael@0 | 3439 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
michael@0 | 3440 | } else if (!delay) { |
michael@0 | 3441 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
michael@0 | 3442 | } else { |
michael@0 | 3443 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
michael@0 | 3444 | entry->interceptKeyWakeupTime = now() + delay; |
michael@0 | 3445 | } |
michael@0 | 3446 | entry->release(); |
michael@0 | 3447 | } |
michael@0 | 3448 | |
michael@0 | 3449 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible( |
michael@0 | 3450 | CommandEntry* commandEntry) { |
michael@0 | 3451 | sp<Connection> connection = commandEntry->connection; |
michael@0 | 3452 | nsecs_t finishTime = commandEntry->eventTime; |
michael@0 | 3453 | uint32_t seq = commandEntry->seq; |
michael@0 | 3454 | bool handled = commandEntry->handled; |
michael@0 | 3455 | |
michael@0 | 3456 | // Handle post-event policy actions. |
michael@0 | 3457 | DispatchEntry* dispatchEntry = connection->findWaitQueueEntry(seq); |
michael@0 | 3458 | if (dispatchEntry) { |
michael@0 | 3459 | nsecs_t eventDuration = finishTime - dispatchEntry->deliveryTime; |
michael@0 | 3460 | if (eventDuration > SLOW_EVENT_PROCESSING_WARNING_TIMEOUT) { |
michael@0 | 3461 | String8 msg; |
michael@0 | 3462 | msg.appendFormat("Window '%s' spent %0.1fms processing the last input event: ", |
michael@0 | 3463 | connection->getWindowName(), eventDuration * 0.000001f); |
michael@0 | 3464 | dispatchEntry->eventEntry->appendDescription(msg); |
michael@0 | 3465 | ALOGI("%s", msg.string()); |
michael@0 | 3466 | } |
michael@0 | 3467 | |
michael@0 | 3468 | bool restartEvent; |
michael@0 | 3469 | if (dispatchEntry->eventEntry->type == EventEntry::TYPE_KEY) { |
michael@0 | 3470 | KeyEntry* keyEntry = static_cast<KeyEntry*>(dispatchEntry->eventEntry); |
michael@0 | 3471 | restartEvent = afterKeyEventLockedInterruptible(connection, |
michael@0 | 3472 | dispatchEntry, keyEntry, handled); |
michael@0 | 3473 | } else if (dispatchEntry->eventEntry->type == EventEntry::TYPE_MOTION) { |
michael@0 | 3474 | MotionEntry* motionEntry = static_cast<MotionEntry*>(dispatchEntry->eventEntry); |
michael@0 | 3475 | restartEvent = afterMotionEventLockedInterruptible(connection, |
michael@0 | 3476 | dispatchEntry, motionEntry, handled); |
michael@0 | 3477 | } else { |
michael@0 | 3478 | restartEvent = false; |
michael@0 | 3479 | } |
michael@0 | 3480 | |
michael@0 | 3481 | // Dequeue the event and start the next cycle. |
michael@0 | 3482 | // Note that because the lock might have been released, it is possible that the |
michael@0 | 3483 | // contents of the wait queue to have been drained, so we need to double-check |
michael@0 | 3484 | // a few things. |
michael@0 | 3485 | if (dispatchEntry == connection->findWaitQueueEntry(seq)) { |
michael@0 | 3486 | connection->waitQueue.dequeue(dispatchEntry); |
michael@0 | 3487 | traceWaitQueueLengthLocked(connection); |
michael@0 | 3488 | if (restartEvent && connection->status == Connection::STATUS_NORMAL) { |
michael@0 | 3489 | connection->outboundQueue.enqueueAtHead(dispatchEntry); |
michael@0 | 3490 | traceOutboundQueueLengthLocked(connection); |
michael@0 | 3491 | } else { |
michael@0 | 3492 | releaseDispatchEntryLocked(dispatchEntry); |
michael@0 | 3493 | } |
michael@0 | 3494 | } |
michael@0 | 3495 | |
michael@0 | 3496 | // Start the next dispatch cycle for this connection. |
michael@0 | 3497 | startDispatchCycleLocked(now(), connection); |
michael@0 | 3498 | } |
michael@0 | 3499 | } |
michael@0 | 3500 | |
michael@0 | 3501 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, |
michael@0 | 3502 | DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled) { |
michael@0 | 3503 | if (!(keyEntry->flags & AKEY_EVENT_FLAG_FALLBACK)) { |
michael@0 | 3504 | // Get the fallback key state. |
michael@0 | 3505 | // Clear it out after dispatching the UP. |
michael@0 | 3506 | int32_t originalKeyCode = keyEntry->keyCode; |
michael@0 | 3507 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
michael@0 | 3508 | if (keyEntry->action == AKEY_EVENT_ACTION_UP) { |
michael@0 | 3509 | connection->inputState.removeFallbackKey(originalKeyCode); |
michael@0 | 3510 | } |
michael@0 | 3511 | |
michael@0 | 3512 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
michael@0 | 3513 | // If the application handles the original key for which we previously |
michael@0 | 3514 | // generated a fallback or if the window is not a foreground window, |
michael@0 | 3515 | // then cancel the associated fallback key, if any. |
michael@0 | 3516 | if (fallbackKeyCode != -1) { |
michael@0 | 3517 | // Dispatch the unhandled key to the policy with the cancel flag. |
michael@0 | 3518 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
michael@0 | 3519 | ALOGD("Unhandled key event: Asking policy to cancel fallback action. " |
michael@0 | 3520 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
michael@0 | 3521 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, |
michael@0 | 3522 | keyEntry->policyFlags); |
michael@0 | 3523 | #endif |
michael@0 | 3524 | KeyEvent event; |
michael@0 | 3525 | initializeKeyEvent(&event, keyEntry); |
michael@0 | 3526 | event.setFlags(event.getFlags() | AKEY_EVENT_FLAG_CANCELED); |
michael@0 | 3527 | |
michael@0 | 3528 | mLock.unlock(); |
michael@0 | 3529 | |
michael@0 | 3530 | mPolicy->dispatchUnhandledKey(connection->inputWindowHandle, |
michael@0 | 3531 | &event, keyEntry->policyFlags, &event); |
michael@0 | 3532 | |
michael@0 | 3533 | mLock.lock(); |
michael@0 | 3534 | |
michael@0 | 3535 | // Cancel the fallback key. |
michael@0 | 3536 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
michael@0 | 3537 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
michael@0 | 3538 | "application handled the original non-fallback key " |
michael@0 | 3539 | "or is no longer a foreground target, " |
michael@0 | 3540 | "canceling previously dispatched fallback key"); |
michael@0 | 3541 | options.keyCode = fallbackKeyCode; |
michael@0 | 3542 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
michael@0 | 3543 | } |
michael@0 | 3544 | connection->inputState.removeFallbackKey(originalKeyCode); |
michael@0 | 3545 | } |
michael@0 | 3546 | } else { |
michael@0 | 3547 | // If the application did not handle a non-fallback key, first check |
michael@0 | 3548 | // that we are in a good state to perform unhandled key event processing |
michael@0 | 3549 | // Then ask the policy what to do with it. |
michael@0 | 3550 | bool initialDown = keyEntry->action == AKEY_EVENT_ACTION_DOWN |
michael@0 | 3551 | && keyEntry->repeatCount == 0; |
michael@0 | 3552 | if (fallbackKeyCode == -1 && !initialDown) { |
michael@0 | 3553 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
michael@0 | 3554 | ALOGD("Unhandled key event: Skipping unhandled key event processing " |
michael@0 | 3555 | "since this is not an initial down. " |
michael@0 | 3556 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
michael@0 | 3557 | originalKeyCode, keyEntry->action, keyEntry->repeatCount, |
michael@0 | 3558 | keyEntry->policyFlags); |
michael@0 | 3559 | #endif |
michael@0 | 3560 | return false; |
michael@0 | 3561 | } |
michael@0 | 3562 | |
michael@0 | 3563 | // Dispatch the unhandled key to the policy. |
michael@0 | 3564 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
michael@0 | 3565 | ALOGD("Unhandled key event: Asking policy to perform fallback action. " |
michael@0 | 3566 | "keyCode=%d, action=%d, repeatCount=%d, policyFlags=0x%08x", |
michael@0 | 3567 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount, |
michael@0 | 3568 | keyEntry->policyFlags); |
michael@0 | 3569 | #endif |
michael@0 | 3570 | KeyEvent event; |
michael@0 | 3571 | initializeKeyEvent(&event, keyEntry); |
michael@0 | 3572 | |
michael@0 | 3573 | mLock.unlock(); |
michael@0 | 3574 | |
michael@0 | 3575 | bool fallback = mPolicy->dispatchUnhandledKey(connection->inputWindowHandle, |
michael@0 | 3576 | &event, keyEntry->policyFlags, &event); |
michael@0 | 3577 | |
michael@0 | 3578 | mLock.lock(); |
michael@0 | 3579 | |
michael@0 | 3580 | if (connection->status != Connection::STATUS_NORMAL) { |
michael@0 | 3581 | connection->inputState.removeFallbackKey(originalKeyCode); |
michael@0 | 3582 | return false; |
michael@0 | 3583 | } |
michael@0 | 3584 | |
michael@0 | 3585 | // Latch the fallback keycode for this key on an initial down. |
michael@0 | 3586 | // The fallback keycode cannot change at any other point in the lifecycle. |
michael@0 | 3587 | if (initialDown) { |
michael@0 | 3588 | if (fallback) { |
michael@0 | 3589 | fallbackKeyCode = event.getKeyCode(); |
michael@0 | 3590 | } else { |
michael@0 | 3591 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
michael@0 | 3592 | } |
michael@0 | 3593 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
michael@0 | 3594 | } |
michael@0 | 3595 | |
michael@0 | 3596 | ALOG_ASSERT(fallbackKeyCode != -1); |
michael@0 | 3597 | |
michael@0 | 3598 | // Cancel the fallback key if the policy decides not to send it anymore. |
michael@0 | 3599 | // We will continue to dispatch the key to the policy but we will no |
michael@0 | 3600 | // longer dispatch a fallback key to the application. |
michael@0 | 3601 | if (fallbackKeyCode != AKEYCODE_UNKNOWN |
michael@0 | 3602 | && (!fallback || fallbackKeyCode != event.getKeyCode())) { |
michael@0 | 3603 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
michael@0 | 3604 | if (fallback) { |
michael@0 | 3605 | ALOGD("Unhandled key event: Policy requested to send key %d" |
michael@0 | 3606 | "as a fallback for %d, but on the DOWN it had requested " |
michael@0 | 3607 | "to send %d instead. Fallback canceled.", |
michael@0 | 3608 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
michael@0 | 3609 | } else { |
michael@0 | 3610 | ALOGD("Unhandled key event: Policy did not request fallback for %d, " |
michael@0 | 3611 | "but on the DOWN it had requested to send %d. " |
michael@0 | 3612 | "Fallback canceled.", |
michael@0 | 3613 | originalKeyCode, fallbackKeyCode); |
michael@0 | 3614 | } |
michael@0 | 3615 | #endif |
michael@0 | 3616 | |
michael@0 | 3617 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
michael@0 | 3618 | "canceling fallback, policy no longer desires it"); |
michael@0 | 3619 | options.keyCode = fallbackKeyCode; |
michael@0 | 3620 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
michael@0 | 3621 | |
michael@0 | 3622 | fallback = false; |
michael@0 | 3623 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
michael@0 | 3624 | if (keyEntry->action != AKEY_EVENT_ACTION_UP) { |
michael@0 | 3625 | connection->inputState.setFallbackKey(originalKeyCode, |
michael@0 | 3626 | fallbackKeyCode); |
michael@0 | 3627 | } |
michael@0 | 3628 | } |
michael@0 | 3629 | |
michael@0 | 3630 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
michael@0 | 3631 | { |
michael@0 | 3632 | String8 msg; |
michael@0 | 3633 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
michael@0 | 3634 | connection->inputState.getFallbackKeys(); |
michael@0 | 3635 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
michael@0 | 3636 | msg.appendFormat(", %d->%d", fallbackKeys.keyAt(i), |
michael@0 | 3637 | fallbackKeys.valueAt(i)); |
michael@0 | 3638 | } |
michael@0 | 3639 | ALOGD("Unhandled key event: %d currently tracked fallback keys%s.", |
michael@0 | 3640 | fallbackKeys.size(), msg.string()); |
michael@0 | 3641 | } |
michael@0 | 3642 | #endif |
michael@0 | 3643 | |
michael@0 | 3644 | if (fallback) { |
michael@0 | 3645 | // Restart the dispatch cycle using the fallback key. |
michael@0 | 3646 | keyEntry->eventTime = event.getEventTime(); |
michael@0 | 3647 | keyEntry->deviceId = event.getDeviceId(); |
michael@0 | 3648 | keyEntry->source = event.getSource(); |
michael@0 | 3649 | keyEntry->flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
michael@0 | 3650 | keyEntry->keyCode = fallbackKeyCode; |
michael@0 | 3651 | keyEntry->scanCode = event.getScanCode(); |
michael@0 | 3652 | keyEntry->metaState = event.getMetaState(); |
michael@0 | 3653 | keyEntry->repeatCount = event.getRepeatCount(); |
michael@0 | 3654 | keyEntry->downTime = event.getDownTime(); |
michael@0 | 3655 | keyEntry->syntheticRepeat = false; |
michael@0 | 3656 | |
michael@0 | 3657 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
michael@0 | 3658 | ALOGD("Unhandled key event: Dispatching fallback key. " |
michael@0 | 3659 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
michael@0 | 3660 | originalKeyCode, fallbackKeyCode, keyEntry->metaState); |
michael@0 | 3661 | #endif |
michael@0 | 3662 | return true; // restart the event |
michael@0 | 3663 | } else { |
michael@0 | 3664 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
michael@0 | 3665 | ALOGD("Unhandled key event: No fallback key."); |
michael@0 | 3666 | #endif |
michael@0 | 3667 | } |
michael@0 | 3668 | } |
michael@0 | 3669 | } |
michael@0 | 3670 | return false; |
michael@0 | 3671 | } |
michael@0 | 3672 | |
michael@0 | 3673 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, |
michael@0 | 3674 | DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled) { |
michael@0 | 3675 | return false; |
michael@0 | 3676 | } |
michael@0 | 3677 | |
michael@0 | 3678 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { |
michael@0 | 3679 | mLock.unlock(); |
michael@0 | 3680 | |
michael@0 | 3681 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType); |
michael@0 | 3682 | |
michael@0 | 3683 | mLock.lock(); |
michael@0 | 3684 | } |
michael@0 | 3685 | |
michael@0 | 3686 | void InputDispatcher::initializeKeyEvent(KeyEvent* event, const KeyEntry* entry) { |
michael@0 | 3687 | event->initialize(entry->deviceId, entry->source, entry->action, entry->flags, |
michael@0 | 3688 | entry->keyCode, entry->scanCode, entry->metaState, entry->repeatCount, |
michael@0 | 3689 | entry->downTime, entry->eventTime); |
michael@0 | 3690 | } |
michael@0 | 3691 | |
michael@0 | 3692 | void InputDispatcher::updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry, |
michael@0 | 3693 | int32_t injectionResult, nsecs_t timeSpentWaitingForApplication) { |
michael@0 | 3694 | // TODO Write some statistics about how long we spend waiting. |
michael@0 | 3695 | } |
michael@0 | 3696 | |
michael@0 | 3697 | void InputDispatcher::traceInboundQueueLengthLocked() { |
michael@0 | 3698 | #ifdef HAVE_ANDROID_OS |
michael@0 | 3699 | if (ATRACE_ENABLED()) { |
michael@0 | 3700 | ATRACE_INT("iq", mInboundQueue.count()); |
michael@0 | 3701 | } |
michael@0 | 3702 | #endif |
michael@0 | 3703 | } |
michael@0 | 3704 | |
michael@0 | 3705 | void InputDispatcher::traceOutboundQueueLengthLocked(const sp<Connection>& connection) { |
michael@0 | 3706 | #ifdef HAVE_ANDROID_OS |
michael@0 | 3707 | if (ATRACE_ENABLED()) { |
michael@0 | 3708 | char counterName[40]; |
michael@0 | 3709 | snprintf(counterName, sizeof(counterName), "oq:%s", connection->getWindowName()); |
michael@0 | 3710 | ATRACE_INT(counterName, connection->outboundQueue.count()); |
michael@0 | 3711 | } |
michael@0 | 3712 | #endif |
michael@0 | 3713 | } |
michael@0 | 3714 | |
michael@0 | 3715 | void InputDispatcher::traceWaitQueueLengthLocked(const sp<Connection>& connection) { |
michael@0 | 3716 | #ifdef HAVE_ANDROID_OS |
michael@0 | 3717 | if (ATRACE_ENABLED()) { |
michael@0 | 3718 | char counterName[40]; |
michael@0 | 3719 | snprintf(counterName, sizeof(counterName), "wq:%s", connection->getWindowName()); |
michael@0 | 3720 | ATRACE_INT(counterName, connection->waitQueue.count()); |
michael@0 | 3721 | } |
michael@0 | 3722 | #endif |
michael@0 | 3723 | } |
michael@0 | 3724 | |
michael@0 | 3725 | void InputDispatcher::dump(String8& dump) { |
michael@0 | 3726 | AutoMutex _l(mLock); |
michael@0 | 3727 | |
michael@0 | 3728 | dump.append("Input Dispatcher State:\n"); |
michael@0 | 3729 | dumpDispatchStateLocked(dump); |
michael@0 | 3730 | |
michael@0 | 3731 | if (!mLastANRState.isEmpty()) { |
michael@0 | 3732 | dump.append("\nInput Dispatcher State at time of last ANR:\n"); |
michael@0 | 3733 | dump.append(mLastANRState); |
michael@0 | 3734 | } |
michael@0 | 3735 | } |
michael@0 | 3736 | |
michael@0 | 3737 | void InputDispatcher::monitor() { |
michael@0 | 3738 | // Acquire and release the lock to ensure that the dispatcher has not deadlocked. |
michael@0 | 3739 | mLock.lock(); |
michael@0 | 3740 | mLooper->wake(); |
michael@0 | 3741 | mDispatcherIsAliveCondition.wait(mLock); |
michael@0 | 3742 | mLock.unlock(); |
michael@0 | 3743 | } |
michael@0 | 3744 | |
michael@0 | 3745 | |
michael@0 | 3746 | // --- InputDispatcher::Queue --- |
michael@0 | 3747 | |
michael@0 | 3748 | template <typename T> |
michael@0 | 3749 | uint32_t InputDispatcher::Queue<T>::count() const { |
michael@0 | 3750 | uint32_t result = 0; |
michael@0 | 3751 | for (const T* entry = head; entry; entry = entry->next) { |
michael@0 | 3752 | result += 1; |
michael@0 | 3753 | } |
michael@0 | 3754 | return result; |
michael@0 | 3755 | } |
michael@0 | 3756 | |
michael@0 | 3757 | |
michael@0 | 3758 | // --- InputDispatcher::InjectionState --- |
michael@0 | 3759 | |
michael@0 | 3760 | InputDispatcher::InjectionState::InjectionState(int32_t injectorPid, int32_t injectorUid) : |
michael@0 | 3761 | refCount(1), |
michael@0 | 3762 | injectorPid(injectorPid), injectorUid(injectorUid), |
michael@0 | 3763 | injectionResult(INPUT_EVENT_INJECTION_PENDING), injectionIsAsync(false), |
michael@0 | 3764 | pendingForegroundDispatches(0) { |
michael@0 | 3765 | } |
michael@0 | 3766 | |
michael@0 | 3767 | InputDispatcher::InjectionState::~InjectionState() { |
michael@0 | 3768 | } |
michael@0 | 3769 | |
michael@0 | 3770 | void InputDispatcher::InjectionState::release() { |
michael@0 | 3771 | refCount -= 1; |
michael@0 | 3772 | if (refCount == 0) { |
michael@0 | 3773 | delete this; |
michael@0 | 3774 | } else { |
michael@0 | 3775 | ALOG_ASSERT(refCount > 0); |
michael@0 | 3776 | } |
michael@0 | 3777 | } |
michael@0 | 3778 | |
michael@0 | 3779 | |
michael@0 | 3780 | // --- InputDispatcher::EventEntry --- |
michael@0 | 3781 | |
michael@0 | 3782 | InputDispatcher::EventEntry::EventEntry(int32_t type, nsecs_t eventTime, uint32_t policyFlags) : |
michael@0 | 3783 | refCount(1), type(type), eventTime(eventTime), policyFlags(policyFlags), |
michael@0 | 3784 | injectionState(NULL), dispatchInProgress(false) { |
michael@0 | 3785 | } |
michael@0 | 3786 | |
michael@0 | 3787 | InputDispatcher::EventEntry::~EventEntry() { |
michael@0 | 3788 | releaseInjectionState(); |
michael@0 | 3789 | } |
michael@0 | 3790 | |
michael@0 | 3791 | void InputDispatcher::EventEntry::release() { |
michael@0 | 3792 | refCount -= 1; |
michael@0 | 3793 | if (refCount == 0) { |
michael@0 | 3794 | delete this; |
michael@0 | 3795 | } else { |
michael@0 | 3796 | ALOG_ASSERT(refCount > 0); |
michael@0 | 3797 | } |
michael@0 | 3798 | } |
michael@0 | 3799 | |
michael@0 | 3800 | void InputDispatcher::EventEntry::releaseInjectionState() { |
michael@0 | 3801 | if (injectionState) { |
michael@0 | 3802 | injectionState->release(); |
michael@0 | 3803 | injectionState = NULL; |
michael@0 | 3804 | } |
michael@0 | 3805 | } |
michael@0 | 3806 | |
michael@0 | 3807 | |
michael@0 | 3808 | // --- InputDispatcher::ConfigurationChangedEntry --- |
michael@0 | 3809 | |
michael@0 | 3810 | InputDispatcher::ConfigurationChangedEntry::ConfigurationChangedEntry(nsecs_t eventTime) : |
michael@0 | 3811 | EventEntry(TYPE_CONFIGURATION_CHANGED, eventTime, 0) { |
michael@0 | 3812 | } |
michael@0 | 3813 | |
michael@0 | 3814 | InputDispatcher::ConfigurationChangedEntry::~ConfigurationChangedEntry() { |
michael@0 | 3815 | } |
michael@0 | 3816 | |
michael@0 | 3817 | void InputDispatcher::ConfigurationChangedEntry::appendDescription(String8& msg) const { |
michael@0 | 3818 | msg.append("ConfigurationChangedEvent()"); |
michael@0 | 3819 | } |
michael@0 | 3820 | |
michael@0 | 3821 | |
michael@0 | 3822 | // --- InputDispatcher::DeviceResetEntry --- |
michael@0 | 3823 | |
michael@0 | 3824 | InputDispatcher::DeviceResetEntry::DeviceResetEntry(nsecs_t eventTime, int32_t deviceId) : |
michael@0 | 3825 | EventEntry(TYPE_DEVICE_RESET, eventTime, 0), |
michael@0 | 3826 | deviceId(deviceId) { |
michael@0 | 3827 | } |
michael@0 | 3828 | |
michael@0 | 3829 | InputDispatcher::DeviceResetEntry::~DeviceResetEntry() { |
michael@0 | 3830 | } |
michael@0 | 3831 | |
michael@0 | 3832 | void InputDispatcher::DeviceResetEntry::appendDescription(String8& msg) const { |
michael@0 | 3833 | msg.appendFormat("DeviceResetEvent(deviceId=%d)", deviceId); |
michael@0 | 3834 | } |
michael@0 | 3835 | |
michael@0 | 3836 | |
michael@0 | 3837 | // --- InputDispatcher::KeyEntry --- |
michael@0 | 3838 | |
michael@0 | 3839 | InputDispatcher::KeyEntry::KeyEntry(nsecs_t eventTime, |
michael@0 | 3840 | int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action, |
michael@0 | 3841 | int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState, |
michael@0 | 3842 | int32_t repeatCount, nsecs_t downTime) : |
michael@0 | 3843 | EventEntry(TYPE_KEY, eventTime, policyFlags), |
michael@0 | 3844 | deviceId(deviceId), source(source), action(action), flags(flags), |
michael@0 | 3845 | keyCode(keyCode), scanCode(scanCode), metaState(metaState), |
michael@0 | 3846 | repeatCount(repeatCount), downTime(downTime), |
michael@0 | 3847 | syntheticRepeat(false), interceptKeyResult(KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN), |
michael@0 | 3848 | interceptKeyWakeupTime(0) { |
michael@0 | 3849 | } |
michael@0 | 3850 | |
michael@0 | 3851 | InputDispatcher::KeyEntry::~KeyEntry() { |
michael@0 | 3852 | } |
michael@0 | 3853 | |
michael@0 | 3854 | void InputDispatcher::KeyEntry::appendDescription(String8& msg) const { |
michael@0 | 3855 | msg.appendFormat("KeyEvent(action=%d, deviceId=%d, source=0x%08x)", |
michael@0 | 3856 | action, deviceId, source); |
michael@0 | 3857 | } |
michael@0 | 3858 | |
michael@0 | 3859 | void InputDispatcher::KeyEntry::recycle() { |
michael@0 | 3860 | releaseInjectionState(); |
michael@0 | 3861 | |
michael@0 | 3862 | dispatchInProgress = false; |
michael@0 | 3863 | syntheticRepeat = false; |
michael@0 | 3864 | interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
michael@0 | 3865 | interceptKeyWakeupTime = 0; |
michael@0 | 3866 | } |
michael@0 | 3867 | |
michael@0 | 3868 | |
michael@0 | 3869 | // --- InputDispatcher::MotionEntry --- |
michael@0 | 3870 | |
michael@0 | 3871 | InputDispatcher::MotionEntry::MotionEntry(nsecs_t eventTime, |
michael@0 | 3872 | int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action, int32_t flags, |
michael@0 | 3873 | int32_t metaState, int32_t buttonState, |
michael@0 | 3874 | int32_t edgeFlags, float xPrecision, float yPrecision, |
michael@0 | 3875 | nsecs_t downTime, int32_t displayId, uint32_t pointerCount, |
michael@0 | 3876 | const PointerProperties* pointerProperties, const PointerCoords* pointerCoords) : |
michael@0 | 3877 | EventEntry(TYPE_MOTION, eventTime, policyFlags), |
michael@0 | 3878 | eventTime(eventTime), |
michael@0 | 3879 | deviceId(deviceId), source(source), action(action), flags(flags), |
michael@0 | 3880 | metaState(metaState), buttonState(buttonState), edgeFlags(edgeFlags), |
michael@0 | 3881 | xPrecision(xPrecision), yPrecision(yPrecision), |
michael@0 | 3882 | downTime(downTime), displayId(displayId), pointerCount(pointerCount) { |
michael@0 | 3883 | for (uint32_t i = 0; i < pointerCount; i++) { |
michael@0 | 3884 | this->pointerProperties[i].copyFrom(pointerProperties[i]); |
michael@0 | 3885 | this->pointerCoords[i].copyFrom(pointerCoords[i]); |
michael@0 | 3886 | } |
michael@0 | 3887 | } |
michael@0 | 3888 | |
michael@0 | 3889 | InputDispatcher::MotionEntry::~MotionEntry() { |
michael@0 | 3890 | } |
michael@0 | 3891 | |
michael@0 | 3892 | void InputDispatcher::MotionEntry::appendDescription(String8& msg) const { |
michael@0 | 3893 | msg.appendFormat("MotionEvent(action=%d, deviceId=%d, source=0x%08x, displayId=%d)", |
michael@0 | 3894 | action, deviceId, source, displayId); |
michael@0 | 3895 | } |
michael@0 | 3896 | |
michael@0 | 3897 | |
michael@0 | 3898 | // --- InputDispatcher::DispatchEntry --- |
michael@0 | 3899 | |
michael@0 | 3900 | volatile int32_t InputDispatcher::DispatchEntry::sNextSeqAtomic; |
michael@0 | 3901 | |
michael@0 | 3902 | InputDispatcher::DispatchEntry::DispatchEntry(EventEntry* eventEntry, |
michael@0 | 3903 | int32_t targetFlags, float xOffset, float yOffset, float scaleFactor) : |
michael@0 | 3904 | seq(nextSeq()), |
michael@0 | 3905 | eventEntry(eventEntry), targetFlags(targetFlags), |
michael@0 | 3906 | xOffset(xOffset), yOffset(yOffset), scaleFactor(scaleFactor), |
michael@0 | 3907 | deliveryTime(0), resolvedAction(0), resolvedFlags(0) { |
michael@0 | 3908 | eventEntry->refCount += 1; |
michael@0 | 3909 | } |
michael@0 | 3910 | |
michael@0 | 3911 | InputDispatcher::DispatchEntry::~DispatchEntry() { |
michael@0 | 3912 | eventEntry->release(); |
michael@0 | 3913 | } |
michael@0 | 3914 | |
michael@0 | 3915 | uint32_t InputDispatcher::DispatchEntry::nextSeq() { |
michael@0 | 3916 | // Sequence number 0 is reserved and will never be returned. |
michael@0 | 3917 | uint32_t seq; |
michael@0 | 3918 | do { |
michael@0 | 3919 | seq = android_atomic_inc(&sNextSeqAtomic); |
michael@0 | 3920 | } while (!seq); |
michael@0 | 3921 | return seq; |
michael@0 | 3922 | } |
michael@0 | 3923 | |
michael@0 | 3924 | |
michael@0 | 3925 | // --- InputDispatcher::InputState --- |
michael@0 | 3926 | |
michael@0 | 3927 | InputDispatcher::InputState::InputState() { |
michael@0 | 3928 | } |
michael@0 | 3929 | |
michael@0 | 3930 | InputDispatcher::InputState::~InputState() { |
michael@0 | 3931 | } |
michael@0 | 3932 | |
michael@0 | 3933 | bool InputDispatcher::InputState::isNeutral() const { |
michael@0 | 3934 | return mKeyMementos.isEmpty() && mMotionMementos.isEmpty(); |
michael@0 | 3935 | } |
michael@0 | 3936 | |
michael@0 | 3937 | bool InputDispatcher::InputState::isHovering(int32_t deviceId, uint32_t source, |
michael@0 | 3938 | int32_t displayId) const { |
michael@0 | 3939 | for (size_t i = 0; i < mMotionMementos.size(); i++) { |
michael@0 | 3940 | const MotionMemento& memento = mMotionMementos.itemAt(i); |
michael@0 | 3941 | if (memento.deviceId == deviceId |
michael@0 | 3942 | && memento.source == source |
michael@0 | 3943 | && memento.displayId == displayId |
michael@0 | 3944 | && memento.hovering) { |
michael@0 | 3945 | return true; |
michael@0 | 3946 | } |
michael@0 | 3947 | } |
michael@0 | 3948 | return false; |
michael@0 | 3949 | } |
michael@0 | 3950 | |
michael@0 | 3951 | bool InputDispatcher::InputState::trackKey(const KeyEntry* entry, |
michael@0 | 3952 | int32_t action, int32_t flags) { |
michael@0 | 3953 | switch (action) { |
michael@0 | 3954 | case AKEY_EVENT_ACTION_UP: { |
michael@0 | 3955 | if (entry->flags & AKEY_EVENT_FLAG_FALLBACK) { |
michael@0 | 3956 | for (size_t i = 0; i < mFallbackKeys.size(); ) { |
michael@0 | 3957 | if (mFallbackKeys.valueAt(i) == entry->keyCode) { |
michael@0 | 3958 | mFallbackKeys.removeItemsAt(i); |
michael@0 | 3959 | } else { |
michael@0 | 3960 | i += 1; |
michael@0 | 3961 | } |
michael@0 | 3962 | } |
michael@0 | 3963 | } |
michael@0 | 3964 | ssize_t index = findKeyMemento(entry); |
michael@0 | 3965 | if (index >= 0) { |
michael@0 | 3966 | mKeyMementos.removeAt(index); |
michael@0 | 3967 | return true; |
michael@0 | 3968 | } |
michael@0 | 3969 | /* FIXME: We can't just drop the key up event because that prevents creating |
michael@0 | 3970 | * popup windows that are automatically shown when a key is held and then |
michael@0 | 3971 | * dismissed when the key is released. The problem is that the popup will |
michael@0 | 3972 | * not have received the original key down, so the key up will be considered |
michael@0 | 3973 | * to be inconsistent with its observed state. We could perhaps handle this |
michael@0 | 3974 | * by synthesizing a key down but that will cause other problems. |
michael@0 | 3975 | * |
michael@0 | 3976 | * So for now, allow inconsistent key up events to be dispatched. |
michael@0 | 3977 | * |
michael@0 | 3978 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
michael@0 | 3979 | ALOGD("Dropping inconsistent key up event: deviceId=%d, source=%08x, " |
michael@0 | 3980 | "keyCode=%d, scanCode=%d", |
michael@0 | 3981 | entry->deviceId, entry->source, entry->keyCode, entry->scanCode); |
michael@0 | 3982 | #endif |
michael@0 | 3983 | return false; |
michael@0 | 3984 | */ |
michael@0 | 3985 | return true; |
michael@0 | 3986 | } |
michael@0 | 3987 | |
michael@0 | 3988 | case AKEY_EVENT_ACTION_DOWN: { |
michael@0 | 3989 | ssize_t index = findKeyMemento(entry); |
michael@0 | 3990 | if (index >= 0) { |
michael@0 | 3991 | mKeyMementos.removeAt(index); |
michael@0 | 3992 | } |
michael@0 | 3993 | addKeyMemento(entry, flags); |
michael@0 | 3994 | return true; |
michael@0 | 3995 | } |
michael@0 | 3996 | |
michael@0 | 3997 | default: |
michael@0 | 3998 | return true; |
michael@0 | 3999 | } |
michael@0 | 4000 | } |
michael@0 | 4001 | |
michael@0 | 4002 | bool InputDispatcher::InputState::trackMotion(const MotionEntry* entry, |
michael@0 | 4003 | int32_t action, int32_t flags) { |
michael@0 | 4004 | int32_t actionMasked = action & AMOTION_EVENT_ACTION_MASK; |
michael@0 | 4005 | switch (actionMasked) { |
michael@0 | 4006 | case AMOTION_EVENT_ACTION_UP: |
michael@0 | 4007 | case AMOTION_EVENT_ACTION_CANCEL: { |
michael@0 | 4008 | ssize_t index = findMotionMemento(entry, false /*hovering*/); |
michael@0 | 4009 | if (index >= 0) { |
michael@0 | 4010 | mMotionMementos.removeAt(index); |
michael@0 | 4011 | return true; |
michael@0 | 4012 | } |
michael@0 | 4013 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
michael@0 | 4014 | ALOGD("Dropping inconsistent motion up or cancel event: deviceId=%d, source=%08x, " |
michael@0 | 4015 | "actionMasked=%d", |
michael@0 | 4016 | entry->deviceId, entry->source, actionMasked); |
michael@0 | 4017 | #endif |
michael@0 | 4018 | return false; |
michael@0 | 4019 | } |
michael@0 | 4020 | |
michael@0 | 4021 | case AMOTION_EVENT_ACTION_DOWN: { |
michael@0 | 4022 | ssize_t index = findMotionMemento(entry, false /*hovering*/); |
michael@0 | 4023 | if (index >= 0) { |
michael@0 | 4024 | mMotionMementos.removeAt(index); |
michael@0 | 4025 | } |
michael@0 | 4026 | addMotionMemento(entry, flags, false /*hovering*/); |
michael@0 | 4027 | return true; |
michael@0 | 4028 | } |
michael@0 | 4029 | |
michael@0 | 4030 | case AMOTION_EVENT_ACTION_POINTER_UP: |
michael@0 | 4031 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
michael@0 | 4032 | case AMOTION_EVENT_ACTION_MOVE: { |
michael@0 | 4033 | ssize_t index = findMotionMemento(entry, false /*hovering*/); |
michael@0 | 4034 | if (index >= 0) { |
michael@0 | 4035 | MotionMemento& memento = mMotionMementos.editItemAt(index); |
michael@0 | 4036 | memento.setPointers(entry); |
michael@0 | 4037 | return true; |
michael@0 | 4038 | } |
michael@0 | 4039 | if (actionMasked == AMOTION_EVENT_ACTION_MOVE |
michael@0 | 4040 | && (entry->source & (AINPUT_SOURCE_CLASS_JOYSTICK |
michael@0 | 4041 | | AINPUT_SOURCE_CLASS_NAVIGATION))) { |
michael@0 | 4042 | // Joysticks and trackballs can send MOVE events without corresponding DOWN or UP. |
michael@0 | 4043 | return true; |
michael@0 | 4044 | } |
michael@0 | 4045 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
michael@0 | 4046 | ALOGD("Dropping inconsistent motion pointer up/down or move event: " |
michael@0 | 4047 | "deviceId=%d, source=%08x, actionMasked=%d", |
michael@0 | 4048 | entry->deviceId, entry->source, actionMasked); |
michael@0 | 4049 | #endif |
michael@0 | 4050 | return false; |
michael@0 | 4051 | } |
michael@0 | 4052 | |
michael@0 | 4053 | case AMOTION_EVENT_ACTION_HOVER_EXIT: { |
michael@0 | 4054 | ssize_t index = findMotionMemento(entry, true /*hovering*/); |
michael@0 | 4055 | if (index >= 0) { |
michael@0 | 4056 | mMotionMementos.removeAt(index); |
michael@0 | 4057 | return true; |
michael@0 | 4058 | } |
michael@0 | 4059 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
michael@0 | 4060 | ALOGD("Dropping inconsistent motion hover exit event: deviceId=%d, source=%08x", |
michael@0 | 4061 | entry->deviceId, entry->source); |
michael@0 | 4062 | #endif |
michael@0 | 4063 | return false; |
michael@0 | 4064 | } |
michael@0 | 4065 | |
michael@0 | 4066 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
michael@0 | 4067 | case AMOTION_EVENT_ACTION_HOVER_MOVE: { |
michael@0 | 4068 | ssize_t index = findMotionMemento(entry, true /*hovering*/); |
michael@0 | 4069 | if (index >= 0) { |
michael@0 | 4070 | mMotionMementos.removeAt(index); |
michael@0 | 4071 | } |
michael@0 | 4072 | addMotionMemento(entry, flags, true /*hovering*/); |
michael@0 | 4073 | return true; |
michael@0 | 4074 | } |
michael@0 | 4075 | |
michael@0 | 4076 | default: |
michael@0 | 4077 | return true; |
michael@0 | 4078 | } |
michael@0 | 4079 | } |
michael@0 | 4080 | |
michael@0 | 4081 | ssize_t InputDispatcher::InputState::findKeyMemento(const KeyEntry* entry) const { |
michael@0 | 4082 | for (size_t i = 0; i < mKeyMementos.size(); i++) { |
michael@0 | 4083 | const KeyMemento& memento = mKeyMementos.itemAt(i); |
michael@0 | 4084 | if (memento.deviceId == entry->deviceId |
michael@0 | 4085 | && memento.source == entry->source |
michael@0 | 4086 | && memento.keyCode == entry->keyCode |
michael@0 | 4087 | && memento.scanCode == entry->scanCode) { |
michael@0 | 4088 | return i; |
michael@0 | 4089 | } |
michael@0 | 4090 | } |
michael@0 | 4091 | return -1; |
michael@0 | 4092 | } |
michael@0 | 4093 | |
michael@0 | 4094 | ssize_t InputDispatcher::InputState::findMotionMemento(const MotionEntry* entry, |
michael@0 | 4095 | bool hovering) const { |
michael@0 | 4096 | for (size_t i = 0; i < mMotionMementos.size(); i++) { |
michael@0 | 4097 | const MotionMemento& memento = mMotionMementos.itemAt(i); |
michael@0 | 4098 | if (memento.deviceId == entry->deviceId |
michael@0 | 4099 | && memento.source == entry->source |
michael@0 | 4100 | && memento.displayId == entry->displayId |
michael@0 | 4101 | && memento.hovering == hovering) { |
michael@0 | 4102 | return i; |
michael@0 | 4103 | } |
michael@0 | 4104 | } |
michael@0 | 4105 | return -1; |
michael@0 | 4106 | } |
michael@0 | 4107 | |
michael@0 | 4108 | void InputDispatcher::InputState::addKeyMemento(const KeyEntry* entry, int32_t flags) { |
michael@0 | 4109 | mKeyMementos.push(); |
michael@0 | 4110 | KeyMemento& memento = mKeyMementos.editTop(); |
michael@0 | 4111 | memento.deviceId = entry->deviceId; |
michael@0 | 4112 | memento.source = entry->source; |
michael@0 | 4113 | memento.keyCode = entry->keyCode; |
michael@0 | 4114 | memento.scanCode = entry->scanCode; |
michael@0 | 4115 | memento.metaState = entry->metaState; |
michael@0 | 4116 | memento.flags = flags; |
michael@0 | 4117 | memento.downTime = entry->downTime; |
michael@0 | 4118 | memento.policyFlags = entry->policyFlags; |
michael@0 | 4119 | } |
michael@0 | 4120 | |
michael@0 | 4121 | void InputDispatcher::InputState::addMotionMemento(const MotionEntry* entry, |
michael@0 | 4122 | int32_t flags, bool hovering) { |
michael@0 | 4123 | mMotionMementos.push(); |
michael@0 | 4124 | MotionMemento& memento = mMotionMementos.editTop(); |
michael@0 | 4125 | memento.deviceId = entry->deviceId; |
michael@0 | 4126 | memento.source = entry->source; |
michael@0 | 4127 | memento.flags = flags; |
michael@0 | 4128 | memento.xPrecision = entry->xPrecision; |
michael@0 | 4129 | memento.yPrecision = entry->yPrecision; |
michael@0 | 4130 | memento.downTime = entry->downTime; |
michael@0 | 4131 | memento.displayId = entry->displayId; |
michael@0 | 4132 | memento.setPointers(entry); |
michael@0 | 4133 | memento.hovering = hovering; |
michael@0 | 4134 | memento.policyFlags = entry->policyFlags; |
michael@0 | 4135 | } |
michael@0 | 4136 | |
michael@0 | 4137 | void InputDispatcher::InputState::MotionMemento::setPointers(const MotionEntry* entry) { |
michael@0 | 4138 | pointerCount = entry->pointerCount; |
michael@0 | 4139 | for (uint32_t i = 0; i < entry->pointerCount; i++) { |
michael@0 | 4140 | pointerProperties[i].copyFrom(entry->pointerProperties[i]); |
michael@0 | 4141 | pointerCoords[i].copyFrom(entry->pointerCoords[i]); |
michael@0 | 4142 | } |
michael@0 | 4143 | } |
michael@0 | 4144 | |
michael@0 | 4145 | void InputDispatcher::InputState::synthesizeCancelationEvents(nsecs_t currentTime, |
michael@0 | 4146 | Vector<EventEntry*>& outEvents, const CancelationOptions& options) { |
michael@0 | 4147 | for (size_t i = 0; i < mKeyMementos.size(); i++) { |
michael@0 | 4148 | const KeyMemento& memento = mKeyMementos.itemAt(i); |
michael@0 | 4149 | if (shouldCancelKey(memento, options)) { |
michael@0 | 4150 | outEvents.push(new KeyEntry(currentTime, |
michael@0 | 4151 | memento.deviceId, memento.source, memento.policyFlags, |
michael@0 | 4152 | AKEY_EVENT_ACTION_UP, memento.flags | AKEY_EVENT_FLAG_CANCELED, |
michael@0 | 4153 | memento.keyCode, memento.scanCode, memento.metaState, 0, memento.downTime)); |
michael@0 | 4154 | } |
michael@0 | 4155 | } |
michael@0 | 4156 | |
michael@0 | 4157 | for (size_t i = 0; i < mMotionMementos.size(); i++) { |
michael@0 | 4158 | const MotionMemento& memento = mMotionMementos.itemAt(i); |
michael@0 | 4159 | if (shouldCancelMotion(memento, options)) { |
michael@0 | 4160 | outEvents.push(new MotionEntry(currentTime, |
michael@0 | 4161 | memento.deviceId, memento.source, memento.policyFlags, |
michael@0 | 4162 | memento.hovering |
michael@0 | 4163 | ? AMOTION_EVENT_ACTION_HOVER_EXIT |
michael@0 | 4164 | : AMOTION_EVENT_ACTION_CANCEL, |
michael@0 | 4165 | memento.flags, 0, 0, 0, |
michael@0 | 4166 | memento.xPrecision, memento.yPrecision, memento.downTime, |
michael@0 | 4167 | memento.displayId, |
michael@0 | 4168 | memento.pointerCount, memento.pointerProperties, memento.pointerCoords)); |
michael@0 | 4169 | } |
michael@0 | 4170 | } |
michael@0 | 4171 | } |
michael@0 | 4172 | |
michael@0 | 4173 | void InputDispatcher::InputState::clear() { |
michael@0 | 4174 | mKeyMementos.clear(); |
michael@0 | 4175 | mMotionMementos.clear(); |
michael@0 | 4176 | mFallbackKeys.clear(); |
michael@0 | 4177 | } |
michael@0 | 4178 | |
michael@0 | 4179 | void InputDispatcher::InputState::copyPointerStateTo(InputState& other) const { |
michael@0 | 4180 | for (size_t i = 0; i < mMotionMementos.size(); i++) { |
michael@0 | 4181 | const MotionMemento& memento = mMotionMementos.itemAt(i); |
michael@0 | 4182 | if (memento.source & AINPUT_SOURCE_CLASS_POINTER) { |
michael@0 | 4183 | for (size_t j = 0; j < other.mMotionMementos.size(); ) { |
michael@0 | 4184 | const MotionMemento& otherMemento = other.mMotionMementos.itemAt(j); |
michael@0 | 4185 | if (memento.deviceId == otherMemento.deviceId |
michael@0 | 4186 | && memento.source == otherMemento.source |
michael@0 | 4187 | && memento.displayId == otherMemento.displayId) { |
michael@0 | 4188 | other.mMotionMementos.removeAt(j); |
michael@0 | 4189 | } else { |
michael@0 | 4190 | j += 1; |
michael@0 | 4191 | } |
michael@0 | 4192 | } |
michael@0 | 4193 | other.mMotionMementos.push(memento); |
michael@0 | 4194 | } |
michael@0 | 4195 | } |
michael@0 | 4196 | } |
michael@0 | 4197 | |
michael@0 | 4198 | int32_t InputDispatcher::InputState::getFallbackKey(int32_t originalKeyCode) { |
michael@0 | 4199 | ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode); |
michael@0 | 4200 | return index >= 0 ? mFallbackKeys.valueAt(index) : -1; |
michael@0 | 4201 | } |
michael@0 | 4202 | |
michael@0 | 4203 | void InputDispatcher::InputState::setFallbackKey(int32_t originalKeyCode, |
michael@0 | 4204 | int32_t fallbackKeyCode) { |
michael@0 | 4205 | ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode); |
michael@0 | 4206 | if (index >= 0) { |
michael@0 | 4207 | mFallbackKeys.replaceValueAt(index, fallbackKeyCode); |
michael@0 | 4208 | } else { |
michael@0 | 4209 | mFallbackKeys.add(originalKeyCode, fallbackKeyCode); |
michael@0 | 4210 | } |
michael@0 | 4211 | } |
michael@0 | 4212 | |
michael@0 | 4213 | void InputDispatcher::InputState::removeFallbackKey(int32_t originalKeyCode) { |
michael@0 | 4214 | mFallbackKeys.removeItem(originalKeyCode); |
michael@0 | 4215 | } |
michael@0 | 4216 | |
michael@0 | 4217 | bool InputDispatcher::InputState::shouldCancelKey(const KeyMemento& memento, |
michael@0 | 4218 | const CancelationOptions& options) { |
michael@0 | 4219 | if (options.keyCode != -1 && memento.keyCode != options.keyCode) { |
michael@0 | 4220 | return false; |
michael@0 | 4221 | } |
michael@0 | 4222 | |
michael@0 | 4223 | if (options.deviceId != -1 && memento.deviceId != options.deviceId) { |
michael@0 | 4224 | return false; |
michael@0 | 4225 | } |
michael@0 | 4226 | |
michael@0 | 4227 | switch (options.mode) { |
michael@0 | 4228 | case CancelationOptions::CANCEL_ALL_EVENTS: |
michael@0 | 4229 | case CancelationOptions::CANCEL_NON_POINTER_EVENTS: |
michael@0 | 4230 | return true; |
michael@0 | 4231 | case CancelationOptions::CANCEL_FALLBACK_EVENTS: |
michael@0 | 4232 | return memento.flags & AKEY_EVENT_FLAG_FALLBACK; |
michael@0 | 4233 | default: |
michael@0 | 4234 | return false; |
michael@0 | 4235 | } |
michael@0 | 4236 | } |
michael@0 | 4237 | |
michael@0 | 4238 | bool InputDispatcher::InputState::shouldCancelMotion(const MotionMemento& memento, |
michael@0 | 4239 | const CancelationOptions& options) { |
michael@0 | 4240 | if (options.deviceId != -1 && memento.deviceId != options.deviceId) { |
michael@0 | 4241 | return false; |
michael@0 | 4242 | } |
michael@0 | 4243 | |
michael@0 | 4244 | switch (options.mode) { |
michael@0 | 4245 | case CancelationOptions::CANCEL_ALL_EVENTS: |
michael@0 | 4246 | return true; |
michael@0 | 4247 | case CancelationOptions::CANCEL_POINTER_EVENTS: |
michael@0 | 4248 | return memento.source & AINPUT_SOURCE_CLASS_POINTER; |
michael@0 | 4249 | case CancelationOptions::CANCEL_NON_POINTER_EVENTS: |
michael@0 | 4250 | return !(memento.source & AINPUT_SOURCE_CLASS_POINTER); |
michael@0 | 4251 | default: |
michael@0 | 4252 | return false; |
michael@0 | 4253 | } |
michael@0 | 4254 | } |
michael@0 | 4255 | |
michael@0 | 4256 | |
michael@0 | 4257 | // --- InputDispatcher::Connection --- |
michael@0 | 4258 | |
michael@0 | 4259 | InputDispatcher::Connection::Connection(const sp<InputChannel>& inputChannel, |
michael@0 | 4260 | const sp<InputWindowHandle>& inputWindowHandle, bool monitor) : |
michael@0 | 4261 | status(STATUS_NORMAL), inputChannel(inputChannel), inputWindowHandle(inputWindowHandle), |
michael@0 | 4262 | monitor(monitor), |
michael@0 | 4263 | inputPublisher(inputChannel), inputPublisherBlocked(false) { |
michael@0 | 4264 | } |
michael@0 | 4265 | |
michael@0 | 4266 | InputDispatcher::Connection::~Connection() { |
michael@0 | 4267 | } |
michael@0 | 4268 | |
michael@0 | 4269 | const char* InputDispatcher::Connection::getWindowName() const { |
michael@0 | 4270 | if (inputWindowHandle != NULL) { |
michael@0 | 4271 | return inputWindowHandle->getName().string(); |
michael@0 | 4272 | } |
michael@0 | 4273 | if (monitor) { |
michael@0 | 4274 | return "monitor"; |
michael@0 | 4275 | } |
michael@0 | 4276 | return "?"; |
michael@0 | 4277 | } |
michael@0 | 4278 | |
michael@0 | 4279 | const char* InputDispatcher::Connection::getStatusLabel() const { |
michael@0 | 4280 | switch (status) { |
michael@0 | 4281 | case STATUS_NORMAL: |
michael@0 | 4282 | return "NORMAL"; |
michael@0 | 4283 | |
michael@0 | 4284 | case STATUS_BROKEN: |
michael@0 | 4285 | return "BROKEN"; |
michael@0 | 4286 | |
michael@0 | 4287 | case STATUS_ZOMBIE: |
michael@0 | 4288 | return "ZOMBIE"; |
michael@0 | 4289 | |
michael@0 | 4290 | default: |
michael@0 | 4291 | return "UNKNOWN"; |
michael@0 | 4292 | } |
michael@0 | 4293 | } |
michael@0 | 4294 | |
michael@0 | 4295 | InputDispatcher::DispatchEntry* InputDispatcher::Connection::findWaitQueueEntry(uint32_t seq) { |
michael@0 | 4296 | for (DispatchEntry* entry = waitQueue.head; entry != NULL; entry = entry->next) { |
michael@0 | 4297 | if (entry->seq == seq) { |
michael@0 | 4298 | return entry; |
michael@0 | 4299 | } |
michael@0 | 4300 | } |
michael@0 | 4301 | return NULL; |
michael@0 | 4302 | } |
michael@0 | 4303 | |
michael@0 | 4304 | |
michael@0 | 4305 | // --- InputDispatcher::CommandEntry --- |
michael@0 | 4306 | |
michael@0 | 4307 | InputDispatcher::CommandEntry::CommandEntry(Command command) : |
michael@0 | 4308 | command(command), eventTime(0), keyEntry(NULL), userActivityEventType(0), |
michael@0 | 4309 | seq(0), handled(false) { |
michael@0 | 4310 | } |
michael@0 | 4311 | |
michael@0 | 4312 | InputDispatcher::CommandEntry::~CommandEntry() { |
michael@0 | 4313 | } |
michael@0 | 4314 | |
michael@0 | 4315 | |
michael@0 | 4316 | // --- InputDispatcher::TouchState --- |
michael@0 | 4317 | |
michael@0 | 4318 | InputDispatcher::TouchState::TouchState() : |
michael@0 | 4319 | down(false), split(false), deviceId(-1), source(0), displayId(-1) { |
michael@0 | 4320 | } |
michael@0 | 4321 | |
michael@0 | 4322 | InputDispatcher::TouchState::~TouchState() { |
michael@0 | 4323 | } |
michael@0 | 4324 | |
michael@0 | 4325 | void InputDispatcher::TouchState::reset() { |
michael@0 | 4326 | down = false; |
michael@0 | 4327 | split = false; |
michael@0 | 4328 | deviceId = -1; |
michael@0 | 4329 | source = 0; |
michael@0 | 4330 | displayId = -1; |
michael@0 | 4331 | windows.clear(); |
michael@0 | 4332 | } |
michael@0 | 4333 | |
michael@0 | 4334 | void InputDispatcher::TouchState::copyFrom(const TouchState& other) { |
michael@0 | 4335 | down = other.down; |
michael@0 | 4336 | split = other.split; |
michael@0 | 4337 | deviceId = other.deviceId; |
michael@0 | 4338 | source = other.source; |
michael@0 | 4339 | displayId = other.displayId; |
michael@0 | 4340 | windows = other.windows; |
michael@0 | 4341 | } |
michael@0 | 4342 | |
michael@0 | 4343 | void InputDispatcher::TouchState::addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle, |
michael@0 | 4344 | int32_t targetFlags, BitSet32 pointerIds) { |
michael@0 | 4345 | if (targetFlags & InputTarget::FLAG_SPLIT) { |
michael@0 | 4346 | split = true; |
michael@0 | 4347 | } |
michael@0 | 4348 | |
michael@0 | 4349 | for (size_t i = 0; i < windows.size(); i++) { |
michael@0 | 4350 | TouchedWindow& touchedWindow = windows.editItemAt(i); |
michael@0 | 4351 | if (touchedWindow.windowHandle == windowHandle) { |
michael@0 | 4352 | touchedWindow.targetFlags |= targetFlags; |
michael@0 | 4353 | if (targetFlags & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
michael@0 | 4354 | touchedWindow.targetFlags &= ~InputTarget::FLAG_DISPATCH_AS_IS; |
michael@0 | 4355 | } |
michael@0 | 4356 | touchedWindow.pointerIds.value |= pointerIds.value; |
michael@0 | 4357 | return; |
michael@0 | 4358 | } |
michael@0 | 4359 | } |
michael@0 | 4360 | |
michael@0 | 4361 | windows.push(); |
michael@0 | 4362 | |
michael@0 | 4363 | TouchedWindow& touchedWindow = windows.editTop(); |
michael@0 | 4364 | touchedWindow.windowHandle = windowHandle; |
michael@0 | 4365 | touchedWindow.targetFlags = targetFlags; |
michael@0 | 4366 | touchedWindow.pointerIds = pointerIds; |
michael@0 | 4367 | } |
michael@0 | 4368 | |
michael@0 | 4369 | void InputDispatcher::TouchState::removeWindow(const sp<InputWindowHandle>& windowHandle) { |
michael@0 | 4370 | for (size_t i = 0; i < windows.size(); i++) { |
michael@0 | 4371 | if (windows.itemAt(i).windowHandle == windowHandle) { |
michael@0 | 4372 | windows.removeAt(i); |
michael@0 | 4373 | return; |
michael@0 | 4374 | } |
michael@0 | 4375 | } |
michael@0 | 4376 | } |
michael@0 | 4377 | |
michael@0 | 4378 | void InputDispatcher::TouchState::filterNonAsIsTouchWindows() { |
michael@0 | 4379 | for (size_t i = 0 ; i < windows.size(); ) { |
michael@0 | 4380 | TouchedWindow& window = windows.editItemAt(i); |
michael@0 | 4381 | if (window.targetFlags & (InputTarget::FLAG_DISPATCH_AS_IS |
michael@0 | 4382 | | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER)) { |
michael@0 | 4383 | window.targetFlags &= ~InputTarget::FLAG_DISPATCH_MASK; |
michael@0 | 4384 | window.targetFlags |= InputTarget::FLAG_DISPATCH_AS_IS; |
michael@0 | 4385 | i += 1; |
michael@0 | 4386 | } else { |
michael@0 | 4387 | windows.removeAt(i); |
michael@0 | 4388 | } |
michael@0 | 4389 | } |
michael@0 | 4390 | } |
michael@0 | 4391 | |
michael@0 | 4392 | sp<InputWindowHandle> InputDispatcher::TouchState::getFirstForegroundWindowHandle() const { |
michael@0 | 4393 | for (size_t i = 0; i < windows.size(); i++) { |
michael@0 | 4394 | const TouchedWindow& window = windows.itemAt(i); |
michael@0 | 4395 | if (window.targetFlags & InputTarget::FLAG_FOREGROUND) { |
michael@0 | 4396 | return window.windowHandle; |
michael@0 | 4397 | } |
michael@0 | 4398 | } |
michael@0 | 4399 | return NULL; |
michael@0 | 4400 | } |
michael@0 | 4401 | |
michael@0 | 4402 | bool InputDispatcher::TouchState::isSlippery() const { |
michael@0 | 4403 | // Must have exactly one foreground window. |
michael@0 | 4404 | bool haveSlipperyForegroundWindow = false; |
michael@0 | 4405 | for (size_t i = 0; i < windows.size(); i++) { |
michael@0 | 4406 | const TouchedWindow& window = windows.itemAt(i); |
michael@0 | 4407 | if (window.targetFlags & InputTarget::FLAG_FOREGROUND) { |
michael@0 | 4408 | if (haveSlipperyForegroundWindow |
michael@0 | 4409 | || !(window.windowHandle->getInfo()->layoutParamsFlags |
michael@0 | 4410 | & InputWindowInfo::FLAG_SLIPPERY)) { |
michael@0 | 4411 | return false; |
michael@0 | 4412 | } |
michael@0 | 4413 | haveSlipperyForegroundWindow = true; |
michael@0 | 4414 | } |
michael@0 | 4415 | } |
michael@0 | 4416 | return haveSlipperyForegroundWindow; |
michael@0 | 4417 | } |
michael@0 | 4418 | |
michael@0 | 4419 | |
michael@0 | 4420 | // --- InputDispatcherThread --- |
michael@0 | 4421 | |
michael@0 | 4422 | InputDispatcherThread::InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher) : |
michael@0 | 4423 | Thread(/*canCallJava*/ true), mDispatcher(dispatcher) { |
michael@0 | 4424 | } |
michael@0 | 4425 | |
michael@0 | 4426 | InputDispatcherThread::~InputDispatcherThread() { |
michael@0 | 4427 | } |
michael@0 | 4428 | |
michael@0 | 4429 | bool InputDispatcherThread::threadLoop() { |
michael@0 | 4430 | mDispatcher->dispatchOnce(); |
michael@0 | 4431 | return true; |
michael@0 | 4432 | } |
michael@0 | 4433 | |
michael@0 | 4434 | } // namespace android |