blob: 72511e9bf237e58e472168ecf83b382ac76286d5 [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
59/* Dispatches events to input targets. Some functions of the input dispatcher, such as
60 * identifying input targets, are controlled by a separate policy object.
61 *
62 * IMPORTANT INVARIANT:
63 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
64 * the input dispatcher never calls into the policy while holding its internal locks.
65 * The implementation is also carefully designed to recover from scenarios such as an
66 * input channel becoming unregistered while identifying input targets or processing timeouts.
67 *
68 * Methods marked 'Locked' must be called with the lock acquired.
69 *
70 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
71 * may during the course of their execution release the lock, call into the policy, and
72 * then reacquire the lock. The caller is responsible for recovering gracefully.
73 *
74 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
75 */
Garfield Tane84e6f92019-08-29 17:28:41 -070076class InputDispatcher : public android::InputDispatcherInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -080077protected:
78 virtual ~InputDispatcher();
79
80public:
81 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
82
Michael Wright3dd60e22019-03-27 22:06:44 +000083 virtual void dump(std::string& dump) override;
84 virtual void monitor() override;
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080085 virtual bool waitForIdle() override;
Prabir Pradhan3608aad2019-10-02 17:08:26 -070086 virtual status_t start() override;
87 virtual status_t stop() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080088
Michael Wright3dd60e22019-03-27 22:06:44 +000089 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
90 virtual void notifyKey(const NotifyKeyArgs* args) override;
91 virtual void notifyMotion(const NotifyMotionArgs* args) override;
92 virtual void notifySwitch(const NotifySwitchArgs* args) override;
93 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080094
Garfield Tan0fc2fa72019-08-29 17:22:15 -070095 virtual int32_t injectInputEvent(const InputEvent* event, int32_t injectorPid,
96 int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
97 uint32_t policyFlags) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080098
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080099 virtual std::unique_ptr<VerifiedInputEvent> verifyInputEvent(const InputEvent& event) override;
100
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700101 virtual void setInputWindows(
102 const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId,
Michael Wright3dd60e22019-03-27 22:06:44 +0000103 const sp<ISetInputWindowsListener>& setInputWindowsListener = nullptr) override;
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700104 virtual void setFocusedApplication(
105 int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) override;
Michael Wright3dd60e22019-03-27 22:06:44 +0000106 virtual void setFocusedDisplay(int32_t displayId) override;
107 virtual void setInputDispatchMode(bool enabled, bool frozen) override;
108 virtual void setInputFilterEnabled(bool enabled) override;
Siarhei Vishniakouf3bc1aa2019-11-25 13:48:53 -0800109 virtual void setInTouchMode(bool inTouchMode) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700111 virtual bool transferTouchFocus(const sp<IBinder>& fromToken,
112 const sp<IBinder>& toToken) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113
Siarhei Vishniakou7c34b232019-10-11 19:08:48 -0700114 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel) override;
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700115 virtual status_t registerInputMonitor(const sp<InputChannel>& inputChannel, int32_t displayId,
116 bool isGestureMonitor) override;
Michael Wright3dd60e22019-03-27 22:06:44 +0000117 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) override;
118 virtual status_t pilferPointers(const sp<IBinder>& token) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800119
120private:
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -0700121 enum class DropReason {
122 NOT_DROPPED,
123 POLICY,
124 APP_SWITCH,
125 DISABLED,
126 BLOCKED,
127 STALE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800128 };
129
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700130 std::unique_ptr<InputThread> mThread;
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700131
Michael Wrightd02c5b62014-02-10 15:10:22 -0800132 sp<InputDispatcherPolicyInterface> mPolicy;
Garfield Tane84e6f92019-08-29 17:28:41 -0700133 android::InputDispatcherConfiguration mConfig;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800134
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800135 std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800137 std::condition_variable mDispatcherIsAlive;
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800138 std::condition_variable mDispatcherEnteredIdle;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800139
140 sp<Looper> mLooper;
141
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800142 EventEntry* mPendingEvent GUARDED_BY(mLock);
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -0700143 std::deque<EventEntry*> mInboundQueue GUARDED_BY(mLock);
144 std::deque<EventEntry*> mRecentQueue GUARDED_BY(mLock);
Siarhei Vishniakoue7c94b92019-07-29 09:17:54 -0700145 std::deque<std::unique_ptr<CommandEntry>> mCommandQueue GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800147 DropReason mLastDropReason GUARDED_BY(mLock);
Michael Wright3a981722015-06-10 15:26:13 +0100148
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700149 // With each iteration, InputDispatcher nominally processes one queued event,
150 // a timeout, or a response from an input consumer.
151 // This method should only be called on the input dispatcher's own thread.
152 void dispatchOnce();
153
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800154 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155
156 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800157 bool enqueueInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800158
159 // Cleans up input state when dropping an inbound event.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700160 void dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800161
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100162 // Enqueues a focus event.
163 void enqueueFocusEventLocked(const InputWindowHandle& window, bool hasFocus) REQUIRES(mLock);
164
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165 // Adds an event to a queue of recent events for debugging purposes.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800166 void addRecentEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167
168 // App switch latency optimization.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800169 bool mAppSwitchSawKeyDown GUARDED_BY(mLock);
170 nsecs_t mAppSwitchDueTime GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700172 bool isAppSwitchKeyEvent(const KeyEntry& keyEntry);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800173 bool isAppSwitchPendingLocked() REQUIRES(mLock);
174 void resetPendingAppSwitchLocked(bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176 // Blocked event latency optimization. Drops old events when the user intends
177 // to transfer focus to a new application.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800178 EventEntry* mNextUnblockedEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179
Tiger Huang85b8c5e2019-01-17 18:34:54 +0800180 sp<InputWindowHandle> findTouchedWindowAtLocked(int32_t displayId, int32_t x, int32_t y,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700181 bool addOutsideTargets = false,
182 bool addPortalWindows = false) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183
184 // All registered connections mapped by channel file descriptor.
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -0700185 std::unordered_map<int, sp<Connection>> mConnectionsByFd GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186
Robert Carr5c8a0262018-10-03 16:30:44 -0700187 struct IBinderHash {
188 std::size_t operator()(const sp<IBinder>& b) const {
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700189 return std::hash<IBinder*>{}(b.get());
Robert Carr5c8a0262018-10-03 16:30:44 -0700190 }
191 };
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800192 std::unordered_map<sp<IBinder>, sp<InputChannel>, IBinderHash> mInputChannelsByToken
193 GUARDED_BY(mLock);
Robert Carr5c8a0262018-10-03 16:30:44 -0700194
Michael Wright3dd60e22019-03-27 22:06:44 +0000195 // Finds the display ID of the gesture monitor identified by the provided token.
196 std::optional<int32_t> findGestureMonitorDisplayByTokenLocked(const sp<IBinder>& token)
197 REQUIRES(mLock);
198
Siarhei Vishniakoud0d71b62019-10-14 14:50:45 -0700199 sp<Connection> getConnectionLocked(const sp<IBinder>& inputConnectionToken) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800200
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800201 // Input channels that will receive a copy of all input events sent to the provided display.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700202 std::unordered_map<int32_t, std::vector<Monitor>> mGlobalMonitorsByDisplay GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203
Michael Wright3dd60e22019-03-27 22:06:44 +0000204 // Input channels that will receive pointer events that start within the corresponding display.
205 // These are a bit special when compared to global monitors since they'll cause gesture streams
206 // to continue even when there isn't a touched window,and have the ability to steal the rest of
207 // the pointer stream in order to claim it for a system gesture.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700208 std::unordered_map<int32_t, std::vector<Monitor>> mGestureMonitorsByDisplay GUARDED_BY(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +0000209
Michael Wrightd02c5b62014-02-10 15:10:22 -0800210 // Event injection and synchronization.
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800211 std::condition_variable mInjectionResultAvailable;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800212 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800213 void setInjectionResult(EventEntry* entry, int32_t injectionResult);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800214
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800215 std::condition_variable mInjectionSyncFinished;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800216 void incrementPendingForegroundDispatches(EventEntry* entry);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800217 void decrementPendingForegroundDispatches(EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218
219 // Key repeat tracking.
220 struct KeyRepeatState {
221 KeyEntry* lastKeyEntry; // or null if no repeat
222 nsecs_t nextRepeatTime;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800223 } mKeyRepeatState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800224
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800225 void resetKeyRepeatLocked() REQUIRES(mLock);
226 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227
Michael Wright78f24442014-08-06 15:55:28 -0700228 // Key replacement tracking
229 struct KeyReplacement {
230 int32_t keyCode;
231 int32_t deviceId;
232 bool operator==(const KeyReplacement& rhs) const {
233 return keyCode == rhs.keyCode && deviceId == rhs.deviceId;
234 }
235 bool operator<(const KeyReplacement& rhs) const {
236 return keyCode != rhs.keyCode ? keyCode < rhs.keyCode : deviceId < rhs.deviceId;
237 }
238 };
239 // Maps the key code replaced, device id tuple to the key code it was replaced with
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800240 KeyedVector<KeyReplacement, int32_t> mReplacedKeys GUARDED_BY(mLock);
Siarhei Vishniakou61fafdd2018-04-13 11:00:58 -0500241 // Process certain Meta + Key combinations
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700242 void accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, int32_t& keyCode,
243 int32_t& metaState);
Michael Wright78f24442014-08-06 15:55:28 -0700244
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245 // Deferred command processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800246 bool haveCommandsLocked() const REQUIRES(mLock);
247 bool runCommandsLockedInterruptible() REQUIRES(mLock);
Siarhei Vishniakoue7c94b92019-07-29 09:17:54 -0700248 void postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249
250 // Input filter processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800251 bool shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) REQUIRES(mLock);
252 bool shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800253
254 // Inbound event processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800255 void drainInboundQueueLocked() REQUIRES(mLock);
256 void releasePendingEventLocked() REQUIRES(mLock);
257 void releaseInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258
259 // Dispatch state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800260 bool mDispatchEnabled GUARDED_BY(mLock);
261 bool mDispatchFrozen GUARDED_BY(mLock);
262 bool mInputFilterEnabled GUARDED_BY(mLock);
Siarhei Vishniakouf3bc1aa2019-11-25 13:48:53 -0800263 bool mInTouchMode GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800265 std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> mWindowHandlesByDisplay
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800266 GUARDED_BY(mLock);
Arthur Hungb92218b2018-08-14 12:00:21 +0800267 // Get window handles by display, return an empty vector if not found.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800268 std::vector<sp<InputWindowHandle>> getWindowHandlesLocked(int32_t displayId) const
269 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800270 sp<InputWindowHandle> getWindowHandleLocked(const sp<IBinder>& windowHandleToken) const
271 REQUIRES(mLock);
272 sp<InputChannel> getInputChannelLocked(const sp<IBinder>& windowToken) const REQUIRES(mLock);
273 bool hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800274
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -0700275 /*
276 * Validate and update InputWindowHandles for a given display.
277 */
278 void updateWindowHandlesForDisplayLocked(
279 const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId)
280 REQUIRES(mLock);
281
Michael Wrightd02c5b62014-02-10 15:10:22 -0800282 // Focus tracking for keys, trackball, etc.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800283 std::unordered_map<int32_t, sp<InputWindowHandle>> mFocusedWindowHandlesByDisplay
284 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800286 KeyedVector<int32_t, TouchState> mTouchStatesByDisplay GUARDED_BY(mLock);
287 TouchState mTempTouchState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800288
Tiger Huang721e26f2018-07-24 22:26:19 +0800289 // Focused applications.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800290 std::unordered_map<int32_t, sp<InputApplicationHandle>> mFocusedApplicationHandlesByDisplay
291 GUARDED_BY(mLock);
Tiger Huang721e26f2018-07-24 22:26:19 +0800292
293 // Top focused display.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800294 int32_t mFocusedDisplayId GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295
296 // Dispatcher state at time of last ANR.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800297 std::string mLastANRState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298
299 // Dispatch inbound events.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700300 bool dispatchConfigurationChangedLocked(nsecs_t currentTime, ConfigurationChangedEntry* entry)
301 REQUIRES(mLock);
302 bool dispatchDeviceResetLocked(nsecs_t currentTime, DeviceResetEntry* entry) REQUIRES(mLock);
303 bool dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, DropReason* dropReason,
304 nsecs_t* nextWakeupTime) REQUIRES(mLock);
305 bool dispatchMotionLocked(nsecs_t currentTime, MotionEntry* entry, DropReason* dropReason,
306 nsecs_t* nextWakeupTime) REQUIRES(mLock);
Siarhei Vishniakouf1035d42019-09-20 16:32:01 +0100307 void dispatchFocusLocked(nsecs_t currentTime, FocusEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 void dispatchEventLocked(nsecs_t currentTime, EventEntry* entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700309 const std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800310
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700311 void logOutboundKeyDetails(const char* prefix, const KeyEntry& entry);
312 void logOutboundMotionDetails(const char* prefix, const MotionEntry& entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313
314 // Keeping track of ANR timeouts.
315 enum InputTargetWaitCause {
316 INPUT_TARGET_WAIT_CAUSE_NONE,
317 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
318 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
319 };
320
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800321 InputTargetWaitCause mInputTargetWaitCause GUARDED_BY(mLock);
322 nsecs_t mInputTargetWaitStartTime GUARDED_BY(mLock);
323 nsecs_t mInputTargetWaitTimeoutTime GUARDED_BY(mLock);
324 bool mInputTargetWaitTimeoutExpired GUARDED_BY(mLock);
325 sp<IBinder> mInputTargetWaitApplicationToken GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326
327 // Contains the last window which received a hover event.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800328 sp<InputWindowHandle> mLastHoverWindowHandle GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329
330 // Finding targets for input events.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700331 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry& entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700332 const sp<InputApplicationHandle>& applicationHandle,
333 const sp<InputWindowHandle>& windowHandle,
334 nsecs_t* nextWakeupTime, const char* reason)
335 REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -0700336
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800337 void removeWindowByTokenLocked(const sp<IBinder>& token) REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -0700338
Michael Wrightd02c5b62014-02-10 15:10:22 -0800339 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
Siarhei Vishniakoud0d71b62019-10-14 14:50:45 -0700340 const sp<IBinder>& inputConnectionToken)
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700341 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800342 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime) REQUIRES(mLock);
343 void resetANRTimeoutsLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700345 int32_t getTargetDisplayId(const EventEntry& entry);
346 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry& entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700347 std::vector<InputTarget>& inputTargets,
348 nsecs_t* nextWakeupTime) REQUIRES(mLock);
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700349 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry& entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700350 std::vector<InputTarget>& inputTargets,
351 nsecs_t* nextWakeupTime,
352 bool* outConflictingPointerActions) REQUIRES(mLock);
353 std::vector<TouchedMonitor> findTouchedGestureMonitorsLocked(
354 int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows)
355 REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +0000356 void addGestureMonitors(const std::vector<Monitor>& monitors,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700357 std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0,
358 float yOffset = 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800359
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700360 void addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, int32_t targetFlags,
361 BitSet32 pointerIds, std::vector<InputTarget>& inputTargets)
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800362 REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +0000363 void addMonitoringTargetLocked(const Monitor& monitor, float xOffset, float yOffset,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700364 std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
365 void addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, int32_t displayId,
366 float xOffset = 0, float yOffset = 0) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700368 void pokeUserActivityLocked(const EventEntry& eventEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369 bool checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700370 const InjectionState* injectionState);
371 bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, int32_t x,
372 int32_t y) const REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800373 bool isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
374 std::string getApplicationWindowLabel(const sp<InputApplicationHandle>& applicationHandle,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700375 const sp<InputWindowHandle>& windowHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800376
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800377 std::string checkWindowReadyForMoreInputLocked(nsecs_t currentTime,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700378 const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700379 const EventEntry& eventEntry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700380 const char* targetType) REQUIRES(mLock);
Jeff Brownffb49772014-10-10 19:01:34 -0700381
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 // Manage the dispatch cycle for a single connection.
383 // These methods are deliberately not Interruptible because doing all of the work
384 // with the mutex held makes it easier to ensure that connection invariants are maintained.
385 // If needed, the methods post commands to run later once the critical bits are done.
386 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -0800387 EventEntry* eventEntry, const InputTarget& inputTarget)
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700388 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389 void enqueueDispatchEntriesLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -0800390 EventEntry* eventEntry, const InputTarget& inputTarget)
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700391 REQUIRES(mLock);
392 void enqueueDispatchEntryLocked(const sp<Connection>& connection, EventEntry* eventEntry,
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -0800393 const InputTarget& inputTarget, int32_t dispatchMode)
chaviw8c9cf542019-03-25 13:02:48 -0700394 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800395 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection)
396 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700398 uint32_t seq, bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800399 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700400 bool notify) REQUIRES(mLock);
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -0700401 void drainDispatchQueue(std::deque<DispatchEntry*>& queue);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800402 void releaseDispatchEntry(DispatchEntry* dispatchEntry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800403 static int handleReceiveCallback(int fd, int events, void* data);
chaviw8c9cf542019-03-25 13:02:48 -0700404 // The action sent should only be of type AMOTION_EVENT_*
chaviwfd6d3512019-03-25 13:23:49 -0700405 void dispatchPointerDownOutsideFocus(uint32_t source, int32_t action,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700406 const sp<IBinder>& newToken) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800407
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700408 void synthesizeCancelationEventsForAllConnectionsLocked(const CancelationOptions& options)
409 REQUIRES(mLock);
410 void synthesizeCancelationEventsForMonitorsLocked(const CancelationOptions& options)
411 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800412 void synthesizeCancelationEventsForMonitorsLocked(
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700413 const CancelationOptions& options,
Michael Wright3dd60e22019-03-27 22:06:44 +0000414 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800415 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700416 const CancelationOptions& options)
417 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800418 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700419 const CancelationOptions& options)
420 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800421
Svet Ganov5d3bc372020-01-26 23:11:07 -0800422 void synthesizePointerDownEventsForConnectionLocked(const sp<Connection>& connection)
423 REQUIRES(mLock);
424
Michael Wrightd02c5b62014-02-10 15:10:22 -0800425 // Splitting motion events across windows.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700426 MotionEntry* splitMotionEvent(const MotionEntry& originalMotionEntry, BitSet32 pointerIds);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800427
428 // Reset and drop everything the dispatcher is doing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800429 void resetAndDropEverythingLocked(const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800430
431 // Dump state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800432 void dumpDispatchStateLocked(std::string& dump) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +0000433 void dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800434 void logDispatchStateLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435
436 // Registration.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800437 void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock);
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700438 void removeMonitorChannelLocked(
439 const sp<InputChannel>& inputChannel,
440 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800441 status_t unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, bool notify)
442 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800443
444 // Interesting events that we might like to log or tell the framework about.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700445 void onDispatchCycleFinishedLocked(nsecs_t currentTime, const sp<Connection>& connection,
446 uint32_t seq, bool handled) REQUIRES(mLock);
447 void onDispatchCycleBrokenLocked(nsecs_t currentTime, const sp<Connection>& connection)
448 REQUIRES(mLock);
chaviw0c06c6e2019-01-09 13:27:07 -0800449 void onFocusChangedLocked(const sp<InputWindowHandle>& oldFocus,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700450 const sp<InputWindowHandle>& newFocus) REQUIRES(mLock);
451 void onANRLocked(nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
452 const sp<InputWindowHandle>& windowHandle, nsecs_t eventTime,
453 nsecs_t waitStartTime, const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800454
455 // Outbound policy interactions.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800456 void doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry)
457 REQUIRES(mLock);
458 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
459 void doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
460 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
461 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry)
462 REQUIRES(mLock);
463 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800464 bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700465 DispatchEntry* dispatchEntry, KeyEntry* keyEntry,
466 bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800467 bool afterMotionEventLockedInterruptible(const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700468 DispatchEntry* dispatchEntry, MotionEntry* motionEntry,
469 bool handled) REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800470 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Siarhei Vishniakou9757f782019-10-29 12:53:08 -0700471 KeyEvent createKeyEvent(const KeyEntry& entry);
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700472 void doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800473
474 // Statistics gathering.
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -0500475 static constexpr std::chrono::duration TOUCH_STATS_REPORT_PERIOD = 5min;
476 LatencyStatistics mTouchStatistics{TOUCH_STATS_REPORT_PERIOD};
477
478 void reportTouchEventForStatistics(const MotionEntry& entry);
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700479 void updateDispatchStatistics(nsecs_t currentTime, const EventEntry& entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700480 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800481 void traceInboundQueueLengthLocked() REQUIRES(mLock);
482 void traceOutboundQueueLength(const sp<Connection>& connection);
483 void traceWaitQueueLength(const sp<Connection>& connection);
Prabir Pradhanf93562f2018-11-29 12:13:37 -0800484
Prabir Pradhan79a4f0c2019-01-09 11:24:01 -0800485 sp<InputReporterInterface> mReporter;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800486};
487
Garfield Tane84e6f92019-08-29 17:28:41 -0700488} // namespace android::inputdispatcher
Michael Wrightd02c5b62014-02-10 15:10:22 -0800489
490#endif // _UI_INPUT_DISPATCHER_H