blob: 81e3e9ab242c3aabd4d3cc073fd25b3c861782c4 [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 Pradhanbaa5c822019-08-30 15:27:05 -070017#ifndef _UI_INPUTREADER_INPUT_READER_H
18#define _UI_INPUTREADER_INPUT_READER_H
Michael Wrightd02c5b62014-02-10 15:10:22 -080019
Michael Wright17db18e2020-06-26 20:51:44 +010020#include <PointerControllerInterface.h>
Chris Ye1c2e0892020-11-30 21:41:44 -080021#include <android-base/thread_annotations.h>
Michael Wright17db18e2020-06-26 20:51:44 +010022#include <utils/Condition.h>
23#include <utils/Mutex.h>
24
25#include <memory>
26#include <unordered_map>
27#include <vector>
28
Michael Wrightd02c5b62014-02-10 15:10:22 -080029#include "EventHub.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080030#include "InputListener.h"
Prabir Pradhan29c95332018-11-14 20:14:11 -080031#include "InputReaderBase.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070032#include "InputReaderContext.h"
Prabir Pradhan5a57cff2019-10-31 18:40:33 -070033#include "InputThread.h"
Michael Wrightd02c5b62014-02-10 15:10:22 -080034
Michael Wrightd02c5b62014-02-10 15:10:22 -080035namespace android {
36
37class InputDevice;
38class InputMapper;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070039struct StylusState;
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Michael Wrightd02c5b62014-02-10 15:10:22 -080041/* The input reader reads raw event data from the event hub and processes it into input events
42 * that it sends to the input listener. Some functions of the input reader, such as early
43 * event filtering in low power states, are controlled by a separate policy object.
44 *
Prabir Pradhan28efc192019-11-05 01:10:04 +000045 * The InputReader owns a collection of InputMappers. InputReader starts its own thread, where
46 * most of the work happens, but the InputReader can receive queries from other system
Michael Wrightd02c5b62014-02-10 15:10:22 -080047 * components running on arbitrary threads. To keep things manageable, the InputReader
48 * uses a single Mutex to guard its state. The Mutex may be held while calling into the
49 * EventHub or the InputReaderPolicy but it is never held while calling into the
Prabir Pradhan28efc192019-11-05 01:10:04 +000050 * InputListener. All calls to InputListener must happen from InputReader's thread.
Michael Wrightd02c5b62014-02-10 15:10:22 -080051 */
52class InputReader : public InputReaderInterface {
53public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -070054 InputReader(std::shared_ptr<EventHubInterface> eventHub,
55 const sp<InputReaderPolicyInterface>& policy,
56 const sp<InputListenerInterface>& listener);
Michael Wrightd02c5b62014-02-10 15:10:22 -080057 virtual ~InputReader();
58
arthurhungc903df12020-08-11 15:08:42 +080059 void dump(std::string& dump) override;
60 void monitor() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080061
arthurhungc903df12020-08-11 15:08:42 +080062 status_t start() override;
63 status_t stop() override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080064
Chris Ye98d3f532020-10-01 21:48:59 -070065 std::vector<InputDeviceInfo> getInputDevices() const override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080066
arthurhungc903df12020-08-11 15:08:42 +080067 bool isInputDeviceEnabled(int32_t deviceId) override;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -070068
arthurhungc903df12020-08-11 15:08:42 +080069 int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) override;
70 int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) override;
71 int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t sw) override;
Michael Wrightd02c5b62014-02-10 15:10:22 -080072
arthurhungc903df12020-08-11 15:08:42 +080073 void toggleCapsLockState(int32_t deviceId) override;
Andrii Kulian763a3a42016-03-08 10:46:16 -080074
arthurhungc903df12020-08-11 15:08:42 +080075 bool hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes, const int32_t* keyCodes,
76 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
Michael Wrightd02c5b62014-02-10 15:10:22 -0800102protected:
103 // These members are protected so they can be instrumented by test cases.
Chris Ye1c2e0892020-11-30 21:41:44 -0800104 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t deviceId,
105 const InputDeviceIdentifier& identifier)
106 REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107
Prabir Pradhan28efc192019-11-05 01:10:04 +0000108 // With each iteration of the loop, InputReader reads and processes one incoming message from
109 // the EventHub.
110 void loopOnce();
111
Michael Wrightd02c5b62014-02-10 15:10:22 -0800112 class ContextImpl : public InputReaderContext {
113 InputReader* mReader;
Garfield Tanff1f1bb2020-01-28 13:24:04 -0800114 IdGenerator mIdGenerator;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115
116 public:
Chih-Hung Hsieh6d2ede12016-09-01 11:28:23 -0700117 explicit ContextImpl(InputReader* reader);
Chris Ye1c2e0892020-11-30 21:41:44 -0800118 // lock is already held by the input loop
119 void updateGlobalMetaState() NO_THREAD_SAFETY_ANALYSIS override;
120 int32_t getGlobalMetaState() NO_THREAD_SAFETY_ANALYSIS override;
121 void disableVirtualKeysUntil(nsecs_t time) NO_THREAD_SAFETY_ANALYSIS override;
122 bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode,
123 int32_t scanCode) NO_THREAD_SAFETY_ANALYSIS override;
124 void fadePointer() NO_THREAD_SAFETY_ANALYSIS override;
125 std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId)
126 NO_THREAD_SAFETY_ANALYSIS override;
127 void requestTimeoutAtTime(nsecs_t when) NO_THREAD_SAFETY_ANALYSIS override;
128 int32_t bumpGeneration() NO_THREAD_SAFETY_ANALYSIS override;
129 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices)
130 NO_THREAD_SAFETY_ANALYSIS override;
131 void dispatchExternalStylusState(const StylusState& outState)
132 NO_THREAD_SAFETY_ANALYSIS override;
133 InputReaderPolicyInterface* getPolicy() NO_THREAD_SAFETY_ANALYSIS override;
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000134 InputListenerInterface* getListener() NO_THREAD_SAFETY_ANALYSIS override;
Chris Ye1c2e0892020-11-30 21:41:44 -0800135 EventHubInterface* getEventHub() NO_THREAD_SAFETY_ANALYSIS override;
Siarhei Vishniakouf2f073b2021-02-09 21:59:56 +0000136 int32_t getNextId() NO_THREAD_SAFETY_ANALYSIS override;
Chris Ye1c2e0892020-11-30 21:41:44 -0800137 void updateLedMetaState(int32_t metaState) NO_THREAD_SAFETY_ANALYSIS override;
138 int32_t getLedMetaState() NO_THREAD_SAFETY_ANALYSIS override;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800139 } mContext;
140
141 friend class ContextImpl;
Chris Ye1c2e0892020-11-30 21:41:44 -0800142 // Test cases need to override the locked functions
143 mutable std::mutex mLock;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144
145private:
Prabir Pradhan5a57cff2019-10-31 18:40:33 -0700146 std::unique_ptr<InputThread> mThread;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000147
Chris Ye1c2e0892020-11-30 21:41:44 -0800148 std::condition_variable mReaderIsAliveCondition;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700150 // This could be unique_ptr, but due to the way InputReader tests are written,
151 // it is made shared_ptr here. In the tests, an EventHub reference is retained by the test
152 // in parallel to passing it to the InputReader.
153 std::shared_ptr<EventHubInterface> mEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 sp<InputReaderPolicyInterface> mPolicy;
155 sp<QueuedInputListener> mQueuedListener;
156
Chris Ye1c2e0892020-11-30 21:41:44 -0800157 InputReaderConfiguration mConfig GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800158
159 // The event queue.
160 static const int EVENT_BUFFER_SIZE = 256;
Chris Ye1c2e0892020-11-30 21:41:44 -0800161 RawEvent mEventBuffer[EVENT_BUFFER_SIZE] GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800162
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800163 // An input device can represent a collection of EventHub devices. This map provides a way
164 // to lookup the input device instance from the EventHub device id.
Chris Ye1c2e0892020-11-30 21:41:44 -0800165 std::unordered_map<int32_t /*eventHubId*/, std::shared_ptr<InputDevice>> mDevices
166 GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167
Chris Yee7310032020-09-22 15:36:28 -0700168 // An input device contains one or more eventHubId, this map provides a way to lookup the
169 // EventHubIds contained in the input device from the input device instance.
170 std::unordered_map<std::shared_ptr<InputDevice>, std::vector<int32_t> /*eventHubId*/>
Chris Ye1c2e0892020-11-30 21:41:44 -0800171 mDeviceToEventHubIdsMap GUARDED_BY(mLock);
Chris Yee7310032020-09-22 15:36:28 -0700172
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173 // low-level input event decoding and device management
Chris Ye1c2e0892020-11-30 21:41:44 -0800174 void processEventsLocked(const RawEvent* rawEvents, size_t count) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175
Chris Ye1c2e0892020-11-30 21:41:44 -0800176 void addDeviceLocked(nsecs_t when, int32_t eventHubId) REQUIRES(mLock);
177 void removeDeviceLocked(nsecs_t when, int32_t eventHubId) REQUIRES(mLock);
178 void processEventsForDeviceLocked(int32_t eventHubId, const RawEvent* rawEvents, size_t count)
179 REQUIRES(mLock);
180 void timeoutExpiredLocked(nsecs_t when) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181
Chris Ye1c2e0892020-11-30 21:41:44 -0800182 void handleConfigurationChangedLocked(nsecs_t when) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183
Chris Ye1c2e0892020-11-30 21:41:44 -0800184 int32_t mGlobalMetaState GUARDED_BY(mLock);
185 void updateGlobalMetaStateLocked() REQUIRES(mLock);
186 int32_t getGlobalMetaStateLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187
Chris Ye1c2e0892020-11-30 21:41:44 -0800188 int32_t mLedMetaState GUARDED_BY(mLock);
189 void updateLedMetaStateLocked(int32_t metaState) REQUIRES(mLock);
190 int32_t getLedMetaStateLocked() REQUIRES(mLock);
arthurhungc903df12020-08-11 15:08:42 +0800191
Chris Ye1c2e0892020-11-30 21:41:44 -0800192 void notifyExternalStylusPresenceChangedLocked() REQUIRES(mLock);
193 void getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) REQUIRES(mLock);
Michael Wright842500e2015-03-13 17:32:02 -0700194 void dispatchExternalStylusState(const StylusState& state);
195
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -0800196 // The PointerController that is shared among all the input devices that need it.
Michael Wright17db18e2020-06-26 20:51:44 +0100197 std::weak_ptr<PointerControllerInterface> mPointerController;
Chris Ye1c2e0892020-11-30 21:41:44 -0800198 std::shared_ptr<PointerControllerInterface> getPointerControllerLocked(int32_t deviceId)
199 REQUIRES(mLock);
200 void updatePointerDisplayLocked() REQUIRES(mLock);
201 void fadePointerLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800202
Chris Ye1c2e0892020-11-30 21:41:44 -0800203 int32_t mGeneration GUARDED_BY(mLock);
204 int32_t bumpGenerationLocked() REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800205
Chris Ye1c2e0892020-11-30 21:41:44 -0800206 int32_t mNextInputDeviceId GUARDED_BY(mLock);
207 int32_t nextInputDeviceIdLocked() REQUIRES(mLock);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800208
Chris Ye1c2e0892020-11-30 21:41:44 -0800209 std::vector<InputDeviceInfo> getInputDevicesLocked() const REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800210
Chris Ye1c2e0892020-11-30 21:41:44 -0800211 nsecs_t mDisableVirtualKeysTimeout GUARDED_BY(mLock);
212 void disableVirtualKeysUntilLocked(nsecs_t time) REQUIRES(mLock);
213 bool shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800214
Chris Ye1c2e0892020-11-30 21:41:44 -0800215 nsecs_t mNextTimeout GUARDED_BY(mLock);
216 void requestTimeoutAtTimeLocked(nsecs_t when) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217
Chris Ye1c2e0892020-11-30 21:41:44 -0800218 uint32_t mConfigurationChangesToRefresh GUARDED_BY(mLock);
219 void refreshConfigurationLocked(uint32_t changes) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800220
221 // state queries
222 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
223 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Chris Ye1c2e0892020-11-30 21:41:44 -0800224 GetStateFunc getStateFunc) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
Chris Ye1c2e0892020-11-30 21:41:44 -0800226 const int32_t* keyCodes, uint8_t* outFlags) REQUIRES(mLock);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800227
228 // find an InputDevice from an InputDevice id
Chris Ye1c2e0892020-11-30 21:41:44 -0800229 InputDevice* findInputDeviceLocked(int32_t deviceId) REQUIRES(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230};
231
Michael Wrightd02c5b62014-02-10 15:10:22 -0800232} // namespace android
233
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700234#endif // _UI_INPUTREADER_INPUT_READER_H