blob: 10d5abe3484f40644d25aed2c29455630317ec58 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
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
Siarhei Vishniakoue4623042020-03-25 16:16:40 -070020#include "AnrTracker.h"
Garfield Tane84e6f92019-08-29 17:28:41 -070021#include "CancelationOptions.h"
22#include "Entry.h"
23#include "InjectionState.h"
24#include "InputDispatcherConfiguration.h"
25#include "InputDispatcherInterface.h"
26#include "InputDispatcherPolicyInterface.h"
27#include "InputState.h"
28#include "InputTarget.h"
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070029#include "InputThread.h"
Garfield Tane84e6f92019-08-29 17:28:41 -070030#include "Monitor.h"
31#include "TouchState.h"
32#include "TouchedWindow.h"
33
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <input/Input.h>
Robert Carr3720ed02018-08-08 16:08:27 -070035#include <input/InputApplication.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080036#include <input/InputTransport.h>
Robert Carr3720ed02018-08-08 16:08:27 -070037#include <input/InputWindow.h>
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050038#include <input/LatencyStatistics.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070039#include <limits.h>
40#include <stddef.h>
Michael Wright3dd60e22019-03-27 22:06:44 +000041#include <ui/Region.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070042#include <unistd.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080043#include <utils/BitSet.h>
Garfield Tan0fc2fa72019-08-29 17:22:15 -070044#include <utils/Looper.h>
45#include <utils/RefBase.h>
46#include <utils/Timers.h>
47#include <utils/threads.h>
48#include <condition_variable>
Garfield Tane84e6f92019-08-29 17:28:41 -070049#include <deque>
Garfield Tan0fc2fa72019-08-29 17:22:15 -070050#include <optional>
Robert Carr5c8a0262018-10-03 16:30:44 -070051#include <unordered_map>
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +000052#include <unordered_set>
Michael Wrightd02c5b62014-02-10 15:10:22 -080053
Garfield Tane84e6f92019-08-29 17:28:41 -070054#include <InputListener.h>
55#include <InputReporterInterface.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
Garfield Tane84e6f92019-08-29 17:28:41 -070057namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080058
Garfield Tane84e6f92019-08-29 17:28:41 -070059class Connection;
Michael Wrightd02c5b62014-02-10 15:10:22 -080060
Gang Wang342c9272020-01-13 13:15:04 -050061class HmacKeyManager {
62public:
63 HmacKeyManager();
64 std::array<uint8_t, 32> sign(const VerifiedInputEvent& event) const;
65
66private:
Edgar Arriaga3d61bc12020-04-16 18:46:48 -070067 std::array<uint8_t, 32> sign(const uint8_t* data, size_t size) const;
Gang Wang342c9272020-01-13 13:15:04 -050068 const std::array<uint8_t, 128> mHmacKey;
69};
70
Michael Wrightd02c5b62014-02-10 15:10:22 -080071/* Dispatches events to input targets. Some functions of the input dispatcher, such as
72 * identifying input targets, are controlled by a separate policy object.
73 *
74 * IMPORTANT INVARIANT:
75 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
76 * the input dispatcher never calls into the policy while holding its internal locks.
77 * The implementation is also carefully designed to recover from scenarios such as an
78 * input channel becoming unregistered while identifying input targets or processing timeouts.
79 *
80 * Methods marked 'Locked' must be called with the lock acquired.
81 *
82 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
83 * may during the course of their execution release the lock, call into the policy, and
84 * then reacquire the lock. The caller is responsible for recovering gracefully.
85 *
86 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
87 */
Garfield Tane84e6f92019-08-29 17:28:41 -070088class InputDispatcher : public android::InputDispatcherInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -080089protected:
90 virtual ~InputDispatcher();
91
92public:
93 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
94
Michael Wright3dd60e22019-03-27 22:06:44 +000095 virtual void dump(std::string& dump) override;
96 virtual void monitor() override;
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080097 virtual bool waitForIdle() override;
Prabir Pradhan3608aad2019-10-02 17:08:26 -070098 virtual status_t start() override;
99 virtual status_t stop() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800100
Michael Wright3dd60e22019-03-27 22:06:44 +0000101 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
102 virtual void notifyKey(const NotifyKeyArgs* args) override;
103 virtual void notifyMotion(const NotifyMotionArgs* args) override;
104 virtual void notifySwitch(const NotifySwitchArgs* args) override;
105 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800106
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700107 virtual int32_t injectInputEvent(const InputEvent* event, int32_t injectorPid,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700108 int32_t injectorUid, int32_t syncMode,
109 std::chrono::milliseconds timeout,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700110 uint32_t policyFlags) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800112 virtual std::unique_ptr<VerifiedInputEvent> verifyInputEvent(const InputEvent& event) override;
113
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700114 virtual void setInputWindows(
Arthur Hung72d8dc32020-03-28 00:48:39 +0000115 const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>&
116 handlesPerDisplay) override;
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700117 virtual void setFocusedApplication(
Chris Yea209fde2020-07-22 13:54:51 -0700118 int32_t displayId,
119 const std::shared_ptr<InputApplicationHandle>& inputApplicationHandle) override;
Michael Wright3dd60e22019-03-27 22:06:44 +0000120 virtual void setFocusedDisplay(int32_t displayId) override;
121 virtual void setInputDispatchMode(bool enabled, bool frozen) override;
122 virtual void setInputFilterEnabled(bool enabled) override;
Siarhei Vishniakouf3bc1aa2019-11-25 13:48:53 -0800123 virtual void setInTouchMode(bool inTouchMode) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800124
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700125 virtual bool transferTouchFocus(const sp<IBinder>& fromToken,
126 const sp<IBinder>& toToken) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800127
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500128 virtual status_t registerInputChannel(
129 const std::shared_ptr<InputChannel>& inputChannel) override;
Vishnu Naire798b472020-07-23 13:52:21 -0700130 virtual void setFocusedWindow(const FocusRequest&) override;
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500131 virtual status_t registerInputMonitor(const std::shared_ptr<InputChannel>& inputChannel,
132 int32_t displayId, bool isGestureMonitor) override;
133 virtual status_t unregisterInputChannel(const InputChannel& inputChannel) override;
Michael Wright3dd60e22019-03-27 22:06:44 +0000134 virtual status_t pilferPointers(const sp<IBinder>& token) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800135
136private:
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -0700137 enum class DropReason {
138 NOT_DROPPED,
139 POLICY,
140 APP_SWITCH,
141 DISABLED,
142 BLOCKED,
143 STALE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144 };
145
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700146 std::unique_ptr<InputThread> mThread;
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700147
Michael Wrightd02c5b62014-02-10 15:10:22 -0800148 sp<InputDispatcherPolicyInterface> mPolicy;
Garfield Tane84e6f92019-08-29 17:28:41 -0700149 android::InputDispatcherConfiguration mConfig;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800151 std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800153 std::condition_variable mDispatcherIsAlive;
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800154 std::condition_variable mDispatcherEnteredIdle;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155
156 sp<Looper> mLooper;
157
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800158 EventEntry* mPendingEvent GUARDED_BY(mLock);
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -0700159 std::deque<EventEntry*> mInboundQueue GUARDED_BY(mLock);
160 std::deque<EventEntry*> mRecentQueue GUARDED_BY(mLock);
Siarhei Vishniakoue7c94b92019-07-29 09:17:54 -0700161 std::deque<std::unique_ptr<CommandEntry>> mCommandQueue GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800162
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800163 DropReason mLastDropReason GUARDED_BY(mLock);
Michael Wright3a981722015-06-10 15:26:13 +0100164
Garfield Tan1c7bc862020-01-28 13:24:04 -0800165 const IdGenerator mIdGenerator;
166
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700167 // With each iteration, InputDispatcher nominally processes one queued event,
168 // a timeout, or a response from an input consumer.
169 // This method should only be called on the input dispatcher's own thread.
170 void dispatchOnce();
171
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800172 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173
174 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800175 bool enqueueInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176
177 // Cleans up input state when dropping an inbound event.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700178 void dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100180 // Enqueues a focus event.
Vishnu Nair7d3d00d2020-08-03 11:20:42 -0700181 void enqueueFocusEventLocked(const InputWindowHandle& window, bool hasFocus,
182 std::string_view reason) REQUIRES(mLock);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100183
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184 // Adds an event to a queue of recent events for debugging purposes.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800185 void addRecentEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186
187 // App switch latency optimization.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800188 bool mAppSwitchSawKeyDown GUARDED_BY(mLock);
189 nsecs_t mAppSwitchDueTime GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700191 bool isAppSwitchKeyEvent(const KeyEntry& keyEntry);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800192 bool isAppSwitchPendingLocked() REQUIRES(mLock);
193 void resetPendingAppSwitchLocked(bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800194
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195 // Blocked event latency optimization. Drops old events when the user intends
196 // to transfer focus to a new application.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800197 EventEntry* mNextUnblockedEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800198
Tiger Huang85b8c5e2019-01-17 18:34:54 +0800199 sp<InputWindowHandle> findTouchedWindowAtLocked(int32_t displayId, int32_t x, int32_t y,
Siarhei Vishniakou1ea3cf12020-03-24 19:50:03 -0700200 TouchState* touchState,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700201 bool addOutsideTargets = false,
202 bool addPortalWindows = false) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203
204 // All registered connections mapped by channel file descriptor.
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -0700205 std::unordered_map<int, sp<Connection>> mConnectionsByFd GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206
Siarhei Vishniakouffaa2b12020-05-26 21:43:02 -0700207 sp<Connection> getConnectionLocked(const sp<IBinder>& inputConnectionToken) const
208 REQUIRES(mLock);
209
210 void removeConnectionLocked(const sp<Connection>& connection) REQUIRES(mLock);
211
Robert Carr5c8a0262018-10-03 16:30:44 -0700212 struct IBinderHash {
213 std::size_t operator()(const sp<IBinder>& b) const {
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700214 return std::hash<IBinder*>{}(b.get());
Robert Carr5c8a0262018-10-03 16:30:44 -0700215 }
216 };
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500217 std::unordered_map<sp<IBinder>, std::shared_ptr<InputChannel>, IBinderHash>
218 mInputChannelsByToken GUARDED_BY(mLock);
Robert Carr5c8a0262018-10-03 16:30:44 -0700219
Michael Wright3dd60e22019-03-27 22:06:44 +0000220 // Finds the display ID of the gesture monitor identified by the provided token.
221 std::optional<int32_t> findGestureMonitorDisplayByTokenLocked(const sp<IBinder>& token)
222 REQUIRES(mLock);
223
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800224 // Input channels that will receive a copy of all input events sent to the provided display.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700225 std::unordered_map<int32_t, std::vector<Monitor>> mGlobalMonitorsByDisplay GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800226
Michael Wright3dd60e22019-03-27 22:06:44 +0000227 // Input channels that will receive pointer events that start within the corresponding display.
228 // These are a bit special when compared to global monitors since they'll cause gesture streams
229 // to continue even when there isn't a touched window,and have the ability to steal the rest of
230 // the pointer stream in order to claim it for a system gesture.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700231 std::unordered_map<int32_t, std::vector<Monitor>> mGestureMonitorsByDisplay GUARDED_BY(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +0000232
Edgar Arriaga3d61bc12020-04-16 18:46:48 -0700233 const HmacKeyManager mHmacKeyManager;
234 const std::array<uint8_t, 32> getSignature(const MotionEntry& motionEntry,
235 const DispatchEntry& dispatchEntry) const;
236 const std::array<uint8_t, 32> getSignature(const KeyEntry& keyEntry,
237 const DispatchEntry& dispatchEntry) const;
Gang Wange9087892020-01-07 12:17:14 -0500238
Michael Wrightd02c5b62014-02-10 15:10:22 -0800239 // Event injection and synchronization.
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800240 std::condition_variable mInjectionResultAvailable;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800241 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800242 void setInjectionResult(EventEntry* entry, int32_t injectionResult);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800244 std::condition_variable mInjectionSyncFinished;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800245 void incrementPendingForegroundDispatches(EventEntry* entry);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800246 void decrementPendingForegroundDispatches(EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800247
248 // Key repeat tracking.
249 struct KeyRepeatState {
250 KeyEntry* lastKeyEntry; // or null if no repeat
251 nsecs_t nextRepeatTime;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800252 } mKeyRepeatState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800253
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800254 void resetKeyRepeatLocked() REQUIRES(mLock);
255 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256
Michael Wright78f24442014-08-06 15:55:28 -0700257 // Key replacement tracking
258 struct KeyReplacement {
259 int32_t keyCode;
260 int32_t deviceId;
261 bool operator==(const KeyReplacement& rhs) const {
262 return keyCode == rhs.keyCode && deviceId == rhs.deviceId;
263 }
Siarhei Vishniakoubde3d9e2020-03-24 19:05:54 -0700264 };
265 struct KeyReplacementHash {
266 size_t operator()(const KeyReplacement& key) const {
267 return std::hash<int32_t>()(key.keyCode) ^ (std::hash<int32_t>()(key.deviceId) << 1);
Michael Wright78f24442014-08-06 15:55:28 -0700268 }
269 };
270 // Maps the key code replaced, device id tuple to the key code it was replaced with
Siarhei Vishniakoubde3d9e2020-03-24 19:05:54 -0700271 std::unordered_map<KeyReplacement, int32_t, KeyReplacementHash> mReplacedKeys GUARDED_BY(mLock);
Siarhei Vishniakou61fafdd2018-04-13 11:00:58 -0500272 // Process certain Meta + Key combinations
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700273 void accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, int32_t& keyCode,
274 int32_t& metaState);
Michael Wright78f24442014-08-06 15:55:28 -0700275
Michael Wrightd02c5b62014-02-10 15:10:22 -0800276 // Deferred command processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800277 bool haveCommandsLocked() const REQUIRES(mLock);
278 bool runCommandsLockedInterruptible() REQUIRES(mLock);
Siarhei Vishniakoue7c94b92019-07-29 09:17:54 -0700279 void postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280
Siarhei Vishniakoue4623042020-03-25 16:16:40 -0700281 nsecs_t processAnrsLocked() REQUIRES(mLock);
282 nsecs_t getDispatchingTimeoutLocked(const sp<IBinder>& token) REQUIRES(mLock);
283
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284 // Input filter processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800285 bool shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) REQUIRES(mLock);
286 bool shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287
288 // Inbound event processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800289 void drainInboundQueueLocked() REQUIRES(mLock);
290 void releasePendingEventLocked() REQUIRES(mLock);
291 void releaseInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292
293 // Dispatch state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800294 bool mDispatchEnabled GUARDED_BY(mLock);
295 bool mDispatchFrozen GUARDED_BY(mLock);
296 bool mInputFilterEnabled GUARDED_BY(mLock);
Siarhei Vishniakouf3bc1aa2019-11-25 13:48:53 -0800297 bool mInTouchMode GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800299 std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> mWindowHandlesByDisplay
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800300 GUARDED_BY(mLock);
Arthur Hung72d8dc32020-03-28 00:48:39 +0000301 void setInputWindowsLocked(const std::vector<sp<InputWindowHandle>>& inputWindowHandles,
302 int32_t displayId) REQUIRES(mLock);
Arthur Hungb92218b2018-08-14 12:00:21 +0800303 // Get window handles by display, return an empty vector if not found.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800304 std::vector<sp<InputWindowHandle>> getWindowHandlesLocked(int32_t displayId) const
305 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800306 sp<InputWindowHandle> getWindowHandleLocked(const sp<IBinder>& windowHandleToken) const
307 REQUIRES(mLock);
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500308 std::shared_ptr<InputChannel> getInputChannelLocked(const sp<IBinder>& windowToken) const
309 REQUIRES(mLock);
Mady Mellor017bcd12020-06-23 19:12:00 +0000310 bool hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
Siarhei Vishniakoua2862a02020-07-20 16:36:46 -0500311 bool hasResponsiveConnectionLocked(InputWindowHandle& windowHandle) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800312
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -0700313 /*
314 * Validate and update InputWindowHandles for a given display.
315 */
316 void updateWindowHandlesForDisplayLocked(
317 const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId)
318 REQUIRES(mLock);
319
Michael Wrightd02c5b62014-02-10 15:10:22 -0800320 // Focus tracking for keys, trackball, etc.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800321 std::unordered_map<int32_t, sp<InputWindowHandle>> mFocusedWindowHandlesByDisplay
322 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800323
Siarhei Vishniakoubde3d9e2020-03-24 19:05:54 -0700324 std::unordered_map<int32_t, TouchState> mTouchStatesByDisplay GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325
Tiger Huang721e26f2018-07-24 22:26:19 +0800326 // Focused applications.
Chris Yea209fde2020-07-22 13:54:51 -0700327 std::unordered_map<int32_t, std::shared_ptr<InputApplicationHandle>>
328 mFocusedApplicationHandlesByDisplay GUARDED_BY(mLock);
Tiger Huang721e26f2018-07-24 22:26:19 +0800329
330 // Top focused display.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800331 int32_t mFocusedDisplayId GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800332
333 // Dispatcher state at time of last ANR.
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700334 std::string mLastAnrState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335
Siarhei Vishniakou887b7d92020-06-18 00:43:02 +0000336 // The connection tokens of the channels that the user last interacted, for debugging
337 std::unordered_set<sp<IBinder>, IBinderHash> mInteractionConnectionTokens GUARDED_BY(mLock);
338 void updateInteractionTokensLocked(const EventEntry& entry,
339 const std::vector<InputTarget>& targets) REQUIRES(mLock);
340
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341 // Dispatch inbound events.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700342 bool dispatchConfigurationChangedLocked(nsecs_t currentTime, ConfigurationChangedEntry* entry)
343 REQUIRES(mLock);
344 bool dispatchDeviceResetLocked(nsecs_t currentTime, DeviceResetEntry* entry) REQUIRES(mLock);
345 bool dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, DropReason* dropReason,
346 nsecs_t* nextWakeupTime) REQUIRES(mLock);
347 bool dispatchMotionLocked(nsecs_t currentTime, MotionEntry* entry, DropReason* dropReason,
348 nsecs_t* nextWakeupTime) REQUIRES(mLock);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100349 void dispatchFocusLocked(nsecs_t currentTime, FocusEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800350 void dispatchEventLocked(nsecs_t currentTime, EventEntry* entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700351 const std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800352
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700353 void logOutboundKeyDetails(const char* prefix, const KeyEntry& entry);
354 void logOutboundMotionDetails(const char* prefix, const MotionEntry& entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800355
Siarhei Vishniakoue4623042020-03-25 16:16:40 -0700356 /**
357 * This field is set if there is no focused window, and we have an event that requires
358 * a focused window to be dispatched (for example, a KeyEvent).
359 * When this happens, we will wait until *mNoFocusedWindowTimeoutTime before
360 * dropping the event and raising an ANR for that application.
361 * This is useful if an application is slow to add a focused window.
362 */
363 std::optional<nsecs_t> mNoFocusedWindowTimeoutTime GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364
Siarhei Vishniakoude1bc4a2020-05-26 22:39:43 -0700365 bool shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) REQUIRES(mLock);
366
Siarhei Vishniakoue4623042020-03-25 16:16:40 -0700367 /**
368 * Time to stop waiting for the events to be processed while trying to dispatch a key.
369 * When this time expires, we just send the pending key event to the currently focused window,
370 * without waiting on other events to be processed first.
371 */
372 std::optional<nsecs_t> mKeyIsWaitingForEventsTimeout GUARDED_BY(mLock);
373 bool shouldWaitToSendKeyLocked(nsecs_t currentTime, const char* focusedWindowName)
374 REQUIRES(mLock);
375
376 /**
377 * The focused application at the time when no focused window was present.
378 * Used to raise an ANR when we have no focused window.
379 */
Chris Yea209fde2020-07-22 13:54:51 -0700380 std::shared_ptr<InputApplicationHandle> mAwaitedFocusedApplication GUARDED_BY(mLock);
Siarhei Vishniakoue4623042020-03-25 16:16:40 -0700381
382 // Optimization: AnrTracker is used to quickly find which connection is due for a timeout next.
383 // AnrTracker must be kept in-sync with all responsive connection.waitQueues.
384 // If a connection is not responsive, then the entries should not be added to the AnrTracker.
385 // Once a connection becomes unresponsive, its entries are removed from AnrTracker to
386 // prevent unneeded wakeups.
387 AnrTracker mAnrTracker GUARDED_BY(mLock);
Chris Yea209fde2020-07-22 13:54:51 -0700388 void extendAnrTimeoutsLocked(const std::shared_ptr<InputApplicationHandle>& application,
Siarhei Vishniakouc1ae5562020-06-30 14:22:57 -0500389 const sp<IBinder>& connectionToken,
390 std::chrono::nanoseconds timeoutExtension) REQUIRES(mLock);
Siarhei Vishniakoue4623042020-03-25 16:16:40 -0700391
Michael Wrightd02c5b62014-02-10 15:10:22 -0800392 // Contains the last window which received a hover event.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800393 sp<InputWindowHandle> mLastHoverWindowHandle GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394
Siarhei Vishniakoue4623042020-03-25 16:16:40 -0700395 void cancelEventsForAnrLocked(const sp<Connection>& connection) REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800396 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime) REQUIRES(mLock);
Siarhei Vishniakoue4623042020-03-25 16:16:40 -0700397 // If a focused application changes, we should stop counting down the "no focused window" time,
398 // because we will have no way of knowing when the previous application actually added a window.
399 // This also means that we will miss cases like pulling down notification shade when the
400 // focused application does not have a focused window (no ANR will be raised if notification
401 // shade is pulled down while we are counting down the timeout).
402 void resetNoFocusedWindowTimeoutLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800403
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700404 int32_t getTargetDisplayId(const EventEntry& entry);
405 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry& entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700406 std::vector<InputTarget>& inputTargets,
407 nsecs_t* nextWakeupTime) REQUIRES(mLock);
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700408 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry& entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700409 std::vector<InputTarget>& inputTargets,
410 nsecs_t* nextWakeupTime,
411 bool* outConflictingPointerActions) REQUIRES(mLock);
412 std::vector<TouchedMonitor> findTouchedGestureMonitorsLocked(
Siarhei Vishniakoue0fb6bd2020-04-13 11:40:37 -0700413 int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) const
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700414 REQUIRES(mLock);
Siarhei Vishniakoue4623042020-03-25 16:16:40 -0700415 std::vector<TouchedMonitor> selectResponsiveMonitorsLocked(
416 const std::vector<TouchedMonitor>& gestureMonitors) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700418 void addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, int32_t targetFlags,
419 BitSet32 pointerIds, std::vector<InputTarget>& inputTargets)
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800420 REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +0000421 void addMonitoringTargetLocked(const Monitor& monitor, float xOffset, float yOffset,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700422 std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
423 void addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, int32_t displayId,
424 float xOffset = 0, float yOffset = 0) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800425
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700426 void pokeUserActivityLocked(const EventEntry& eventEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800427 bool checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700428 const InjectionState* injectionState);
429 bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, int32_t x,
430 int32_t y) const REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800431 bool isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
Chris Yea209fde2020-07-22 13:54:51 -0700432 std::string getApplicationWindowLabel(
433 const std::shared_ptr<InputApplicationHandle>& applicationHandle,
434 const sp<InputWindowHandle>& windowHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435
Michael Wrightd02c5b62014-02-10 15:10:22 -0800436 // Manage the dispatch cycle for a single connection.
437 // These methods are deliberately not Interruptible because doing all of the work
438 // with the mutex held makes it easier to ensure that connection invariants are maintained.
439 // If needed, the methods post commands to run later once the critical bits are done.
440 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -0800441 EventEntry* eventEntry, const InputTarget& inputTarget)
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700442 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800443 void enqueueDispatchEntriesLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -0800444 EventEntry* eventEntry, const InputTarget& inputTarget)
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700445 REQUIRES(mLock);
446 void enqueueDispatchEntryLocked(const sp<Connection>& connection, EventEntry* eventEntry,
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -0800447 const InputTarget& inputTarget, int32_t dispatchMode)
chaviw8c9cf542019-03-25 13:02:48 -0700448 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800449 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection)
450 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800451 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700452 uint32_t seq, bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800453 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700454 bool notify) REQUIRES(mLock);
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -0700455 void drainDispatchQueue(std::deque<DispatchEntry*>& queue);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800456 void releaseDispatchEntry(DispatchEntry* dispatchEntry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800457 static int handleReceiveCallback(int fd, int events, void* data);
chaviw8c9cf542019-03-25 13:02:48 -0700458 // The action sent should only be of type AMOTION_EVENT_*
chaviwfd6d3512019-03-25 13:23:49 -0700459 void dispatchPointerDownOutsideFocus(uint32_t source, int32_t action,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700460 const sp<IBinder>& newToken) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700462 void synthesizeCancelationEventsForAllConnectionsLocked(const CancelationOptions& options)
463 REQUIRES(mLock);
464 void synthesizeCancelationEventsForMonitorsLocked(const CancelationOptions& options)
465 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800466 void synthesizeCancelationEventsForMonitorsLocked(
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700467 const CancelationOptions& options,
Michael Wright3dd60e22019-03-27 22:06:44 +0000468 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock);
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500469 void synthesizeCancelationEventsForInputChannelLocked(
470 const std::shared_ptr<InputChannel>& channel, const CancelationOptions& options)
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700471 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800472 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700473 const CancelationOptions& options)
474 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475
Svet Ganov5d3bc372020-01-26 23:11:07 -0800476 void synthesizePointerDownEventsForConnectionLocked(const sp<Connection>& connection)
477 REQUIRES(mLock);
478
Michael Wrightd02c5b62014-02-10 15:10:22 -0800479 // Splitting motion events across windows.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700480 MotionEntry* splitMotionEvent(const MotionEntry& originalMotionEntry, BitSet32 pointerIds);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800481
482 // Reset and drop everything the dispatcher is doing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800483 void resetAndDropEverythingLocked(const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484
485 // Dump state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800486 void dumpDispatchStateLocked(std::string& dump) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +0000487 void dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800488 void logDispatchStateLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800489
490 // Registration.
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500491 void removeMonitorChannelLocked(const InputChannel& inputChannel) REQUIRES(mLock);
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700492 void removeMonitorChannelLocked(
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500493 const InputChannel& inputChannel,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700494 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock);
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500495 status_t unregisterInputChannelLocked(const InputChannel& inputChannel, bool notify)
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800496 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800497
498 // Interesting events that we might like to log or tell the framework about.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700499 void onDispatchCycleFinishedLocked(nsecs_t currentTime, const sp<Connection>& connection,
500 uint32_t seq, bool handled) REQUIRES(mLock);
501 void onDispatchCycleBrokenLocked(nsecs_t currentTime, const sp<Connection>& connection)
502 REQUIRES(mLock);
chaviw0c06c6e2019-01-09 13:27:07 -0800503 void onFocusChangedLocked(const sp<InputWindowHandle>& oldFocus,
Vishnu Nair7d3d00d2020-08-03 11:20:42 -0700504 const sp<InputWindowHandle>& newFocus, int32_t displayId,
505 std::string_view reason) REQUIRES(mLock);
506 void notifyFocusChangedLocked(const sp<InputWindowHandle>& oldFocus,
507 const sp<InputWindowHandle>& newFocus) REQUIRES(mLock);
Siarhei Vishniakoue4623042020-03-25 16:16:40 -0700508 void onAnrLocked(const sp<Connection>& connection) REQUIRES(mLock);
Chris Yea209fde2020-07-22 13:54:51 -0700509 void onAnrLocked(const std::shared_ptr<InputApplicationHandle>& application) REQUIRES(mLock);
Siarhei Vishniakoue4623042020-03-25 16:16:40 -0700510 void updateLastAnrStateLocked(const sp<InputWindowHandle>& window, const std::string& reason)
511 REQUIRES(mLock);
Chris Yea209fde2020-07-22 13:54:51 -0700512 void updateLastAnrStateLocked(const std::shared_ptr<InputApplicationHandle>& application,
Siarhei Vishniakoue4623042020-03-25 16:16:40 -0700513 const std::string& reason) REQUIRES(mLock);
514 void updateLastAnrStateLocked(const std::string& windowLabel, const std::string& reason)
515 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800516
517 // Outbound policy interactions.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800518 void doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry)
519 REQUIRES(mLock);
520 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
521 void doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700522 void doNotifyAnrLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800523 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry)
524 REQUIRES(mLock);
525 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800526 bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700527 DispatchEntry* dispatchEntry, KeyEntry* keyEntry,
528 bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800529 bool afterMotionEventLockedInterruptible(const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700530 DispatchEntry* dispatchEntry, MotionEntry* motionEntry,
531 bool handled) REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800532 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Siarhei Vishniakou9757f782019-10-29 12:53:08 -0700533 KeyEvent createKeyEvent(const KeyEntry& entry);
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700534 void doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800535
536 // Statistics gathering.
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -0500537 static constexpr std::chrono::duration TOUCH_STATS_REPORT_PERIOD = 5min;
538 LatencyStatistics mTouchStatistics{TOUCH_STATS_REPORT_PERIOD};
539
540 void reportTouchEventForStatistics(const MotionEntry& entry);
Siarhei Vishniakou7f0a4392020-03-24 20:49:09 -0700541 void reportDispatchStatistics(std::chrono::nanoseconds eventDuration,
542 const Connection& connection, bool handled);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800543 void traceInboundQueueLengthLocked() REQUIRES(mLock);
544 void traceOutboundQueueLength(const sp<Connection>& connection);
545 void traceWaitQueueLength(const sp<Connection>& connection);
Prabir Pradhanf93562f2018-11-29 12:13:37 -0800546
Prabir Pradhan79a4f0c2019-01-09 11:24:01 -0800547 sp<InputReporterInterface> mReporter;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800548};
549
Garfield Tane84e6f92019-08-29 17:28:41 -0700550} // namespace android::inputdispatcher
Michael Wrightd02c5b62014-02-10 15:10:22 -0800551
552#endif // _UI_INPUT_DISPATCHER_H