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