1 /* 2 * Copyright (C) 2019 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_DEVICE_H 18 #define _UI_INPUTREADER_INPUT_DEVICE_H 19 20 #include <input/DisplayViewport.h> 21 #include <input/Flags.h> 22 #include <input/InputDevice.h> 23 #include <input/PropertyMap.h> 24 #include <stdint.h> 25 26 #include <optional> 27 #include <unordered_map> 28 #include <vector> 29 30 #include "EventHub.h" 31 #include "InputReaderBase.h" 32 #include "InputReaderContext.h" 33 34 namespace android { 35 // TODO b/180733860 support multiple battery in API and remove this. 36 constexpr int32_t DEFAULT_BATTERY_ID = 1; 37 38 class PeripheralController; 39 class PeripheralControllerInterface; 40 class InputDeviceContext; 41 class InputMapper; 42 43 /* Represents the state of a single input device. */ 44 class InputDevice { 45 public: 46 InputDevice(InputReaderContext* context, int32_t id, int32_t generation, 47 const InputDeviceIdentifier& identifier); 48 ~InputDevice(); 49 getContext()50 inline InputReaderContext* getContext() { return mContext; } getId()51 inline int32_t getId() const { return mId; } getControllerNumber()52 inline int32_t getControllerNumber() const { return mControllerNumber; } getGeneration()53 inline int32_t getGeneration() const { return mGeneration; } getName()54 inline const std::string getName() const { return mIdentifier.name; } getDescriptor()55 inline const std::string getDescriptor() { return mIdentifier.descriptor; } getClasses()56 inline Flags<InputDeviceClass> getClasses() const { return mClasses; } getSources()57 inline uint32_t getSources() const { return mSources; } hasEventHubDevices()58 inline bool hasEventHubDevices() const { return !mDevices.empty(); } 59 isExternal()60 inline bool isExternal() { return mIsExternal; } getAssociatedDisplayPort()61 inline std::optional<uint8_t> getAssociatedDisplayPort() const { 62 return mAssociatedDisplayPort; 63 } getAssociatedViewport()64 inline std::optional<DisplayViewport> getAssociatedViewport() const { 65 return mAssociatedViewport; 66 } hasMic()67 inline bool hasMic() const { return mHasMic; } 68 isIgnored()69 inline bool isIgnored() { return !getMapperCount(); } 70 71 bool isEnabled(); 72 void setEnabled(bool enabled, nsecs_t when); 73 74 void dump(std::string& dump, const std::string& eventHubDevStr); 75 void addEventHubDevice(int32_t eventHubId, bool populateMappers = true); 76 void removeEventHubDevice(int32_t eventHubId); 77 void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes); 78 void reset(nsecs_t when); 79 void process(const RawEvent* rawEvents, size_t count); 80 void timeoutExpired(nsecs_t when); 81 void updateExternalStylusState(const StylusState& state); 82 83 InputDeviceInfo getDeviceInfo(); 84 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode); 85 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode); 86 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode); 87 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, const int32_t* keyCodes, 88 uint8_t* outFlags); 89 void vibrate(const VibrationSequence& sequence, ssize_t repeat, int32_t token); 90 void cancelVibrate(int32_t token); 91 bool isVibrating(); 92 std::vector<int32_t> getVibratorIds(); 93 void cancelTouch(nsecs_t when, nsecs_t readTime); 94 bool enableSensor(InputDeviceSensorType sensorType, std::chrono::microseconds samplingPeriod, 95 std::chrono::microseconds maxBatchReportLatency); 96 void disableSensor(InputDeviceSensorType sensorType); 97 void flushSensor(InputDeviceSensorType sensorType); 98 99 std::optional<int32_t> getBatteryCapacity(); 100 std::optional<int32_t> getBatteryStatus(); 101 102 bool setLightColor(int32_t lightId, int32_t color); 103 bool setLightPlayerId(int32_t lightId, int32_t playerId); 104 std::optional<int32_t> getLightColor(int32_t lightId); 105 std::optional<int32_t> getLightPlayerId(int32_t lightId); 106 107 int32_t getMetaState(); 108 void updateMetaState(int32_t keyCode); 109 110 void bumpGeneration(); 111 112 void notifyReset(nsecs_t when); 113 getConfiguration()114 inline const PropertyMap& getConfiguration() { return mConfiguration; } getEventHub()115 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); } 116 117 std::optional<int32_t> getAssociatedDisplayId(); 118 119 void updateLedState(bool reset); 120 121 size_t getMapperCount(); 122 123 // construct and add a mapper to the input device 124 template <class T, typename... Args> addMapper(int32_t eventHubId,Args...args)125 T& addMapper(int32_t eventHubId, Args... args) { 126 // ensure a device entry exists for this eventHubId 127 addEventHubDevice(eventHubId, false); 128 129 // create mapper 130 auto& devicePair = mDevices[eventHubId]; 131 auto& deviceContext = devicePair.first; 132 auto& mappers = devicePair.second; 133 T* mapper = new T(*deviceContext, args...); 134 mappers.emplace_back(mapper); 135 return *mapper; 136 } 137 138 // construct and add a controller to the input device 139 template <class T> addController(int32_t eventHubId)140 T& addController(int32_t eventHubId) { 141 // ensure a device entry exists for this eventHubId 142 addEventHubDevice(eventHubId, false); 143 144 // create controller 145 auto& devicePair = mDevices[eventHubId]; 146 auto& deviceContext = devicePair.first; 147 148 mController = std::make_unique<T>(*deviceContext); 149 return *(reinterpret_cast<T*>(mController.get())); 150 } 151 152 private: 153 InputReaderContext* mContext; 154 int32_t mId; 155 int32_t mGeneration; 156 int32_t mControllerNumber; 157 InputDeviceIdentifier mIdentifier; 158 std::string mAlias; 159 Flags<InputDeviceClass> mClasses; 160 161 // map from eventHubId to device context and mappers 162 using MapperVector = std::vector<std::unique_ptr<InputMapper>>; 163 using DevicePair = std::pair<std::unique_ptr<InputDeviceContext>, MapperVector>; 164 // Map from EventHub ID to pair of device context and vector of mapper. 165 std::unordered_map<int32_t, DevicePair> mDevices; 166 // Misc devices controller for lights, battery, etc. 167 std::unique_ptr<PeripheralControllerInterface> mController; 168 169 uint32_t mSources; 170 bool mIsExternal; 171 std::optional<uint8_t> mAssociatedDisplayPort; 172 std::optional<std::string> mAssociatedDisplayUniqueId; 173 std::optional<DisplayViewport> mAssociatedViewport; 174 bool mHasMic; 175 bool mDropUntilNextSync; 176 177 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code); 178 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc); 179 180 PropertyMap mConfiguration; 181 182 // helpers to interate over the devices collection 183 // run a function against every mapper on every subdevice for_each_mapper(std::function<void (InputMapper &)> f)184 inline void for_each_mapper(std::function<void(InputMapper&)> f) { 185 for (auto& deviceEntry : mDevices) { 186 auto& devicePair = deviceEntry.second; 187 auto& mappers = devicePair.second; 188 for (auto& mapperPtr : mappers) { 189 f(*mapperPtr); 190 } 191 } 192 } 193 194 // run a function against every mapper on a specific subdevice for_each_mapper_in_subdevice(int32_t eventHubDevice,std::function<void (InputMapper &)> f)195 inline void for_each_mapper_in_subdevice(int32_t eventHubDevice, 196 std::function<void(InputMapper&)> f) { 197 auto deviceIt = mDevices.find(eventHubDevice); 198 if (deviceIt != mDevices.end()) { 199 auto& devicePair = deviceIt->second; 200 auto& mappers = devicePair.second; 201 for (auto& mapperPtr : mappers) { 202 f(*mapperPtr); 203 } 204 } 205 } 206 207 // run a function against every subdevice for_each_subdevice(std::function<void (InputDeviceContext &)> f)208 inline void for_each_subdevice(std::function<void(InputDeviceContext&)> f) { 209 for (auto& deviceEntry : mDevices) { 210 auto& devicePair = deviceEntry.second; 211 auto& contextPtr = devicePair.first; 212 f(*contextPtr); 213 } 214 } 215 216 // return the first value returned by a function over every mapper. 217 // if all mappers return nullopt, return nullopt. 218 template <typename T> first_in_mappers(std::function<std::optional<T> (InputMapper &)> f)219 inline std::optional<T> first_in_mappers(std::function<std::optional<T>(InputMapper&)> f) { 220 for (auto& deviceEntry : mDevices) { 221 auto& devicePair = deviceEntry.second; 222 auto& mappers = devicePair.second; 223 for (auto& mapperPtr : mappers) { 224 std::optional<T> ret = f(*mapperPtr); 225 if (ret) { 226 return ret; 227 } 228 } 229 } 230 return std::nullopt; 231 } 232 }; 233 234 /* Provides access to EventHub methods, but limits access to the current InputDevice. 235 * Essentially an implementation of EventHubInterface, but for a specific device id. 236 * Helps hide implementation details of InputDevice and EventHub. Used by mappers to 237 * check the status of the associated hardware device 238 */ 239 class InputDeviceContext { 240 public: 241 InputDeviceContext(InputDevice& device, int32_t eventHubId); 242 ~InputDeviceContext(); 243 getContext()244 inline InputReaderContext* getContext() { return mContext; } getId()245 inline int32_t getId() { return mDeviceId; } getEventHubId()246 inline int32_t getEventHubId() { return mId; } 247 getDeviceClasses()248 inline Flags<InputDeviceClass> getDeviceClasses() const { 249 return mEventHub->getDeviceClasses(mId); 250 } getDeviceIdentifier()251 inline InputDeviceIdentifier getDeviceIdentifier() const { 252 return mEventHub->getDeviceIdentifier(mId); 253 } getDeviceControllerNumber()254 inline int32_t getDeviceControllerNumber() const { 255 return mEventHub->getDeviceControllerNumber(mId); 256 } getConfiguration(PropertyMap * outConfiguration)257 inline void getConfiguration(PropertyMap* outConfiguration) const { 258 return mEventHub->getConfiguration(mId, outConfiguration); 259 } getAbsoluteAxisInfo(int32_t code,RawAbsoluteAxisInfo * axisInfo)260 inline status_t getAbsoluteAxisInfo(int32_t code, RawAbsoluteAxisInfo* axisInfo) const { 261 return mEventHub->getAbsoluteAxisInfo(mId, code, axisInfo); 262 } hasRelativeAxis(int32_t code)263 inline bool hasRelativeAxis(int32_t code) const { 264 return mEventHub->hasRelativeAxis(mId, code); 265 } hasInputProperty(int32_t property)266 inline bool hasInputProperty(int32_t property) const { 267 return mEventHub->hasInputProperty(mId, property); 268 } 269 hasMscEvent(int mscEvent)270 inline bool hasMscEvent(int mscEvent) const { return mEventHub->hasMscEvent(mId, mscEvent); } 271 mapKey(int32_t scanCode,int32_t usageCode,int32_t metaState,int32_t * outKeycode,int32_t * outMetaState,uint32_t * outFlags)272 inline status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t metaState, 273 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const { 274 return mEventHub->mapKey(mId, scanCode, usageCode, metaState, outKeycode, outMetaState, 275 outFlags); 276 } mapAxis(int32_t scanCode,AxisInfo * outAxisInfo)277 inline status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const { 278 return mEventHub->mapAxis(mId, scanCode, outAxisInfo); 279 } mapSensor(int32_t absCode)280 inline base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t absCode) { 281 return mEventHub->mapSensor(mId, absCode); 282 } 283 getRawLightIds()284 inline const std::vector<int32_t> getRawLightIds() { return mEventHub->getRawLightIds(mId); } 285 getRawLightInfo(int32_t lightId)286 inline std::optional<RawLightInfo> getRawLightInfo(int32_t lightId) { 287 return mEventHub->getRawLightInfo(mId, lightId); 288 } 289 getLightBrightness(int32_t lightId)290 inline std::optional<int32_t> getLightBrightness(int32_t lightId) { 291 return mEventHub->getLightBrightness(mId, lightId); 292 } 293 setLightBrightness(int32_t lightId,int32_t brightness)294 inline void setLightBrightness(int32_t lightId, int32_t brightness) { 295 return mEventHub->setLightBrightness(mId, lightId, brightness); 296 } 297 getLightIntensities(int32_t lightId)298 inline std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities( 299 int32_t lightId) { 300 return mEventHub->getLightIntensities(mId, lightId); 301 } 302 setLightIntensities(int32_t lightId,std::unordered_map<LightColor,int32_t> intensities)303 inline void setLightIntensities(int32_t lightId, 304 std::unordered_map<LightColor, int32_t> intensities) { 305 return mEventHub->setLightIntensities(mId, lightId, intensities); 306 } 307 getVideoFrames()308 inline std::vector<TouchVideoFrame> getVideoFrames() { return mEventHub->getVideoFrames(mId); } getScanCodeState(int32_t scanCode)309 inline int32_t getScanCodeState(int32_t scanCode) const { 310 return mEventHub->getScanCodeState(mId, scanCode); 311 } getKeyCodeState(int32_t keyCode)312 inline int32_t getKeyCodeState(int32_t keyCode) const { 313 return mEventHub->getKeyCodeState(mId, keyCode); 314 } getSwitchState(int32_t sw)315 inline int32_t getSwitchState(int32_t sw) const { return mEventHub->getSwitchState(mId, sw); } getAbsoluteAxisValue(int32_t code,int32_t * outValue)316 inline status_t getAbsoluteAxisValue(int32_t code, int32_t* outValue) const { 317 return mEventHub->getAbsoluteAxisValue(mId, code, outValue); 318 } markSupportedKeyCodes(size_t numCodes,const int32_t * keyCodes,uint8_t * outFlags)319 inline bool markSupportedKeyCodes(size_t numCodes, const int32_t* keyCodes, 320 uint8_t* outFlags) const { 321 return mEventHub->markSupportedKeyCodes(mId, numCodes, keyCodes, outFlags); 322 } hasScanCode(int32_t scanCode)323 inline bool hasScanCode(int32_t scanCode) const { 324 return mEventHub->hasScanCode(mId, scanCode); 325 } hasLed(int32_t led)326 inline bool hasLed(int32_t led) const { return mEventHub->hasLed(mId, led); } setLedState(int32_t led,bool on)327 inline void setLedState(int32_t led, bool on) { return mEventHub->setLedState(mId, led, on); } getVirtualKeyDefinitions(std::vector<VirtualKeyDefinition> & outVirtualKeys)328 inline void getVirtualKeyDefinitions(std::vector<VirtualKeyDefinition>& outVirtualKeys) const { 329 return mEventHub->getVirtualKeyDefinitions(mId, outVirtualKeys); 330 } getKeyCharacterMap()331 inline const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const { 332 return mEventHub->getKeyCharacterMap(mId); 333 } setKeyboardLayoutOverlay(std::shared_ptr<KeyCharacterMap> map)334 inline bool setKeyboardLayoutOverlay(std::shared_ptr<KeyCharacterMap> map) { 335 return mEventHub->setKeyboardLayoutOverlay(mId, map); 336 } vibrate(const VibrationElement & element)337 inline void vibrate(const VibrationElement& element) { 338 return mEventHub->vibrate(mId, element); 339 } cancelVibrate()340 inline void cancelVibrate() { return mEventHub->cancelVibrate(mId); } 341 getVibratorIds()342 inline std::vector<int32_t> getVibratorIds() { return mEventHub->getVibratorIds(mId); } 343 getRawBatteryIds()344 inline const std::vector<int32_t> getRawBatteryIds() { 345 return mEventHub->getRawBatteryIds(mId); 346 } 347 getRawBatteryInfo(int32_t batteryId)348 inline std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t batteryId) { 349 return mEventHub->getRawBatteryInfo(mId, batteryId); 350 } 351 getBatteryCapacity(int32_t batteryId)352 inline std::optional<int32_t> getBatteryCapacity(int32_t batteryId) { 353 return mEventHub->getBatteryCapacity(mId, batteryId); 354 } 355 getBatteryStatus(int32_t batteryId)356 inline std::optional<int32_t> getBatteryStatus(int32_t batteryId) { 357 return mEventHub->getBatteryStatus(mId, batteryId); 358 } 359 hasAbsoluteAxis(int32_t code)360 inline bool hasAbsoluteAxis(int32_t code) const { 361 RawAbsoluteAxisInfo info; 362 mEventHub->getAbsoluteAxisInfo(mId, code, &info); 363 return info.valid; 364 } isKeyPressed(int32_t code)365 inline bool isKeyPressed(int32_t code) const { 366 return mEventHub->getScanCodeState(mId, code) == AKEY_STATE_DOWN; 367 } getAbsoluteAxisValue(int32_t code)368 inline int32_t getAbsoluteAxisValue(int32_t code) const { 369 int32_t value; 370 mEventHub->getAbsoluteAxisValue(mId, code, &value); 371 return value; 372 } isDeviceEnabled()373 inline bool isDeviceEnabled() { return mEventHub->isDeviceEnabled(mId); } enableDevice()374 inline status_t enableDevice() { return mEventHub->enableDevice(mId); } disableDevice()375 inline status_t disableDevice() { return mEventHub->disableDevice(mId); } 376 getName()377 inline const std::string getName() { return mDevice.getName(); } getDescriptor()378 inline const std::string getDescriptor() { return mDevice.getDescriptor(); } isExternal()379 inline bool isExternal() { return mDevice.isExternal(); } getAssociatedDisplayPort()380 inline std::optional<uint8_t> getAssociatedDisplayPort() const { 381 return mDevice.getAssociatedDisplayPort(); 382 } getAssociatedViewport()383 inline std::optional<DisplayViewport> getAssociatedViewport() const { 384 return mDevice.getAssociatedViewport(); 385 } cancelTouch(nsecs_t when,nsecs_t readTime)386 inline void cancelTouch(nsecs_t when, nsecs_t readTime) { mDevice.cancelTouch(when, readTime); } bumpGeneration()387 inline void bumpGeneration() { mDevice.bumpGeneration(); } getConfiguration()388 inline const PropertyMap& getConfiguration() { return mDevice.getConfiguration(); } 389 390 private: 391 InputDevice& mDevice; 392 InputReaderContext* mContext; 393 EventHubInterface* mEventHub; 394 int32_t mId; 395 int32_t mDeviceId; 396 }; 397 398 } // namespace android 399 400 #endif //_UI_INPUTREADER_INPUT_DEVICE_H 401