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, bool transferEntireGesture) 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 void setDisplayTopology(const DisplayTopologyGraph& displayTopologyGraph) override; 168 169 private: 170 enum class DropReason { 171 NOT_DROPPED, 172 POLICY, 173 DISABLED, 174 BLOCKED, 175 STALE, 176 NO_POINTER_CAPTURE, 177 }; 178 179 std::unique_ptr<InputThread> mThread; 180 181 InputDispatcherPolicyInterface& mPolicy; 182 android::InputDispatcherConfiguration mConfig GUARDED_BY(mLock); 183 184 mutable std::mutex mLock; 185 186 std::condition_variable mDispatcherIsAlive; 187 mutable std::condition_variable mDispatcherEnteredIdle; 188 189 // Input event tracer. The tracer will only exist on builds where input tracing is allowed. 190 std::unique_ptr<trace::InputTracerInterface> mTracer GUARDED_BY(mLock); 191 192 sp<Looper> mLooper; 193 194 std::shared_ptr<const EventEntry> mPendingEvent GUARDED_BY(mLock); 195 std::deque<std::shared_ptr<const EventEntry>> mInboundQueue GUARDED_BY(mLock); 196 std::deque<std::shared_ptr<const EventEntry>> mRecentQueue GUARDED_BY(mLock); 197 198 // A command entry captures state and behavior for an action to be performed in the 199 // dispatch loop after the initial processing has taken place. It is essentially 200 // a kind of continuation used to postpone sensitive policy interactions to a point 201 // in the dispatch loop where it is safe to release the lock (generally after finishing 202 // the critical parts of the dispatch cycle). 203 // 204 // The special thing about commands is that they can voluntarily release and reacquire 205 // the dispatcher lock at will. Initially when the command starts running, the 206 // dispatcher lock is held. However, if the command needs to call into the policy to 207 // do some work, it can release the lock, do the work, then reacquire the lock again 208 // before returning. 209 // 210 // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch 211 // never calls into the policy while holding its lock. 212 // 213 // Commands are called with the lock held, but they can release and re-acquire the lock from 214 // within. 215 using Command = std::function<void()>; 216 std::deque<Command> mCommandQueue GUARDED_BY(mLock); 217 218 DropReason mLastDropReason GUARDED_BY(mLock); 219 220 const IdGenerator mIdGenerator GUARDED_BY(mLock); 221 222 int64_t mWindowInfosVsyncId GUARDED_BY(mLock); 223 224 std::chrono::milliseconds mMinTimeBetweenUserActivityPokes GUARDED_BY(mLock); 225 226 /** Stores the latest user-activity poke event times per user activity types. */ 227 std::array<nsecs_t, USER_ACTIVITY_EVENT_LAST + 1> mLastUserActivityTimes GUARDED_BY(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 class ConnectionManager { 235 public: 236 ConnectionManager(const sp<Looper>& lopper); 237 ~ConnectionManager(); 238 239 std::shared_ptr<Connection> getConnection(const sp<IBinder>& inputConnectionToken) const; 240 241 // Find a monitor pid by the provided token. 242 std::optional<gui::Pid> findMonitorPidByToken(const sp<IBinder>& token) const; 243 void forEachGlobalMonitorConnection( 244 std::function<void(const std::shared_ptr<Connection>&)> f) const; 245 void forEachGlobalMonitorConnection( 246 ui::LogicalDisplayId displayId, 247 std::function<void(const std::shared_ptr<Connection>&)> f) const; 248 249 void createGlobalInputMonitor(ui::LogicalDisplayId displayId, 250 std::unique_ptr<InputChannel>&& inputChannel, 251 const IdGenerator& idGenerator, gui::Pid pid, 252 std::function<int(int)> callback); 253 254 status_t removeConnection(const std::shared_ptr<Connection>& connection); 255 256 void createConnection(std::unique_ptr<InputChannel>&& inputChannel, 257 const IdGenerator& idGenerator, std::function<int(int)> callback); 258 259 std::string dump(nsecs_t currentTime) const; 260 261 private: 262 const sp<Looper> mLooper; 263 264 // All registered connections mapped by input channel token. 265 std::unordered_map<sp<IBinder>, std::shared_ptr<Connection>, StrongPointerHash<IBinder>> 266 mConnectionsByToken; 267 268 // Input channels that will receive a copy of all input events sent to the provided display. 269 std::unordered_map<ui::LogicalDisplayId, std::vector<Monitor>> mGlobalMonitorsByDisplay; 270 271 void removeMonitorChannel(const sp<IBinder>& connectionToken); 272 }; 273 274 ConnectionManager mConnectionManager GUARDED_BY(mLock); 275 276 class DispatcherWindowInfo { 277 public: 278 struct TouchOcclusionInfo { 279 bool hasBlockingOcclusion; 280 float obscuringOpacity; 281 std::string obscuringPackage; 282 gui::Uid obscuringUid = gui::Uid::INVALID; 283 std::vector<std::string> debugInfo; 284 }; 285 286 void setWindowHandlesForDisplay( 287 ui::LogicalDisplayId displayId, 288 std::vector<sp<android::gui::WindowInfoHandle>>&& windowHandles); 289 290 void setDisplayInfos(const std::vector<android::gui::DisplayInfo>& displayInfos); 291 292 void removeDisplay(ui::LogicalDisplayId displayId); 293 294 void setMaximumObscuringOpacityForTouch(float opacity); 295 296 void setDisplayTopology(const DisplayTopologyGraph& displayTopologyGraph); 297 298 // Get a reference to window handles by display, return an empty vector if not found. 299 const std::vector<sp<android::gui::WindowInfoHandle>>& getWindowHandlesForDisplay( 300 ui::LogicalDisplayId displayId) const; 301 302 void forEachWindowHandle( 303 std::function<void(const sp<android::gui::WindowInfoHandle>&)> f) const; 304 305 void forEachDisplayId(std::function<void(ui::LogicalDisplayId)> f) const; 306 307 // Get the transform for display, returns Identity-transform if display is missing. 308 ui::Transform getDisplayTransform(ui::LogicalDisplayId displayId) const; 309 310 // Get the raw transform to use for motion events going to the given window. Optionally a 311 // pointer displayId may be supplied if pointer is on a different display from the window. 312 ui::Transform getRawTransform( 313 const android::gui::WindowInfo& windowInfo, 314 std::optional<ui::LogicalDisplayId> pointerDisplayId = std::nullopt) const; 315 316 // Lookup for WindowInfoHandle from token and optionally a display-id. In cases where 317 // display-id is not provided lookup is done for all displays. 318 sp<android::gui::WindowInfoHandle> findWindowHandle( 319 const sp<IBinder>& windowHandleToken, 320 std::optional<ui::LogicalDisplayId> displayId = {}) const; 321 322 // Lookup for WindowInfoHandle from token and a display-id. Lookup is done for all connected 323 // displays in the topology of the queried display. 324 sp<android::gui::WindowInfoHandle> findWindowHandleOnConnectedDisplays( 325 const sp<IBinder>& windowHandleToken, ui::LogicalDisplayId displayId) const; 326 327 bool isWindowPresent(const sp<android::gui::WindowInfoHandle>& windowHandle) const; 328 329 // Returns the touched window at the given location, excluding the ignoreWindow if provided. 330 sp<android::gui::WindowInfoHandle> findTouchedWindowAt( 331 ui::LogicalDisplayId displayId, float x, float y, bool isStylus = false, 332 const sp<android::gui::WindowInfoHandle> ignoreWindow = nullptr) const; 333 334 TouchOcclusionInfo computeTouchOcclusionInfo( 335 const sp<android::gui::WindowInfoHandle>& windowHandle, float x, float y) const; 336 337 bool isWindowObscured(const sp<android::gui::WindowInfoHandle>& windowHandle) const; 338 339 bool isWindowObscuredAtPoint(const sp<android::gui::WindowInfoHandle>& windowHandle, 340 float x, float y) const; 341 342 sp<android::gui::WindowInfoHandle> findWallpaperWindowBelow( 343 const sp<android::gui::WindowInfoHandle>& windowHandle) const; 344 345 bool isTouchTrusted(const TouchOcclusionInfo& occlusionInfo) const; 346 347 // Returns topology's primary display if the display belongs to it, otherwise the 348 // same displayId. 349 ui::LogicalDisplayId getPrimaryDisplayId(ui::LogicalDisplayId displayId) const; 350 351 bool areDisplaysConnected(ui::LogicalDisplayId display1, 352 ui::LogicalDisplayId display2) const; 353 354 std::string dumpDisplayAndWindowInfo() const; 355 356 private: 357 std::vector<ui::LogicalDisplayId> getConnectedDisplays( 358 ui::LogicalDisplayId displayId) const; 359 360 sp<android::gui::WindowInfoHandle> findWindowHandleOnDisplay( 361 const sp<IBinder>& windowHandleToken, ui::LogicalDisplayId displayId) const; 362 363 std::unordered_map<ui::LogicalDisplayId /*displayId*/, 364 std::vector<sp<android::gui::WindowInfoHandle>>> 365 mWindowHandlesByDisplay; 366 std::unordered_map<ui::LogicalDisplayId /*displayId*/, android::gui::DisplayInfo> 367 mDisplayInfos; 368 float mMaximumObscuringOpacityForTouch{1.0f}; 369 370 // Topology is initialized with default-constructed value, which is an empty topology until 371 // we receive setDisplayTopology call. Meanwhile we will treat every display as an 372 // independent display. 373 DisplayTopologyGraph mTopology; 374 }; 375 376 DispatcherWindowInfo mWindowInfos GUARDED_BY(mLock); 377 378 class DispatcherTouchState { 379 public: 380 struct CancellationArgs { 381 const sp<gui::WindowInfoHandle> windowHandle; 382 CancelationOptions::Mode mode; 383 std::optional<DeviceId> deviceId{std::nullopt}; 384 ui::LogicalDisplayId displayId{ui::LogicalDisplayId::INVALID}; 385 std::bitset<MAX_POINTER_ID + 1> pointerIds{}; 386 }; 387 388 struct PointerDownArgs { 389 const nsecs_t downTimeInTarget; 390 const std::shared_ptr<Connection> connection; 391 const ftl::Flags<InputTarget::Flags> targetFlags; 392 }; 393 394 DispatcherTouchState(const DispatcherWindowInfo& windowInfos, 395 const ConnectionManager& connections); 396 397 void addPointerWindowTarget(const sp<android::gui::WindowInfoHandle>& windowHandle, 398 InputTarget::DispatchMode dispatchMode, 399 ftl::Flags<InputTarget::Flags> targetFlags, 400 std::bitset<MAX_POINTER_ID + 1> pointerIds, 401 std::optional<nsecs_t> firstDownTimeInTarget, 402 std::optional<ui::LogicalDisplayId> pointerDisplayId, 403 std::function<void()> dump, 404 std::vector<InputTarget>& inputTargets); 405 406 base::Result<std::vector<InputTarget>, android::os::InputEventInjectionResult> 407 findTouchedWindowTargets(nsecs_t currentTime, const MotionEntry& entry, 408 const sp<android::gui::WindowInfoHandle> dragWindow, 409 std::function<void(const MotionEntry&)> addDragEvent, 410 std::function<void()> dump); 411 412 sp<android::gui::WindowInfoHandle> findTouchedForegroundWindow( 413 ui::LogicalDisplayId displayId) const; 414 415 bool hasTouchingOrHoveringPointers(ui::LogicalDisplayId displayId, int32_t deviceId) const; 416 417 bool isPointerInWindow(const sp<android::IBinder>& token, ui::LogicalDisplayId displayId, 418 DeviceId deviceId, int32_t pointerId) const; 419 420 // Find an existing touched windowHandle and display by token. 421 std::tuple<const sp<gui::WindowInfoHandle>&, ui::LogicalDisplayId> 422 findExistingTouchedWindowHandleAndDisplay(const sp<IBinder>& token) const; 423 424 void forAllTouchedWindows(std::function<void(const sp<gui::WindowInfoHandle>&)> f) const; 425 426 void forAllTouchedWindowsOnDisplay( 427 ui::LogicalDisplayId displayId, 428 std::function<void(const sp<gui::WindowInfoHandle>&)> f) const; 429 430 std::string dump() const; 431 432 // Updates the touchState for display from WindowInfo, 433 // returns list of CancellationArgs for every cancelled touch 434 std::list<CancellationArgs> updateFromWindowInfo(ui::LogicalDisplayId displayId); 435 436 void removeAllPointersForDevice(DeviceId deviceId); 437 438 // transfer touch between provided tokens, returns destination WindowHandle, deviceId, 439 // pointers, list of cancelled windows and pointers on successful transfer. 440 std::optional< 441 std::tuple<sp<gui::WindowInfoHandle>, DeviceId, std::vector<PointerProperties>, 442 std::list<CancellationArgs>, std::list<PointerDownArgs>>> 443 transferTouchGesture(const sp<IBinder>& fromToken, const sp<IBinder>& toToken, 444 bool transferEntireGesture); 445 446 base::Result<std::list<CancellationArgs>, status_t> pilferPointers( 447 const sp<IBinder>& token, const Connection& requestingConnection); 448 449 void clear(); 450 451 private: 452 std::unordered_map<ui::LogicalDisplayId, TouchState> mTouchStatesByDisplay; 453 454 // As there can be only one CursorState per topology group, we will treat all displays in 455 // the topology as one connected display-group. These will be identified by 456 // DisplayTopologyGraph::primaryDisplayId. 457 // Cursor on the any of the displays that are not part of the topology will be identified by 458 // the displayId similar to mTouchStatesByDisplay. 459 std::unordered_map<ui::LogicalDisplayId, TouchState> mCursorStateByDisplay; 460 461 // The supplied lambda is invoked for each touch and cursor state of the display. 462 // The function iterates until the lambda returns true, effectively performing a 'break' 463 // from the iteration. 464 void forTouchAndCursorStatesOnDisplay(ui::LogicalDisplayId displayId, 465 std::function<bool(const TouchState&)> f) const; 466 467 void forTouchAndCursorStatesOnDisplay(ui::LogicalDisplayId displayId, 468 std::function<bool(TouchState&)> f); 469 470 // The supplied lambda is invoked for each touchState. The function iterates until 471 // the lambda returns true, effectively performing a 'break' from the iteration. 472 void forAllTouchAndCursorStates( 473 std::function<bool(ui::LogicalDisplayId, const TouchState&)> f) const; 474 475 void forAllTouchAndCursorStates(std::function<bool(ui::LogicalDisplayId, TouchState&)> f); 476 477 std::optional<std::tuple<TouchState&, TouchedWindow&, ui::LogicalDisplayId>> 478 findTouchStateWindowAndDisplay(const sp<IBinder>& token); 479 480 std::pair<std::list<CancellationArgs>, std::list<PointerDownArgs>> transferWallpaperTouch( 481 const sp<gui::WindowInfoHandle> fromWindowHandle, 482 const sp<gui::WindowInfoHandle> toWindowHandle, TouchState& state, 483 DeviceId deviceId, const std::vector<PointerProperties>& pointers, 484 ftl::Flags<InputTarget::Flags> oldTargetFlags, 485 ftl::Flags<InputTarget::Flags> newTargetFlags); 486 487 void saveTouchStateForMotionEntry(const MotionEntry& entry, TouchState&& touchState); 488 489 void eraseTouchStateForMotionEntry(const MotionEntry& entry); 490 491 const TouchState* getTouchStateForMotionEntry( 492 const android::inputdispatcher::MotionEntry& entry) const; 493 494 bool canWindowReceiveMotion(const sp<gui::WindowInfoHandle>& window, 495 const MotionEntry& motionEntry) const; 496 497 // Return true if stylus is currently down anywhere on the specified display, 498 // and false otherwise. 499 bool isStylusActiveInDisplay(ui::LogicalDisplayId displayId) const; 500 501 std::list<CancellationArgs> eraseRemovedWindowsFromWindowInfo( 502 TouchState& state, ui::LogicalDisplayId displayId); 503 504 std::list<CancellationArgs> updateHoveringStateFromWindowInfo( 505 TouchState& state, ui::LogicalDisplayId displayId); 506 507 std::vector<InputTarget> findOutsideTargets(ui::LogicalDisplayId displayId, 508 const sp<gui::WindowInfoHandle>& touchedWindow, 509 int32_t pointerId, std::function<void()> dump); 510 511 /** 512 * Slip the wallpaper touch if necessary. 513 * 514 * @param targetFlags the target flags 515 * @param oldWindowHandle the old window that the touch slipped out of 516 * @param newWindowHandle the new window that the touch is slipping into 517 * @param state the current touch state. This will be updated if necessary to reflect the 518 * new windows that are receiving touch. 519 * @param deviceId the device id of the current motion being processed 520 * @param pointerProperties the pointer properties of the current motion being processed 521 * @param targets the current targets to add the walpaper ones to 522 * @param eventTime the new downTime for the wallpaper target 523 */ 524 void slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFlags, 525 const sp<android::gui::WindowInfoHandle>& oldWindowHandle, 526 const sp<android::gui::WindowInfoHandle>& newWindowHandle, 527 TouchState& state, const MotionEntry& entry, 528 std::vector<InputTarget>& targets, std::function<void()> dump); 529 530 ftl::Flags<InputTarget::Flags> getTargetFlags( 531 const sp<android::gui::WindowInfoHandle>& targetWindow, vec2 targetPosition, 532 bool isSplit); 533 534 const DispatcherWindowInfo& mWindowInfos; 535 const ConnectionManager& mConnectionManager; 536 }; 537 538 DispatcherTouchState mTouchStates GUARDED_BY(mLock); 539 540 // With each iteration, InputDispatcher nominally processes one queued event, 541 // a timeout, or a response from an input consumer. 542 // This method should only be called on the input dispatcher's own thread. 543 void dispatchOnce(); 544 545 void dispatchOnceInnerLocked(nsecs_t& nextWakeupTime) REQUIRES(mLock); 546 547 // Enqueues an inbound event. Returns true if mLooper->wake() should be called. 548 bool enqueueInboundEventLocked(std::unique_ptr<EventEntry> entry) REQUIRES(mLock); 549 550 // Cleans up input state when dropping an inbound event. 551 void dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) REQUIRES(mLock); 552 553 // Enqueues a focus event. 554 void enqueueFocusEventLocked(const sp<IBinder>& windowToken, bool hasFocus, 555 const std::string& reason) REQUIRES(mLock); 556 // Enqueues a drag event. 557 void enqueueDragEventLocked(const sp<android::gui::WindowInfoHandle>& windowToken, 558 bool isExiting, const int32_t rawX, const int32_t rawY) 559 REQUIRES(mLock); 560 561 // Adds an event to a queue of recent events for debugging purposes. 562 void addRecentEventLocked(std::shared_ptr<const EventEntry> entry) REQUIRES(mLock); 563 564 // Blocked event latency optimization. Drops old events when the user intends 565 // to transfer focus to a new application. 566 std::shared_ptr<const EventEntry> mNextUnblockedEvent GUARDED_BY(mLock); 567 568 status_t pilferPointersLocked(const sp<IBinder>& token) REQUIRES(mLock); 569 570 const HmacKeyManager mHmacKeyManager; 571 const std::array<uint8_t, 32> getSignature(const MotionEntry& motionEntry, 572 const DispatchEntry& dispatchEntry) const; 573 const std::array<uint8_t, 32> getSignature(const KeyEntry& keyEntry, 574 const DispatchEntry& dispatchEntry) const; 575 576 // Event injection and synchronization. 577 std::condition_variable mInjectionResultAvailable; 578 bool shouldRejectInjectedMotionLocked(const MotionEvent& motion, DeviceId deviceId, 579 ui::LogicalDisplayId displayId, 580 std::optional<gui::Uid> targetUid, int32_t flags) 581 REQUIRES(mLock); 582 void setInjectionResult(const EventEntry& entry, 583 android::os::InputEventInjectionResult injectionResult); 584 void transformMotionEntryForInjectionLocked(MotionEntry&, 585 const ui::Transform& injectedTransform) const 586 REQUIRES(mLock); 587 // Per-display correction of injected events 588 std::map<android::ui::LogicalDisplayId, InputVerifier> mInputFilterVerifiersByDisplay 589 GUARDED_BY(mLock); 590 std::condition_variable mInjectionSyncFinished; 591 void incrementPendingForegroundDispatches(const EventEntry& entry); 592 void decrementPendingForegroundDispatches(const EventEntry& entry); 593 594 // Key repeat tracking. 595 struct KeyRepeatState { 596 std::shared_ptr<const KeyEntry> lastKeyEntry; // or null if no repeat 597 nsecs_t nextRepeatTime; 598 } mKeyRepeatState GUARDED_BY(mLock); 599 600 void resetKeyRepeatLocked() REQUIRES(mLock); 601 std::shared_ptr<KeyEntry> synthesizeKeyRepeatLocked(nsecs_t currentTime) REQUIRES(mLock); 602 603 // Deferred command processing. 604 bool haveCommandsLocked() const REQUIRES(mLock); 605 bool runCommandsLockedInterruptable() REQUIRES(mLock); 606 void postCommandLocked(Command&& command) REQUIRES(mLock); 607 608 // The dispatching timeout to use for Monitors. 609 std::chrono::nanoseconds mMonitorDispatchingTimeout GUARDED_BY(mLock); 610 611 nsecs_t processAnrsLocked() REQUIRES(mLock); 612 void processLatencyStatisticsLocked() REQUIRES(mLock); 613 std::chrono::nanoseconds getDispatchingTimeoutLocked( 614 const std::shared_ptr<Connection>& connection) REQUIRES(mLock); 615 616 // Input filter processing. 617 bool shouldSendKeyToInputFilterLocked(const NotifyKeyArgs& args) REQUIRES(mLock); 618 bool shouldSendMotionToInputFilterLocked(const NotifyMotionArgs& args) REQUIRES(mLock); 619 620 // Inbound event processing. 621 void drainInboundQueueLocked() REQUIRES(mLock); 622 void releasePendingEventLocked() REQUIRES(mLock); 623 void releaseInboundEventLocked(std::shared_ptr<const EventEntry> entry) REQUIRES(mLock); 624 625 // Dispatch state. 626 bool mDispatchEnabled GUARDED_BY(mLock); 627 bool mDispatchFrozen GUARDED_BY(mLock); 628 bool mInputFilterEnabled GUARDED_BY(mLock); 629 630 // This map is not really needed, but it helps a lot with debugging (dumpsys input). 631 // In the java layer, touch mode states are spread across multiple DisplayContent objects, 632 // making harder to snapshot and retrieve them. 633 std::map<ui::LogicalDisplayId /*displayId*/, bool /*inTouchMode*/> mTouchModePerDisplay 634 GUARDED_BY(mLock); 635 636 class DispatcherWindowListener : public gui::WindowInfosListener { 637 public: DispatcherWindowListener(InputDispatcher & dispatcher)638 explicit DispatcherWindowListener(InputDispatcher& dispatcher) : mDispatcher(dispatcher){}; 639 void onWindowInfosChanged(const gui::WindowInfosUpdate&) override; 640 641 private: 642 InputDispatcher& mDispatcher; 643 }; 644 sp<gui::WindowInfosListener> mWindowInfoListener; 645 646 void setInputWindowsLocked( 647 const std::vector<sp<android::gui::WindowInfoHandle>>& inputWindowHandles, 648 ui::LogicalDisplayId displayId) REQUIRES(mLock); 649 650 sp<android::gui::WindowInfoHandle> getFocusedWindowHandleLocked( 651 ui::LogicalDisplayId displayId) const REQUIRES(mLock); 652 653 // Returns all the input targets (with their respective input channels) from the window handles 654 // passed as argument. 655 std::vector<InputTarget> getInputTargetsFromWindowHandlesLocked( 656 const std::vector<sp<android::gui::WindowInfoHandle>>& windowHandles) const 657 REQUIRES(mLock); 658 659 /* 660 * Validate and update InputWindowHandles for a given display. 661 */ 662 void updateWindowHandlesForDisplayLocked( 663 const std::vector<sp<android::gui::WindowInfoHandle>>& inputWindowHandles, 664 ui::LogicalDisplayId displayId) REQUIRES(mLock); 665 666 std::unique_ptr<DragState> mDragState GUARDED_BY(mLock); 667 668 void setFocusedApplicationLocked( 669 ui::LogicalDisplayId displayId, 670 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) REQUIRES(mLock); 671 // Focused applications. 672 std::unordered_map<ui::LogicalDisplayId /*displayId*/, std::shared_ptr<InputApplicationHandle>> 673 mFocusedApplicationHandlesByDisplay GUARDED_BY(mLock); 674 675 // Top focused display. 676 ui::LogicalDisplayId mFocusedDisplayId GUARDED_BY(mLock); 677 678 // Keeps track of the focused window per display and determines focus changes. 679 FocusResolver mFocusResolver GUARDED_BY(mLock); 680 681 // The enabled state of this request is true iff the focused window on the focused display has 682 // requested Pointer Capture. This request also contains the sequence number associated with the 683 // current request. The state of this variable should always be in sync with the state of 684 // Pointer Capture in the policy, and is only updated through setPointerCaptureLocked(request). 685 PointerCaptureRequest mCurrentPointerCaptureRequest GUARDED_BY(mLock); 686 687 // The window token that has Pointer Capture. 688 // This should be in sync with PointerCaptureChangedEvents dispatched to the input channel. 689 sp<IBinder> mWindowTokenWithPointerCapture GUARDED_BY(mLock); 690 691 // Displays that are ineligible for pointer capture. 692 // TODO(b/214621487): Remove or move to a display flag. 693 std::vector<ui::LogicalDisplayId /*displayId*/> mIneligibleDisplaysForPointerCapture 694 GUARDED_BY(mLock); 695 696 // Disable Pointer Capture as a result of loss of window focus. 697 void disablePointerCaptureForcedLocked() REQUIRES(mLock); 698 699 // Set the Pointer Capture state in the Policy. 700 // The window is not nullptr for requests to enable, otherwise it is nullptr. 701 void setPointerCaptureLocked(const sp<IBinder>& window) REQUIRES(mLock); 702 703 // Dispatcher state at time of last ANR. 704 std::string mLastAnrState GUARDED_BY(mLock); 705 706 // The connection tokens of the channels that the user last interacted (used for debugging and 707 // when switching touch mode state). 708 std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> mInteractionConnectionTokens 709 GUARDED_BY(mLock); 710 void processInteractionsLocked(const EventEntry& entry, const std::vector<InputTarget>& targets) 711 REQUIRES(mLock); 712 713 // Dispatch inbound events. 714 bool dispatchDeviceResetLocked(nsecs_t currentTime, const DeviceResetEntry& entry) 715 REQUIRES(mLock); 716 bool dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<const KeyEntry> entry, 717 DropReason* dropReason, nsecs_t& nextWakeupTime) REQUIRES(mLock); 718 bool dispatchMotionLocked(nsecs_t currentTime, std::shared_ptr<const MotionEntry> entry, 719 DropReason* dropReason, nsecs_t& nextWakeupTime) REQUIRES(mLock); 720 void dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<const FocusEntry> entry) 721 REQUIRES(mLock); 722 void dispatchPointerCaptureChangedLocked( 723 nsecs_t currentTime, const std::shared_ptr<const PointerCaptureChangedEntry>& entry, 724 DropReason& dropReason) REQUIRES(mLock); 725 void dispatchTouchModeChangeLocked(nsecs_t currentTime, 726 const std::shared_ptr<const TouchModeEntry>& entry) 727 REQUIRES(mLock); 728 void dispatchEventLocked(nsecs_t currentTime, std::shared_ptr<const EventEntry> entry, 729 const std::vector<InputTarget>& inputTargets) REQUIRES(mLock); 730 void dispatchSensorLocked(nsecs_t currentTime, const std::shared_ptr<const SensorEntry>& entry, 731 DropReason* dropReason, nsecs_t& nextWakeupTime) REQUIRES(mLock); 732 void dispatchDragLocked(nsecs_t currentTime, std::shared_ptr<const DragEntry> entry) 733 REQUIRES(mLock); 734 void logOutboundKeyDetails(const char* prefix, const KeyEntry& entry); 735 void logOutboundMotionDetails(const char* prefix, const MotionEntry& entry); 736 737 /** 738 * This field is set if there is no focused window, and we have an event that requires 739 * a focused window to be dispatched (for example, a KeyEvent). 740 * When this happens, we will wait until *mNoFocusedWindowTimeoutTime before 741 * dropping the event and raising an ANR for that application. 742 * This is useful if an application is slow to add a focused window. 743 */ 744 std::optional<nsecs_t> mNoFocusedWindowTimeoutTime GUARDED_BY(mLock); 745 746 bool isStaleEvent(nsecs_t currentTime, const EventEntry& entry); 747 748 bool shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) const REQUIRES(mLock); 749 750 /** 751 * Time to stop waiting for the events to be processed while trying to dispatch a key. 752 * When this time expires, we just send the pending key event to the currently focused window, 753 * without waiting on other events to be processed first. 754 */ 755 std::optional<nsecs_t> mKeyIsWaitingForEventsTimeout GUARDED_BY(mLock); 756 bool shouldWaitToSendKeyLocked(nsecs_t currentTime, const char* focusedWindowName) 757 REQUIRES(mLock); 758 759 /** 760 * The focused application at the time when no focused window was present. 761 * Used to raise an ANR when we have no focused window. 762 */ 763 std::shared_ptr<InputApplicationHandle> mAwaitedFocusedApplication GUARDED_BY(mLock); 764 /** 765 * The displayId that the focused application is associated with. 766 */ 767 ui::LogicalDisplayId mAwaitedApplicationDisplayId GUARDED_BY(mLock); 768 void processNoFocusedWindowAnrLocked() REQUIRES(mLock); 769 770 /** 771 * Tell policy about a window or a monitor that just became unresponsive. Starts ANR. 772 */ 773 void processConnectionUnresponsiveLocked(const Connection& connection, std::string reason) 774 REQUIRES(mLock); 775 /** 776 * Tell policy about a window or a monitor that just became responsive. 777 */ 778 void processConnectionResponsiveLocked(const Connection& connection) REQUIRES(mLock); 779 780 void sendWindowUnresponsiveCommandLocked(const sp<IBinder>& connectionToken, 781 std::optional<gui::Pid> pid, std::string reason) 782 REQUIRES(mLock); 783 void sendWindowResponsiveCommandLocked(const sp<IBinder>& connectionToken, 784 std::optional<gui::Pid> pid) REQUIRES(mLock); 785 786 // Optimization: AnrTracker is used to quickly find which connection is due for a timeout next. 787 // AnrTracker must be kept in-sync with all responsive connection.waitQueues. 788 // If a connection is not responsive, then the entries should not be added to the AnrTracker. 789 // Once a connection becomes unresponsive, its entries are removed from AnrTracker to 790 // prevent unneeded wakeups. 791 AnrTracker mAnrTracker GUARDED_BY(mLock); 792 793 void cancelEventsForAnrLocked(const std::shared_ptr<Connection>& connection) REQUIRES(mLock); 794 // If a focused application changes, we should stop counting down the "no focused window" time, 795 // because we will have no way of knowing when the previous application actually added a window. 796 // This also means that we will miss cases like pulling down notification shade when the 797 // focused application does not have a focused window (no ANR will be raised if notification 798 // shade is pulled down while we are counting down the timeout). 799 void resetNoFocusedWindowTimeoutLocked() REQUIRES(mLock); 800 801 ui::LogicalDisplayId getTargetDisplayId(const EventEntry& entry); 802 base::Result<sp<android::gui::WindowInfoHandle>, android::os::InputEventInjectionResult> 803 findFocusedWindowTargetLocked(nsecs_t currentTime, const EventEntry& entry, 804 nsecs_t& nextWakeupTime) REQUIRES(mLock); 805 806 void addWindowTargetLocked(const sp<android::gui::WindowInfoHandle>& windowHandle, 807 InputTarget::DispatchMode dispatchMode, 808 ftl::Flags<InputTarget::Flags> targetFlags, 809 std::optional<nsecs_t> firstDownTimeInTarget, 810 std::vector<InputTarget>& inputTargets) const REQUIRES(mLock); 811 void addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, 812 ui::LogicalDisplayId displayId) REQUIRES(mLock); 813 void pokeUserActivityLocked(const EventEntry& eventEntry) REQUIRES(mLock); 814 // Enqueue a drag event if needed, and update the touch state. 815 // Uses findTouchedWindowTargetsLocked to make the decision 816 void addDragEventLocked(const MotionEntry& entry) REQUIRES(mLock); 817 void finishDragAndDrop(ui::LogicalDisplayId displayId, float x, float y) REQUIRES(mLock); 818 819 std::string getApplicationWindowLabel(const InputApplicationHandle* applicationHandle, 820 const sp<android::gui::WindowInfoHandle>& windowHandle); 821 822 static std::vector<sp<android::gui::WindowInfoHandle>> findTouchedSpyWindowsAt( 823 ui::LogicalDisplayId displayId, float x, float y, bool isStylus, DeviceId deviceId, 824 const DispatcherWindowInfo& windowInfos); 825 826 static bool shouldDropInput(const EventEntry& entry, 827 const sp<android::gui::WindowInfoHandle>& windowHandle, 828 const DispatcherWindowInfo& windowInfo); 829 830 // Manage the dispatch cycle for a single connection. 831 // These methods are deliberately not Interruptible because doing all of the work 832 // with the mutex held makes it easier to ensure that connection invariants are maintained. 833 // If needed, the methods post commands to run later once the critical bits are done. 834 void prepareDispatchCycleLocked(nsecs_t currentTime, 835 const std::shared_ptr<Connection>& connection, 836 std::shared_ptr<const EventEntry>, 837 const InputTarget& inputTarget) REQUIRES(mLock); 838 void enqueueDispatchEntryAndStartDispatchCycleLocked( 839 nsecs_t currentTime, const std::shared_ptr<Connection>& connection, 840 std::shared_ptr<const EventEntry>, const InputTarget& inputTarget) REQUIRES(mLock); 841 void enqueueDispatchEntryLocked(const std::shared_ptr<Connection>& connection, 842 std::shared_ptr<const EventEntry>, 843 const InputTarget& inputTarget) REQUIRES(mLock); 844 status_t publishMotionEvent(Connection& connection, DispatchEntry& dispatchEntry) const; 845 void startDispatchCycleLocked(nsecs_t currentTime, 846 const std::shared_ptr<Connection>& connection) REQUIRES(mLock); 847 void finishDispatchCycleLocked(nsecs_t currentTime, 848 const std::shared_ptr<Connection>& connection, uint32_t seq, 849 bool handled, nsecs_t consumeTime) REQUIRES(mLock); 850 void abortBrokenDispatchCycleLocked(const std::shared_ptr<Connection>& connection, bool notify) 851 REQUIRES(mLock); 852 void drainDispatchQueue(std::deque<std::unique_ptr<DispatchEntry>>& queue); 853 void releaseDispatchEntry(std::unique_ptr<DispatchEntry> dispatchEntry); 854 int handleReceiveCallback(int events, sp<IBinder> connectionToken); 855 // The action sent should only be of type AMOTION_EVENT_* 856 void dispatchPointerDownOutsideFocus(uint32_t source, int32_t action, 857 const sp<IBinder>& newToken) REQUIRES(mLock); 858 859 void synthesizeCancelationEventsForAllConnectionsLocked(const CancelationOptions& options) 860 REQUIRES(mLock); 861 void synthesizeCancelationEventsForMonitorsLocked(const CancelationOptions& options) 862 REQUIRES(mLock); 863 void synthesizeCancelationEventsForWindowLocked(const sp<gui::WindowInfoHandle>&, 864 const CancelationOptions&, 865 const std::shared_ptr<Connection>& = nullptr) 866 REQUIRES(mLock); 867 // This is a convenience function used to generate cancellation for a connection without having 868 // to check whether it's a monitor or a window. For non-monitors, the window handle must not be 869 // null. Always prefer the "-ForWindow" method above when explicitly dealing with windows. 870 void synthesizeCancelationEventsForConnectionLocked( 871 const std::shared_ptr<Connection>& connection, const CancelationOptions& options, 872 const sp<gui::WindowInfoHandle>& window) REQUIRES(mLock); 873 874 void synthesizePointerDownEventsForConnectionLocked( 875 const nsecs_t downTime, const std::shared_ptr<Connection>& connection, 876 ftl::Flags<InputTarget::Flags> targetFlags, 877 const std::unique_ptr<trace::EventTrackerInterface>& traceTracker) REQUIRES(mLock); 878 879 // Splitting motion events across windows. When splitting motion event for a target, 880 // splitDownTime refers to the time of first 'down' event on that particular target 881 std::unique_ptr<MotionEntry> splitMotionEvent(const MotionEntry& originalMotionEntry, 882 std::bitset<MAX_POINTER_ID + 1> pointerIds, 883 nsecs_t splitDownTime) REQUIRES(mLock); 884 885 // Reset and drop everything the dispatcher is doing. 886 void resetAndDropEverythingLocked(const char* reason) REQUIRES(mLock); 887 888 // Dump state. 889 void dumpDispatchStateLocked(std::string& dump) const REQUIRES(mLock); 890 void logDispatchStateLocked() const REQUIRES(mLock); 891 std::string dumpPointerCaptureStateLocked() const REQUIRES(mLock); 892 893 status_t removeInputChannelLocked(const std::shared_ptr<Connection>& connection, bool notify) 894 REQUIRES(mLock); 895 896 // Interesting events that we might like to log or tell the framework about. 897 void doDispatchCycleFinishedCommand(nsecs_t finishTime, 898 const std::shared_ptr<Connection>& connection, uint32_t seq, 899 bool handled, nsecs_t consumeTime) REQUIRES(mLock); 900 void doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken, 901 const KeyEntry& entry) REQUIRES(mLock); 902 void onFocusChangedLocked(const FocusResolver::FocusChanges& changes, 903 const std::unique_ptr<trace::EventTrackerInterface>& traceTracker, 904 const sp<gui::WindowInfoHandle> removedFocusedWindowHandle = nullptr) 905 REQUIRES(mLock); 906 void sendFocusChangedCommandLocked(const sp<IBinder>& oldToken, const sp<IBinder>& newToken) 907 REQUIRES(mLock); 908 void sendDropWindowCommandLocked(const sp<IBinder>& token, float x, float y) REQUIRES(mLock); 909 void onAnrLocked(const std::shared_ptr<Connection>& connection) REQUIRES(mLock); 910 void onAnrLocked(std::shared_ptr<InputApplicationHandle> application) REQUIRES(mLock); 911 void updateLastAnrStateLocked(const sp<android::gui::WindowInfoHandle>& window, 912 const std::string& reason) REQUIRES(mLock); 913 void updateLastAnrStateLocked(const InputApplicationHandle& application, 914 const std::string& reason) REQUIRES(mLock); 915 void updateLastAnrStateLocked(const std::string& windowLabel, const std::string& reason) 916 REQUIRES(mLock); 917 std::map<ui::LogicalDisplayId, InputVerifier> mVerifiersByDisplay; 918 // Returns a fallback KeyEntry that should be sent to the connection, if required. 919 std::unique_ptr<const KeyEntry> afterKeyEventLockedInterruptable( 920 const std::shared_ptr<Connection>& connection, DispatchEntry* dispatchEntry, 921 bool handled) REQUIRES(mLock); 922 void findAndDispatchFallbackEvent(nsecs_t currentTime, std::shared_ptr<const KeyEntry> entry, 923 std::vector<InputTarget>& inputTargets) REQUIRES(mLock); 924 925 // Statistics gathering. 926 nsecs_t mLastStatisticPushTime = 0; 927 std::unique_ptr<InputEventTimelineProcessor> mInputEventTimelineProcessor GUARDED_BY(mLock); 928 // Must outlive `mLatencyTracker`. 929 std::vector<InputDeviceInfo> mInputDevices; 930 LatencyTracker mLatencyTracker GUARDED_BY(mLock); 931 void traceInboundQueueLengthLocked() REQUIRES(mLock); 932 void traceOutboundQueueLength(const Connection& connection); 933 void traceWaitQueueLength(const Connection& connection); 934 935 // Check window ownership 936 bool focusedWindowIsOwnedByLocked(gui::Pid pid, gui::Uid uid) REQUIRES(mLock); 937 bool recentWindowsAreOwnedByLocked(gui::Pid pid, gui::Uid uid) REQUIRES(mLock); 938 939 sp<InputReporterInterface> mReporter; 940 941 /** Stores the value of the input flag for per device input latency metrics. */ 942 const bool mPerDeviceInputLatencyMetricsFlag = 943 com::android::input::flags::enable_per_device_input_latency_metrics(); 944 }; 945 946 } // namespace android::inputdispatcher 947