blob: 50b52502a01c421bf8360488aebe909166bdbdb9 [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"
28#include "Monitor.h"
29#include "TouchState.h"
30#include "TouchedWindow.h"
31
Michael Wrightd02c5b62014-02-10 15:10:22 -080032#include <input/Input.h>
Robert Carr3720ed02018-08-08 16:08:27 -070033#include <input/InputApplication.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <input/InputTransport.h>
Robert Carr3720ed02018-08-08 16:08:27 -070035#include <input/InputWindow.h>
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -050036#include <input/LatencyStatistics.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070037#include <limits.h>
38#include <stddef.h>
Michael Wright3dd60e22019-03-27 22:06:44 +000039#include <ui/Region.h>
Garfield Tane84e6f92019-08-29 17:28:41 -070040#include <unistd.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041#include <utils/BitSet.h>
Garfield Tan0fc2fa72019-08-29 17:22:15 -070042#include <utils/Looper.h>
43#include <utils/RefBase.h>
44#include <utils/Timers.h>
45#include <utils/threads.h>
46#include <condition_variable>
Garfield Tane84e6f92019-08-29 17:28:41 -070047#include <deque>
Garfield Tan0fc2fa72019-08-29 17:22:15 -070048#include <optional>
Robert Carr5c8a0262018-10-03 16:30:44 -070049#include <unordered_map>
Michael Wrightd02c5b62014-02-10 15:10:22 -080050
Garfield Tane84e6f92019-08-29 17:28:41 -070051#include <InputListener.h>
52#include <InputReporterInterface.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080053
Garfield Tane84e6f92019-08-29 17:28:41 -070054namespace android::inputdispatcher {
Michael Wrightd02c5b62014-02-10 15:10:22 -080055
Garfield Tane84e6f92019-08-29 17:28:41 -070056class Connection;
Michael Wrightd02c5b62014-02-10 15:10:22 -080057
58/* Dispatches events to input targets. Some functions of the input dispatcher, such as
59 * identifying input targets, are controlled by a separate policy object.
60 *
61 * IMPORTANT INVARIANT:
62 * Because the policy can potentially block or cause re-entrance into the input dispatcher,
63 * the input dispatcher never calls into the policy while holding its internal locks.
64 * The implementation is also carefully designed to recover from scenarios such as an
65 * input channel becoming unregistered while identifying input targets or processing timeouts.
66 *
67 * Methods marked 'Locked' must be called with the lock acquired.
68 *
69 * Methods marked 'LockedInterruptible' must be called with the lock acquired but
70 * may during the course of their execution release the lock, call into the policy, and
71 * then reacquire the lock. The caller is responsible for recovering gracefully.
72 *
73 * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa.
74 */
Garfield Tane84e6f92019-08-29 17:28:41 -070075class InputDispatcher : public android::InputDispatcherInterface {
Michael Wrightd02c5b62014-02-10 15:10:22 -080076protected:
77 virtual ~InputDispatcher();
78
79public:
80 explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy);
81
Michael Wright3dd60e22019-03-27 22:06:44 +000082 virtual void dump(std::string& dump) override;
83 virtual void monitor() override;
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -080084 virtual bool waitForIdle() override;
Prabir Pradhan3608aad2019-10-02 17:08:26 -070085 virtual status_t start() override;
86 virtual status_t stop() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080087
Michael Wright3dd60e22019-03-27 22:06:44 +000088 virtual void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override;
89 virtual void notifyKey(const NotifyKeyArgs* args) override;
90 virtual void notifyMotion(const NotifyMotionArgs* args) override;
91 virtual void notifySwitch(const NotifySwitchArgs* args) override;
92 virtual void notifyDeviceReset(const NotifyDeviceResetArgs* args) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080093
Garfield Tan0fc2fa72019-08-29 17:22:15 -070094 virtual int32_t injectInputEvent(const InputEvent* event, int32_t injectorPid,
95 int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis,
96 uint32_t policyFlags) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080097
Garfield Tan0fc2fa72019-08-29 17:22:15 -070098 virtual void setInputWindows(
99 const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId,
Michael Wright3dd60e22019-03-27 22:06:44 +0000100 const sp<ISetInputWindowsListener>& setInputWindowsListener = nullptr) override;
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700101 virtual void setFocusedApplication(
102 int32_t displayId, const sp<InputApplicationHandle>& inputApplicationHandle) override;
Michael Wright3dd60e22019-03-27 22:06:44 +0000103 virtual void setFocusedDisplay(int32_t displayId) override;
104 virtual void setInputDispatchMode(bool enabled, bool frozen) override;
105 virtual void setInputFilterEnabled(bool enabled) override;
Siarhei Vishniakouf3bc1aa2019-11-25 13:48:53 -0800106 virtual void setInTouchMode(bool inTouchMode) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700108 virtual bool transferTouchFocus(const sp<IBinder>& fromToken,
109 const sp<IBinder>& toToken) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800110
Siarhei Vishniakou7c34b232019-10-11 19:08:48 -0700111 virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel) override;
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700112 virtual status_t registerInputMonitor(const sp<InputChannel>& inputChannel, int32_t displayId,
113 bool isGestureMonitor) override;
Michael Wright3dd60e22019-03-27 22:06:44 +0000114 virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) override;
115 virtual status_t pilferPointers(const sp<IBinder>& token) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116
117private:
Siarhei Vishniakou0fb1a0e2019-10-22 11:23:36 -0700118 enum class DropReason {
119 NOT_DROPPED,
120 POLICY,
121 APP_SWITCH,
122 DISABLED,
123 BLOCKED,
124 STALE,
Michael Wrightd02c5b62014-02-10 15:10:22 -0800125 };
126
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700127 class InputDispatcherThread;
128 sp<InputDispatcherThread> mThread;
129
Michael Wrightd02c5b62014-02-10 15:10:22 -0800130 sp<InputDispatcherPolicyInterface> mPolicy;
Garfield Tane84e6f92019-08-29 17:28:41 -0700131 android::InputDispatcherConfiguration mConfig;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800132
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800133 std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800134
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800135 std::condition_variable mDispatcherIsAlive;
Siarhei Vishniakou2bfa9052019-11-21 18:10:54 -0800136 std::condition_variable mDispatcherEnteredIdle;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137
138 sp<Looper> mLooper;
139
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800140 EventEntry* mPendingEvent GUARDED_BY(mLock);
Siarhei Vishniakou44a2aed2019-07-29 08:59:52 -0700141 std::deque<EventEntry*> mInboundQueue GUARDED_BY(mLock);
142 std::deque<EventEntry*> mRecentQueue GUARDED_BY(mLock);
Siarhei Vishniakoue7c94b92019-07-29 09:17:54 -0700143 std::deque<std::unique_ptr<CommandEntry>> mCommandQueue GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800145 DropReason mLastDropReason GUARDED_BY(mLock);
Michael Wright3a981722015-06-10 15:26:13 +0100146
Prabir Pradhan3608aad2019-10-02 17:08:26 -0700147 // With each iteration, InputDispatcher nominally processes one queued event,
148 // a timeout, or a response from an input consumer.
149 // This method should only be called on the input dispatcher's own thread.
150 void dispatchOnce();
151
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800152 void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153
154 // Enqueues an inbound event. Returns true if mLooper->wake() should be called.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800155 bool enqueueInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156
157 // Cleans up input state when dropping an inbound event.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700158 void dropInboundEventLocked(const EventEntry& entry, DropReason dropReason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800159
160 // Adds an event to a queue of recent events for debugging purposes.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800161 void addRecentEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800162
163 // App switch latency optimization.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800164 bool mAppSwitchSawKeyDown GUARDED_BY(mLock);
165 nsecs_t mAppSwitchDueTime GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700167 bool isAppSwitchKeyEvent(const KeyEntry& keyEntry);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800168 bool isAppSwitchPendingLocked() REQUIRES(mLock);
169 void resetPendingAppSwitchLocked(bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171 // Blocked event latency optimization. Drops old events when the user intends
172 // to transfer focus to a new application.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800173 EventEntry* mNextUnblockedEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174
Tiger Huang85b8c5e2019-01-17 18:34:54 +0800175 sp<InputWindowHandle> findTouchedWindowAtLocked(int32_t displayId, int32_t x, int32_t y,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700176 bool addOutsideTargets = false,
177 bool addPortalWindows = false) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178
179 // All registered connections mapped by channel file descriptor.
Siarhei Vishniakou146ecfd2019-07-29 16:04:31 -0700180 std::unordered_map<int, sp<Connection>> mConnectionsByFd GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181
Robert Carr5c8a0262018-10-03 16:30:44 -0700182 struct IBinderHash {
183 std::size_t operator()(const sp<IBinder>& b) const {
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700184 return std::hash<IBinder*>{}(b.get());
Robert Carr5c8a0262018-10-03 16:30:44 -0700185 }
186 };
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800187 std::unordered_map<sp<IBinder>, sp<InputChannel>, IBinderHash> mInputChannelsByToken
188 GUARDED_BY(mLock);
Robert Carr5c8a0262018-10-03 16:30:44 -0700189
Michael Wright3dd60e22019-03-27 22:06:44 +0000190 // Finds the display ID of the gesture monitor identified by the provided token.
191 std::optional<int32_t> findGestureMonitorDisplayByTokenLocked(const sp<IBinder>& token)
192 REQUIRES(mLock);
193
Siarhei Vishniakoud0d71b62019-10-14 14:50:45 -0700194 sp<Connection> getConnectionLocked(const sp<IBinder>& inputConnectionToken) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195
Arthur Hung2fbf37f2018-09-13 18:16:41 +0800196 // Input channels that will receive a copy of all input events sent to the provided display.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700197 std::unordered_map<int32_t, std::vector<Monitor>> mGlobalMonitorsByDisplay GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800198
Michael Wright3dd60e22019-03-27 22:06:44 +0000199 // Input channels that will receive pointer events that start within the corresponding display.
200 // These are a bit special when compared to global monitors since they'll cause gesture streams
201 // to continue even when there isn't a touched window,and have the ability to steal the rest of
202 // the pointer stream in order to claim it for a system gesture.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700203 std::unordered_map<int32_t, std::vector<Monitor>> mGestureMonitorsByDisplay GUARDED_BY(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +0000204
Michael Wrightd02c5b62014-02-10 15:10:22 -0800205 // Event injection and synchronization.
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800206 std::condition_variable mInjectionResultAvailable;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207 bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800208 void setInjectionResult(EventEntry* entry, int32_t injectionResult);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209
Siarhei Vishniakou443ad902019-03-06 17:25:41 -0800210 std::condition_variable mInjectionSyncFinished;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800211 void incrementPendingForegroundDispatches(EventEntry* entry);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800212 void decrementPendingForegroundDispatches(EventEntry* entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213
214 // Key repeat tracking.
215 struct KeyRepeatState {
216 KeyEntry* lastKeyEntry; // or null if no repeat
217 nsecs_t nextRepeatTime;
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800218 } mKeyRepeatState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800220 void resetKeyRepeatLocked() REQUIRES(mLock);
221 KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800222
Michael Wright78f24442014-08-06 15:55:28 -0700223 // Key replacement tracking
224 struct KeyReplacement {
225 int32_t keyCode;
226 int32_t deviceId;
227 bool operator==(const KeyReplacement& rhs) const {
228 return keyCode == rhs.keyCode && deviceId == rhs.deviceId;
229 }
230 bool operator<(const KeyReplacement& rhs) const {
231 return keyCode != rhs.keyCode ? keyCode < rhs.keyCode : deviceId < rhs.deviceId;
232 }
233 };
234 // Maps the key code replaced, device id tuple to the key code it was replaced with
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800235 KeyedVector<KeyReplacement, int32_t> mReplacedKeys GUARDED_BY(mLock);
Siarhei Vishniakou61fafdd2018-04-13 11:00:58 -0500236 // Process certain Meta + Key combinations
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700237 void accelerateMetaShortcuts(const int32_t deviceId, const int32_t action, int32_t& keyCode,
238 int32_t& metaState);
Michael Wright78f24442014-08-06 15:55:28 -0700239
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240 // Deferred command processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800241 bool haveCommandsLocked() const REQUIRES(mLock);
242 bool runCommandsLockedInterruptible() REQUIRES(mLock);
Siarhei Vishniakoue7c94b92019-07-29 09:17:54 -0700243 void postCommandLocked(std::unique_ptr<CommandEntry> commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800244
245 // Input filter processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800246 bool shouldSendKeyToInputFilterLocked(const NotifyKeyArgs* args) REQUIRES(mLock);
247 bool shouldSendMotionToInputFilterLocked(const NotifyMotionArgs* args) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248
249 // Inbound event processing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800250 void drainInboundQueueLocked() REQUIRES(mLock);
251 void releasePendingEventLocked() REQUIRES(mLock);
252 void releaseInboundEventLocked(EventEntry* entry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800253
254 // Dispatch state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800255 bool mDispatchEnabled GUARDED_BY(mLock);
256 bool mDispatchFrozen GUARDED_BY(mLock);
257 bool mInputFilterEnabled GUARDED_BY(mLock);
Siarhei Vishniakouf3bc1aa2019-11-25 13:48:53 -0800258 bool mInTouchMode GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800260 std::unordered_map<int32_t, std::vector<sp<InputWindowHandle>>> mWindowHandlesByDisplay
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800261 GUARDED_BY(mLock);
Arthur Hungb92218b2018-08-14 12:00:21 +0800262 // Get window handles by display, return an empty vector if not found.
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800263 std::vector<sp<InputWindowHandle>> getWindowHandlesLocked(int32_t displayId) const
264 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800265 sp<InputWindowHandle> getWindowHandleLocked(const sp<IBinder>& windowHandleToken) const
266 REQUIRES(mLock);
267 sp<InputChannel> getInputChannelLocked(const sp<IBinder>& windowToken) const REQUIRES(mLock);
268 bool hasWindowHandleLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269
Siarhei Vishniakoub3ad35c2019-04-05 10:50:52 -0700270 /*
271 * Validate and update InputWindowHandles for a given display.
272 */
273 void updateWindowHandlesForDisplayLocked(
274 const std::vector<sp<InputWindowHandle>>& inputWindowHandles, int32_t displayId)
275 REQUIRES(mLock);
276
Michael Wrightd02c5b62014-02-10 15:10:22 -0800277 // Focus tracking for keys, trackball, etc.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800278 std::unordered_map<int32_t, sp<InputWindowHandle>> mFocusedWindowHandlesByDisplay
279 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800281 KeyedVector<int32_t, TouchState> mTouchStatesByDisplay GUARDED_BY(mLock);
282 TouchState mTempTouchState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283
Tiger Huang721e26f2018-07-24 22:26:19 +0800284 // Focused applications.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800285 std::unordered_map<int32_t, sp<InputApplicationHandle>> mFocusedApplicationHandlesByDisplay
286 GUARDED_BY(mLock);
Tiger Huang721e26f2018-07-24 22:26:19 +0800287
288 // Top focused display.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800289 int32_t mFocusedDisplayId GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800290
291 // Dispatcher state at time of last ANR.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800292 std::string mLastANRState GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293
294 // Dispatch inbound events.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700295 bool dispatchConfigurationChangedLocked(nsecs_t currentTime, ConfigurationChangedEntry* entry)
296 REQUIRES(mLock);
297 bool dispatchDeviceResetLocked(nsecs_t currentTime, DeviceResetEntry* entry) REQUIRES(mLock);
298 bool dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, DropReason* dropReason,
299 nsecs_t* nextWakeupTime) REQUIRES(mLock);
300 bool dispatchMotionLocked(nsecs_t currentTime, MotionEntry* entry, DropReason* dropReason,
301 nsecs_t* nextWakeupTime) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800302 void dispatchEventLocked(nsecs_t currentTime, EventEntry* entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700303 const std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800304
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700305 void logOutboundKeyDetails(const char* prefix, const KeyEntry& entry);
306 void logOutboundMotionDetails(const char* prefix, const MotionEntry& entry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800307
308 // Keeping track of ANR timeouts.
309 enum InputTargetWaitCause {
310 INPUT_TARGET_WAIT_CAUSE_NONE,
311 INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY,
312 INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY,
313 };
314
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800315 InputTargetWaitCause mInputTargetWaitCause GUARDED_BY(mLock);
316 nsecs_t mInputTargetWaitStartTime GUARDED_BY(mLock);
317 nsecs_t mInputTargetWaitTimeoutTime GUARDED_BY(mLock);
318 bool mInputTargetWaitTimeoutExpired GUARDED_BY(mLock);
319 sp<IBinder> mInputTargetWaitApplicationToken GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800320
321 // Contains the last window which received a hover event.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800322 sp<InputWindowHandle> mLastHoverWindowHandle GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800323
324 // Finding targets for input events.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700325 int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry& entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700326 const sp<InputApplicationHandle>& applicationHandle,
327 const sp<InputWindowHandle>& windowHandle,
328 nsecs_t* nextWakeupTime, const char* reason)
329 REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -0700330
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800331 void removeWindowByTokenLocked(const sp<IBinder>& token) REQUIRES(mLock);
Robert Carr803535b2018-08-02 16:38:15 -0700332
Michael Wrightd02c5b62014-02-10 15:10:22 -0800333 void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout,
Siarhei Vishniakoud0d71b62019-10-14 14:50:45 -0700334 const sp<IBinder>& inputConnectionToken)
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700335 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800336 nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime) REQUIRES(mLock);
337 void resetANRTimeoutsLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700339 int32_t getTargetDisplayId(const EventEntry& entry);
340 int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry& entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700341 std::vector<InputTarget>& inputTargets,
342 nsecs_t* nextWakeupTime) REQUIRES(mLock);
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700343 int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry& entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700344 std::vector<InputTarget>& inputTargets,
345 nsecs_t* nextWakeupTime,
346 bool* outConflictingPointerActions) REQUIRES(mLock);
347 std::vector<TouchedMonitor> findTouchedGestureMonitorsLocked(
348 int32_t displayId, const std::vector<sp<InputWindowHandle>>& portalWindows)
349 REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +0000350 void addGestureMonitors(const std::vector<Monitor>& monitors,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700351 std::vector<TouchedMonitor>& outTouchedMonitors, float xOffset = 0,
352 float yOffset = 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700354 void addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, int32_t targetFlags,
355 BitSet32 pointerIds, std::vector<InputTarget>& inputTargets)
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800356 REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +0000357 void addMonitoringTargetLocked(const Monitor& monitor, float xOffset, float yOffset,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700358 std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
359 void addGlobalMonitoringTargetsLocked(std::vector<InputTarget>& inputTargets, int32_t displayId,
360 float xOffset = 0, float yOffset = 0) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700362 void pokeUserActivityLocked(const EventEntry& eventEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800363 bool checkInjectionPermission(const sp<InputWindowHandle>& windowHandle,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700364 const InjectionState* injectionState);
365 bool isWindowObscuredAtPointLocked(const sp<InputWindowHandle>& windowHandle, int32_t x,
366 int32_t y) const REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800367 bool isWindowObscuredLocked(const sp<InputWindowHandle>& windowHandle) const REQUIRES(mLock);
368 std::string getApplicationWindowLabel(const sp<InputApplicationHandle>& applicationHandle,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700369 const sp<InputWindowHandle>& windowHandle);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800370
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800371 std::string checkWindowReadyForMoreInputLocked(nsecs_t currentTime,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700372 const sp<InputWindowHandle>& windowHandle,
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700373 const EventEntry& eventEntry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700374 const char* targetType) REQUIRES(mLock);
Jeff Brownffb49772014-10-10 19:01:34 -0700375
Michael Wrightd02c5b62014-02-10 15:10:22 -0800376 // Manage the dispatch cycle for a single connection.
377 // These methods are deliberately not Interruptible because doing all of the work
378 // with the mutex held makes it easier to ensure that connection invariants are maintained.
379 // If needed, the methods post commands to run later once the critical bits are done.
380 void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -0800381 EventEntry* eventEntry, const InputTarget& inputTarget)
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700382 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800383 void enqueueDispatchEntriesLocked(nsecs_t currentTime, const sp<Connection>& connection,
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -0800384 EventEntry* eventEntry, const InputTarget& inputTarget)
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700385 REQUIRES(mLock);
386 void enqueueDispatchEntryLocked(const sp<Connection>& connection, EventEntry* eventEntry,
Siarhei Vishniakou5d6b6612020-01-08 16:03:04 -0800387 const InputTarget& inputTarget, int32_t dispatchMode)
chaviw8c9cf542019-03-25 13:02:48 -0700388 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800389 void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection)
390 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391 void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700392 uint32_t seq, bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800393 void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700394 bool notify) REQUIRES(mLock);
Siarhei Vishniakou13bda6c2019-07-29 08:34:33 -0700395 void drainDispatchQueue(std::deque<DispatchEntry*>& queue);
Siarhei Vishniakou62683e82019-03-06 17:59:56 -0800396 void releaseDispatchEntry(DispatchEntry* dispatchEntry);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397 static int handleReceiveCallback(int fd, int events, void* data);
chaviw8c9cf542019-03-25 13:02:48 -0700398 // The action sent should only be of type AMOTION_EVENT_*
chaviwfd6d3512019-03-25 13:23:49 -0700399 void dispatchPointerDownOutsideFocus(uint32_t source, int32_t action,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700400 const sp<IBinder>& newToken) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700402 void synthesizeCancelationEventsForAllConnectionsLocked(const CancelationOptions& options)
403 REQUIRES(mLock);
404 void synthesizeCancelationEventsForMonitorsLocked(const CancelationOptions& options)
405 REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800406 void synthesizeCancelationEventsForMonitorsLocked(
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700407 const CancelationOptions& options,
Michael Wright3dd60e22019-03-27 22:06:44 +0000408 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800409 void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700410 const CancelationOptions& options)
411 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800412 void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700413 const CancelationOptions& options)
414 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800415
416 // Splitting motion events across windows.
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700417 MotionEntry* splitMotionEvent(const MotionEntry& originalMotionEntry, BitSet32 pointerIds);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800418
419 // Reset and drop everything the dispatcher is doing.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800420 void resetAndDropEverythingLocked(const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800421
422 // Dump state.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800423 void dumpDispatchStateLocked(std::string& dump) REQUIRES(mLock);
Michael Wright3dd60e22019-03-27 22:06:44 +0000424 void dumpMonitors(std::string& dump, const std::vector<Monitor>& monitors);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800425 void logDispatchStateLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800426
427 // Registration.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800428 void removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) REQUIRES(mLock);
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700429 void removeMonitorChannelLocked(
430 const sp<InputChannel>& inputChannel,
431 std::unordered_map<int32_t, std::vector<Monitor>>& monitorsByDisplay) REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800432 status_t unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, bool notify)
433 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800434
435 // Interesting events that we might like to log or tell the framework about.
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700436 void onDispatchCycleFinishedLocked(nsecs_t currentTime, const sp<Connection>& connection,
437 uint32_t seq, bool handled) REQUIRES(mLock);
438 void onDispatchCycleBrokenLocked(nsecs_t currentTime, const sp<Connection>& connection)
439 REQUIRES(mLock);
chaviw0c06c6e2019-01-09 13:27:07 -0800440 void onFocusChangedLocked(const sp<InputWindowHandle>& oldFocus,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700441 const sp<InputWindowHandle>& newFocus) REQUIRES(mLock);
442 void onANRLocked(nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle,
443 const sp<InputWindowHandle>& windowHandle, nsecs_t eventTime,
444 nsecs_t waitStartTime, const char* reason) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800445
446 // Outbound policy interactions.
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800447 void doNotifyConfigurationChangedLockedInterruptible(CommandEntry* commandEntry)
448 REQUIRES(mLock);
449 void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
450 void doNotifyFocusChangedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
451 void doNotifyANRLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
452 void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry)
453 REQUIRES(mLock);
454 void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800455 bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700456 DispatchEntry* dispatchEntry, KeyEntry* keyEntry,
457 bool handled) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458 bool afterMotionEventLockedInterruptible(const sp<Connection>& connection,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700459 DispatchEntry* dispatchEntry, MotionEntry* motionEntry,
460 bool handled) REQUIRES(mLock);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800461 void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Siarhei Vishniakou9757f782019-10-29 12:53:08 -0700462 KeyEvent createKeyEvent(const KeyEntry& entry);
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700463 void doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800464
465 // Statistics gathering.
Siarhei Vishniakoude4bf152019-08-16 11:12:52 -0500466 static constexpr std::chrono::duration TOUCH_STATS_REPORT_PERIOD = 5min;
467 LatencyStatistics mTouchStatistics{TOUCH_STATS_REPORT_PERIOD};
468
469 void reportTouchEventForStatistics(const MotionEntry& entry);
Siarhei Vishniakoud2770042019-10-29 11:08:14 -0700470 void updateDispatchStatistics(nsecs_t currentTime, const EventEntry& entry,
Garfield Tan0fc2fa72019-08-29 17:22:15 -0700471 int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
Siarhei Vishniakou61291d42019-02-11 18:13:20 -0800472 void traceInboundQueueLengthLocked() REQUIRES(mLock);
473 void traceOutboundQueueLength(const sp<Connection>& connection);
474 void traceWaitQueueLength(const sp<Connection>& connection);
Prabir Pradhanf93562f2018-11-29 12:13:37 -0800475
Prabir Pradhan79a4f0c2019-01-09 11:24:01 -0800476 sp<InputReporterInterface> mReporter;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800477};
478
Garfield Tane84e6f92019-08-29 17:28:41 -0700479} // namespace android::inputdispatcher
Michael Wrightd02c5b62014-02-10 15:10:22 -0800480
481#endif // _UI_INPUT_DISPATCHER_H