blob: 7614a05470d5ca11d9f778c4197f5f0268fb1a18 [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
Prabir Pradhan48108662022-09-09 21:22:04 +000017#pragma once
Michael Wrightd02c5b62014-02-10 15:10:22 -080018
Chris Ye1c2e0892020-11-30 21:41:44 -080019#include <android-base/thread_annotations.h>
Michael Wright17db18e2020-06-26 20:51:44 +010020#include <utils/Condition.h>
21#include <utils/Mutex.h>
22
23#include <memory>
24#include <unordered_map>
25#include <vector>
26
Michael Wrightd02c5b62014-02-10 15:10:22 -080027#include "EventHub.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include "InputListener.h"
Prabir Pradhan29c95332018-11-14 20:14:11 -080029#include "InputReaderBase.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070030#include "InputReaderContext.h"
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070031#include "InputThread.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080032
Michael Wrightd02c5b62014-02-10 15:10:22 -080033namespace android {
34
35class InputDevice;
36class InputMapper;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070037struct StylusState;
Michael Wrightd02c5b62014-02-10 15:10:22 -080038
Michael Wrightd02c5b62014-02-10 15:10:22 -080039/* The input reader reads raw event data from the event hub and processes it into input events
40 * that it sends to the input listener. Some functions of the input reader, such as early
41 * event filtering in low power states, are controlled by a separate policy object.
42 *
Prabir Pradhan28efc192019-11-05 01:10:04 +000043 * The InputReader owns a collection of InputMappers. InputReader starts its own thread, where
44 * most of the work happens, but the InputReader can receive queries from other system
Michael Wrightd02c5b62014-02-10 15:10:22 -080045 * components running on arbitrary threads. To keep things manageable, the InputReader
46 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
47 * EventHub or the InputReaderPolicy but it is never held while calling into the
Prabir Pradhan28efc192019-11-05 01:10:04 +000048 * InputListener. All calls to InputListener must happen from InputReader's thread.
Michael Wrightd02c5b62014-02-10 15:10:22 -080049 */
50class InputReader : public InputReaderInterface {
51public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070052 InputReader(std::shared_ptr<EventHubInterface> eventHub,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070053 const sp<InputReaderPolicyInterface>& policy, InputListenerInterface& listener);
Michael Wrightd02c5b62014-02-10 15:10:22 -080054 virtual ~InputReader();
55
arthurhungc903df12020-08-11 15:08:42 +080056 void dump(std::string& dump) override;
57 void monitor() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080058
arthurhungc903df12020-08-11 15:08:42 +080059 status_t start() override;
60 status_t stop() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080061
Chris Ye98d3f532020-10-01 21:48:59 -070062 std::vector<InputDeviceInfo> getInputDevices() const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080063
arthurhungc903df12020-08-11 15:08:42 +080064 int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) override;
65 int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) override;
66 int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t sw) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080067
Philip Junker4af3b3d2021-12-14 10:36:55 +010068 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override;
69
arthurhungc903df12020-08-11 15:08:42 +080070 void toggleCapsLockState(int32_t deviceId) override;
Andrii Kulian763a3a42016-03-08 10:46:16 -080071
Siarhei Vishniakou74007942022-06-13 13:57:47 -070072 bool hasKeys(int32_t deviceId, uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
arthurhungc903df12020-08-11 15:08:42 +080073 uint8_t* outFlags) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080074
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000075 void requestRefreshConfiguration(ConfigurationChanges changes) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080076
Chris Ye87143712020-11-10 05:05:58 +000077 void vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
arthurhungc903df12020-08-11 15:08:42 +080078 int32_t token) override;
79 void cancelVibrate(int32_t deviceId, int32_t token) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080080
Chris Ye87143712020-11-10 05:05:58 +000081 bool isVibrating(int32_t deviceId) override;
82
83 std::vector<int32_t> getVibratorIds(int32_t deviceId) override;
84
Linnan Li13bf76a2024-05-05 19:18:02 +080085 bool canDispatchToDisplay(int32_t deviceId, ui::LogicalDisplayId displayId) override;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070086
Chris Yef59a2f42020-10-16 12:55:26 -070087 bool enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
88 std::chrono::microseconds samplingPeriod,
89 std::chrono::microseconds maxBatchReportLatency) override;
90
91 void disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) override;
92
93 void flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) override;
94
Kim Low03ea0352020-11-06 12:45:07 -080095 std::optional<int32_t> getBatteryCapacity(int32_t deviceId) override;
96
97 std::optional<int32_t> getBatteryStatus(int32_t deviceId) override;
98
Prabir Pradhane287ecd2022-09-07 21:18:05 +000099 std::optional<std::string> getBatteryDevicePath(int32_t deviceId) override;
100
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000101 std::vector<InputDeviceLightInfo> getLights(int32_t deviceId) override;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800102
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000103 std::vector<InputDeviceSensorInfo> getSensors(int32_t deviceId) override;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800104
Omar Abdelmonem5e70e962024-08-06 09:38:42 +0000105 std::optional<HardwareProperties> getTouchpadHardwareProperties(int32_t deviceId) override;
106
Chris Ye3fdbfef2021-01-06 18:45:18 -0800107 bool setLightColor(int32_t deviceId, int32_t lightId, int32_t color) override;
108
109 bool setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) override;
110
111 std::optional<int32_t> getLightColor(int32_t deviceId, int32_t lightId) override;
112
113 std::optional<int32_t> getLightPlayerId(int32_t deviceId, int32_t lightId) override;
114
Prabir Pradhanb54ffb22022-10-27 18:03:34 +0000115 std::optional<std::string> getBluetoothAddress(int32_t deviceId) const override;
116
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000117 void sysfsNodeChanged(const std::string& sysfsNodePath) override;
118
Prabir Pradhan018faea2024-05-08 21:52:54 +0000119 DeviceId getLastUsedInputDeviceId() override;
120
Arpit Singh849beb42024-06-06 07:14:17 +0000121 void notifyMouseCursorFadedOnTyping() override;
122
Yanye Li81a590c2024-10-22 19:25:54 +0000123 bool setKernelWakeEnabled(int32_t deviceId, bool enabled) override;
124
Michael Wrightd02c5b62014-02-10 15:10:22 -0800125protected:
126 // These members are protected so they can be instrumented by test cases.
Arpit Singh82f29a12023-06-13 15:05:53 +0000127 virtual std::shared_ptr<InputDevice> createDeviceLocked(nsecs_t when, int32_t deviceId,
Chris Ye1c2e0892020-11-30 21:41:44 -0800128 const InputDeviceIdentifier& identifier)
129 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800130
Prabir Pradhan28efc192019-11-05 01:10:04 +0000131 // With each iteration of the loop, InputReader reads and processes one incoming message from
132 // the EventHub.
133 void loopOnce();
134
Michael Wrightd02c5b62014-02-10 15:10:22 -0800135 class ContextImpl : public InputReaderContext {
136 InputReader* mReader;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800137 IdGenerator mIdGenerator;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800138
139 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700140 explicit ContextImpl(InputReader* reader);
Chris Ye1c2e0892020-11-30 21:41:44 -0800141 // lock is already held by the input loop
142 void updateGlobalMetaState() NO_THREAD_SAFETY_ANALYSIS override;
143 int32_t getGlobalMetaState() NO_THREAD_SAFETY_ANALYSIS override;
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000144 void disableVirtualKeysUntil(nsecs_t time) REQUIRES(mReader->mLock) override;
145 bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode)
146 REQUIRES(mReader->mLock) override;
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000147 void requestTimeoutAtTime(nsecs_t when) REQUIRES(mReader->mLock) override;
Chris Ye1c2e0892020-11-30 21:41:44 -0800148 int32_t bumpGeneration() NO_THREAD_SAFETY_ANALYSIS override;
149 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices)
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000150 REQUIRES(mReader->mLock) override;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700151 [[nodiscard]] std::list<NotifyArgs> dispatchExternalStylusState(const StylusState& outState)
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000152 REQUIRES(mReader->mLock) override;
153 InputReaderPolicyInterface* getPolicy() REQUIRES(mReader->mLock) override;
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000154 EventHubInterface* getEventHub() REQUIRES(mReader->mLock) override;
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000155 int32_t getNextId() NO_THREAD_SAFETY_ANALYSIS override;
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000156 void updateLedMetaState(int32_t metaState) REQUIRES(mReader->mLock) override;
157 int32_t getLedMetaState() REQUIRES(mReader->mLock) REQUIRES(mLock) override;
Arpit Singha5ea7c12023-07-05 15:39:25 +0000158 void setPreventingTouchpadTaps(bool prevent) REQUIRES(mReader->mLock)
159 REQUIRES(mLock) override;
160 bool isPreventingTouchpadTaps() REQUIRES(mReader->mLock) REQUIRES(mLock) override;
Arpit Singh82e413e2023-10-10 19:30:58 +0000161 void setLastKeyDownTimestamp(nsecs_t when) REQUIRES(mReader->mLock)
162 REQUIRES(mLock) override;
163 nsecs_t getLastKeyDownTimestamp() REQUIRES(mReader->mLock) REQUIRES(mLock) override;
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000164 KeyboardClassifier& getKeyboardClassifier() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165 } mContext;
166
167 friend class ContextImpl;
Chris Ye1c2e0892020-11-30 21:41:44 -0800168 // Test cases need to override the locked functions
169 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170
171private:
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700172 std::unique_ptr<InputThread> mThread;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000173
Chris Ye1c2e0892020-11-30 21:41:44 -0800174 std::condition_variable mReaderIsAliveCondition;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700176 // This could be unique_ptr, but due to the way InputReader tests are written,
177 // it is made shared_ptr here. In the tests, an EventHub reference is retained by the test
178 // in parallel to passing it to the InputReader.
179 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180 sp<InputReaderPolicyInterface> mPolicy;
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700181
182 // The next stage that should receive the events generated inside InputReader.
183 InputListenerInterface& mNextListener;
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000184
185 // Classifier for keyboard/keyboard-like devices
186 std::unique_ptr<KeyboardClassifier> mKeyboardClassifier;
187
Siarhei Vishniakou23a98bf2023-08-15 17:28:49 -0700188 // As various events are generated inside InputReader, they are stored inside this list. The
189 // list can only be accessed with the lock, so the events inside it are well-ordered.
190 // Once the reader is done working, these events will be swapped into a temporary storage and
191 // sent to the 'mNextListener' without holding the lock.
192 std::list<NotifyArgs> mPendingArgs GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193
Chris Ye1c2e0892020-11-30 21:41:44 -0800194 InputReaderConfiguration mConfig GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800196 // An input device can represent a collection of EventHub devices. This map provides a way
197 // to lookup the input device instance from the EventHub device id.
Chris Ye1c2e0892020-11-30 21:41:44 -0800198 std::unordered_map<int32_t /*eventHubId*/, std::shared_ptr<InputDevice>> mDevices
199 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800200
Chris Yee7310032020-09-22 15:36:28 -0700201 // An input device contains one or more eventHubId, this map provides a way to lookup the
202 // EventHubIds contained in the input device from the input device instance.
203 std::unordered_map<std::shared_ptr<InputDevice>, std::vector<int32_t> /*eventHubId*/>
Chris Ye1c2e0892020-11-30 21:41:44 -0800204 mDeviceToEventHubIdsMap GUARDED_BY(mLock);
Chris Yee7310032020-09-22 15:36:28 -0700205
Arpit Singh849beb42024-06-06 07:14:17 +0000206 // true if tap-to-click on touchpad is currently disabled
Arpit Singha5ea7c12023-07-05 15:39:25 +0000207 bool mPreventingTouchpadTaps GUARDED_BY(mLock){false};
208
Arpit Singh82e413e2023-10-10 19:30:58 +0000209 // records timestamp of the last key press on the physical keyboard
210 nsecs_t mLastKeyDownTimestamp GUARDED_BY(mLock){0};
211
Prabir Pradhan018faea2024-05-08 21:52:54 +0000212 // The input device that produced a new gesture most recently.
213 DeviceId mLastUsedDeviceId GUARDED_BY(mLock){ReservedInputDeviceId::INVALID_INPUT_DEVICE_ID};
214
Michael Wrightd02c5b62014-02-10 15:10:22 -0800215 // low-level input event decoding and device management
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700216 [[nodiscard]] std::list<NotifyArgs> processEventsLocked(const RawEvent* rawEvents, size_t count)
217 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218
Chris Ye1c2e0892020-11-30 21:41:44 -0800219 void addDeviceLocked(nsecs_t when, int32_t eventHubId) REQUIRES(mLock);
220 void removeDeviceLocked(nsecs_t when, int32_t eventHubId) REQUIRES(mLock);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700221 [[nodiscard]] std::list<NotifyArgs> processEventsForDeviceLocked(int32_t eventHubId,
222 const RawEvent* rawEvents,
223 size_t count) REQUIRES(mLock);
224 [[nodiscard]] std::list<NotifyArgs> timeoutExpiredLocked(nsecs_t when) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225
Chris Ye1c2e0892020-11-30 21:41:44 -0800226 int32_t mGlobalMetaState GUARDED_BY(mLock);
227 void updateGlobalMetaStateLocked() REQUIRES(mLock);
228 int32_t getGlobalMetaStateLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800229
Chris Ye1c2e0892020-11-30 21:41:44 -0800230 int32_t mLedMetaState GUARDED_BY(mLock);
231 void updateLedMetaStateLocked(int32_t metaState) REQUIRES(mLock);
232 int32_t getLedMetaStateLocked() REQUIRES(mLock);
arthurhungc903df12020-08-11 15:08:42 +0800233
Chris Ye1c2e0892020-11-30 21:41:44 -0800234 void notifyExternalStylusPresenceChangedLocked() REQUIRES(mLock);
235 void getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) REQUIRES(mLock);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700236 [[nodiscard]] std::list<NotifyArgs> dispatchExternalStylusStateLocked(const StylusState& state)
237 REQUIRES(mLock);
Michael Wright842500e2015-03-13 17:32:02 -0700238
Chris Ye1c2e0892020-11-30 21:41:44 -0800239 int32_t mGeneration GUARDED_BY(mLock);
240 int32_t bumpGenerationLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800241
Chris Ye1c2e0892020-11-30 21:41:44 -0800242 int32_t mNextInputDeviceId GUARDED_BY(mLock);
243 int32_t nextInputDeviceIdLocked() REQUIRES(mLock);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800244
Chris Ye1c2e0892020-11-30 21:41:44 -0800245 std::vector<InputDeviceInfo> getInputDevicesLocked() const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800246
Chris Ye1c2e0892020-11-30 21:41:44 -0800247 nsecs_t mDisableVirtualKeysTimeout GUARDED_BY(mLock);
248 void disableVirtualKeysUntilLocked(nsecs_t time) REQUIRES(mLock);
249 bool shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250
Chris Ye1c2e0892020-11-30 21:41:44 -0800251 nsecs_t mNextTimeout GUARDED_BY(mLock);
252 void requestTimeoutAtTimeLocked(nsecs_t when) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800253
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000254 ConfigurationChanges mConfigurationChangesToRefresh GUARDED_BY(mLock);
255 void refreshConfigurationLocked(ConfigurationChanges changes) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000257 PointerCaptureRequest mCurrentPointerCaptureRequest GUARDED_BY(mLock);
258
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 // state queries
260 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
261 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Chris Ye1c2e0892020-11-30 21:41:44 -0800262 GetStateFunc getStateFunc) REQUIRES(mLock);
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700263 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
264 const std::vector<int32_t>& keyCodes, uint8_t* outFlags)
265 REQUIRES(mLock);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800266
267 // find an InputDevice from an InputDevice id
Philip Junker4af3b3d2021-12-14 10:36:55 +0100268 InputDevice* findInputDeviceLocked(int32_t deviceId) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269};
270
Michael Wrightd02c5b62014-02-10 15:10:22 -0800271} // namespace android