1 /* 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_INPUTREADER_INPUT_READER_H 18 #define _UI_INPUTREADER_INPUT_READER_H 19 20 #include <PointerControllerInterface.h> 21 #include <android-base/thread_annotations.h> 22 #include <utils/Condition.h> 23 #include <utils/Mutex.h> 24 25 #include <memory> 26 #include <unordered_map> 27 #include <vector> 28 29 #include "EventHub.h" 30 #include "InputListener.h" 31 #include "InputReaderBase.h" 32 #include "InputReaderContext.h" 33 #include "InputThread.h" 34 35 namespace android { 36 37 class InputDevice; 38 class InputMapper; 39 struct StylusState; 40 41 /* 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 * 45 * 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 47 * 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 50 * InputListener. All calls to InputListener must happen from InputReader's thread. 51 */ 52 class InputReader : public InputReaderInterface { 53 public: 54 InputReader(std::shared_ptr<EventHubInterface> eventHub, 55 const sp<InputReaderPolicyInterface>& policy, 56 const sp<InputListenerInterface>& listener); 57 virtual ~InputReader(); 58 59 void dump(std::string& dump) override; 60 void monitor() override; 61 62 status_t start() override; 63 status_t stop() override; 64 65 std::vector<InputDeviceInfo> getInputDevices() const override; 66 67 bool isInputDeviceEnabled(int32_t deviceId) override; 68 69 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; 72 73 void toggleCapsLockState(int32_t deviceId) override; 74 75 bool hasKeys(int32_t deviceId, uint32_t sourceMask, size_t numCodes, const int32_t* keyCodes, 76 uint8_t* outFlags) override; 77 78 void requestRefreshConfiguration(uint32_t changes) override; 79 80 void vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat, 81 int32_t token) override; 82 void cancelVibrate(int32_t deviceId, int32_t token) override; 83 84 bool isVibrating(int32_t deviceId) override; 85 86 std::vector<int32_t> getVibratorIds(int32_t deviceId) override; 87 88 bool canDispatchToDisplay(int32_t deviceId, int32_t displayId) override; 89 90 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 98 std::optional<int32_t> getBatteryCapacity(int32_t deviceId) override; 99 100 std::optional<int32_t> getBatteryStatus(int32_t deviceId) override; 101 102 std::vector<InputDeviceLightInfo> getLights(int32_t deviceId) override; 103 104 std::vector<InputDeviceSensorInfo> getSensors(int32_t deviceId) override; 105 106 bool setLightColor(int32_t deviceId, int32_t lightId, int32_t color) override; 107 108 bool setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) override; 109 110 std::optional<int32_t> getLightColor(int32_t deviceId, int32_t lightId) override; 111 112 std::optional<int32_t> getLightPlayerId(int32_t deviceId, int32_t lightId) override; 113 114 protected: 115 // These members are protected so they can be instrumented by test cases. 116 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t deviceId, 117 const InputDeviceIdentifier& identifier) 118 REQUIRES(mLock); 119 120 // With each iteration of the loop, InputReader reads and processes one incoming message from 121 // the EventHub. 122 void loopOnce(); 123 124 class ContextImpl : public InputReaderContext { 125 InputReader* mReader; 126 IdGenerator mIdGenerator; 127 128 public: 129 explicit ContextImpl(InputReader* reader); 130 // lock is already held by the input loop 131 void updateGlobalMetaState() NO_THREAD_SAFETY_ANALYSIS override; 132 int32_t getGlobalMetaState() NO_THREAD_SAFETY_ANALYSIS override; 133 void disableVirtualKeysUntil(nsecs_t time) REQUIRES(mReader->mLock) override; 134 bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) 135 REQUIRES(mReader->mLock) override; 136 void fadePointer() REQUIRES(mReader->mLock) override; 137 std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) 138 REQUIRES(mReader->mLock) override; 139 void requestTimeoutAtTime(nsecs_t when) REQUIRES(mReader->mLock) override; 140 int32_t bumpGeneration() NO_THREAD_SAFETY_ANALYSIS override; 141 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) 142 REQUIRES(mReader->mLock) override; 143 void dispatchExternalStylusState(const StylusState& outState) 144 REQUIRES(mReader->mLock) override; 145 InputReaderPolicyInterface* getPolicy() REQUIRES(mReader->mLock) override; 146 InputListenerInterface* getListener() REQUIRES(mReader->mLock) override; 147 EventHubInterface* getEventHub() REQUIRES(mReader->mLock) override; 148 int32_t getNextId() NO_THREAD_SAFETY_ANALYSIS override; 149 void updateLedMetaState(int32_t metaState) REQUIRES(mReader->mLock) override; 150 int32_t getLedMetaState() REQUIRES(mReader->mLock) REQUIRES(mLock) override; 151 } mContext; 152 153 friend class ContextImpl; 154 // Test cases need to override the locked functions 155 mutable std::mutex mLock; 156 157 private: 158 std::unique_ptr<InputThread> mThread; 159 160 std::condition_variable mReaderIsAliveCondition; 161 162 // This could be unique_ptr, but due to the way InputReader tests are written, 163 // it is made shared_ptr here. In the tests, an EventHub reference is retained by the test 164 // in parallel to passing it to the InputReader. 165 std::shared_ptr<EventHubInterface> mEventHub; 166 sp<InputReaderPolicyInterface> mPolicy; 167 sp<QueuedInputListener> mQueuedListener; 168 169 InputReaderConfiguration mConfig GUARDED_BY(mLock); 170 171 // The event queue. 172 static const int EVENT_BUFFER_SIZE = 256; 173 RawEvent mEventBuffer[EVENT_BUFFER_SIZE] GUARDED_BY(mLock); 174 175 // An input device can represent a collection of EventHub devices. This map provides a way 176 // to lookup the input device instance from the EventHub device id. 177 std::unordered_map<int32_t /*eventHubId*/, std::shared_ptr<InputDevice>> mDevices 178 GUARDED_BY(mLock); 179 180 // An input device contains one or more eventHubId, this map provides a way to lookup the 181 // EventHubIds contained in the input device from the input device instance. 182 std::unordered_map<std::shared_ptr<InputDevice>, std::vector<int32_t> /*eventHubId*/> 183 mDeviceToEventHubIdsMap GUARDED_BY(mLock); 184 185 // low-level input event decoding and device management 186 void processEventsLocked(const RawEvent* rawEvents, size_t count) REQUIRES(mLock); 187 188 void addDeviceLocked(nsecs_t when, int32_t eventHubId) REQUIRES(mLock); 189 void removeDeviceLocked(nsecs_t when, int32_t eventHubId) REQUIRES(mLock); 190 void processEventsForDeviceLocked(int32_t eventHubId, const RawEvent* rawEvents, size_t count) 191 REQUIRES(mLock); 192 void timeoutExpiredLocked(nsecs_t when) REQUIRES(mLock); 193 194 void handleConfigurationChangedLocked(nsecs_t when) REQUIRES(mLock); 195 196 int32_t mGlobalMetaState GUARDED_BY(mLock); 197 void updateGlobalMetaStateLocked() REQUIRES(mLock); 198 int32_t getGlobalMetaStateLocked() REQUIRES(mLock); 199 200 int32_t mLedMetaState GUARDED_BY(mLock); 201 void updateLedMetaStateLocked(int32_t metaState) REQUIRES(mLock); 202 int32_t getLedMetaStateLocked() REQUIRES(mLock); 203 204 void notifyExternalStylusPresenceChangedLocked() REQUIRES(mLock); 205 void getExternalStylusDevicesLocked(std::vector<InputDeviceInfo>& outDevices) REQUIRES(mLock); 206 void dispatchExternalStylusStateLocked(const StylusState& state) REQUIRES(mLock); 207 208 // The PointerController that is shared among all the input devices that need it. 209 std::weak_ptr<PointerControllerInterface> mPointerController; 210 std::shared_ptr<PointerControllerInterface> getPointerControllerLocked(int32_t deviceId) 211 REQUIRES(mLock); 212 void updatePointerDisplayLocked() REQUIRES(mLock); 213 void fadePointerLocked() REQUIRES(mLock); 214 215 int32_t mGeneration GUARDED_BY(mLock); 216 int32_t bumpGenerationLocked() REQUIRES(mLock); 217 218 int32_t mNextInputDeviceId GUARDED_BY(mLock); 219 int32_t nextInputDeviceIdLocked() REQUIRES(mLock); 220 221 std::vector<InputDeviceInfo> getInputDevicesLocked() const REQUIRES(mLock); 222 223 nsecs_t mDisableVirtualKeysTimeout GUARDED_BY(mLock); 224 void disableVirtualKeysUntilLocked(nsecs_t time) REQUIRES(mLock); 225 bool shouldDropVirtualKeyLocked(nsecs_t now, int32_t keyCode, int32_t scanCode) REQUIRES(mLock); 226 227 nsecs_t mNextTimeout GUARDED_BY(mLock); 228 void requestTimeoutAtTimeLocked(nsecs_t when) REQUIRES(mLock); 229 230 uint32_t mConfigurationChangesToRefresh GUARDED_BY(mLock); 231 void refreshConfigurationLocked(uint32_t changes) REQUIRES(mLock); 232 233 // state queries 234 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code); 235 int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code, 236 GetStateFunc getStateFunc) REQUIRES(mLock); 237 bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes, 238 const int32_t* keyCodes, uint8_t* outFlags) REQUIRES(mLock); 239 240 // find an InputDevice from an InputDevice id 241 InputDevice* findInputDeviceLocked(int32_t deviceId) REQUIRES(mLock); 242 }; 243 244 } // namespace android 245 246 #endif // _UI_INPUTREADER_INPUT_READER_H 247