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 /** 79 * Holds View related behaviors for an InputDevice. 80 */ 81 struct InputDeviceViewBehavior { 82 /** 83 * The smooth scroll behavior that applies for all source/axis, if defined by the device. 84 * Empty optional if the device has not specified the default smooth scroll behavior. 85 */ 86 std::optional<bool> shouldSmoothScroll; 87 }; 88 89 /* Types of input device sensors. Keep sync with core/java/android/hardware/Sensor.java */ 90 enum class InputDeviceSensorType : int32_t { 91 ACCELEROMETER = ASENSOR_TYPE_ACCELEROMETER, 92 MAGNETIC_FIELD = ASENSOR_TYPE_MAGNETIC_FIELD, 93 ORIENTATION = 3, 94 GYROSCOPE = ASENSOR_TYPE_GYROSCOPE, 95 LIGHT = ASENSOR_TYPE_LIGHT, 96 PRESSURE = ASENSOR_TYPE_PRESSURE, 97 TEMPERATURE = 7, 98 PROXIMITY = ASENSOR_TYPE_PROXIMITY, 99 GRAVITY = ASENSOR_TYPE_GRAVITY, 100 LINEAR_ACCELERATION = ASENSOR_TYPE_LINEAR_ACCELERATION, 101 ROTATION_VECTOR = ASENSOR_TYPE_ROTATION_VECTOR, 102 RELATIVE_HUMIDITY = ASENSOR_TYPE_RELATIVE_HUMIDITY, 103 AMBIENT_TEMPERATURE = ASENSOR_TYPE_AMBIENT_TEMPERATURE, 104 MAGNETIC_FIELD_UNCALIBRATED = ASENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED, 105 GAME_ROTATION_VECTOR = ASENSOR_TYPE_GAME_ROTATION_VECTOR, 106 GYROSCOPE_UNCALIBRATED = ASENSOR_TYPE_GYROSCOPE_UNCALIBRATED, 107 SIGNIFICANT_MOTION = ASENSOR_TYPE_SIGNIFICANT_MOTION, 108 109 ftl_first = ACCELEROMETER, 110 ftl_last = SIGNIFICANT_MOTION 111 }; 112 113 enum class InputDeviceSensorAccuracy : int32_t { 114 ACCURACY_NONE = 0, 115 ACCURACY_LOW = 1, 116 ACCURACY_MEDIUM = 2, 117 ACCURACY_HIGH = 3, 118 119 ftl_last = ACCURACY_HIGH, 120 }; 121 122 enum class InputDeviceSensorReportingMode : int32_t { 123 CONTINUOUS = 0, 124 ON_CHANGE = 1, 125 ONE_SHOT = 2, 126 SPECIAL_TRIGGER = 3, 127 }; 128 129 enum class InputDeviceLightType : int32_t { 130 INPUT = 0, 131 PLAYER_ID = 1, 132 KEYBOARD_BACKLIGHT = 2, 133 KEYBOARD_MIC_MUTE = 3, 134 135 ftl_last = KEYBOARD_MIC_MUTE 136 }; 137 138 enum class InputDeviceLightCapability : uint32_t { 139 /** Capability to change brightness of the light */ 140 BRIGHTNESS = 0x00000001, 141 /** Capability to change color of the light */ 142 RGB = 0x00000002, 143 }; 144 145 struct InputDeviceSensorInfo { InputDeviceSensorInfoInputDeviceSensorInfo146 explicit InputDeviceSensorInfo(std::string name, std::string vendor, int32_t version, 147 InputDeviceSensorType type, InputDeviceSensorAccuracy accuracy, 148 float maxRange, float resolution, float power, int32_t minDelay, 149 int32_t fifoReservedEventCount, int32_t fifoMaxEventCount, 150 std::string stringType, int32_t maxDelay, int32_t flags, 151 int32_t id) 152 : name(name), 153 vendor(vendor), 154 version(version), 155 type(type), 156 accuracy(accuracy), 157 maxRange(maxRange), 158 resolution(resolution), 159 power(power), 160 minDelay(minDelay), 161 fifoReservedEventCount(fifoReservedEventCount), 162 fifoMaxEventCount(fifoMaxEventCount), 163 stringType(stringType), 164 maxDelay(maxDelay), 165 flags(flags), 166 id(id) {} 167 // Name string of the sensor. 168 std::string name; 169 // Vendor string of this sensor. 170 std::string vendor; 171 // Version of the sensor's module. 172 int32_t version; 173 // Generic type of this sensor. 174 InputDeviceSensorType type; 175 // The current accuracy of sensor event. 176 InputDeviceSensorAccuracy accuracy; 177 // Maximum range of the sensor in the sensor's unit. 178 float maxRange; 179 // Resolution of the sensor in the sensor's unit. 180 float resolution; 181 // The power in mA used by this sensor while in use. 182 float power; 183 // The minimum delay allowed between two events in microsecond or zero if this sensor only 184 // returns a value when the data it's measuring changes. 185 int32_t minDelay; 186 // Number of events reserved for this sensor in the batch mode FIFO. 187 int32_t fifoReservedEventCount; 188 // Maximum number of events of this sensor that could be batched. 189 int32_t fifoMaxEventCount; 190 // The type of this sensor as a string. 191 std::string stringType; 192 // The delay between two sensor events corresponding to the lowest frequency that this sensor 193 // supports. 194 int32_t maxDelay; 195 // Sensor flags 196 int32_t flags; 197 // Sensor id, same as the input device ID it belongs to. 198 int32_t id; 199 }; 200 201 struct BrightnessLevel : ftl::DefaultConstructible<BrightnessLevel, std::uint8_t>, 202 ftl::Equatable<BrightnessLevel>, 203 ftl::Orderable<BrightnessLevel>, 204 ftl::Addable<BrightnessLevel> { 205 using DefaultConstructible::DefaultConstructible; 206 }; 207 208 struct InputDeviceLightInfo { InputDeviceLightInfoInputDeviceLightInfo209 explicit InputDeviceLightInfo(std::string name, int32_t id, InputDeviceLightType type, 210 ftl::Flags<InputDeviceLightCapability> capabilityFlags, 211 int32_t ordinal, 212 std::set<BrightnessLevel> preferredBrightnessLevels) 213 : name(name), 214 id(id), 215 type(type), 216 capabilityFlags(capabilityFlags), 217 ordinal(ordinal), 218 preferredBrightnessLevels(std::move(preferredBrightnessLevels)) {} 219 // Name string of the light. 220 std::string name; 221 // Light id 222 int32_t id; 223 // Type of the light. 224 InputDeviceLightType type; 225 // Light capabilities. 226 ftl::Flags<InputDeviceLightCapability> capabilityFlags; 227 // Ordinal of the light 228 int32_t ordinal; 229 // Custom brightness levels for the light 230 std::set<BrightnessLevel> preferredBrightnessLevels; 231 }; 232 233 struct InputDeviceBatteryInfo { InputDeviceBatteryInfoInputDeviceBatteryInfo234 explicit InputDeviceBatteryInfo(std::string name, int32_t id) : name(name), id(id) {} 235 // Name string of the battery. 236 std::string name; 237 // Battery id 238 int32_t id; 239 }; 240 241 struct KeyboardLayoutInfo { KeyboardLayoutInfoKeyboardLayoutInfo242 explicit KeyboardLayoutInfo(std::string languageTag, std::string layoutType) 243 : languageTag(languageTag), layoutType(layoutType) {} 244 245 // A BCP 47 conformant language tag such as "en-US". 246 std::string languageTag; 247 // The layout type such as QWERTY or AZERTY. 248 std::string layoutType; 249 250 inline bool operator==(const KeyboardLayoutInfo& other) const { 251 return languageTag == other.languageTag && layoutType == other.layoutType; 252 } 253 inline bool operator!=(const KeyboardLayoutInfo& other) const { return !(*this == other); } 254 }; 255 256 // The version of the Universal Stylus Initiative (USI) protocol supported by the input device. 257 struct InputDeviceUsiVersion { 258 int32_t majorVersion = -1; 259 int32_t minorVersion = -1; 260 }; 261 262 /* 263 * Describes the characteristics and capabilities of an input device. 264 */ 265 class InputDeviceInfo { 266 public: 267 InputDeviceInfo(); 268 InputDeviceInfo(const InputDeviceInfo& other); 269 ~InputDeviceInfo(); 270 271 struct MotionRange { 272 int32_t axis; 273 uint32_t source; 274 float min; 275 float max; 276 float flat; 277 float fuzz; 278 float resolution; 279 }; 280 281 void initialize(int32_t id, int32_t generation, int32_t controllerNumber, 282 const InputDeviceIdentifier& identifier, const std::string& alias, 283 bool isExternal, bool hasMic, ui::LogicalDisplayId associatedDisplayId, 284 InputDeviceViewBehavior viewBehavior = {{}}, bool enabled = true); 285 getId()286 inline int32_t getId() const { return mId; } getControllerNumber()287 inline int32_t getControllerNumber() const { return mControllerNumber; } getGeneration()288 inline int32_t getGeneration() const { return mGeneration; } getIdentifier()289 inline const InputDeviceIdentifier& getIdentifier() const { return mIdentifier; } getAlias()290 inline const std::string& getAlias() const { return mAlias; } getDisplayName()291 inline const std::string& getDisplayName() const { 292 return mAlias.empty() ? mIdentifier.name : mAlias; 293 } isExternal()294 inline bool isExternal() const { return mIsExternal; } hasMic()295 inline bool hasMic() const { return mHasMic; } getSources()296 inline uint32_t getSources() const { return mSources; } 297 298 const MotionRange* getMotionRange(int32_t axis, uint32_t source) const; 299 300 void addSource(uint32_t source); 301 void addMotionRange(int32_t axis, uint32_t source, 302 float min, float max, float flat, float fuzz, float resolution); 303 void addMotionRange(const MotionRange& range); 304 void addSensorInfo(const InputDeviceSensorInfo& info); 305 void addBatteryInfo(const InputDeviceBatteryInfo& info); 306 void addLightInfo(const InputDeviceLightInfo& info); 307 308 void setKeyboardType(int32_t keyboardType); getKeyboardType()309 inline int32_t getKeyboardType() const { return mKeyboardType; } 310 311 void setKeyboardLayoutInfo(KeyboardLayoutInfo keyboardLayoutInfo); getKeyboardLayoutInfo()312 inline const std::optional<KeyboardLayoutInfo>& getKeyboardLayoutInfo() const { 313 return mKeyboardLayoutInfo; 314 } 315 getViewBehavior()316 inline const InputDeviceViewBehavior& getViewBehavior() const { return mViewBehavior; } 317 setKeyCharacterMap(const std::shared_ptr<KeyCharacterMap> value)318 inline void setKeyCharacterMap(const std::shared_ptr<KeyCharacterMap> value) { 319 mKeyCharacterMap = value; 320 } 321 getKeyCharacterMap()322 inline const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const { 323 return mKeyCharacterMap; 324 } 325 setVibrator(bool hasVibrator)326 inline void setVibrator(bool hasVibrator) { mHasVibrator = hasVibrator; } hasVibrator()327 inline bool hasVibrator() const { return mHasVibrator; } 328 setHasBattery(bool hasBattery)329 inline void setHasBattery(bool hasBattery) { mHasBattery = hasBattery; } hasBattery()330 inline bool hasBattery() const { return mHasBattery; } 331 setButtonUnderPad(bool hasButton)332 inline void setButtonUnderPad(bool hasButton) { mHasButtonUnderPad = hasButton; } hasButtonUnderPad()333 inline bool hasButtonUnderPad() const { return mHasButtonUnderPad; } 334 setHasSensor(bool hasSensor)335 inline void setHasSensor(bool hasSensor) { mHasSensor = hasSensor; } hasSensor()336 inline bool hasSensor() const { return mHasSensor; } 337 getMotionRanges()338 inline const std::vector<MotionRange>& getMotionRanges() const { 339 return mMotionRanges; 340 } 341 342 std::vector<InputDeviceSensorInfo> getSensors(); 343 344 std::vector<InputDeviceLightInfo> getLights(); 345 setUsiVersion(std::optional<InputDeviceUsiVersion> usiVersion)346 inline void setUsiVersion(std::optional<InputDeviceUsiVersion> usiVersion) { 347 mUsiVersion = std::move(usiVersion); 348 } getUsiVersion()349 inline std::optional<InputDeviceUsiVersion> getUsiVersion() const { return mUsiVersion; } 350 getAssociatedDisplayId()351 inline ui::LogicalDisplayId getAssociatedDisplayId() const { return mAssociatedDisplayId; } 352 setEnabled(bool enabled)353 inline void setEnabled(bool enabled) { mEnabled = enabled; } isEnabled()354 inline bool isEnabled() const { return mEnabled; } 355 356 private: 357 int32_t mId; 358 int32_t mGeneration; 359 int32_t mControllerNumber; 360 InputDeviceIdentifier mIdentifier; 361 std::string mAlias; 362 bool mIsExternal; 363 bool mHasMic; 364 std::optional<KeyboardLayoutInfo> mKeyboardLayoutInfo; 365 uint32_t mSources; 366 int32_t mKeyboardType; 367 std::shared_ptr<KeyCharacterMap> mKeyCharacterMap; 368 std::optional<InputDeviceUsiVersion> mUsiVersion; 369 ui::LogicalDisplayId mAssociatedDisplayId{ui::LogicalDisplayId::INVALID}; 370 bool mEnabled; 371 372 bool mHasVibrator; 373 bool mHasBattery; 374 bool mHasButtonUnderPad; 375 bool mHasSensor; 376 377 std::vector<MotionRange> mMotionRanges; 378 std::unordered_map<InputDeviceSensorType, InputDeviceSensorInfo> mSensors; 379 /* Map from light ID to light info */ 380 std::unordered_map<int32_t, InputDeviceLightInfo> mLights; 381 /* Map from battery ID to battery info */ 382 std::unordered_map<int32_t, InputDeviceBatteryInfo> mBatteries; 383 /** The View related behaviors for the device. */ 384 InputDeviceViewBehavior mViewBehavior; 385 }; 386 387 /* Types of input device configuration files. */ 388 enum class InputDeviceConfigurationFileType : int32_t { 389 CONFIGURATION = 0, /* .idc file */ 390 KEY_LAYOUT = 1, /* .kl file */ 391 KEY_CHARACTER_MAP = 2, /* .kcm file */ 392 }; 393 394 /* 395 * Gets the path of an input device configuration file, if one is available. 396 * Considers both system provided and user installed configuration files. 397 * The optional suffix is appended to the end of the file name (before the 398 * extension). 399 * 400 * The device identifier is used to construct several default configuration file 401 * names to try based on the device name, vendor, product, and version. 402 * 403 * Returns an empty string if not found. 404 */ 405 extern std::string getInputDeviceConfigurationFilePathByDeviceIdentifier( 406 const InputDeviceIdentifier& deviceIdentifier, InputDeviceConfigurationFileType type, 407 const char* suffix = ""); 408 409 /* 410 * Gets the path of an input device configuration file, if one is available. 411 * Considers both system provided and user installed configuration files. 412 * 413 * The name is case-sensitive and is used to construct the filename to resolve. 414 * All characters except 'a'-'z', 'A'-'Z', '0'-'9', '-', and '_' are replaced by underscores. 415 * 416 * Returns an empty string if not found. 417 */ 418 extern std::string getInputDeviceConfigurationFilePathByName( 419 const std::string& name, InputDeviceConfigurationFileType type); 420 421 enum ReservedInputDeviceId : int32_t { 422 // Device id representing an invalid device 423 INVALID_INPUT_DEVICE_ID = android::os::IInputConstants::INVALID_INPUT_DEVICE_ID, 424 // Device id of a special "virtual" keyboard that is always present. 425 VIRTUAL_KEYBOARD_ID = -1, 426 // Device id of the "built-in" keyboard if there is one. 427 BUILT_IN_KEYBOARD_ID = 0, 428 // First device id available for dynamic devices 429 END_RESERVED_ID = 1, 430 }; 431 432 } // namespace android 433