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 #ifndef _UI_INPUT_DISPATCHER_H 18 #define _UI_INPUT_DISPATCHER_H 19 20 #include "AnrTracker.h" 21 #include "CancelationOptions.h" 22 #include "DragState.h" 23 #include "Entry.h" 24 #include "FocusResolver.h" 25 #include "InjectionState.h" 26 #include "InputDispatcherConfiguration.h" 27 #include "InputDispatcherInterface.h" 28 #include "InputDispatcherPolicyInterface.h" 29 #include "InputState.h" 30 #include "InputTarget.h" 31 #include "InputThread.h" 32 #include "LatencyAggregator.h" 33 #include "LatencyTracker.h" 34 #include "Monitor.h" 35 #include "TouchState.h" 36 #include "TouchedWindow.h" 37 38 #include <attestation/HmacKeyManager.h> 39 #include <com/android/internal/compat/IPlatformCompatNative.h> 40 #include <gui/InputApplication.h> 41 #include <gui/WindowInfo.h> 42 #include <input/Input.h> 43 #include <input/InputTransport.h> 44 #include <limits.h> 45 #include <stddef.h> 46 #include <ui/Region.h> 47 #include <unistd.h> 48 #include <utils/BitSet.h> 49 #include <utils/Looper.h> 50 #include <utils/RefBase.h> 51 #include <utils/Timers.h> 52 #include <utils/threads.h> 53 #include <condition_variable> 54 #include <deque> 55 #include <optional> 56 #include <unordered_map> 57 #include <unordered_set> 58 59 #include <InputListener.h> 60 #include <InputReporterInterface.h> 61 #include <gui/WindowInfosListener.h> 62 63 namespace android::inputdispatcher { 64 65 class Connection; 66 67 /* Dispatches events to input targets. Some functions of the input dispatcher, such as 68 * identifying input targets, are controlled by a separate policy object. 69 * 70 * IMPORTANT INVARIANT: 71 * Because the policy can potentially block or cause re-entrance into the input dispatcher, 72 * the input dispatcher never calls into the policy while holding its internal locks. 73 * The implementation is also carefully designed to recover from scenarios such as an 74 * input channel becoming unregistered while identifying input targets or processing timeouts. 75 * 76 * Methods marked 'Locked' must be called with the lock acquired. 77 * 78 * Methods marked 'LockedInterruptible' must be called with the lock acquired but 79 * may during the course of their execution release the lock, call into the policy, and 80 * then reacquire the lock. The caller is responsible for recovering gracefully. 81 * 82 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa. 83 */ 84 class InputDispatcher : public android::InputDispatcherInterface, public gui::WindowInfosListener { 85 protected: 86 ~InputDispatcher() override; 87 88 public: 89 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy); 90 91 void dump(std::string& dump) override; 92 void monitor() override; 93 bool waitForIdle() override; 94 status_t start() override; 95 status_t stop() override; 96 97 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override; 98 void notifyKey(const NotifyKeyArgs* args) override; 99 void notifyMotion(const NotifyMotionArgs* args) override; 100 void notifySwitch(const NotifySwitchArgs* args) override; 101 void notifySensor(const NotifySensorArgs* args) override; 102 void notifyVibratorState(const NotifyVibratorStateArgs* args) override; 103 void notifyDeviceReset(const NotifyDeviceResetArgs* args) override; 104 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) override; 105 106 android::os::InputEventInjectionResult injectInputEvent( 107 const InputEvent* event, int32_t injectorPid, int32_t injectorUid, 108 android::os::InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, 109 uint32_t policyFlags) override; 110 111 std::unique_ptr<VerifiedInputEvent> verifyInputEvent(const InputEvent& event) override; 112 113 void setInputWindows( 114 const std::unordered_map<int32_t, std::vector<sp<android::gui::WindowInfoHandle>>>& 115 handlesPerDisplay) override; 116 void setFocusedApplication( 117 int32_t displayId, 118 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) override; 119 void setFocusedDisplay(int32_t displayId) override; 120 void setInputDispatchMode(bool enabled, bool frozen) override; 121 void setInputFilterEnabled(bool enabled) override; 122 void setInTouchMode(bool inTouchMode) override; 123 void setMaximumObscuringOpacityForTouch(float opacity) override; 124 void setBlockUntrustedTouchesMode(android::os::BlockUntrustedTouchesMode mode) override; 125 126 bool transferTouchFocus(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, 127 bool isDragDrop = false) override; 128 bool transferTouch(const sp<IBinder>& destChannelToken) override; 129 130 base::Result<std::unique_ptr<InputChannel>> createInputChannel( 131 const std::string& name) override; 132 void setFocusedWindow(const android::gui::FocusRequest&) override; 133 base::Result<std::unique_ptr<InputChannel>> createInputMonitor(int32_t displayId, 134 bool isGestureMonitor, 135 const std::string& name, 136 int32_t pid) override; 137 status_t removeInputChannel(const sp<IBinder>& connectionToken) override; 138 status_t pilferPointers(const sp<IBinder>& token) override; 139 void requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) override; 140 bool flushSensor(int deviceId, InputDeviceSensorType sensorType) override; 141 142 std::array<uint8_t, 32> sign(const VerifiedInputEvent& event) const; 143 144 void displayRemoved(int32_t displayId) override; 145 146 void onWindowInfosChanged(const std::vector<gui::WindowInfo>& windowInfos) override; 147 148 private: 149 enum class DropReason { 150 NOT_DROPPED, 151 POLICY, 152 APP_SWITCH, 153 DISABLED, 154 BLOCKED, 155 STALE, 156 NO_POINTER_CAPTURE, 157 }; 158 159 std::unique_ptr<InputThread> mThread; 160 161 sp<InputDispatcherPolicyInterface> mPolicy; 162 android::InputDispatcherConfiguration mConfig; 163 164 std::mutex mLock; 165 166 std::condition_variable mDispatcherIsAlive; 167 std::condition_variable mDispatcherEnteredIdle; 168 169 sp<Looper> mLooper; 170 171 std::shared_ptr<EventEntry> mPendingEvent GUARDED_BY(mLock); 172 std::deque<std::shared_ptr<EventEntry>> mInboundQueue GUARDED_BY(mLock); 173 std::deque<std::shared_ptr<EventEntry>> mRecentQueue GUARDED_BY(mLock); 174 std::deque<std::unique_ptr<CommandEntry>> mCommandQueue GUARDED_BY(mLock); 175 176 DropReason mLastDropReason GUARDED_BY(mLock); 177 178 const IdGenerator mIdGenerator; 179 180 // With each iteration, InputDispatcher nominally processes one queued event, 181 // a timeout, or a response from an input consumer. 182 // This method should only be called on the input dispatcher's own thread. 183 void dispatchOnce(); 184 185 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) REQUIRES(mLock); 186 187 // Enqueues an inbound event. Returns true if mLooper->wake() should be called. 188 bool enqueueInboundEventLocked(std::unique_ptr<EventEntry> entry) REQUIRES(mLock); 189 190 // Cleans up input state when dropping an inbound event. 191 void dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) REQUIRES(mLock); 192 193 // Enqueues a focus event. 194 void enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, 195 const std::string& reason) REQUIRES(mLock); 196 // Enqueues a drag event. 197 void enqueueDragEventLocked(const sp<android::gui::WindowInfoHandle>& windowToken, 198 bool isExiting, const MotionEntry& motionEntry) REQUIRES(mLock); 199 200 // Adds an event to a queue of recent events for debugging purposes. 201 void addRecentEventLocked(std::shared_ptr<EventEntry> entry) REQUIRES(mLock); 202 203 // App switch latency optimization. 204 bool mAppSwitchSawKeyDown GUARDED_BY(mLock); 205 nsecs_t mAppSwitchDueTime GUARDED_BY(mLock); 206 207 bool isAppSwitchKeyEvent(const KeyEntry& keyEntry); 208 bool isAppSwitchPendingLocked() REQUIRES(mLock); 209 void resetPendingAppSwitchLocked(bool handled) REQUIRES(mLock); 210 211 // Blocked event latency optimization. Drops old events when the user intends 212 // to transfer focus to a new application. 213 std::shared_ptr<EventEntry> mNextUnblockedEvent GUARDED_BY(mLock); 214 215 sp<android::gui::WindowInfoHandle> findTouchedWindowAtLocked(int32_t displayId, int32_t x, 216 int32_t y, TouchState* touchState, 217 bool addOutsideTargets = false, 218 bool addPortalWindows = false, 219 bool ignoreDragWindow = false) 220 REQUIRES(mLock); 221 222 sp<Connection> getConnectionLocked(const sp<IBinder>& inputConnectionToken) const 223 REQUIRES(mLock); 224 225 std::string getConnectionNameLocked(const sp<IBinder>& connectionToken) const REQUIRES(mLock); 226 227 void removeConnectionLocked(const sp<Connection>& connection) REQUIRES(mLock); 228 229 template <typename T> 230 struct StrongPointerHash { operatorStrongPointerHash231 std::size_t operator()(const sp<T>& b) const { return std::hash<T*>{}(b.get()); } 232 }; 233 234 // All registered connections mapped by input channel token. 235 std::unordered_map<sp<IBinder>, sp<Connection>, StrongPointerHash<IBinder>> mConnectionsByToken 236 GUARDED_BY(mLock); 237 238 // Finds the display ID of the gesture monitor identified by the provided token. 239 std::optional<int32_t> findGestureMonitorDisplayByTokenLocked(const sp<IBinder>& token) 240 REQUIRES(mLock); 241 // Find a monitor pid by the provided token. 242 std::optional<int32_t> findMonitorPidByTokenLocked(const sp<IBinder>& token) REQUIRES(mLock); 243 244 // Input channels that will receive a copy of all input events sent to the provided display. 245 std::unordered_map<int32_t, std::vector<Monitor>> mGlobalMonitorsByDisplay GUARDED_BY(mLock); 246 247 // Input channels that will receive pointer events that start within the corresponding display. 248 // These are a bit special when compared to global monitors since they'll cause gesture streams 249 // to continue even when there isn't a touched window,and have the ability to steal the rest of 250 // the pointer stream in order to claim it for a system gesture. 251 std::unordered_map<int32_t, std::vector<Monitor>> mGestureMonitorsByDisplay GUARDED_BY(mLock); 252 253 const HmacKeyManager mHmacKeyManager; 254 const std::array<uint8_t, 32> getSignature(const MotionEntry& motionEntry, 255 const DispatchEntry& dispatchEntry) const; 256 const std::array<uint8_t, 32> getSignature(const KeyEntry& keyEntry, 257 const DispatchEntry& dispatchEntry) const; 258 259 // Event injection and synchronization. 260 std::condition_variable mInjectionResultAvailable; 261 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid); 262 void setInjectionResult(EventEntry& entry, 263 android::os::InputEventInjectionResult injectionResult); 264 265 std::condition_variable mInjectionSyncFinished; 266 void incrementPendingForegroundDispatches(EventEntry& entry); 267 void decrementPendingForegroundDispatches(EventEntry& entry); 268 269 // Key repeat tracking. 270 struct KeyRepeatState { 271 std::shared_ptr<KeyEntry> lastKeyEntry; // or null if no repeat 272 nsecs_t nextRepeatTime; 273 } mKeyRepeatState GUARDED_BY(mLock); 274 275 void resetKeyRepeatLocked() REQUIRES(mLock); 276 std::shared_ptr<KeyEntry> synthesizeKeyRepeatLocked(nsecs_t currentTime) REQUIRES(mLock); 277 278 // Key replacement tracking 279 struct KeyReplacement { 280 int32_t keyCode; 281 int32_t deviceId; 282 bool operator==(const KeyReplacement& rhs) const { 283 return keyCode == rhs.keyCode && deviceId == rhs.deviceId; 284 } 285 }; 286 struct KeyReplacementHash { operatorKeyReplacementHash287 size_t operator()(const KeyReplacement& key) const { 288 return std::hash<int32_t>()(key.keyCode) ^ (std::hash<int32_t>()(key.deviceId) << 1); 289 } 290 }; 291 // Maps the key code replaced, device id tuple to the key code it was replaced with 292 std::unordered_map<KeyReplacement, int32_t, KeyReplacementHash> mReplacedKeys GUARDED_BY(mLock); 293 // Process certain Meta + Key combinations 294 void accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, int32_t& keyCode, 295 int32_t& metaState); 296 297 // Deferred command processing. 298 bool haveCommandsLocked() const REQUIRES(mLock); 299 bool runCommandsLockedInterruptible() REQUIRES(mLock); 300 void postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) REQUIRES(mLock); 301 302 nsecs_t processAnrsLocked() REQUIRES(mLock); 303 std::chrono::nanoseconds getDispatchingTimeoutLocked(const sp<IBinder>& token) REQUIRES(mLock); 304 305 // Input filter processing. 306 bool shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) REQUIRES(mLock); 307 bool shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) REQUIRES(mLock); 308 309 // Inbound event processing. 310 void drainInboundQueueLocked() REQUIRES(mLock); 311 void releasePendingEventLocked() REQUIRES(mLock); 312 void releaseInboundEventLocked(std::shared_ptr<EventEntry> entry) REQUIRES(mLock); 313 314 // Dispatch state. 315 bool mDispatchEnabled GUARDED_BY(mLock); 316 bool mDispatchFrozen GUARDED_BY(mLock); 317 bool mInputFilterEnabled GUARDED_BY(mLock); 318 bool mInTouchMode GUARDED_BY(mLock); 319 float mMaximumObscuringOpacityForTouch GUARDED_BY(mLock); 320 android::os::BlockUntrustedTouchesMode mBlockUntrustedTouchesMode GUARDED_BY(mLock); 321 322 std::unordered_map<int32_t, std::vector<sp<android::gui::WindowInfoHandle>>> 323 mWindowHandlesByDisplay GUARDED_BY(mLock); 324 void setInputWindowsLocked( 325 const std::vector<sp<android::gui::WindowInfoHandle>>& inputWindowHandles, 326 int32_t displayId) REQUIRES(mLock); 327 // Get a reference to window handles by display, return an empty vector if not found. 328 const std::vector<sp<android::gui::WindowInfoHandle>>& getWindowHandlesLocked( 329 int32_t displayId) const REQUIRES(mLock); 330 sp<android::gui::WindowInfoHandle> getWindowHandleLocked( 331 const sp<IBinder>& windowHandleToken) const REQUIRES(mLock); 332 333 // Same function as above, but faster. Since displayId is provided, this avoids the need 334 // to loop through all displays. 335 sp<android::gui::WindowInfoHandle> getWindowHandleLocked(const sp<IBinder>& windowHandleToken, 336 int displayId) const REQUIRES(mLock); 337 sp<android::gui::WindowInfoHandle> getWindowHandleLocked( 338 const sp<android::gui::WindowInfoHandle>& windowHandle) const REQUIRES(mLock); 339 std::shared_ptr<InputChannel> getInputChannelLocked(const sp<IBinder>& windowToken) const 340 REQUIRES(mLock); 341 sp<android::gui::WindowInfoHandle> getFocusedWindowHandleLocked(int displayId) const 342 REQUIRES(mLock); 343 bool hasResponsiveConnectionLocked(android::gui::WindowInfoHandle& windowHandle) const 344 REQUIRES(mLock); 345 346 /* 347 * Validate and update InputWindowHandles for a given display. 348 */ 349 void updateWindowHandlesForDisplayLocked( 350 const std::vector<sp<android::gui::WindowInfoHandle>>& inputWindowHandles, 351 int32_t displayId) REQUIRES(mLock); 352 353 std::unordered_map<int32_t, TouchState> mTouchStatesByDisplay GUARDED_BY(mLock); 354 std::unique_ptr<DragState> mDragState GUARDED_BY(mLock); 355 356 void setFocusedApplicationLocked( 357 int32_t displayId, 358 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) REQUIRES(mLock); 359 // Focused applications. 360 std::unordered_map<int32_t, std::shared_ptr<InputApplicationHandle>> 361 mFocusedApplicationHandlesByDisplay GUARDED_BY(mLock); 362 363 // Top focused display. 364 int32_t mFocusedDisplayId GUARDED_BY(mLock); 365 366 // Keeps track of the focused window per display and determines focus changes. 367 FocusResolver mFocusResolver GUARDED_BY(mLock); 368 369 // The enabled state of this request is true iff the focused window on the focused display has 370 // requested Pointer Capture. This request also contains the sequence number associated with the 371 // current request. The state of this variable should always be in sync with the state of 372 // Pointer Capture in the policy, and is only updated through setPointerCaptureLocked(request). 373 PointerCaptureRequest mCurrentPointerCaptureRequest GUARDED_BY(mLock); 374 375 // The window token that has Pointer Capture. 376 // This should be in sync with PointerCaptureChangedEvents dispatched to the input channel. 377 sp<IBinder> mWindowTokenWithPointerCapture GUARDED_BY(mLock); 378 379 // Disable Pointer Capture as a result of loss of window focus. 380 void disablePointerCaptureForcedLocked() REQUIRES(mLock); 381 382 // Set the Pointer Capture state in the Policy. 383 void setPointerCaptureLocked(bool enable) REQUIRES(mLock); 384 385 // Dispatcher state at time of last ANR. 386 std::string mLastAnrState GUARDED_BY(mLock); 387 388 // The connection tokens of the channels that the user last interacted, for debugging 389 std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> mInteractionConnectionTokens 390 GUARDED_BY(mLock); 391 void updateInteractionTokensLocked(const EventEntry& entry, 392 const std::vector<InputTarget>& targets) REQUIRES(mLock); 393 394 // Dispatch inbound events. 395 bool dispatchConfigurationChangedLocked(nsecs_t currentTime, 396 const ConfigurationChangedEntry& entry) REQUIRES(mLock); 397 bool dispatchDeviceResetLocked(nsecs_t currentTime, const DeviceResetEntry& entry) 398 REQUIRES(mLock); 399 bool dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<KeyEntry> entry, 400 DropReason* dropReason, nsecs_t* nextWakeupTime) REQUIRES(mLock); 401 bool dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<MotionEntry> entry, 402 DropReason* dropReason, nsecs_t* nextWakeupTime) REQUIRES(mLock); 403 void dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry) 404 REQUIRES(mLock); 405 void dispatchPointerCaptureChangedLocked( 406 nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry, 407 DropReason& dropReason) REQUIRES(mLock); 408 void dispatchEventLocked(nsecs_t currentTime, std::shared_ptr<EventEntry> entry, 409 const std::vector<InputTarget>& inputTargets) REQUIRES(mLock); 410 void dispatchSensorLocked(nsecs_t currentTime, std::shared_ptr<SensorEntry> entry, 411 DropReason* dropReason, nsecs_t* nextWakeupTime) REQUIRES(mLock); 412 void dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<DragEntry> entry) REQUIRES(mLock); 413 void logOutboundKeyDetails(const char* prefix, const KeyEntry& entry); 414 void logOutboundMotionDetails(const char* prefix, const MotionEntry& entry); 415 416 /** 417 * This field is set if there is no focused window, and we have an event that requires 418 * a focused window to be dispatched (for example, a KeyEvent). 419 * When this happens, we will wait until *mNoFocusedWindowTimeoutTime before 420 * dropping the event and raising an ANR for that application. 421 * This is useful if an application is slow to add a focused window. 422 */ 423 std::optional<nsecs_t> mNoFocusedWindowTimeoutTime GUARDED_BY(mLock); 424 425 bool shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) REQUIRES(mLock); 426 427 /** 428 * Time to stop waiting for the events to be processed while trying to dispatch a key. 429 * When this time expires, we just send the pending key event to the currently focused window, 430 * without waiting on other events to be processed first. 431 */ 432 std::optional<nsecs_t> mKeyIsWaitingForEventsTimeout GUARDED_BY(mLock); 433 bool shouldWaitToSendKeyLocked(nsecs_t currentTime, const char* focusedWindowName) 434 REQUIRES(mLock); 435 436 /** 437 * The focused application at the time when no focused window was present. 438 * Used to raise an ANR when we have no focused window. 439 */ 440 std::shared_ptr<InputApplicationHandle> mAwaitedFocusedApplication GUARDED_BY(mLock); 441 /** 442 * The displayId that the focused application is associated with. 443 */ 444 int32_t mAwaitedApplicationDisplayId GUARDED_BY(mLock); 445 void processNoFocusedWindowAnrLocked() REQUIRES(mLock); 446 447 /** 448 * Tell policy about a window or a monitor that just became unresponsive. Starts ANR. 449 */ 450 void processConnectionUnresponsiveLocked(const Connection& connection, std::string reason) 451 REQUIRES(mLock); 452 /** 453 * Tell policy about a window or a monitor that just became responsive. 454 */ 455 void processConnectionResponsiveLocked(const Connection& connection) REQUIRES(mLock); 456 457 /** 458 * Post `doNotifyMonitorUnresponsiveLockedInterruptible` command. 459 */ 460 void sendMonitorUnresponsiveCommandLocked(int32_t pid, std::string reason) REQUIRES(mLock); 461 /** 462 * Post `doNotifyWindowUnresponsiveLockedInterruptible` command. 463 */ 464 void sendWindowUnresponsiveCommandLocked(sp<IBinder> connectionToken, std::string reason) 465 REQUIRES(mLock); 466 /** 467 * Post `doNotifyMonitorResponsiveLockedInterruptible` command. 468 */ 469 void sendMonitorResponsiveCommandLocked(int32_t pid) REQUIRES(mLock); 470 /** 471 * Post `doNotifyWindowResponsiveLockedInterruptible` command. 472 */ 473 void sendWindowResponsiveCommandLocked(sp<IBinder> connectionToken) REQUIRES(mLock); 474 475 476 // Optimization: AnrTracker is used to quickly find which connection is due for a timeout next. 477 // AnrTracker must be kept in-sync with all responsive connection.waitQueues. 478 // If a connection is not responsive, then the entries should not be added to the AnrTracker. 479 // Once a connection becomes unresponsive, its entries are removed from AnrTracker to 480 // prevent unneeded wakeups. 481 AnrTracker mAnrTracker GUARDED_BY(mLock); 482 483 // Contains the last window which received a hover event. 484 sp<android::gui::WindowInfoHandle> mLastHoverWindowHandle GUARDED_BY(mLock); 485 486 void cancelEventsForAnrLocked(const sp<Connection>& connection) REQUIRES(mLock); 487 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime) REQUIRES(mLock); 488 // If a focused application changes, we should stop counting down the "no focused window" time, 489 // because we will have no way of knowing when the previous application actually added a window. 490 // This also means that we will miss cases like pulling down notification shade when the 491 // focused application does not have a focused window (no ANR will be raised if notification 492 // shade is pulled down while we are counting down the timeout). 493 void resetNoFocusedWindowTimeoutLocked() REQUIRES(mLock); 494 495 int32_t getTargetDisplayId(const EventEntry& entry); 496 android::os::InputEventInjectionResult findFocusedWindowTargetsLocked( 497 nsecs_t currentTime, const EventEntry& entry, std::vector<InputTarget>& inputTargets, 498 nsecs_t* nextWakeupTime) REQUIRES(mLock); 499 android::os::InputEventInjectionResult findTouchedWindowTargetsLocked( 500 nsecs_t currentTime, const MotionEntry& entry, std::vector<InputTarget>& inputTargets, 501 nsecs_t* nextWakeupTime, bool* outConflictingPointerActions) REQUIRES(mLock); 502 std::vector<TouchedMonitor> findTouchedGestureMonitorsLocked( 503 int32_t displayId, 504 const std::vector<sp<android::gui::WindowInfoHandle>>& portalWindows) const 505 REQUIRES(mLock); 506 std::vector<TouchedMonitor> selectResponsiveMonitorsLocked( 507 const std::vector<TouchedMonitor>& gestureMonitors) const REQUIRES(mLock); 508 509 void addWindowTargetLocked(const sp<android::gui::WindowInfoHandle>& windowHandle, 510 int32_t targetFlags, BitSet32 pointerIds, 511 std::vector<InputTarget>& inputTargets) REQUIRES(mLock); 512 void addMonitoringTargetLocked(const Monitor& monitor, float xOffset, float yOffset, 513 std::vector<InputTarget>& inputTargets) REQUIRES(mLock); 514 void addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, int32_t displayId, 515 float xOffset = 0, float yOffset = 0) REQUIRES(mLock); 516 void pokeUserActivityLocked(const EventEntry& eventEntry) REQUIRES(mLock); 517 bool checkInjectionPermission(const sp<android::gui::WindowInfoHandle>& windowHandle, 518 const InjectionState* injectionState); 519 // Enqueue a drag event if needed, and update the touch state. 520 // Uses findTouchedWindowTargetsLocked to make the decision 521 void addDragEventLocked(const MotionEntry& entry) REQUIRES(mLock); 522 void finishDragAndDrop(int32_t displayId, float x, float y) REQUIRES(mLock); 523 524 struct TouchOcclusionInfo { 525 bool hasBlockingOcclusion; 526 float obscuringOpacity; 527 std::string obscuringPackage; 528 int32_t obscuringUid; 529 std::vector<std::string> debugInfo; 530 }; 531 532 TouchOcclusionInfo computeTouchOcclusionInfoLocked( 533 const sp<android::gui::WindowInfoHandle>& windowHandle, int32_t x, int32_t y) const 534 REQUIRES(mLock); 535 bool isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const REQUIRES(mLock); 536 bool isWindowObscuredAtPointLocked(const sp<android::gui::WindowInfoHandle>& windowHandle, 537 int32_t x, int32_t y) const REQUIRES(mLock); 538 bool isWindowObscuredLocked(const sp<android::gui::WindowInfoHandle>& windowHandle) const 539 REQUIRES(mLock); 540 std::string dumpWindowForTouchOcclusion(const android::gui::WindowInfo* info, 541 bool isTouchWindow) const; 542 std::string getApplicationWindowLabel(const InputApplicationHandle* applicationHandle, 543 const sp<android::gui::WindowInfoHandle>& windowHandle); 544 545 bool shouldDropInput(const EventEntry& entry, 546 const sp<android::gui::WindowInfoHandle>& windowHandle) const 547 REQUIRES(mLock); 548 549 // Manage the dispatch cycle for a single connection. 550 // These methods are deliberately not Interruptible because doing all of the work 551 // with the mutex held makes it easier to ensure that connection invariants are maintained. 552 // If needed, the methods post commands to run later once the critical bits are done. 553 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection, 554 std::shared_ptr<EventEntry>, const InputTarget& inputTarget) 555 REQUIRES(mLock); 556 void enqueueDispatchEntriesLocked(nsecs_t currentTime, const sp<Connection>& connection, 557 std::shared_ptr<EventEntry>, const InputTarget& inputTarget) 558 REQUIRES(mLock); 559 void enqueueDispatchEntryLocked(const sp<Connection>& connection, std::shared_ptr<EventEntry>, 560 const InputTarget& inputTarget, int32_t dispatchMode) 561 REQUIRES(mLock); 562 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection) 563 REQUIRES(mLock); 564 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection, 565 uint32_t seq, bool handled, nsecs_t consumeTime) REQUIRES(mLock); 566 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection, 567 bool notify) REQUIRES(mLock); 568 void drainDispatchQueue(std::deque<DispatchEntry*>& queue); 569 void releaseDispatchEntry(DispatchEntry* dispatchEntry); 570 int handleReceiveCallback(int events, sp<IBinder> connectionToken); 571 // The action sent should only be of type AMOTION_EVENT_* 572 void dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, 573 const sp<IBinder>& newToken) REQUIRES(mLock); 574 575 void synthesizeCancelationEventsForAllConnectionsLocked(const CancelationOptions& options) 576 REQUIRES(mLock); 577 void synthesizeCancelationEventsForMonitorsLocked(const CancelationOptions& options) 578 REQUIRES(mLock); 579 void synthesizeCancelationEventsForMonitorsLocked( 580 const CancelationOptions& options, 581 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock); 582 void synthesizeCancelationEventsForInputChannelLocked( 583 const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options) 584 REQUIRES(mLock); 585 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection, 586 const CancelationOptions& options) 587 REQUIRES(mLock); 588 589 void synthesizePointerDownEventsForConnectionLocked(const sp<Connection>& connection) 590 REQUIRES(mLock); 591 592 // Splitting motion events across windows. 593 std::unique_ptr<MotionEntry> splitMotionEvent(const MotionEntry& originalMotionEntry, 594 BitSet32 pointerIds); 595 596 // Reset and drop everything the dispatcher is doing. 597 void resetAndDropEverythingLocked(const char* reason) REQUIRES(mLock); 598 599 // Dump state. 600 void dumpDispatchStateLocked(std::string& dump) REQUIRES(mLock); 601 void dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors); 602 void logDispatchStateLocked() REQUIRES(mLock); 603 std::string dumpPointerCaptureStateLocked() REQUIRES(mLock); 604 605 // Registration. 606 void removeMonitorChannelLocked(const sp<IBinder>& connectionToken) REQUIRES(mLock); 607 void removeMonitorChannelLocked( 608 const sp<IBinder>& connectionToken, 609 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock); 610 status_t removeInputChannelLocked(const sp<IBinder>& connectionToken, bool notify) 611 REQUIRES(mLock); 612 613 // Interesting events that we might like to log or tell the framework about. 614 void onDispatchCycleFinishedLocked(nsecs_t currentTime, const sp<Connection>& connection, 615 uint32_t seq, bool handled, nsecs_t consumeTime) 616 REQUIRES(mLock); 617 void onDispatchCycleBrokenLocked(nsecs_t currentTime, const sp<Connection>& connection) 618 REQUIRES(mLock); 619 void onFocusChangedLocked(const FocusResolver::FocusChanges& changes) REQUIRES(mLock); 620 void notifyFocusChangedLocked(const sp<IBinder>& oldFocus, const sp<IBinder>& newFocus) 621 REQUIRES(mLock); 622 void notifyDropWindowLocked(const sp<IBinder>& token, float x, float y) REQUIRES(mLock); 623 void onAnrLocked(const sp<Connection>& connection) REQUIRES(mLock); 624 void onAnrLocked(std::shared_ptr<InputApplicationHandle> application) REQUIRES(mLock); 625 void onUntrustedTouchLocked(const std::string& obscuringPackage) REQUIRES(mLock); 626 void updateLastAnrStateLocked(const sp<android::gui::WindowInfoHandle>& window, 627 const std::string& reason) REQUIRES(mLock); 628 void updateLastAnrStateLocked(const InputApplicationHandle& application, 629 const std::string& reason) REQUIRES(mLock); 630 void updateLastAnrStateLocked(const std::string& windowLabel, const std::string& reason) 631 REQUIRES(mLock); 632 633 // Outbound policy interactions. 634 void doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry) 635 REQUIRES(mLock); 636 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); 637 void doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); 638 void doNotifyDropWindowLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); 639 640 // ANR-related callbacks - start 641 void doNotifyNoFocusedWindowAnrLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); 642 void doNotifyWindowUnresponsiveLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); 643 void doNotifyMonitorUnresponsiveLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); 644 void doNotifyWindowResponsiveLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); 645 void doNotifyMonitorResponsiveLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); 646 // ANR-related callbacks - end 647 void doNotifySensorLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); 648 void doNotifyUntrustedTouchLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); 649 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry) 650 REQUIRES(mLock); 651 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); 652 void doSetPointerCaptureLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); 653 bool afterKeyEventLockedInterruptible(const sp<Connection>& connection, 654 DispatchEntry* dispatchEntry, KeyEntry& keyEntry, 655 bool handled) REQUIRES(mLock); 656 bool afterMotionEventLockedInterruptible(const sp<Connection>& connection, 657 DispatchEntry* dispatchEntry, MotionEntry& motionEntry, 658 bool handled) REQUIRES(mLock); 659 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); 660 void doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock); 661 662 // Find touched state and touched window by token. 663 std::pair<TouchState*, TouchedWindow*> findTouchStateAndWindowLocked(const sp<IBinder>& token) 664 REQUIRES(mLock); 665 666 // Statistics gathering. 667 LatencyAggregator mLatencyAggregator GUARDED_BY(mLock); 668 LatencyTracker mLatencyTracker GUARDED_BY(mLock); 669 void traceInboundQueueLengthLocked() REQUIRES(mLock); 670 void traceOutboundQueueLength(const Connection& connection); 671 void traceWaitQueueLength(const Connection& connection); 672 673 sp<InputReporterInterface> mReporter; 674 sp<com::android::internal::compat::IPlatformCompatNative> mCompatService; 675 676 void onFirstRef() override; 677 }; 678 679 } // namespace android::inputdispatcher 680 681 #endif // _UI_INPUT_DISPATCHER_H 682