1 /* 2 * Copyright (C) 2012 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 #pragma once 18 19 #include <android/sensor.h> 20 #include <ftl/flags.h> 21 #include <ftl/mixins.h> 22 #include <input/Input.h> 23 #include <input/KeyCharacterMap.h> 24 #include <set> 25 #include <unordered_map> 26 #include <vector> 27 28 #include <android/os/IInputConstants.h> 29 30 namespace android { 31 32 /* 33 * Identifies a device. 34 */ 35 struct InputDeviceIdentifier { InputDeviceIdentifierInputDeviceIdentifier36 inline InputDeviceIdentifier() : 37 bus(0), vendor(0), product(0), version(0) { 38 } 39 40 // Information provided by the kernel. 41 std::string name; 42 std::string location; 43 std::string uniqueId; 44 uint16_t bus; 45 uint16_t vendor; 46 uint16_t product; 47 uint16_t version; 48 49 // A composite input device descriptor string that uniquely identifies the device 50 // even across reboots or reconnections. The value of this field is used by 51 // upper layers of the input system to associate settings with individual devices. 52 // It is hashed from whatever kernel provided information is available. 53 // Ideally, the way this value is computed should not change between Android releases 54 // because that would invalidate persistent settings that rely on it. 55 std::string descriptor; 56 57 // A value added to uniquely identify a device in the absence of a unique id. This 58 // is intended to be a minimum way to distinguish from other active devices and may 59 // reuse values that are not associated with an input anymore. 60 uint16_t nonce; 61 62 // The bluetooth address of the device, if known. 63 std::optional<std::string> bluetoothAddress; 64 65 /** 66 * Return InputDeviceIdentifier.name that has been adjusted as follows: 67 * - all characters besides alphanumerics, dash, 68 * and underscore have been replaced with underscores. 69 * This helps in situations where a file that matches the device name is needed, 70 * while conforming to the filename limitations. 71 */ 72 std::string getCanonicalName() const; 73 74 bool operator==(const InputDeviceIdentifier&) const = default; 75 bool operator!=(const InputDeviceIdentifier&) const = default; 76 }; 77 78 /* Types of input device sensors. Keep sync with core/java/android/hardware/Sensor.java */ 79 enum class InputDeviceSensorType : int32_t { 80 ACCELEROMETER = ASENSOR_TYPE_ACCELEROMETER, 81 MAGNETIC_FIELD = ASENSOR_TYPE_MAGNETIC_FIELD, 82 ORIENTATION = 3, 83 GYROSCOPE = ASENSOR_TYPE_GYROSCOPE, 84 LIGHT = ASENSOR_TYPE_LIGHT, 85 PRESSURE = ASENSOR_TYPE_PRESSURE, 86 TEMPERATURE = 7, 87 PROXIMITY = ASENSOR_TYPE_PROXIMITY, 88 GRAVITY = ASENSOR_TYPE_GRAVITY, 89 LINEAR_ACCELERATION = ASENSOR_TYPE_LINEAR_ACCELERATION, 90 ROTATION_VECTOR = ASENSOR_TYPE_ROTATION_VECTOR, 91 RELATIVE_HUMIDITY = ASENSOR_TYPE_RELATIVE_HUMIDITY, 92 AMBIENT_TEMPERATURE = ASENSOR_TYPE_AMBIENT_TEMPERATURE, 93 MAGNETIC_FIELD_UNCALIBRATED = ASENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED, 94 GAME_ROTATION_VECTOR = ASENSOR_TYPE_GAME_ROTATION_VECTOR, 95 GYROSCOPE_UNCALIBRATED = ASENSOR_TYPE_GYROSCOPE_UNCALIBRATED, 96 SIGNIFICANT_MOTION = ASENSOR_TYPE_SIGNIFICANT_MOTION, 97 98 ftl_first = ACCELEROMETER, 99 ftl_last = SIGNIFICANT_MOTION 100 }; 101 102 enum class InputDeviceSensorAccuracy : int32_t { 103 ACCURACY_NONE = 0, 104 ACCURACY_LOW = 1, 105 ACCURACY_MEDIUM = 2, 106 ACCURACY_HIGH = 3, 107 }; 108 109 enum class InputDeviceSensorReportingMode : int32_t { 110 CONTINUOUS = 0, 111 ON_CHANGE = 1, 112 ONE_SHOT = 2, 113 SPECIAL_TRIGGER = 3, 114 }; 115 116 enum class InputDeviceLightType : int32_t { 117 INPUT = 0, 118 PLAYER_ID = 1, 119 KEYBOARD_BACKLIGHT = 2, 120 121 ftl_last = KEYBOARD_BACKLIGHT 122 }; 123 124 enum class InputDeviceLightCapability : uint32_t { 125 /** Capability to change brightness of the light */ 126 BRIGHTNESS = 0x00000001, 127 /** Capability to change color of the light */ 128 RGB = 0x00000002, 129 }; 130 131 struct InputDeviceSensorInfo { InputDeviceSensorInfoInputDeviceSensorInfo132 explicit InputDeviceSensorInfo(std::string name, std::string vendor, int32_t version, 133 InputDeviceSensorType type, InputDeviceSensorAccuracy accuracy, 134 float maxRange, float resolution, float power, int32_t minDelay, 135 int32_t fifoReservedEventCount, int32_t fifoMaxEventCount, 136 std::string stringType, int32_t maxDelay, int32_t flags, 137 int32_t id) 138 : name(name), 139 vendor(vendor), 140 version(version), 141 type(type), 142 accuracy(accuracy), 143 maxRange(maxRange), 144 resolution(resolution), 145 power(power), 146 minDelay(minDelay), 147 fifoReservedEventCount(fifoReservedEventCount), 148 fifoMaxEventCount(fifoMaxEventCount), 149 stringType(stringType), 150 maxDelay(maxDelay), 151 flags(flags), 152 id(id) {} 153 // Name string of the sensor. 154 std::string name; 155 // Vendor string of this sensor. 156 std::string vendor; 157 // Version of the sensor's module. 158 int32_t version; 159 // Generic type of this sensor. 160 InputDeviceSensorType type; 161 // The current accuracy of sensor event. 162 InputDeviceSensorAccuracy accuracy; 163 // Maximum range of the sensor in the sensor's unit. 164 float maxRange; 165 // Resolution of the sensor in the sensor's unit. 166 float resolution; 167 // The power in mA used by this sensor while in use. 168 float power; 169 // The minimum delay allowed between two events in microsecond or zero if this sensor only 170 // returns a value when the data it's measuring changes. 171 int32_t minDelay; 172 // Number of events reserved for this sensor in the batch mode FIFO. 173 int32_t fifoReservedEventCount; 174 // Maximum number of events of this sensor that could be batched. 175 int32_t fifoMaxEventCount; 176 // The type of this sensor as a string. 177 std::string stringType; 178 // The delay between two sensor events corresponding to the lowest frequency that this sensor 179 // supports. 180 int32_t maxDelay; 181 // Sensor flags 182 int32_t flags; 183 // Sensor id, same as the input device ID it belongs to. 184 int32_t id; 185 }; 186 187 struct BrightnessLevel : ftl::DefaultConstructible<BrightnessLevel, std::uint8_t>, 188 ftl::Equatable<BrightnessLevel>, 189 ftl::Orderable<BrightnessLevel>, 190 ftl::Addable<BrightnessLevel> { 191 using DefaultConstructible::DefaultConstructible; 192 }; 193 194 struct InputDeviceLightInfo { InputDeviceLightInfoInputDeviceLightInfo195 explicit InputDeviceLightInfo(std::string name, int32_t id, InputDeviceLightType type, 196 ftl::Flags<InputDeviceLightCapability> capabilityFlags, 197 int32_t ordinal, 198 std::set<BrightnessLevel> preferredBrightnessLevels) 199 : name(name), 200 id(id), 201 type(type), 202 capabilityFlags(capabilityFlags), 203 ordinal(ordinal), 204 preferredBrightnessLevels(std::move(preferredBrightnessLevels)) {} 205 // Name string of the light. 206 std::string name; 207 // Light id 208 int32_t id; 209 // Type of the light. 210 InputDeviceLightType type; 211 // Light capabilities. 212 ftl::Flags<InputDeviceLightCapability> capabilityFlags; 213 // Ordinal of the light 214 int32_t ordinal; 215 // Custom brightness levels for the light 216 std::set<BrightnessLevel> preferredBrightnessLevels; 217 }; 218 219 struct InputDeviceBatteryInfo { InputDeviceBatteryInfoInputDeviceBatteryInfo220 explicit InputDeviceBatteryInfo(std::string name, int32_t id) : name(name), id(id) {} 221 // Name string of the battery. 222 std::string name; 223 // Battery id 224 int32_t id; 225 }; 226 227 struct KeyboardLayoutInfo { KeyboardLayoutInfoKeyboardLayoutInfo228 explicit KeyboardLayoutInfo(std::string languageTag, std::string layoutType) 229 : languageTag(languageTag), layoutType(layoutType) {} 230 231 // A BCP 47 conformant language tag such as "en-US". 232 std::string languageTag; 233 // The layout type such as QWERTY or AZERTY. 234 std::string layoutType; 235 236 inline bool operator==(const KeyboardLayoutInfo& other) const { 237 return languageTag == other.languageTag && layoutType == other.layoutType; 238 } 239 inline bool operator!=(const KeyboardLayoutInfo& other) const { return !(*this == other); } 240 }; 241 242 // The version of the Universal Stylus Initiative (USI) protocol supported by the input device. 243 struct InputDeviceUsiVersion { 244 int32_t majorVersion = -1; 245 int32_t minorVersion = -1; 246 }; 247 248 /* 249 * Describes the characteristics and capabilities of an input device. 250 */ 251 class InputDeviceInfo { 252 public: 253 InputDeviceInfo(); 254 InputDeviceInfo(const InputDeviceInfo& other); 255 ~InputDeviceInfo(); 256 257 struct MotionRange { 258 int32_t axis; 259 uint32_t source; 260 float min; 261 float max; 262 float flat; 263 float fuzz; 264 float resolution; 265 }; 266 267 void initialize(int32_t id, int32_t generation, int32_t controllerNumber, 268 const InputDeviceIdentifier& identifier, const std::string& alias, 269 bool isExternal, bool hasMic, int32_t associatedDisplayId); 270 getId()271 inline int32_t getId() const { return mId; } getControllerNumber()272 inline int32_t getControllerNumber() const { return mControllerNumber; } getGeneration()273 inline int32_t getGeneration() const { return mGeneration; } getIdentifier()274 inline const InputDeviceIdentifier& getIdentifier() const { return mIdentifier; } getAlias()275 inline const std::string& getAlias() const { return mAlias; } getDisplayName()276 inline const std::string& getDisplayName() const { 277 return mAlias.empty() ? mIdentifier.name : mAlias; 278 } isExternal()279 inline bool isExternal() const { return mIsExternal; } hasMic()280 inline bool hasMic() const { return mHasMic; } getSources()281 inline uint32_t getSources() const { return mSources; } 282 283 const MotionRange* getMotionRange(int32_t axis, uint32_t source) const; 284 285 void addSource(uint32_t source); 286 void addMotionRange(int32_t axis, uint32_t source, 287 float min, float max, float flat, float fuzz, float resolution); 288 void addMotionRange(const MotionRange& range); 289 void addSensorInfo(const InputDeviceSensorInfo& info); 290 void addBatteryInfo(const InputDeviceBatteryInfo& info); 291 void addLightInfo(const InputDeviceLightInfo& info); 292 293 void setKeyboardType(int32_t keyboardType); getKeyboardType()294 inline int32_t getKeyboardType() const { return mKeyboardType; } 295 296 void setKeyboardLayoutInfo(KeyboardLayoutInfo keyboardLayoutInfo); getKeyboardLayoutInfo()297 inline const std::optional<KeyboardLayoutInfo>& getKeyboardLayoutInfo() const { 298 return mKeyboardLayoutInfo; 299 } 300 setKeyCharacterMap(const std::shared_ptr<KeyCharacterMap> value)301 inline void setKeyCharacterMap(const std::shared_ptr<KeyCharacterMap> value) { 302 mKeyCharacterMap = value; 303 } 304 getKeyCharacterMap()305 inline const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const { 306 return mKeyCharacterMap; 307 } 308 setVibrator(bool hasVibrator)309 inline void setVibrator(bool hasVibrator) { mHasVibrator = hasVibrator; } hasVibrator()310 inline bool hasVibrator() const { return mHasVibrator; } 311 setHasBattery(bool hasBattery)312 inline void setHasBattery(bool hasBattery) { mHasBattery = hasBattery; } hasBattery()313 inline bool hasBattery() const { return mHasBattery; } 314 setButtonUnderPad(bool hasButton)315 inline void setButtonUnderPad(bool hasButton) { mHasButtonUnderPad = hasButton; } hasButtonUnderPad()316 inline bool hasButtonUnderPad() const { return mHasButtonUnderPad; } 317 setHasSensor(bool hasSensor)318 inline void setHasSensor(bool hasSensor) { mHasSensor = hasSensor; } hasSensor()319 inline bool hasSensor() const { return mHasSensor; } 320 getMotionRanges()321 inline const std::vector<MotionRange>& getMotionRanges() const { 322 return mMotionRanges; 323 } 324 325 std::vector<InputDeviceSensorInfo> getSensors(); 326 327 std::vector<InputDeviceLightInfo> getLights(); 328 setUsiVersion(std::optional<InputDeviceUsiVersion> usiVersion)329 inline void setUsiVersion(std::optional<InputDeviceUsiVersion> usiVersion) { 330 mUsiVersion = std::move(usiVersion); 331 } getUsiVersion()332 inline std::optional<InputDeviceUsiVersion> getUsiVersion() const { return mUsiVersion; } 333 getAssociatedDisplayId()334 inline int32_t getAssociatedDisplayId() const { return mAssociatedDisplayId; } 335 336 private: 337 int32_t mId; 338 int32_t mGeneration; 339 int32_t mControllerNumber; 340 InputDeviceIdentifier mIdentifier; 341 std::string mAlias; 342 bool mIsExternal; 343 bool mHasMic; 344 std::optional<KeyboardLayoutInfo> mKeyboardLayoutInfo; 345 uint32_t mSources; 346 int32_t mKeyboardType; 347 std::shared_ptr<KeyCharacterMap> mKeyCharacterMap; 348 std::optional<InputDeviceUsiVersion> mUsiVersion; 349 int32_t mAssociatedDisplayId; 350 351 bool mHasVibrator; 352 bool mHasBattery; 353 bool mHasButtonUnderPad; 354 bool mHasSensor; 355 356 std::vector<MotionRange> mMotionRanges; 357 std::unordered_map<InputDeviceSensorType, InputDeviceSensorInfo> mSensors; 358 /* Map from light ID to light info */ 359 std::unordered_map<int32_t, InputDeviceLightInfo> mLights; 360 /* Map from battery ID to battery info */ 361 std::unordered_map<int32_t, InputDeviceBatteryInfo> mBatteries; 362 }; 363 364 /* Types of input device configuration files. */ 365 enum class InputDeviceConfigurationFileType : int32_t { 366 CONFIGURATION = 0, /* .idc file */ 367 KEY_LAYOUT = 1, /* .kl file */ 368 KEY_CHARACTER_MAP = 2, /* .kcm file */ 369 }; 370 371 /* 372 * Gets the path of an input device configuration file, if one is available. 373 * Considers both system provided and user installed configuration files. 374 * The optional suffix is appended to the end of the file name (before the 375 * extension). 376 * 377 * The device identifier is used to construct several default configuration file 378 * names to try based on the device name, vendor, product, and version. 379 * 380 * Returns an empty string if not found. 381 */ 382 extern std::string getInputDeviceConfigurationFilePathByDeviceIdentifier( 383 const InputDeviceIdentifier& deviceIdentifier, InputDeviceConfigurationFileType type, 384 const char* suffix = ""); 385 386 /* 387 * Gets the path of an input device configuration file, if one is available. 388 * Considers both system provided and user installed configuration files. 389 * 390 * The name is case-sensitive and is used to construct the filename to resolve. 391 * All characters except 'a'-'z', 'A'-'Z', '0'-'9', '-', and '_' are replaced by underscores. 392 * 393 * Returns an empty string if not found. 394 */ 395 extern std::string getInputDeviceConfigurationFilePathByName( 396 const std::string& name, InputDeviceConfigurationFileType type); 397 398 enum ReservedInputDeviceId : int32_t { 399 // Device id representing an invalid device 400 INVALID_INPUT_DEVICE_ID = android::os::IInputConstants::INVALID_INPUT_DEVICE_ID, 401 // Device id of a special "virtual" keyboard that is always present. 402 VIRTUAL_KEYBOARD_ID = -1, 403 // Device id of the "built-in" keyboard if there is one. 404 BUILT_IN_KEYBOARD_ID = 0, 405 // First device id available for dynamic devices 406 END_RESERVED_ID = 1, 407 }; 408 409 } // namespace android 410