blob: 0c3ea3674cdd4989cc4ae928c0bfc732a88bf736 [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
Garfield Tane84e6f92019-08-29 17:28:41 -070020#include "CancelationOptions.h"
21#include "Entry.h"
22#include "InjectionState.h"
23#include "InputDispatcherConfiguration.h"
24#include "InputDispatcherInterface.h"
25#include "InputDispatcherPolicyInterface.h"
26#include "InputState.h"
27#include "InputTarget.h"
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070028#include "InputThread.h"
Garfield Tane84e6f92019-08-29 17:28:41 -070029#include "Monitor.h"
30#include "TouchState.h"
31#include "TouchedWindow.h"
32
Michael Wrightd02c5b62014-02-10 15:10:22 -080033#include <input/Input.h>
Robert Carr3720ed02018-08-08 16:08:27 -070034#include <input/InputApplication.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <input/InputTransport.h>
Robert Carr3720ed02018-08-08 16:08:27 -070036#include <input/InputWindow.h>
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050037#include <input/LatencyStatistics.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070038#include <limits.h>
39#include <stddef.h>
Michael Wright3dd60e22019-03-27 22:06:44 +000040#include <ui/Region.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070041#include <unistd.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080042#include <utils/BitSet.h>
Garfield Tan0fc2fa72019-08-29 17:22:15 -070043#include <utils/Looper.h>
44#include <utils/RefBase.h>
45#include <utils/Timers.h>
46#include <utils/threads.h>
47#include <condition_variable>
Garfield Tane84e6f92019-08-29 17:28:41 -070048#include <deque>
Garfield Tan0fc2fa72019-08-29 17:22:15 -070049#include <optional>
Robert Carr5c8a0262018-10-03 16:30:44 -070050#include <unordered_map>
Michael Wrightd02c5b62014-02-10 15:10:22 -080051
Garfield Tane84e6f92019-08-29 17:28:41 -070052#include <InputListener.h>
53#include <InputReporterInterface.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080054
Garfield Tane84e6f92019-08-29 17:28:41 -070055namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
Garfield Tane84e6f92019-08-29 17:28:41 -070057class Connection;
Michael Wrightd02c5b62014-02-10 15:10:22 -080058
Gang Wang342c9272020-01-13 13:15:04 -050059class HmacKeyManager {
60public:
61 HmacKeyManager();
62 std::array<uint8_t, 32> sign(const VerifiedInputEvent& event) const;
63
64private:
Edgar Arriaga3d61bc12020-04-16 18:46:48 -070065 std::array<uint8_t, 32> sign(const uint8_t* data, size_t size) const;
Gang Wang342c9272020-01-13 13:15:04 -050066 const std::array<uint8_t, 128> mHmacKey;
67};
68
Michael Wrightd02c5b62014-02-10 15:10:22 -080069/* 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 */
Garfield Tane84e6f92019-08-29 17:28:41 -070086class InputDispatcher : public android::InputDispatcherInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -080087protected:
88 virtual ~InputDispatcher();
89
90public:
91 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
92
Michael Wright3dd60e22019-03-27 22:06:44 +000093 virtual void dump(std::string& dump) override;
94 virtual void monitor() override;
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080095 virtual bool waitForIdle() override;
Prabir Pradhan3608aad2019-10-02 17:08:26 -070096 virtual status_t start() override;
97 virtual status_t stop() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080098
Michael Wright3dd60e22019-03-27 22:06:44 +000099 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
100 virtual void notifyKey(const NotifyKeyArgs* args) override;
101 virtual void notifyMotion(const NotifyMotionArgs* args) override;
102 virtual void notifySwitch(const NotifySwitchArgs* args) override;
103 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800104
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700105 virtual int32_t injectInputEvent(const InputEvent* event, int32_t injectorPid,
Siarhei Vishniakou097c3db2020-05-06 14:18:38 -0700106 int32_t injectorUid, int32_t syncMode,
107 std::chrono::milliseconds timeout,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700108 uint32_t policyFlags) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800109
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800110 virtual std::unique_ptr<VerifiedInputEvent> verifyInputEvent(const InputEvent& event) override;
111
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700112 virtual void setInputWindows(
Arthur Hung72d8dc32020-03-28 00:48:39 +0000113 const std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>>&
114 handlesPerDisplay) override;
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700115 virtual void setFocusedApplication(
116 int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) override;
Michael Wright3dd60e22019-03-27 22:06:44 +0000117 virtual void setFocusedDisplay(int32_t displayId) override;
118 virtual void setInputDispatchMode(bool enabled, bool frozen) override;
119 virtual void setInputFilterEnabled(bool enabled) override;
Siarhei Vishniakouf3bc1aa2019-11-25 13:48:53 -0800120 virtual void setInTouchMode(bool inTouchMode) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800121
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700122 virtual bool transferTouchFocus(const sp<IBinder>& fromToken,
123 const sp<IBinder>& toToken) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800124
Siarhei Vishniakou7c34b232019-10-11 19:08:48 -0700125 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel) override;
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700126 virtual status_t registerInputMonitor(const sp<InputChannel>& inputChannel, int32_t displayId,
127 bool isGestureMonitor) override;
Michael Wright3dd60e22019-03-27 22:06:44 +0000128 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) override;
129 virtual status_t pilferPointers(const sp<IBinder>& token) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800130
131private:
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -0700132 enum class DropReason {
133 NOT_DROPPED,
134 POLICY,
135 APP_SWITCH,
136 DISABLED,
137 BLOCKED,
138 STALE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800139 };
140
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700141 std::unique_ptr<InputThread> mThread;
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700142
Michael Wrightd02c5b62014-02-10 15:10:22 -0800143 sp<InputDispatcherPolicyInterface> mPolicy;
Garfield Tane84e6f92019-08-29 17:28:41 -0700144 android::InputDispatcherConfiguration mConfig;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800146 std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800147
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800148 std::condition_variable mDispatcherIsAlive;
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800149 std::condition_variable mDispatcherEnteredIdle;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150
151 sp<Looper> mLooper;
152
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800153 EventEntry* mPendingEvent GUARDED_BY(mLock);
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -0700154 std::deque<EventEntry*> mInboundQueue GUARDED_BY(mLock);
155 std::deque<EventEntry*> mRecentQueue GUARDED_BY(mLock);
Siarhei Vishniakoue7c94b92019-07-29 09:17:54 -0700156 std::deque<std::unique_ptr<CommandEntry>> mCommandQueue GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800158 DropReason mLastDropReason GUARDED_BY(mLock);
Michael Wright3a981722015-06-10 15:26:13 +0100159
Garfield Tan1c7bc862020-01-28 13:24:04 -0800160 const IdGenerator mIdGenerator;
161
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700162 // With each iteration, InputDispatcher nominally processes one queued event,
163 // a timeout, or a response from an input consumer.
164 // This method should only be called on the input dispatcher's own thread.
165 void dispatchOnce();
166
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800167 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
169 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800170 bool enqueueInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171
172 // Cleans up input state when dropping an inbound event.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700173 void dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100175 // Enqueues a focus event.
176 void enqueueFocusEventLocked(const InputWindowHandle& window, bool hasFocus) REQUIRES(mLock);
177
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178 // Adds an event to a queue of recent events for debugging purposes.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800179 void addRecentEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180
181 // App switch latency optimization.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800182 bool mAppSwitchSawKeyDown GUARDED_BY(mLock);
183 nsecs_t mAppSwitchDueTime GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700185 bool isAppSwitchKeyEvent(const KeyEntry& keyEntry);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800186 bool isAppSwitchPendingLocked() REQUIRES(mLock);
187 void resetPendingAppSwitchLocked(bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
Michael Wrightd02c5b62014-02-10 15:10:22 -0800189 // Blocked event latency optimization. Drops old events when the user intends
190 // to transfer focus to a new application.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800191 EventEntry* mNextUnblockedEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192
Tiger Huang85b8c5e2019-01-17 18:34:54 +0800193 sp<InputWindowHandle> findTouchedWindowAtLocked(int32_t displayId, int32_t x, int32_t y,
Siarhei Vishniakou1ea3cf12020-03-24 19:50:03 -0700194 TouchState* touchState,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700195 bool addOutsideTargets = false,
196 bool addPortalWindows = false) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800197
198 // All registered connections mapped by channel file descriptor.
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -0700199 std::unordered_map<int, sp<Connection>> mConnectionsByFd GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800200
Siarhei Vishniakouffaa2b12020-05-26 21:43:02 -0700201 sp<Connection> getConnectionLocked(const sp<IBinder>& inputConnectionToken) const
202 REQUIRES(mLock);
203
204 void removeConnectionLocked(const sp<Connection>& connection) REQUIRES(mLock);
205
Robert Carr5c8a0262018-10-03 16:30:44 -0700206 struct IBinderHash {
207 std::size_t operator()(const sp<IBinder>& b) const {
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700208 return std::hash<IBinder*>{}(b.get());
Robert Carr5c8a0262018-10-03 16:30:44 -0700209 }
210 };
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800211 std::unordered_map<sp<IBinder>, sp<InputChannel>, IBinderHash> mInputChannelsByToken
212 GUARDED_BY(mLock);
Robert Carr5c8a0262018-10-03 16:30:44 -0700213
Michael Wright3dd60e22019-03-27 22:06:44 +0000214 // Finds the display ID of the gesture monitor identified by the provided token.
215 std::optional<int32_t> findGestureMonitorDisplayByTokenLocked(const sp<IBinder>& token)
216 REQUIRES(mLock);
217
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800219 // Input channels that will receive a copy of all input events sent to the provided display.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700220 std::unordered_map<int32_t, std::vector<Monitor>> mGlobalMonitorsByDisplay GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800221
Michael Wright3dd60e22019-03-27 22:06:44 +0000222 // Input channels that will receive pointer events that start within the corresponding display.
223 // These are a bit special when compared to global monitors since they'll cause gesture streams
224 // to continue even when there isn't a touched window,and have the ability to steal the rest of
225 // the pointer stream in order to claim it for a system gesture.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700226 std::unordered_map<int32_t, std::vector<Monitor>> mGestureMonitorsByDisplay GUARDED_BY(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +0000227
Edgar Arriaga3d61bc12020-04-16 18:46:48 -0700228 const HmacKeyManager mHmacKeyManager;
229 const std::array<uint8_t, 32> getSignature(const MotionEntry& motionEntry,
230 const DispatchEntry& dispatchEntry) const;
231 const std::array<uint8_t, 32> getSignature(const KeyEntry& keyEntry,
232 const DispatchEntry& dispatchEntry) const;
Gang Wange9087892020-01-07 12:17:14 -0500233
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234 // Event injection and synchronization.
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800235 std::condition_variable mInjectionResultAvailable;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800236 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800237 void setInjectionResult(EventEntry* entry, int32_t injectionResult);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800238
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800239 std::condition_variable mInjectionSyncFinished;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800240 void incrementPendingForegroundDispatches(EventEntry* entry);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800241 void decrementPendingForegroundDispatches(EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800242
243 // Key repeat tracking.
244 struct KeyRepeatState {
245 KeyEntry* lastKeyEntry; // or null if no repeat
246 nsecs_t nextRepeatTime;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800247 } mKeyRepeatState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800249 void resetKeyRepeatLocked() REQUIRES(mLock);
250 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800251
Michael Wright78f24442014-08-06 15:55:28 -0700252 // Key replacement tracking
253 struct KeyReplacement {
254 int32_t keyCode;
255 int32_t deviceId;
256 bool operator==(const KeyReplacement& rhs) const {
257 return keyCode == rhs.keyCode && deviceId == rhs.deviceId;
258 }
Siarhei Vishniakoubde3d9e2020-03-24 19:05:54 -0700259 };
260 struct KeyReplacementHash {
261 size_t operator()(const KeyReplacement& key) const {
262 return std::hash<int32_t>()(key.keyCode) ^ (std::hash<int32_t>()(key.deviceId) << 1);
Michael Wright78f24442014-08-06 15:55:28 -0700263 }
264 };
265 // Maps the key code replaced, device id tuple to the key code it was replaced with
Siarhei Vishniakoubde3d9e2020-03-24 19:05:54 -0700266 std::unordered_map<KeyReplacement, int32_t, KeyReplacementHash> mReplacedKeys GUARDED_BY(mLock);
Siarhei Vishniakou61fafdd2018-04-13 11:00:58 -0500267 // Process certain Meta + Key combinations
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700268 void accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, int32_t& keyCode,
269 int32_t& metaState);
Michael Wright78f24442014-08-06 15:55:28 -0700270
Michael Wrightd02c5b62014-02-10 15:10:22 -0800271 // Deferred command processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800272 bool haveCommandsLocked() const REQUIRES(mLock);
273 bool runCommandsLockedInterruptible() REQUIRES(mLock);
Siarhei Vishniakoue7c94b92019-07-29 09:17:54 -0700274 void postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800275
276 // Input filter processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800277 bool shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) REQUIRES(mLock);
278 bool shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279
280 // Inbound event processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800281 void drainInboundQueueLocked() REQUIRES(mLock);
282 void releasePendingEventLocked() REQUIRES(mLock);
283 void releaseInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284
285 // Dispatch state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800286 bool mDispatchEnabled GUARDED_BY(mLock);
287 bool mDispatchFrozen GUARDED_BY(mLock);
288 bool mInputFilterEnabled GUARDED_BY(mLock);
Siarhei Vishniakouf3bc1aa2019-11-25 13:48:53 -0800289 bool mInTouchMode GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800290
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800291 std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> mWindowHandlesByDisplay
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800292 GUARDED_BY(mLock);
Arthur Hung72d8dc32020-03-28 00:48:39 +0000293 void setInputWindowsLocked(const std::vector<sp<InputWindowHandle>>& inputWindowHandles,
294 int32_t displayId) REQUIRES(mLock);
Arthur Hungb92218b2018-08-14 12:00:21 +0800295 // Get window handles by display, return an empty vector if not found.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800296 std::vector<sp<InputWindowHandle>> getWindowHandlesLocked(int32_t displayId) const
297 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800298 sp<InputWindowHandle> getWindowHandleLocked(const sp<IBinder>& windowHandleToken) const
299 REQUIRES(mLock);
300 sp<InputChannel> getInputChannelLocked(const sp<IBinder>& windowToken) const REQUIRES(mLock);
301 bool hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800302
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -0700303 /*
304 * Validate and update InputWindowHandles for a given display.
305 */
306 void updateWindowHandlesForDisplayLocked(
307 const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId)
308 REQUIRES(mLock);
309
Michael Wrightd02c5b62014-02-10 15:10:22 -0800310 // Focus tracking for keys, trackball, etc.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800311 std::unordered_map<int32_t, sp<InputWindowHandle>> mFocusedWindowHandlesByDisplay
312 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313
Siarhei Vishniakoubde3d9e2020-03-24 19:05:54 -0700314 std::unordered_map<int32_t, TouchState> mTouchStatesByDisplay GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800315
Tiger Huang721e26f2018-07-24 22:26:19 +0800316 // Focused applications.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800317 std::unordered_map<int32_t, sp<InputApplicationHandle>> mFocusedApplicationHandlesByDisplay
318 GUARDED_BY(mLock);
Tiger Huang721e26f2018-07-24 22:26:19 +0800319
320 // Top focused display.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800321 int32_t mFocusedDisplayId GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322
323 // Dispatcher state at time of last ANR.
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700324 std::string mLastAnrState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325
326 // Dispatch inbound events.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700327 bool dispatchConfigurationChangedLocked(nsecs_t currentTime, ConfigurationChangedEntry* entry)
328 REQUIRES(mLock);
329 bool dispatchDeviceResetLocked(nsecs_t currentTime, DeviceResetEntry* entry) REQUIRES(mLock);
330 bool dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, DropReason* dropReason,
331 nsecs_t* nextWakeupTime) REQUIRES(mLock);
332 bool dispatchMotionLocked(nsecs_t currentTime, MotionEntry* entry, DropReason* dropReason,
333 nsecs_t* nextWakeupTime) REQUIRES(mLock);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100334 void dispatchFocusLocked(nsecs_t currentTime, FocusEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335 void dispatchEventLocked(nsecs_t currentTime, EventEntry* entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700336 const std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700338 void logOutboundKeyDetails(const char* prefix, const KeyEntry& entry);
339 void logOutboundMotionDetails(const char* prefix, const MotionEntry& entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800340
341 // Keeping track of ANR timeouts.
342 enum InputTargetWaitCause {
343 INPUT_TARGET_WAIT_CAUSE_NONE,
344 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
345 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
346 };
347
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800348 InputTargetWaitCause mInputTargetWaitCause GUARDED_BY(mLock);
349 nsecs_t mInputTargetWaitStartTime GUARDED_BY(mLock);
350 nsecs_t mInputTargetWaitTimeoutTime GUARDED_BY(mLock);
351 bool mInputTargetWaitTimeoutExpired GUARDED_BY(mLock);
352 sp<IBinder> mInputTargetWaitApplicationToken GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353
Siarhei Vishniakoude1bc4a2020-05-26 22:39:43 -0700354 bool shouldPruneInboundQueueLocked(const MotionEntry& motionEntry) REQUIRES(mLock);
355
Michael Wrightd02c5b62014-02-10 15:10:22 -0800356 // Contains the last window which received a hover event.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800357 sp<InputWindowHandle> mLastHoverWindowHandle GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358
359 // Finding targets for input events.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700360 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry& entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700361 const sp<InputApplicationHandle>& applicationHandle,
362 const sp<InputWindowHandle>& windowHandle,
363 nsecs_t* nextWakeupTime, const char* reason)
364 REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -0700365
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800366 void removeWindowByTokenLocked(const sp<IBinder>& token) REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -0700367
Michael Wrightd02c5b62014-02-10 15:10:22 -0800368 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
Siarhei Vishniakoud0d71b62019-10-14 14:50:45 -0700369 const sp<IBinder>& inputConnectionToken)
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700370 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800371 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime) REQUIRES(mLock);
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700372 void resetAnrTimeoutsLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800373
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700374 int32_t getTargetDisplayId(const EventEntry& entry);
375 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry& entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700376 std::vector<InputTarget>& inputTargets,
377 nsecs_t* nextWakeupTime) REQUIRES(mLock);
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700378 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry& entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700379 std::vector<InputTarget>& inputTargets,
380 nsecs_t* nextWakeupTime,
381 bool* outConflictingPointerActions) REQUIRES(mLock);
382 std::vector<TouchedMonitor> findTouchedGestureMonitorsLocked(
Siarhei Vishniakoue0fb6bd2020-04-13 11:40:37 -0700383 int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows) const
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700384 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800385
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700386 void addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, int32_t targetFlags,
387 BitSet32 pointerIds, std::vector<InputTarget>& inputTargets)
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800388 REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +0000389 void addMonitoringTargetLocked(const Monitor& monitor, float xOffset, float yOffset,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700390 std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
391 void addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, int32_t displayId,
392 float xOffset = 0, float yOffset = 0) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800393
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700394 void pokeUserActivityLocked(const EventEntry& eventEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395 bool checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700396 const InjectionState* injectionState);
397 bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, int32_t x,
398 int32_t y) const REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800399 bool isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
400 std::string getApplicationWindowLabel(const sp<InputApplicationHandle>& applicationHandle,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700401 const sp<InputWindowHandle>& windowHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800402
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800403 std::string checkWindowReadyForMoreInputLocked(nsecs_t currentTime,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700404 const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700405 const EventEntry& eventEntry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700406 const char* targetType) REQUIRES(mLock);
Jeff Brownffb49772014-10-10 19:01:34 -0700407
Michael Wrightd02c5b62014-02-10 15:10:22 -0800408 // Manage the dispatch cycle for a single connection.
409 // These methods are deliberately not Interruptible because doing all of the work
410 // with the mutex held makes it easier to ensure that connection invariants are maintained.
411 // If needed, the methods post commands to run later once the critical bits are done.
412 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -0800413 EventEntry* eventEntry, const InputTarget& inputTarget)
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700414 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800415 void enqueueDispatchEntriesLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -0800416 EventEntry* eventEntry, const InputTarget& inputTarget)
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700417 REQUIRES(mLock);
418 void enqueueDispatchEntryLocked(const sp<Connection>& connection, EventEntry* eventEntry,
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -0800419 const InputTarget& inputTarget, int32_t dispatchMode)
chaviw8c9cf542019-03-25 13:02:48 -0700420 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800421 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection)
422 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700424 uint32_t seq, bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800425 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700426 bool notify) REQUIRES(mLock);
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -0700427 void drainDispatchQueue(std::deque<DispatchEntry*>& queue);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800428 void releaseDispatchEntry(DispatchEntry* dispatchEntry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800429 static int handleReceiveCallback(int fd, int events, void* data);
chaviw8c9cf542019-03-25 13:02:48 -0700430 // The action sent should only be of type AMOTION_EVENT_*
chaviwfd6d3512019-03-25 13:23:49 -0700431 void dispatchPointerDownOutsideFocus(uint32_t source, int32_t action,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700432 const sp<IBinder>& newToken) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700434 void synthesizeCancelationEventsForAllConnectionsLocked(const CancelationOptions& options)
435 REQUIRES(mLock);
436 void synthesizeCancelationEventsForMonitorsLocked(const CancelationOptions& options)
437 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800438 void synthesizeCancelationEventsForMonitorsLocked(
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700439 const CancelationOptions& options,
Michael Wright3dd60e22019-03-27 22:06:44 +0000440 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800441 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700442 const CancelationOptions& options)
443 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800444 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700445 const CancelationOptions& options)
446 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800447
Svet Ganov5d3bc372020-01-26 23:11:07 -0800448 void synthesizePointerDownEventsForConnectionLocked(const sp<Connection>& connection)
449 REQUIRES(mLock);
450
Michael Wrightd02c5b62014-02-10 15:10:22 -0800451 // Splitting motion events across windows.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700452 MotionEntry* splitMotionEvent(const MotionEntry& originalMotionEntry, BitSet32 pointerIds);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800453
454 // Reset and drop everything the dispatcher is doing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800455 void resetAndDropEverythingLocked(const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800456
457 // Dump state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800458 void dumpDispatchStateLocked(std::string& dump) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +0000459 void dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800460 void logDispatchStateLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461
462 // Registration.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800463 void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock);
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700464 void removeMonitorChannelLocked(
465 const sp<InputChannel>& inputChannel,
466 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800467 status_t unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, bool notify)
468 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800469
470 // Interesting events that we might like to log or tell the framework about.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700471 void onDispatchCycleFinishedLocked(nsecs_t currentTime, const sp<Connection>& connection,
472 uint32_t seq, bool handled) REQUIRES(mLock);
473 void onDispatchCycleBrokenLocked(nsecs_t currentTime, const sp<Connection>& connection)
474 REQUIRES(mLock);
chaviw0c06c6e2019-01-09 13:27:07 -0800475 void onFocusChangedLocked(const sp<InputWindowHandle>& oldFocus,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700476 const sp<InputWindowHandle>& newFocus) REQUIRES(mLock);
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700477 void onAnrLocked(nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700478 const sp<InputWindowHandle>& windowHandle, nsecs_t eventTime,
479 nsecs_t waitStartTime, const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800480
481 // Outbound policy interactions.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800482 void doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry)
483 REQUIRES(mLock);
484 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
485 void doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Siarhei Vishniakoub1a16272020-05-06 16:09:19 -0700486 void doNotifyAnrLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800487 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry)
488 REQUIRES(mLock);
489 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490 bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700491 DispatchEntry* dispatchEntry, KeyEntry* keyEntry,
492 bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800493 bool afterMotionEventLockedInterruptible(const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700494 DispatchEntry* dispatchEntry, MotionEntry* motionEntry,
495 bool handled) REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800496 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Siarhei Vishniakou9757f782019-10-29 12:53:08 -0700497 KeyEvent createKeyEvent(const KeyEntry& entry);
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700498 void doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499
500 // Statistics gathering.
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -0500501 static constexpr std::chrono::duration TOUCH_STATS_REPORT_PERIOD = 5min;
502 LatencyStatistics mTouchStatistics{TOUCH_STATS_REPORT_PERIOD};
503
504 void reportTouchEventForStatistics(const MotionEntry& entry);
Siarhei Vishniakou7f0a4392020-03-24 20:49:09 -0700505 void reportDispatchStatistics(std::chrono::nanoseconds eventDuration,
506 const Connection& connection, bool handled);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800507 void traceInboundQueueLengthLocked() REQUIRES(mLock);
508 void traceOutboundQueueLength(const sp<Connection>& connection);
509 void traceWaitQueueLength(const sp<Connection>& connection);
Prabir Pradhanf93562f2018-11-29 12:13:37 -0800510
Prabir Pradhan79a4f0c2019-01-09 11:24:01 -0800511 sp<InputReporterInterface> mReporter;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512};
513
Garfield Tane84e6f92019-08-29 17:28:41 -0700514} // namespace android::inputdispatcher
Michael Wrightd02c5b62014-02-10 15:10:22 -0800515
516#endif // _UI_INPUT_DISPATCHER_H