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 #pragma once 18 19 #include <com_android_input_flags.h> 20 21 #include "AnrTracker.h" 22 #include "CancelationOptions.h" 23 #include "DragState.h" 24 #include "Entry.h" 25 #include "FocusResolver.h" 26 #include "InjectionState.h" 27 #include "InputDispatcherConfiguration.h" 28 #include "InputDispatcherInterface.h" 29 #include "InputDispatcherPolicyInterface.h" 30 #include "InputTarget.h" 31 #include "InputThread.h" 32 #include "LatencyAggregator.h" 33 #include "LatencyAggregatorWithHistograms.h" 34 #include "LatencyTracker.h" 35 #include "Monitor.h" 36 #include "TouchState.h" 37 #include "TouchedWindow.h" 38 #include "trace/InputTracerInterface.h" 39 #include "trace/InputTracingBackendInterface.h" 40 41 #include <attestation/HmacKeyManager.h> 42 #include <gui/InputApplication.h> 43 #include <gui/WindowInfosUpdate.h> 44 #include <input/Input.h> 45 #include <input/InputTransport.h> 46 #include <limits.h> 47 #include <powermanager/PowerManager.h> 48 #include <stddef.h> 49 #include <unistd.h> 50 #include <utils/BitSet.h> 51 #include <utils/Looper.h> 52 #include <utils/Timers.h> 53 #include <utils/threads.h> 54 #include <bitset> 55 #include <condition_variable> 56 #include <deque> 57 #include <optional> 58 #include <unordered_map> 59 #include <unordered_set> 60 61 #include <InputListener.h> 62 #include <InputReporterInterface.h> 63 #include <gui/WindowInfosListener.h> 64 65 namespace android::inputdispatcher { 66 67 class Connection; 68 69 /* Dispatches events to input targets. Some functions of the input dispatcher, such as 70 * identifying input targets, are controlled by a separate policy object. 71 * 72 * IMPORTANT INVARIANT: 73 * Because the policy can potentially block or cause re-entrance into the input dispatcher, 74 * the input dispatcher never calls into the policy while holding its internal locks. 75 * The implementation is also carefully designed to recover from scenarios such as an 76 * input channel becoming unregistered while identifying input targets or processing timeouts. 77 * 78 * Methods marked 'Locked' must be called with the lock acquired. 79 * 80 * Methods marked 'LockedInterruptible' must be called with the lock acquired but 81 * may during the course of their execution release the lock, call into the policy, and 82 * then reacquire the lock. The caller is responsible for recovering gracefully. 83 * 84 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa. 85 */ 86 class InputDispatcher : public android::InputDispatcherInterface { 87 public: 88 static constexpr bool kDefaultInTouchMode = true; 89 90 explicit InputDispatcher(InputDispatcherPolicyInterface& policy); 91 // Constructor used for testing. 92 explicit InputDispatcher(InputDispatcherPolicyInterface&, 93 std::unique_ptr<trace::InputTracingBackendInterface>); 94 ~InputDispatcher() override; 95 96 void dump(std::string& dump) const override; 97 void monitor() override; 98 bool waitForIdle() const override; 99 status_t start() override; 100 status_t stop() override; 101 102 void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override; 103 void notifyKey(const NotifyKeyArgs& args) override; 104 void notifyMotion(const NotifyMotionArgs& args) override; 105 void notifySwitch(const NotifySwitchArgs& args) override; 106 void notifySensor(const NotifySensorArgs& args) override; 107 void notifyVibratorState(const NotifyVibratorStateArgs& args) override; 108 void notifyDeviceReset(const NotifyDeviceResetArgs& args) override; 109 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override; 110 111 android::os::InputEventInjectionResult injectInputEvent( 112 const InputEvent* event, std::optional<gui::Uid> targetUid, 113 android::os::InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, 114 uint32_t policyFlags) override; 115 116 std::unique_ptr<VerifiedInputEvent> verifyInputEvent(const InputEvent& event) override; 117 118 void setFocusedApplication( 119 ui::LogicalDisplayId displayId, 120 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) override; 121 void setFocusedDisplay(ui::LogicalDisplayId displayId) override; 122 void setMinTimeBetweenUserActivityPokes(std::chrono::milliseconds interval) override; 123 void setInputDispatchMode(bool enabled, bool frozen) override; 124 void setInputFilterEnabled(bool enabled) override; 125 bool setInTouchMode(bool inTouchMode, gui::Pid pid, gui::Uid uid, bool hasPermission, 126 ui::LogicalDisplayId displayId) override; 127 void setMaximumObscuringOpacityForTouch(float opacity) override; 128 129 bool transferTouchGesture(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, 130 bool isDragDrop = false) override; 131 bool transferTouchOnDisplay(const sp<IBinder>& destChannelToken, 132 ui::LogicalDisplayId displayId) override; 133 134 base::Result<std::unique_ptr<InputChannel>> createInputChannel( 135 const std::string& name) override; 136 void setFocusedWindow(const android::gui::FocusRequest&) override; 137 base::Result<std::unique_ptr<InputChannel>> createInputMonitor(ui::LogicalDisplayId displayId, 138 const std::string& name, 139 gui::Pid pid) override; 140 status_t removeInputChannel(const sp<IBinder>& connectionToken) override; 141 status_t pilferPointers(const sp<IBinder>& token) override; 142 void requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) override; 143 bool flushSensor(int deviceId, InputDeviceSensorType sensorType) override; 144 void setDisplayEligibilityForPointerCapture(ui::LogicalDisplayId displayId, 145 bool isEligible) override; 146 147 std::array<uint8_t, 32> sign(const VerifiedInputEvent& event) const; 148 149 void displayRemoved(ui::LogicalDisplayId displayId) override; 150 151 // Public because it's also used by tests to simulate the WindowInfosListener callback 152 void onWindowInfosChanged(const gui::WindowInfosUpdate&); 153 154 void cancelCurrentTouch() override; 155 156 // Public to allow tests to verify that a Monitor can get ANR. 157 void setMonitorDispatchingTimeoutForTest(std::chrono::nanoseconds timeout); 158 159 void setKeyRepeatConfiguration(std::chrono::nanoseconds timeout, std::chrono::nanoseconds delay, 160 bool keyRepeatEnabled) override; 161 162 bool isPointerInWindow(const sp<IBinder>& token, ui::LogicalDisplayId displayId, 163 DeviceId deviceId, int32_t pointerId) override; 164 165 void setInputMethodConnectionIsActive(bool isActive) override; 166 167 private: 168 enum class DropReason { 169 NOT_DROPPED, 170 POLICY, 171 DISABLED, 172 BLOCKED, 173 STALE, 174 NO_POINTER_CAPTURE, 175 }; 176 177 std::unique_ptr<InputThread> mThread; 178 179 InputDispatcherPolicyInterface& mPolicy; 180 android::InputDispatcherConfiguration mConfig GUARDED_BY(mLock); 181 182 mutable std::mutex mLock; 183 184 std::condition_variable mDispatcherIsAlive; 185 mutable std::condition_variable mDispatcherEnteredIdle; 186 187 // Input event tracer. The tracer will only exist on builds where input tracing is allowed. 188 std::unique_ptr<trace::InputTracerInterface> mTracer GUARDED_BY(mLock); 189 190 sp<Looper> mLooper; 191 192 std::shared_ptr<const EventEntry> mPendingEvent GUARDED_BY(mLock); 193 std::deque<std::shared_ptr<const EventEntry>> mInboundQueue GUARDED_BY(mLock); 194 std::deque<std::shared_ptr<const EventEntry>> mRecentQueue GUARDED_BY(mLock); 195 196 // A command entry captures state and behavior for an action to be performed in the 197 // dispatch loop after the initial processing has taken place. It is essentially 198 // a kind of continuation used to postpone sensitive policy interactions to a point 199 // in the dispatch loop where it is safe to release the lock (generally after finishing 200 // the critical parts of the dispatch cycle). 201 // 202 // The special thing about commands is that they can voluntarily release and reacquire 203 // the dispatcher lock at will. Initially when the command starts running, the 204 // dispatcher lock is held. However, if the command needs to call into the policy to 205 // do some work, it can release the lock, do the work, then reacquire the lock again 206 // before returning. 207 // 208 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch 209 // never calls into the policy while holding its lock. 210 // 211 // Commands are called with the lock held, but they can release and re-acquire the lock from 212 // within. 213 using Command = std::function<void()>; 214 std::deque<Command> mCommandQueue GUARDED_BY(mLock); 215 216 DropReason mLastDropReason GUARDED_BY(mLock); 217 218 const IdGenerator mIdGenerator GUARDED_BY(mLock); 219 220 int64_t mWindowInfosVsyncId GUARDED_BY(mLock); 221 222 std::chrono::milliseconds mMinTimeBetweenUserActivityPokes GUARDED_BY(mLock); 223 224 /** Stores the latest user-activity poke event times per user activity types. */ 225 std::array<nsecs_t, USER_ACTIVITY_EVENT_LAST + 1> mLastUserActivityTimes GUARDED_BY(mLock); 226 227 // With each iteration, InputDispatcher nominally processes one queued event, 228 // a timeout, or a response from an input consumer. 229 // This method should only be called on the input dispatcher's own thread. 230 void dispatchOnce(); 231 232 void dispatchOnceInnerLocked(nsecs_t& nextWakeupTime) REQUIRES(mLock); 233 234 // Enqueues an inbound event. Returns true if mLooper->wake() should be called. 235 bool enqueueInboundEventLocked(std::unique_ptr<EventEntry> entry) REQUIRES(mLock); 236 237 // Cleans up input state when dropping an inbound event. 238 void dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) REQUIRES(mLock); 239 240 // Enqueues a focus event. 241 void enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, 242 const std::string& reason) REQUIRES(mLock); 243 // Enqueues a drag event. 244 void enqueueDragEventLocked(const sp<android::gui::WindowInfoHandle>& windowToken, 245 bool isExiting, const int32_t rawX, const int32_t rawY) 246 REQUIRES(mLock); 247 248 // Adds an event to a queue of recent events for debugging purposes. 249 void addRecentEventLocked(std::shared_ptr<const EventEntry> entry) REQUIRES(mLock); 250 251 // Blocked event latency optimization. Drops old events when the user intends 252 // to transfer focus to a new application. 253 std::shared_ptr<const EventEntry> mNextUnblockedEvent GUARDED_BY(mLock); 254 255 sp<android::gui::WindowInfoHandle> findTouchedWindowAtLocked( 256 ui::LogicalDisplayId displayId, float x, float y, bool isStylus = false, 257 bool ignoreDragWindow = false) const REQUIRES(mLock); 258 std::vector<InputTarget> findOutsideTargetsLocked( 259 ui::LogicalDisplayId displayId, const sp<android::gui::WindowInfoHandle>& touchedWindow, 260 int32_t pointerId) const REQUIRES(mLock); 261 262 std::vector<sp<android::gui::WindowInfoHandle>> findTouchedSpyWindowsAtLocked( 263 ui::LogicalDisplayId displayId, float x, float y, bool isStylus, 264 DeviceId deviceId) const REQUIRES(mLock); 265 266 sp<android::gui::WindowInfoHandle> findTouchedForegroundWindowLocked( 267 ui::LogicalDisplayId displayId) const REQUIRES(mLock); 268 269 std::shared_ptr<Connection> getConnectionLocked(const sp<IBinder>& inputConnectionToken) const 270 REQUIRES(mLock); 271 272 std::string getConnectionNameLocked(const sp<IBinder>& connectionToken) const REQUIRES(mLock); 273 274 void removeConnectionLocked(const std::shared_ptr<Connection>& connection) REQUIRES(mLock); 275 276 status_t pilferPointersLocked(const sp<IBinder>& token) REQUIRES(mLock); 277 278 template <typename T> 279 struct StrongPointerHash { operatorStrongPointerHash280 std::size_t operator()(const sp<T>& b) const { return std::hash<T*>{}(b.get()); } 281 }; 282 283 // All registered connections mapped by input channel token. 284 std::unordered_map<sp<IBinder>, std::shared_ptr<Connection>, StrongPointerHash<IBinder>> 285 mConnectionsByToken GUARDED_BY(mLock); 286 287 // Find a monitor pid by the provided token. 288 std::optional<gui::Pid> findMonitorPidByTokenLocked(const sp<IBinder>& token) REQUIRES(mLock); 289 290 // Input channels that will receive a copy of all input events sent to the provided display. 291 std::unordered_map<ui::LogicalDisplayId, std::vector<Monitor>> mGlobalMonitorsByDisplay 292 GUARDED_BY(mLock); 293 294 const HmacKeyManager mHmacKeyManager; 295 const std::array<uint8_t, 32> getSignature(const MotionEntry& motionEntry, 296 const DispatchEntry& dispatchEntry) const; 297 const std::array<uint8_t, 32> getSignature(const KeyEntry& keyEntry, 298 const DispatchEntry& dispatchEntry) const; 299 300 // Event injection and synchronization. 301 std::condition_variable mInjectionResultAvailable; 302 bool shouldRejectInjectedMotionLocked(const MotionEvent& motion, DeviceId deviceId, 303 ui::LogicalDisplayId displayId, 304 std::optional<gui::Uid> targetUid, int32_t flags) 305 REQUIRES(mLock); 306 void setInjectionResult(const EventEntry& entry, 307 android::os::InputEventInjectionResult injectionResult); 308 void transformMotionEntryForInjectionLocked(MotionEntry&, 309 const ui::Transform& injectedTransform) const 310 REQUIRES(mLock); 311 // Per-display correction of injected events 312 std::map<android::ui::LogicalDisplayId, InputVerifier> mInputFilterVerifiersByDisplay 313 GUARDED_BY(mLock); 314 std::condition_variable mInjectionSyncFinished; 315 void incrementPendingForegroundDispatches(const EventEntry& entry); 316 void decrementPendingForegroundDispatches(const EventEntry& entry); 317 318 // Key repeat tracking. 319 struct KeyRepeatState { 320 std::shared_ptr<const KeyEntry> lastKeyEntry; // or null if no repeat 321 nsecs_t nextRepeatTime; 322 } mKeyRepeatState GUARDED_BY(mLock); 323 324 void resetKeyRepeatLocked() REQUIRES(mLock); 325 std::shared_ptr<KeyEntry> synthesizeKeyRepeatLocked(nsecs_t currentTime) REQUIRES(mLock); 326 327 // Deferred command processing. 328 bool haveCommandsLocked() const REQUIRES(mLock); 329 bool runCommandsLockedInterruptable() REQUIRES(mLock); 330 void postCommandLocked(Command&& command) REQUIRES(mLock); 331 332 // The dispatching timeout to use for Monitors. 333 std::chrono::nanoseconds mMonitorDispatchingTimeout GUARDED_BY(mLock); 334 335 nsecs_t processAnrsLocked() REQUIRES(mLock); 336 void processLatencyStatisticsLocked() REQUIRES(mLock); 337 std::chrono::nanoseconds getDispatchingTimeoutLocked( 338 const std::shared_ptr<Connection>& connection) REQUIRES(mLock); 339 340 // Input filter processing. 341 bool shouldSendKeyToInputFilterLocked(const NotifyKeyArgs& args) REQUIRES(mLock); 342 bool shouldSendMotionToInputFilterLocked(const NotifyMotionArgs& args) REQUIRES(mLock); 343 344 // Inbound event processing. 345 void drainInboundQueueLocked() REQUIRES(mLock); 346 void releasePendingEventLocked() REQUIRES(mLock); 347 void releaseInboundEventLocked(std::shared_ptr<const EventEntry> entry) REQUIRES(mLock); 348 349 // Dispatch state. 350 bool mDispatchEnabled GUARDED_BY(mLock); 351 bool mDispatchFrozen GUARDED_BY(mLock); 352 bool mInputFilterEnabled GUARDED_BY(mLock); 353 float mMaximumObscuringOpacityForTouch GUARDED_BY(mLock); 354 355 // This map is not really needed, but it helps a lot with debugging (dumpsys input). 356 // In the java layer, touch mode states are spread across multiple DisplayContent objects, 357 // making harder to snapshot and retrieve them. 358 std::map<ui::LogicalDisplayId /*displayId*/, bool /*inTouchMode*/> mTouchModePerDisplay 359 GUARDED_BY(mLock); 360 361 class DispatcherWindowListener : public gui::WindowInfosListener { 362 public: DispatcherWindowListener(InputDispatcher & dispatcher)363 explicit DispatcherWindowListener(InputDispatcher& dispatcher) : mDispatcher(dispatcher){}; 364 void onWindowInfosChanged(const gui::WindowInfosUpdate&) override; 365 366 private: 367 InputDispatcher& mDispatcher; 368 }; 369 sp<gui::WindowInfosListener> mWindowInfoListener; 370 371 std::unordered_map<ui::LogicalDisplayId /*displayId*/, 372 std::vector<sp<android::gui::WindowInfoHandle>>> 373 mWindowHandlesByDisplay GUARDED_BY(mLock); 374 std::unordered_map<ui::LogicalDisplayId /*displayId*/, android::gui::DisplayInfo> mDisplayInfos 375 GUARDED_BY(mLock); 376 void setInputWindowsLocked( 377 const std::vector<sp<android::gui::WindowInfoHandle>>& inputWindowHandles, 378 ui::LogicalDisplayId displayId) REQUIRES(mLock); 379 // Get a reference to window handles by display, return an empty vector if not found. 380 const std::vector<sp<android::gui::WindowInfoHandle>>& getWindowHandlesLocked( 381 ui::LogicalDisplayId displayId) const REQUIRES(mLock); 382 ui::Transform getTransformLocked(ui::LogicalDisplayId displayId) const REQUIRES(mLock); 383 384 sp<android::gui::WindowInfoHandle> getWindowHandleLocked( 385 const sp<IBinder>& windowHandleToken, 386 std::optional<ui::LogicalDisplayId> displayId = {}) const REQUIRES(mLock); 387 sp<android::gui::WindowInfoHandle> getWindowHandleLocked( 388 const sp<android::gui::WindowInfoHandle>& windowHandle) const REQUIRES(mLock); 389 sp<android::gui::WindowInfoHandle> getFocusedWindowHandleLocked( 390 ui::LogicalDisplayId displayId) const REQUIRES(mLock); 391 bool canWindowReceiveMotionLocked(const sp<android::gui::WindowInfoHandle>& window, 392 const MotionEntry& motionEntry) const REQUIRES(mLock); 393 394 // Returns all the input targets (with their respective input channels) from the window handles 395 // passed as argument. 396 std::vector<InputTarget> getInputTargetsFromWindowHandlesLocked( 397 const std::vector<sp<android::gui::WindowInfoHandle>>& windowHandles) const 398 REQUIRES(mLock); 399 400 /* 401 * Validate and update InputWindowHandles for a given display. 402 */ 403 void updateWindowHandlesForDisplayLocked( 404 const std::vector<sp<android::gui::WindowInfoHandle>>& inputWindowHandles, 405 ui::LogicalDisplayId displayId) REQUIRES(mLock); 406 407 std::unordered_map<ui::LogicalDisplayId /*displayId*/, TouchState> mTouchStatesByDisplay 408 GUARDED_BY(mLock); 409 std::unique_ptr<DragState> mDragState GUARDED_BY(mLock); 410 411 void setFocusedApplicationLocked( 412 ui::LogicalDisplayId displayId, 413 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) REQUIRES(mLock); 414 // Focused applications. 415 std::unordered_map<ui::LogicalDisplayId /*displayId*/, std::shared_ptr<InputApplicationHandle>> 416 mFocusedApplicationHandlesByDisplay GUARDED_BY(mLock); 417 418 // Top focused display. 419 ui::LogicalDisplayId mFocusedDisplayId GUARDED_BY(mLock); 420 421 // Keeps track of the focused window per display and determines focus changes. 422 FocusResolver mFocusResolver GUARDED_BY(mLock); 423 424 // The enabled state of this request is true iff the focused window on the focused display has 425 // requested Pointer Capture. This request also contains the sequence number associated with the 426 // current request. The state of this variable should always be in sync with the state of 427 // Pointer Capture in the policy, and is only updated through setPointerCaptureLocked(request). 428 PointerCaptureRequest mCurrentPointerCaptureRequest GUARDED_BY(mLock); 429 430 // The window token that has Pointer Capture. 431 // This should be in sync with PointerCaptureChangedEvents dispatched to the input channel. 432 sp<IBinder> mWindowTokenWithPointerCapture GUARDED_BY(mLock); 433 434 // Displays that are ineligible for pointer capture. 435 // TODO(b/214621487): Remove or move to a display flag. 436 std::vector<ui::LogicalDisplayId /*displayId*/> mIneligibleDisplaysForPointerCapture 437 GUARDED_BY(mLock); 438 439 // Disable Pointer Capture as a result of loss of window focus. 440 void disablePointerCaptureForcedLocked() REQUIRES(mLock); 441 442 // Set the Pointer Capture state in the Policy. 443 // The window is not nullptr for requests to enable, otherwise it is nullptr. 444 void setPointerCaptureLocked(const sp<IBinder>& window) REQUIRES(mLock); 445 446 // Dispatcher state at time of last ANR. 447 std::string mLastAnrState GUARDED_BY(mLock); 448 449 // The connection tokens of the channels that the user last interacted (used for debugging and 450 // when switching touch mode state). 451 std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> mInteractionConnectionTokens 452 GUARDED_BY(mLock); 453 void processInteractionsLocked(const EventEntry& entry, const std::vector<InputTarget>& targets) 454 REQUIRES(mLock); 455 456 // Dispatch inbound events. 457 bool dispatchDeviceResetLocked(nsecs_t currentTime, const DeviceResetEntry& entry) 458 REQUIRES(mLock); 459 bool dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<const KeyEntry> entry, 460 DropReason* dropReason, nsecs_t& nextWakeupTime) REQUIRES(mLock); 461 bool dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<const MotionEntry> entry, 462 DropReason* dropReason, nsecs_t& nextWakeupTime) REQUIRES(mLock); 463 void dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<const FocusEntry> entry) 464 REQUIRES(mLock); 465 void dispatchPointerCaptureChangedLocked( 466 nsecs_t currentTime, const std::shared_ptr<const PointerCaptureChangedEntry>& entry, 467 DropReason& dropReason) REQUIRES(mLock); 468 void dispatchTouchModeChangeLocked(nsecs_t currentTime, 469 const std::shared_ptr<const TouchModeEntry>& entry) 470 REQUIRES(mLock); 471 void dispatchEventLocked(nsecs_t currentTime, std::shared_ptr<const EventEntry> entry, 472 const std::vector<InputTarget>& inputTargets) REQUIRES(mLock); 473 void dispatchSensorLocked(nsecs_t currentTime, const std::shared_ptr<const SensorEntry>& entry, 474 DropReason* dropReason, nsecs_t& nextWakeupTime) REQUIRES(mLock); 475 void dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<const DragEntry> entry) 476 REQUIRES(mLock); 477 void logOutboundKeyDetails(const char* prefix, const KeyEntry& entry); 478 void logOutboundMotionDetails(const char* prefix, const MotionEntry& entry); 479 480 /** 481 * This field is set if there is no focused window, and we have an event that requires 482 * a focused window to be dispatched (for example, a KeyEvent). 483 * When this happens, we will wait until *mNoFocusedWindowTimeoutTime before 484 * dropping the event and raising an ANR for that application. 485 * This is useful if an application is slow to add a focused window. 486 */ 487 std::optional<nsecs_t> mNoFocusedWindowTimeoutTime GUARDED_BY(mLock); 488 489 bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry); 490 491 bool shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) const REQUIRES(mLock); 492 493 /** 494 * Time to stop waiting for the events to be processed while trying to dispatch a key. 495 * When this time expires, we just send the pending key event to the currently focused window, 496 * without waiting on other events to be processed first. 497 */ 498 std::optional<nsecs_t> mKeyIsWaitingForEventsTimeout GUARDED_BY(mLock); 499 bool shouldWaitToSendKeyLocked(nsecs_t currentTime, const char* focusedWindowName) 500 REQUIRES(mLock); 501 502 /** 503 * The focused application at the time when no focused window was present. 504 * Used to raise an ANR when we have no focused window. 505 */ 506 std::shared_ptr<InputApplicationHandle> mAwaitedFocusedApplication GUARDED_BY(mLock); 507 /** 508 * The displayId that the focused application is associated with. 509 */ 510 ui::LogicalDisplayId mAwaitedApplicationDisplayId GUARDED_BY(mLock); 511 void processNoFocusedWindowAnrLocked() REQUIRES(mLock); 512 513 /** 514 * Tell policy about a window or a monitor that just became unresponsive. Starts ANR. 515 */ 516 void processConnectionUnresponsiveLocked(const Connection& connection, std::string reason) 517 REQUIRES(mLock); 518 /** 519 * Tell policy about a window or a monitor that just became responsive. 520 */ 521 void processConnectionResponsiveLocked(const Connection& connection) REQUIRES(mLock); 522 523 void sendWindowUnresponsiveCommandLocked(const sp<IBinder>& connectionToken, 524 std::optional<gui::Pid> pid, std::string reason) 525 REQUIRES(mLock); 526 void sendWindowResponsiveCommandLocked(const sp<IBinder>& connectionToken, 527 std::optional<gui::Pid> pid) REQUIRES(mLock); 528 529 // Optimization: AnrTracker is used to quickly find which connection is due for a timeout next. 530 // AnrTracker must be kept in-sync with all responsive connection.waitQueues. 531 // If a connection is not responsive, then the entries should not be added to the AnrTracker. 532 // Once a connection becomes unresponsive, its entries are removed from AnrTracker to 533 // prevent unneeded wakeups. 534 AnrTracker mAnrTracker GUARDED_BY(mLock); 535 536 void cancelEventsForAnrLocked(const std::shared_ptr<Connection>& connection) REQUIRES(mLock); 537 // If a focused application changes, we should stop counting down the "no focused window" time, 538 // because we will have no way of knowing when the previous application actually added a window. 539 // This also means that we will miss cases like pulling down notification shade when the 540 // focused application does not have a focused window (no ANR will be raised if notification 541 // shade is pulled down while we are counting down the timeout). 542 void resetNoFocusedWindowTimeoutLocked() REQUIRES(mLock); 543 544 ui::LogicalDisplayId getTargetDisplayId(const EventEntry& entry); 545 base::Result<sp<android::gui::WindowInfoHandle>, android::os::InputEventInjectionResult> 546 findFocusedWindowTargetLocked(nsecs_t currentTime, const EventEntry& entry, 547 nsecs_t& nextWakeupTime) REQUIRES(mLock); 548 base::Result<std::vector<InputTarget>, android::os::InputEventInjectionResult> 549 findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry& entry) REQUIRES(mLock); 550 std::vector<Monitor> selectResponsiveMonitorsLocked( 551 const std::vector<Monitor>& gestureMonitors) const REQUIRES(mLock); 552 553 std::optional<InputTarget> createInputTargetLocked( 554 const sp<android::gui::WindowInfoHandle>& windowHandle, 555 InputTarget::DispatchMode dispatchMode, ftl::Flags<InputTarget::Flags> targetFlags, 556 std::optional<nsecs_t> firstDownTimeInTarget) const REQUIRES(mLock); 557 void addWindowTargetLocked(const sp<android::gui::WindowInfoHandle>& windowHandle, 558 InputTarget::DispatchMode dispatchMode, 559 ftl::Flags<InputTarget::Flags> targetFlags, 560 std::optional<nsecs_t> firstDownTimeInTarget, 561 std::vector<InputTarget>& inputTargets) const REQUIRES(mLock); 562 void addPointerWindowTargetLocked(const sp<android::gui::WindowInfoHandle>& windowHandle, 563 InputTarget::DispatchMode dispatchMode, 564 ftl::Flags<InputTarget::Flags> targetFlags, 565 std::bitset<MAX_POINTER_ID + 1> pointerIds, 566 std::optional<nsecs_t> firstDownTimeInTarget, 567 std::vector<InputTarget>& inputTargets) const REQUIRES(mLock); 568 void addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, 569 ui::LogicalDisplayId displayId) REQUIRES(mLock); 570 void pokeUserActivityLocked(const EventEntry& eventEntry) REQUIRES(mLock); 571 // Enqueue a drag event if needed, and update the touch state. 572 // Uses findTouchedWindowTargetsLocked to make the decision 573 void addDragEventLocked(const MotionEntry& entry) REQUIRES(mLock); 574 void finishDragAndDrop(ui::LogicalDisplayId displayId, float x, float y) REQUIRES(mLock); 575 576 struct TouchOcclusionInfo { 577 bool hasBlockingOcclusion; 578 float obscuringOpacity; 579 std::string obscuringPackage; 580 gui::Uid obscuringUid = gui::Uid::INVALID; 581 std::vector<std::string> debugInfo; 582 }; 583 584 TouchOcclusionInfo computeTouchOcclusionInfoLocked( 585 const sp<android::gui::WindowInfoHandle>& windowHandle, float x, float y) const 586 REQUIRES(mLock); 587 bool isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const REQUIRES(mLock); 588 bool isWindowObscuredAtPointLocked(const sp<android::gui::WindowInfoHandle>& windowHandle, 589 float x, float y) const REQUIRES(mLock); 590 bool isWindowObscuredLocked(const sp<android::gui::WindowInfoHandle>& windowHandle) const 591 REQUIRES(mLock); 592 std::string dumpWindowForTouchOcclusion(const android::gui::WindowInfo* info, 593 bool isTouchWindow) const; 594 std::string getApplicationWindowLabel(const InputApplicationHandle* applicationHandle, 595 const sp<android::gui::WindowInfoHandle>& windowHandle); 596 597 bool shouldDropInput(const EventEntry& entry, 598 const sp<android::gui::WindowInfoHandle>& windowHandle) const 599 REQUIRES(mLock); 600 601 // Manage the dispatch cycle for a single connection. 602 // These methods are deliberately not Interruptible because doing all of the work 603 // with the mutex held makes it easier to ensure that connection invariants are maintained. 604 // If needed, the methods post commands to run later once the critical bits are done. 605 void prepareDispatchCycleLocked(nsecs_t currentTime, 606 const std::shared_ptr<Connection>& connection, 607 std::shared_ptr<const EventEntry>, 608 const InputTarget& inputTarget) REQUIRES(mLock); 609 void enqueueDispatchEntryAndStartDispatchCycleLocked( 610 nsecs_t currentTime, const std::shared_ptr<Connection>& connection, 611 std::shared_ptr<const EventEntry>, const InputTarget& inputTarget) REQUIRES(mLock); 612 void enqueueDispatchEntryLocked(const std::shared_ptr<Connection>& connection, 613 std::shared_ptr<const EventEntry>, 614 const InputTarget& inputTarget) REQUIRES(mLock); 615 status_t publishMotionEvent(Connection& connection, DispatchEntry& dispatchEntry) const; 616 void startDispatchCycleLocked(nsecs_t currentTime, 617 const std::shared_ptr<Connection>& connection) REQUIRES(mLock); 618 void finishDispatchCycleLocked(nsecs_t currentTime, 619 const std::shared_ptr<Connection>& connection, uint32_t seq, 620 bool handled, nsecs_t consumeTime) REQUIRES(mLock); 621 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, 622 const std::shared_ptr<Connection>& connection, bool notify) 623 REQUIRES(mLock); 624 void drainDispatchQueue(std::deque<std::unique_ptr<DispatchEntry>>& queue); 625 void releaseDispatchEntry(std::unique_ptr<DispatchEntry> dispatchEntry); 626 int handleReceiveCallback(int events, sp<IBinder> connectionToken); 627 // The action sent should only be of type AMOTION_EVENT_* 628 void dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, 629 const sp<IBinder>& newToken) REQUIRES(mLock); 630 631 void synthesizeCancelationEventsForAllConnectionsLocked(const CancelationOptions& options) 632 REQUIRES(mLock); 633 void synthesizeCancelationEventsForMonitorsLocked(const CancelationOptions& options) 634 REQUIRES(mLock); 635 void synthesizeCancelationEventsForWindowLocked(const sp<gui::WindowInfoHandle>&, 636 const CancelationOptions&, 637 const std::shared_ptr<Connection>& = nullptr) 638 REQUIRES(mLock); 639 // This is a convenience function used to generate cancellation for a connection without having 640 // to check whether it's a monitor or a window. For non-monitors, the window handle must not be 641 // null. Always prefer the "-ForWindow" method above when explicitly dealing with windows. 642 void synthesizeCancelationEventsForConnectionLocked( 643 const std::shared_ptr<Connection>& connection, const CancelationOptions& options, 644 const sp<gui::WindowInfoHandle>& window) REQUIRES(mLock); 645 646 void synthesizePointerDownEventsForConnectionLocked( 647 const nsecs_t downTime, const std::shared_ptr<Connection>& connection, 648 ftl::Flags<InputTarget::Flags> targetFlags, 649 const std::unique_ptr<trace::EventTrackerInterface>& traceTracker) REQUIRES(mLock); 650 651 // Splitting motion events across windows. When splitting motion event for a target, 652 // splitDownTime refers to the time of first 'down' event on that particular target 653 std::unique_ptr<MotionEntry> splitMotionEvent(const MotionEntry& originalMotionEntry, 654 std::bitset<MAX_POINTER_ID + 1> pointerIds, 655 nsecs_t splitDownTime) REQUIRES(mLock); 656 657 // Reset and drop everything the dispatcher is doing. 658 void resetAndDropEverythingLocked(const char* reason) REQUIRES(mLock); 659 660 // Dump state. 661 void dumpDispatchStateLocked(std::string& dump) const REQUIRES(mLock); 662 void dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors) const; 663 void logDispatchStateLocked() const REQUIRES(mLock); 664 std::string dumpPointerCaptureStateLocked() const REQUIRES(mLock); 665 666 // Registration. 667 void removeMonitorChannelLocked(const sp<IBinder>& connectionToken) REQUIRES(mLock); 668 status_t removeInputChannelLocked(const sp<IBinder>& connectionToken, bool notify) 669 REQUIRES(mLock); 670 671 // Interesting events that we might like to log or tell the framework about. 672 void doDispatchCycleFinishedCommand(nsecs_t finishTime, 673 const std::shared_ptr<Connection>& connection, uint32_t seq, 674 bool handled, nsecs_t consumeTime) REQUIRES(mLock); 675 void doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, 676 const KeyEntry& entry) REQUIRES(mLock); 677 void onFocusChangedLocked(const FocusResolver::FocusChanges& changes, 678 const std::unique_ptr<trace::EventTrackerInterface>& traceTracker, 679 const sp<gui::WindowInfoHandle> removedFocusedWindowHandle = nullptr) 680 REQUIRES(mLock); 681 void sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, const sp<IBinder>& newToken) 682 REQUIRES(mLock); 683 void sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) REQUIRES(mLock); 684 void onAnrLocked(const std::shared_ptr<Connection>& connection) REQUIRES(mLock); 685 void onAnrLocked(std::shared_ptr<InputApplicationHandle> application) REQUIRES(mLock); 686 void updateLastAnrStateLocked(const sp<android::gui::WindowInfoHandle>& window, 687 const std::string& reason) REQUIRES(mLock); 688 void updateLastAnrStateLocked(const InputApplicationHandle& application, 689 const std::string& reason) REQUIRES(mLock); 690 void updateLastAnrStateLocked(const std::string& windowLabel, const std::string& reason) 691 REQUIRES(mLock); 692 std::map<ui::LogicalDisplayId, InputVerifier> mVerifiersByDisplay; 693 // Returns a fallback KeyEntry that should be sent to the connection, if required. 694 std::unique_ptr<const KeyEntry> afterKeyEventLockedInterruptable( 695 const std::shared_ptr<Connection>& connection, DispatchEntry* dispatchEntry, 696 bool handled) REQUIRES(mLock); 697 698 // Find touched state and touched window by token. 699 std::tuple<TouchState*, TouchedWindow*, ui::LogicalDisplayId> 700 findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) REQUIRES(mLock); 701 702 std::tuple<const TouchState*, const TouchedWindow*, ui::LogicalDisplayId> 703 findTouchStateWindowAndDisplayLocked(const sp<IBinder>& token) const REQUIRES(mLock); 704 bool windowHasTouchingPointersLocked(const sp<android::gui::WindowInfoHandle>& windowHandle, 705 DeviceId deviceId) const REQUIRES(mLock); 706 707 // Statistics gathering. 708 nsecs_t mLastStatisticPushTime = 0; 709 std::unique_ptr<InputEventTimelineProcessor> mInputEventTimelineProcessor GUARDED_BY(mLock); 710 LatencyTracker mLatencyTracker GUARDED_BY(mLock); 711 void traceInboundQueueLengthLocked() REQUIRES(mLock); 712 void traceOutboundQueueLength(const Connection& connection); 713 void traceWaitQueueLength(const Connection& connection); 714 715 // Check window ownership 716 bool focusedWindowIsOwnedByLocked(gui::Pid pid, gui::Uid uid) REQUIRES(mLock); 717 bool recentWindowsAreOwnedByLocked(gui::Pid pid, gui::Uid uid) REQUIRES(mLock); 718 719 sp<InputReporterInterface> mReporter; 720 721 /** 722 * Slip the wallpaper touch if necessary. 723 * 724 * @param targetFlags the target flags 725 * @param oldWindowHandle the old window that the touch slipped out of 726 * @param newWindowHandle the new window that the touch is slipping into 727 * @param state the current touch state. This will be updated if necessary to reflect the new 728 * windows that are receiving touch. 729 * @param deviceId the device id of the current motion being processed 730 * @param pointerProperties the pointer properties of the current motion being processed 731 * @param targets the current targets to add the walpaper ones to 732 * @param eventTime the new downTime for the wallpaper target 733 */ 734 void slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags, 735 const sp<android::gui::WindowInfoHandle>& oldWindowHandle, 736 const sp<android::gui::WindowInfoHandle>& newWindowHandle, 737 TouchState& state, const MotionEntry& entry, 738 std::vector<InputTarget>& targets) const REQUIRES(mLock); 739 void transferWallpaperTouch(ftl::Flags<InputTarget::Flags> oldTargetFlags, 740 ftl::Flags<InputTarget::Flags> newTargetFlags, 741 const sp<android::gui::WindowInfoHandle> fromWindowHandle, 742 const sp<android::gui::WindowInfoHandle> toWindowHandle, 743 TouchState& state, DeviceId deviceId, 744 const std::vector<PointerProperties>& pointers, 745 const std::unique_ptr<trace::EventTrackerInterface>& traceTracker) 746 REQUIRES(mLock); 747 748 sp<android::gui::WindowInfoHandle> findWallpaperWindowBelow( 749 const sp<android::gui::WindowInfoHandle>& windowHandle) const REQUIRES(mLock); 750 751 /** Stores the value of the input flag for per device input latency metrics. */ 752 const bool mPerDeviceInputLatencyMetricsFlag = 753 com::android::input::flags::enable_per_device_input_latency_metrics(); 754 }; 755 756 } // namespace android::inputdispatcher 757