blob: 4f2503ad5cf509684a109daa3cd92e2e3a111519 [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
Michael Wright17db18e2020-06-26 20:51:44 +010019#include <PointerControllerInterface.h>
Chris Ye1c2e0892020-11-30 21:41:44 -080020#include <android-base/thread_annotations.h>
Michael Wright17db18e2020-06-26 20:51:44 +010021#include <utils/Condition.h>
22#include <utils/Mutex.h>
23
24#include <memory>
25#include <unordered_map>
26#include <vector>
27
Michael Wrightd02c5b62014-02-10 15:10:22 -080028#include "EventHub.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080029#include "InputListener.h"
Prabir Pradhan29c95332018-11-14 20:14:11 -080030#include "InputReaderBase.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070031#include "InputReaderContext.h"
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070032#include "InputThread.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080033
Michael Wrightd02c5b62014-02-10 15:10:22 -080034namespace android {
35
36class InputDevice;
37class InputMapper;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070038struct StylusState;
Michael Wrightd02c5b62014-02-10 15:10:22 -080039
Michael Wrightd02c5b62014-02-10 15:10:22 -080040/* The input reader reads raw event data from the event hub and processes it into input events
41 * that it sends to the input listener. Some functions of the input reader, such as early
42 * event filtering in low power states, are controlled by a separate policy object.
43 *
Prabir Pradhan28efc192019-11-05 01:10:04 +000044 * The InputReader owns a collection of InputMappers. InputReader starts its own thread, where
45 * most of the work happens, but the InputReader can receive queries from other system
Michael Wrightd02c5b62014-02-10 15:10:22 -080046 * components running on arbitrary threads. To keep things manageable, the InputReader
47 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
48 * EventHub or the InputReaderPolicy but it is never held while calling into the
Prabir Pradhan28efc192019-11-05 01:10:04 +000049 * InputListener. All calls to InputListener must happen from InputReader's thread.
Michael Wrightd02c5b62014-02-10 15:10:22 -080050 */
51class InputReader : public InputReaderInterface {
52public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070053 InputReader(std::shared_ptr<EventHubInterface> eventHub,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070054 const sp<InputReaderPolicyInterface>& policy, InputListenerInterface& listener);
Michael Wrightd02c5b62014-02-10 15:10:22 -080055 virtual ~InputReader();
56
arthurhungc903df12020-08-11 15:08:42 +080057 void dump(std::string& dump) override;
58 void monitor() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080059
arthurhungc903df12020-08-11 15:08:42 +080060 status_t start() override;
61 status_t stop() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080062
Chris Ye98d3f532020-10-01 21:48:59 -070063 std::vector<InputDeviceInfo> getInputDevices() const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080064
arthurhungc903df12020-08-11 15:08:42 +080065 bool isInputDeviceEnabled(int32_t deviceId) override;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070066
arthurhungc903df12020-08-11 15:08:42 +080067 int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) override;
68 int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) override;
69 int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t sw) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080070
Philip Junker4af3b3d2021-12-14 10:36:55 +010071 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override;
72
arthurhungc903df12020-08-11 15:08:42 +080073 void toggleCapsLockState(int32_t deviceId) override;
Andrii Kulian763a3a42016-03-08 10:46:16 -080074
Siarhei Vishniakou74007942022-06-13 13:57:47 -070075 bool hasKeys(int32_t deviceId, uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
arthurhungc903df12020-08-11 15:08:42 +080076 uint8_t* outFlags) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080077
arthurhungc903df12020-08-11 15:08:42 +080078 void requestRefreshConfiguration(uint32_t changes) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080079
Chris Ye87143712020-11-10 05:05:58 +000080 void vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
arthurhungc903df12020-08-11 15:08:42 +080081 int32_t token) override;
82 void cancelVibrate(int32_t deviceId, int32_t token) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080083
Chris Ye87143712020-11-10 05:05:58 +000084 bool isVibrating(int32_t deviceId) override;
85
86 std::vector<int32_t> getVibratorIds(int32_t deviceId) override;
87
arthurhungc903df12020-08-11 15:08:42 +080088 bool canDispatchToDisplay(int32_t deviceId, int32_t displayId) override;
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070089
Chris Yef59a2f42020-10-16 12:55:26 -070090 bool enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
91 std::chrono::microseconds samplingPeriod,
92 std::chrono::microseconds maxBatchReportLatency) override;
93
94 void disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) override;
95
96 void flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) override;
97
Kim Low03ea0352020-11-06 12:45:07 -080098 std::optional<int32_t> getBatteryCapacity(int32_t deviceId) override;
99
100 std::optional<int32_t> getBatteryStatus(int32_t deviceId) override;
101
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000102 std::optional<std::string> getBatteryDevicePath(int32_t deviceId) override;
103
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000104 std::vector<InputDeviceLightInfo> getLights(int32_t deviceId) override;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800105
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000106 std::vector<InputDeviceSensorInfo> getSensors(int32_t deviceId) override;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800107
108 bool setLightColor(int32_t deviceId, int32_t lightId, int32_t color) override;
109
110 bool setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) override;
111
112 std::optional<int32_t> getLightColor(int32_t deviceId, int32_t lightId) override;
113
114 std::optional<int32_t> getLightPlayerId(int32_t deviceId, int32_t lightId) override;
115
Prabir Pradhanb54ffb22022-10-27 18:03:34 +0000116 std::optional<std::string> getBluetoothAddress(int32_t deviceId) const override;
117
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118protected:
119 // These members are protected so they can be instrumented by test cases.
Chris Ye1c2e0892020-11-30 21:41:44 -0800120 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t deviceId,
121 const InputDeviceIdentifier& identifier)
122 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800123
Prabir Pradhan28efc192019-11-05 01:10:04 +0000124 // With each iteration of the loop, InputReader reads and processes one incoming message from
125 // the EventHub.
126 void loopOnce();
127
Michael Wrightd02c5b62014-02-10 15:10:22 -0800128 class ContextImpl : public InputReaderContext {
129 InputReader* mReader;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800130 IdGenerator mIdGenerator;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800131
132 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700133 explicit ContextImpl(InputReader* reader);
Chris Ye1c2e0892020-11-30 21:41:44 -0800134 // lock is already held by the input loop
135 void updateGlobalMetaState() NO_THREAD_SAFETY_ANALYSIS override;
136 int32_t getGlobalMetaState() NO_THREAD_SAFETY_ANALYSIS override;
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000137 void disableVirtualKeysUntil(nsecs_t time) REQUIRES(mReader->mLock) override;
138 bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode)
139 REQUIRES(mReader->mLock) override;
140 void fadePointer() REQUIRES(mReader->mLock) override;
Chris Ye1c2e0892020-11-30 21:41:44 -0800141 std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId)
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000142 REQUIRES(mReader->mLock) override;
143 void requestTimeoutAtTime(nsecs_t when) REQUIRES(mReader->mLock) override;
Chris Ye1c2e0892020-11-30 21:41:44 -0800144 int32_t bumpGeneration() NO_THREAD_SAFETY_ANALYSIS override;
145 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices)
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000146 REQUIRES(mReader->mLock) override;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700147 [[nodiscard]] std::list<NotifyArgs> dispatchExternalStylusState(const StylusState& outState)
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000148 REQUIRES(mReader->mLock) override;
149 InputReaderPolicyInterface* getPolicy() REQUIRES(mReader->mLock) override;
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000150 EventHubInterface* getEventHub() REQUIRES(mReader->mLock) override;
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000151 int32_t getNextId() NO_THREAD_SAFETY_ANALYSIS override;
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000152 void updateLedMetaState(int32_t metaState) REQUIRES(mReader->mLock) override;
153 int32_t getLedMetaState() REQUIRES(mReader->mLock) REQUIRES(mLock) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 } mContext;
155
156 friend class ContextImpl;
Chris Ye1c2e0892020-11-30 21:41:44 -0800157 // Test cases need to override the locked functions
158 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800159
160private:
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700161 std::unique_ptr<InputThread> mThread;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000162
Chris Ye1c2e0892020-11-30 21:41:44 -0800163 std::condition_variable mReaderIsAliveCondition;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700165 // This could be unique_ptr, but due to the way InputReader tests are written,
166 // it is made shared_ptr here. In the tests, an EventHub reference is retained by the test
167 // in parallel to passing it to the InputReader.
168 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169 sp<InputReaderPolicyInterface> mPolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700170 QueuedInputListener mQueuedListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171
Chris Ye1c2e0892020-11-30 21:41:44 -0800172 InputReaderConfiguration mConfig GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800174 // An input device can represent a collection of EventHub devices. This map provides a way
175 // to lookup the input device instance from the EventHub device id.
Chris Ye1c2e0892020-11-30 21:41:44 -0800176 std::unordered_map<int32_t /*eventHubId*/, std::shared_ptr<InputDevice>> mDevices
177 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178
Chris Yee7310032020-09-22 15:36:28 -0700179 // An input device contains one or more eventHubId, this map provides a way to lookup the
180 // EventHubIds contained in the input device from the input device instance.
181 std::unordered_map<std::shared_ptr<InputDevice>, std::vector<int32_t> /*eventHubId*/>
Chris Ye1c2e0892020-11-30 21:41:44 -0800182 mDeviceToEventHubIdsMap GUARDED_BY(mLock);
Chris Yee7310032020-09-22 15:36:28 -0700183
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184 // low-level input event decoding and device management
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700185 [[nodiscard]] std::list<NotifyArgs> processEventsLocked(const RawEvent* rawEvents, size_t count)
186 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187
Chris Ye1c2e0892020-11-30 21:41:44 -0800188 void addDeviceLocked(nsecs_t when, int32_t eventHubId) REQUIRES(mLock);
189 void removeDeviceLocked(nsecs_t when, int32_t eventHubId) REQUIRES(mLock);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700190 [[nodiscard]] std::list<NotifyArgs> processEventsForDeviceLocked(int32_t eventHubId,
191 const RawEvent* rawEvents,
192 size_t count) REQUIRES(mLock);
193 [[nodiscard]] std::list<NotifyArgs> timeoutExpiredLocked(nsecs_t when) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800194
Chris Ye1c2e0892020-11-30 21:41:44 -0800195 void handleConfigurationChangedLocked(nsecs_t when) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800196
Chris Ye1c2e0892020-11-30 21:41:44 -0800197 int32_t mGlobalMetaState GUARDED_BY(mLock);
198 void updateGlobalMetaStateLocked() REQUIRES(mLock);
199 int32_t getGlobalMetaStateLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800200
Chris Ye1c2e0892020-11-30 21:41:44 -0800201 int32_t mLedMetaState GUARDED_BY(mLock);
202 void updateLedMetaStateLocked(int32_t metaState) REQUIRES(mLock);
203 int32_t getLedMetaStateLocked() REQUIRES(mLock);
arthurhungc903df12020-08-11 15:08:42 +0800204
Chris Ye1c2e0892020-11-30 21:41:44 -0800205 void notifyExternalStylusPresenceChangedLocked() REQUIRES(mLock);
206 void getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) REQUIRES(mLock);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700207 [[nodiscard]] std::list<NotifyArgs> dispatchExternalStylusStateLocked(const StylusState& state)
208 REQUIRES(mLock);
Michael Wright842500e2015-03-13 17:32:02 -0700209
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800210 // The PointerController that is shared among all the input devices that need it.
Michael Wright17db18e2020-06-26 20:51:44 +0100211 std::weak_ptr<PointerControllerInterface> mPointerController;
Chris Ye1c2e0892020-11-30 21:41:44 -0800212 std::shared_ptr<PointerControllerInterface> getPointerControllerLocked(int32_t deviceId)
213 REQUIRES(mLock);
214 void updatePointerDisplayLocked() REQUIRES(mLock);
215 void fadePointerLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216
Chris Ye1c2e0892020-11-30 21:41:44 -0800217 int32_t mGeneration GUARDED_BY(mLock);
218 int32_t bumpGenerationLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219
Chris Ye1c2e0892020-11-30 21:41:44 -0800220 int32_t mNextInputDeviceId GUARDED_BY(mLock);
221 int32_t nextInputDeviceIdLocked() REQUIRES(mLock);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800222
Chris Ye1c2e0892020-11-30 21:41:44 -0800223 std::vector<InputDeviceInfo> getInputDevicesLocked() const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800224
Chris Ye1c2e0892020-11-30 21:41:44 -0800225 nsecs_t mDisableVirtualKeysTimeout GUARDED_BY(mLock);
226 void disableVirtualKeysUntilLocked(nsecs_t time) REQUIRES(mLock);
227 bool shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800228
Chris Ye1c2e0892020-11-30 21:41:44 -0800229 nsecs_t mNextTimeout GUARDED_BY(mLock);
230 void requestTimeoutAtTimeLocked(nsecs_t when) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231
Chris Ye1c2e0892020-11-30 21:41:44 -0800232 uint32_t mConfigurationChangesToRefresh GUARDED_BY(mLock);
233 void refreshConfigurationLocked(uint32_t changes) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700235 void notifyAll(std::list<NotifyArgs>&& argsList);
236
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000237 PointerCaptureRequest mCurrentPointerCaptureRequest GUARDED_BY(mLock);
238
Michael Wrightd02c5b62014-02-10 15:10:22 -0800239 // state queries
240 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
241 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Chris Ye1c2e0892020-11-30 21:41:44 -0800242 GetStateFunc getStateFunc) REQUIRES(mLock);
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700243 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
244 const std::vector<int32_t>& keyCodes, uint8_t* outFlags)
245 REQUIRES(mLock);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800246
247 // find an InputDevice from an InputDevice id
Philip Junker4af3b3d2021-12-14 10:36:55 +0100248 InputDevice* findInputDeviceLocked(int32_t deviceId) const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249};
250
Michael Wrightd02c5b62014-02-10 15:10:22 -0800251} // namespace android