blob: ffb2cb1fc1c3a4525ff5edd96907ada4d8f5f2d0 [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
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
Prabir Pradhanc08b0db2022-09-10 00:57:15 +000017#pragma once
Jeff Brown5912f952013-07-01 19:10:31 -070018
Chris Yef59a2f42020-10-16 12:55:26 -070019#include <android/sensor.h>
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000020#include <ftl/flags.h>
Jeff Brown5912f952013-07-01 19:10:31 -070021#include <input/Input.h>
22#include <input/KeyCharacterMap.h>
Chris Yef59a2f42020-10-16 12:55:26 -070023#include <unordered_map>
Arthur Hung7c3ae9c2019-03-11 11:23:03 +080024#include <vector>
Jeff Brown5912f952013-07-01 19:10:31 -070025
Sandro Meierd3d40602022-10-19 16:18:26 +000026#include <android/os/IInputConstants.h>
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000027
Jeff Brown5912f952013-07-01 19:10:31 -070028namespace android {
29
30/*
31 * Identifies a device.
32 */
33struct InputDeviceIdentifier {
34 inline InputDeviceIdentifier() :
35 bus(0), vendor(0), product(0), version(0) {
36 }
37
38 // Information provided by the kernel.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010039 std::string name;
40 std::string location;
41 std::string uniqueId;
Jeff Brown5912f952013-07-01 19:10:31 -070042 uint16_t bus;
43 uint16_t vendor;
44 uint16_t product;
45 uint16_t version;
46
47 // A composite input device descriptor string that uniquely identifies the device
48 // even across reboots or reconnections. The value of this field is used by
49 // upper layers of the input system to associate settings with individual devices.
50 // It is hashed from whatever kernel provided information is available.
51 // Ideally, the way this value is computed should not change between Android releases
52 // because that would invalidate persistent settings that rely on it.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010053 std::string descriptor;
RoboErikec2a15a2013-12-19 11:54:29 -080054
55 // A value added to uniquely identify a device in the absence of a unique id. This
56 // is intended to be a minimum way to distinguish from other active devices and may
57 // reuse values that are not associated with an input anymore.
58 uint16_t nonce;
Siarhei Vishniakoub45635c2019-02-20 19:22:09 -060059
Prabir Pradhan07525ef2022-10-03 21:51:26 +000060 // The bluetooth address of the device, if known.
61 std::optional<std::string> bluetoothAddress;
62
Siarhei Vishniakoub45635c2019-02-20 19:22:09 -060063 /**
64 * Return InputDeviceIdentifier.name that has been adjusted as follows:
65 * - all characters besides alphanumerics, dash,
66 * and underscore have been replaced with underscores.
67 * This helps in situations where a file that matches the device name is needed,
68 * while conforming to the filename limitations.
69 */
70 std::string getCanonicalName() const;
Prabir Pradhan852db892023-04-06 22:16:44 +000071
72 bool operator==(const InputDeviceIdentifier&) const = default;
73 bool operator!=(const InputDeviceIdentifier&) const = default;
Jeff Brown5912f952013-07-01 19:10:31 -070074};
75
Chris Yef59a2f42020-10-16 12:55:26 -070076/* Types of input device sensors. Keep sync with core/java/android/hardware/Sensor.java */
77enum class InputDeviceSensorType : int32_t {
78 ACCELEROMETER = ASENSOR_TYPE_ACCELEROMETER,
79 MAGNETIC_FIELD = ASENSOR_TYPE_MAGNETIC_FIELD,
80 ORIENTATION = 3,
81 GYROSCOPE = ASENSOR_TYPE_GYROSCOPE,
82 LIGHT = ASENSOR_TYPE_LIGHT,
83 PRESSURE = ASENSOR_TYPE_PRESSURE,
84 TEMPERATURE = 7,
85 PROXIMITY = ASENSOR_TYPE_PROXIMITY,
86 GRAVITY = ASENSOR_TYPE_GRAVITY,
87 LINEAR_ACCELERATION = ASENSOR_TYPE_LINEAR_ACCELERATION,
88 ROTATION_VECTOR = ASENSOR_TYPE_ROTATION_VECTOR,
89 RELATIVE_HUMIDITY = ASENSOR_TYPE_RELATIVE_HUMIDITY,
90 AMBIENT_TEMPERATURE = ASENSOR_TYPE_AMBIENT_TEMPERATURE,
91 MAGNETIC_FIELD_UNCALIBRATED = ASENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED,
92 GAME_ROTATION_VECTOR = ASENSOR_TYPE_GAME_ROTATION_VECTOR,
93 GYROSCOPE_UNCALIBRATED = ASENSOR_TYPE_GYROSCOPE_UNCALIBRATED,
94 SIGNIFICANT_MOTION = ASENSOR_TYPE_SIGNIFICANT_MOTION,
Dominik Laskowski75788452021-02-09 18:51:25 -080095
96 ftl_first = ACCELEROMETER,
97 ftl_last = SIGNIFICANT_MOTION
Chris Yef59a2f42020-10-16 12:55:26 -070098};
99
100enum class InputDeviceSensorAccuracy : int32_t {
101 ACCURACY_NONE = 0,
102 ACCURACY_LOW = 1,
103 ACCURACY_MEDIUM = 2,
104 ACCURACY_HIGH = 3,
105};
106
107enum class InputDeviceSensorReportingMode : int32_t {
108 CONTINUOUS = 0,
109 ON_CHANGE = 1,
110 ONE_SHOT = 2,
111 SPECIAL_TRIGGER = 3,
112};
113
Chris Ye3fdbfef2021-01-06 18:45:18 -0800114enum class InputDeviceLightType : int32_t {
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000115 INPUT = 0,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800116 PLAYER_ID = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000117 KEYBOARD_BACKLIGHT = 2,
Dominik Laskowski75788452021-02-09 18:51:25 -0800118
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000119 ftl_last = KEYBOARD_BACKLIGHT
120};
121
122enum class InputDeviceLightCapability : uint32_t {
123 /** Capability to change brightness of the light */
124 BRIGHTNESS = 0x00000001,
125 /** Capability to change color of the light */
126 RGB = 0x00000002,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800127};
128
Chris Yef59a2f42020-10-16 12:55:26 -0700129struct InputDeviceSensorInfo {
130 explicit InputDeviceSensorInfo(std::string name, std::string vendor, int32_t version,
131 InputDeviceSensorType type, InputDeviceSensorAccuracy accuracy,
132 float maxRange, float resolution, float power, int32_t minDelay,
133 int32_t fifoReservedEventCount, int32_t fifoMaxEventCount,
134 std::string stringType, int32_t maxDelay, int32_t flags,
135 int32_t id)
136 : name(name),
137 vendor(vendor),
138 version(version),
139 type(type),
140 accuracy(accuracy),
141 maxRange(maxRange),
142 resolution(resolution),
143 power(power),
144 minDelay(minDelay),
145 fifoReservedEventCount(fifoReservedEventCount),
146 fifoMaxEventCount(fifoMaxEventCount),
147 stringType(stringType),
148 maxDelay(maxDelay),
149 flags(flags),
150 id(id) {}
151 // Name string of the sensor.
152 std::string name;
153 // Vendor string of this sensor.
154 std::string vendor;
155 // Version of the sensor's module.
156 int32_t version;
157 // Generic type of this sensor.
158 InputDeviceSensorType type;
159 // The current accuracy of sensor event.
160 InputDeviceSensorAccuracy accuracy;
161 // Maximum range of the sensor in the sensor's unit.
162 float maxRange;
163 // Resolution of the sensor in the sensor's unit.
164 float resolution;
165 // The power in mA used by this sensor while in use.
166 float power;
167 // The minimum delay allowed between two events in microsecond or zero if this sensor only
168 // returns a value when the data it's measuring changes.
169 int32_t minDelay;
170 // Number of events reserved for this sensor in the batch mode FIFO.
171 int32_t fifoReservedEventCount;
172 // Maximum number of events of this sensor that could be batched.
173 int32_t fifoMaxEventCount;
174 // The type of this sensor as a string.
175 std::string stringType;
176 // The delay between two sensor events corresponding to the lowest frequency that this sensor
177 // supports.
178 int32_t maxDelay;
179 // Sensor flags
180 int32_t flags;
181 // Sensor id, same as the input device ID it belongs to.
182 int32_t id;
183};
184
Chris Ye3fdbfef2021-01-06 18:45:18 -0800185struct InputDeviceLightInfo {
186 explicit InputDeviceLightInfo(std::string name, int32_t id, InputDeviceLightType type,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000187 ftl::Flags<InputDeviceLightCapability> capabilityFlags,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800188 int32_t ordinal)
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000189 : name(name), id(id), type(type), capabilityFlags(capabilityFlags), ordinal(ordinal) {}
Chris Ye3fdbfef2021-01-06 18:45:18 -0800190 // Name string of the light.
191 std::string name;
192 // Light id
193 int32_t id;
194 // Type of the light.
195 InputDeviceLightType type;
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000196 // Light capabilities.
197 ftl::Flags<InputDeviceLightCapability> capabilityFlags;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800198 // Ordinal of the light
199 int32_t ordinal;
200};
201
Chris Yee2b1e5c2021-03-10 22:45:12 -0800202struct InputDeviceBatteryInfo {
203 explicit InputDeviceBatteryInfo(std::string name, int32_t id) : name(name), id(id) {}
204 // Name string of the battery.
205 std::string name;
206 // Battery id
207 int32_t id;
208};
209
Zixuan Qufecb6062022-11-12 04:44:31 +0000210struct KeyboardLayoutInfo {
211 explicit KeyboardLayoutInfo(std::string languageTag, std::string layoutType)
212 : languageTag(languageTag), layoutType(layoutType) {}
213
214 // A BCP 47 conformant language tag such as "en-US".
215 std::string languageTag;
216 // The layout type such as QWERTY or AZERTY.
217 std::string layoutType;
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +0000218
219 inline bool operator==(const KeyboardLayoutInfo& other) const {
220 return languageTag == other.languageTag && layoutType == other.layoutType;
221 }
222 inline bool operator!=(const KeyboardLayoutInfo& other) const { return !(*this == other); }
Zixuan Qufecb6062022-11-12 04:44:31 +0000223};
224
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000225// The version of the Universal Stylus Initiative (USI) protocol supported by the input device.
226struct InputDeviceUsiVersion {
227 int32_t majorVersion = -1;
228 int32_t minorVersion = -1;
229};
230
Jeff Brown5912f952013-07-01 19:10:31 -0700231/*
232 * Describes the characteristics and capabilities of an input device.
233 */
234class InputDeviceInfo {
235public:
236 InputDeviceInfo();
237 InputDeviceInfo(const InputDeviceInfo& other);
238 ~InputDeviceInfo();
239
240 struct MotionRange {
241 int32_t axis;
242 uint32_t source;
243 float min;
244 float max;
245 float flat;
246 float fuzz;
247 float resolution;
248 };
249
Michael Wright0415d632013-07-17 13:23:26 -0700250 void initialize(int32_t id, int32_t generation, int32_t controllerNumber,
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000251 const InputDeviceIdentifier& identifier, const std::string& alias,
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000252 bool isExternal, bool hasMic, int32_t associatedDisplayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700253
254 inline int32_t getId() const { return mId; }
Michael Wright0415d632013-07-17 13:23:26 -0700255 inline int32_t getControllerNumber() const { return mControllerNumber; }
Jeff Brown5912f952013-07-01 19:10:31 -0700256 inline int32_t getGeneration() const { return mGeneration; }
257 inline const InputDeviceIdentifier& getIdentifier() const { return mIdentifier; }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100258 inline const std::string& getAlias() const { return mAlias; }
259 inline const std::string& getDisplayName() const {
260 return mAlias.empty() ? mIdentifier.name : mAlias;
Jeff Brown5912f952013-07-01 19:10:31 -0700261 }
262 inline bool isExternal() const { return mIsExternal; }
Tim Kilbourn063ff532015-04-08 10:26:18 -0700263 inline bool hasMic() const { return mHasMic; }
Jeff Brown5912f952013-07-01 19:10:31 -0700264 inline uint32_t getSources() const { return mSources; }
265
266 const MotionRange* getMotionRange(int32_t axis, uint32_t source) const;
267
268 void addSource(uint32_t source);
269 void addMotionRange(int32_t axis, uint32_t source,
270 float min, float max, float flat, float fuzz, float resolution);
271 void addMotionRange(const MotionRange& range);
Chris Yef59a2f42020-10-16 12:55:26 -0700272 void addSensorInfo(const InputDeviceSensorInfo& info);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800273 void addBatteryInfo(const InputDeviceBatteryInfo& info);
Chris Ye3fdbfef2021-01-06 18:45:18 -0800274 void addLightInfo(const InputDeviceLightInfo& info);
Jeff Brown5912f952013-07-01 19:10:31 -0700275
Philip Junkerf8437962022-01-25 21:20:19 +0100276 void setKeyboardType(int32_t keyboardType);
Jeff Brown5912f952013-07-01 19:10:31 -0700277 inline int32_t getKeyboardType() const { return mKeyboardType; }
278
Zixuan Qufecb6062022-11-12 04:44:31 +0000279 void setKeyboardLayoutInfo(KeyboardLayoutInfo keyboardLayoutInfo);
280 inline const std::optional<KeyboardLayoutInfo>& getKeyboardLayoutInfo() const {
281 return mKeyboardLayoutInfo;
282 }
283
Chris Ye3a1e4462020-08-12 10:13:15 -0700284 inline void setKeyCharacterMap(const std::shared_ptr<KeyCharacterMap> value) {
Jeff Brown5912f952013-07-01 19:10:31 -0700285 mKeyCharacterMap = value;
286 }
287
Chris Ye3a1e4462020-08-12 10:13:15 -0700288 inline const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const {
Jeff Brown5912f952013-07-01 19:10:31 -0700289 return mKeyCharacterMap;
290 }
291
292 inline void setVibrator(bool hasVibrator) { mHasVibrator = hasVibrator; }
293 inline bool hasVibrator() const { return mHasVibrator; }
294
Kim Low03ea0352020-11-06 12:45:07 -0800295 inline void setHasBattery(bool hasBattery) { mHasBattery = hasBattery; }
296 inline bool hasBattery() const { return mHasBattery; }
297
Michael Wright931fd6d2013-07-10 18:05:15 -0700298 inline void setButtonUnderPad(bool hasButton) { mHasButtonUnderPad = hasButton; }
299 inline bool hasButtonUnderPad() const { return mHasButtonUnderPad; }
300
Chris Yef59a2f42020-10-16 12:55:26 -0700301 inline void setHasSensor(bool hasSensor) { mHasSensor = hasSensor; }
302 inline bool hasSensor() const { return mHasSensor; }
303
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800304 inline const std::vector<MotionRange>& getMotionRanges() const {
Jeff Brown5912f952013-07-01 19:10:31 -0700305 return mMotionRanges;
306 }
307
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000308 std::vector<InputDeviceSensorInfo> getSensors();
Chris Yef59a2f42020-10-16 12:55:26 -0700309
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000310 std::vector<InputDeviceLightInfo> getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800311
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000312 inline void setUsiVersion(std::optional<InputDeviceUsiVersion> usiVersion) {
313 mUsiVersion = std::move(usiVersion);
314 }
315 inline std::optional<InputDeviceUsiVersion> getUsiVersion() const { return mUsiVersion; }
316
317 inline int32_t getAssociatedDisplayId() const { return mAssociatedDisplayId; }
Prabir Pradhan167c2702022-09-14 00:37:24 +0000318
Jeff Brown5912f952013-07-01 19:10:31 -0700319private:
320 int32_t mId;
321 int32_t mGeneration;
Michael Wright0415d632013-07-17 13:23:26 -0700322 int32_t mControllerNumber;
Jeff Brown5912f952013-07-01 19:10:31 -0700323 InputDeviceIdentifier mIdentifier;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100324 std::string mAlias;
Jeff Brown5912f952013-07-01 19:10:31 -0700325 bool mIsExternal;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700326 bool mHasMic;
Zixuan Qufecb6062022-11-12 04:44:31 +0000327 std::optional<KeyboardLayoutInfo> mKeyboardLayoutInfo;
Jeff Brown5912f952013-07-01 19:10:31 -0700328 uint32_t mSources;
329 int32_t mKeyboardType;
Chris Ye3a1e4462020-08-12 10:13:15 -0700330 std::shared_ptr<KeyCharacterMap> mKeyCharacterMap;
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000331 std::optional<InputDeviceUsiVersion> mUsiVersion;
332 int32_t mAssociatedDisplayId;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000333
Jeff Brown5912f952013-07-01 19:10:31 -0700334 bool mHasVibrator;
Kim Low03ea0352020-11-06 12:45:07 -0800335 bool mHasBattery;
Michael Wright931fd6d2013-07-10 18:05:15 -0700336 bool mHasButtonUnderPad;
Chris Yef59a2f42020-10-16 12:55:26 -0700337 bool mHasSensor;
Jeff Brown5912f952013-07-01 19:10:31 -0700338
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800339 std::vector<MotionRange> mMotionRanges;
Chris Yef59a2f42020-10-16 12:55:26 -0700340 std::unordered_map<InputDeviceSensorType, InputDeviceSensorInfo> mSensors;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800341 /* Map from light ID to light info */
342 std::unordered_map<int32_t, InputDeviceLightInfo> mLights;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800343 /* Map from battery ID to battery info */
344 std::unordered_map<int32_t, InputDeviceBatteryInfo> mBatteries;
Jeff Brown5912f952013-07-01 19:10:31 -0700345};
346
347/* Types of input device configuration files. */
Chris Ye1d927aa2020-07-04 18:22:41 -0700348enum class InputDeviceConfigurationFileType : int32_t {
349 CONFIGURATION = 0, /* .idc file */
350 KEY_LAYOUT = 1, /* .kl file */
351 KEY_CHARACTER_MAP = 2, /* .kcm file */
Jeff Brown5912f952013-07-01 19:10:31 -0700352};
353
354/*
355 * Gets the path of an input device configuration file, if one is available.
356 * Considers both system provided and user installed configuration files.
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700357 * The optional suffix is appended to the end of the file name (before the
358 * extension).
Jeff Brown5912f952013-07-01 19:10:31 -0700359 *
360 * The device identifier is used to construct several default configuration file
361 * names to try based on the device name, vendor, product, and version.
362 *
363 * Returns an empty string if not found.
364 */
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100365extern std::string getInputDeviceConfigurationFilePathByDeviceIdentifier(
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700366 const InputDeviceIdentifier& deviceIdentifier, InputDeviceConfigurationFileType type,
367 const char* suffix = "");
Jeff Brown5912f952013-07-01 19:10:31 -0700368
369/*
370 * Gets the path of an input device configuration file, if one is available.
371 * Considers both system provided and user installed configuration files.
372 *
373 * The name is case-sensitive and is used to construct the filename to resolve.
374 * All characters except 'a'-'z', 'A'-'Z', '0'-'9', '-', and '_' are replaced by underscores.
375 *
376 * Returns an empty string if not found.
377 */
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100378extern std::string getInputDeviceConfigurationFilePathByName(
379 const std::string& name, InputDeviceConfigurationFileType type);
Jeff Brown5912f952013-07-01 19:10:31 -0700380
Prabir Pradhancae4b3a2019-02-05 18:51:32 -0800381enum ReservedInputDeviceId : int32_t {
Sandro Meierd3d40602022-10-19 16:18:26 +0000382 // Device id representing an invalid device
383 INVALID_INPUT_DEVICE_ID = android::os::IInputConstants::INVALID_INPUT_DEVICE_ID,
Prabir Pradhancae4b3a2019-02-05 18:51:32 -0800384 // Device id of a special "virtual" keyboard that is always present.
385 VIRTUAL_KEYBOARD_ID = -1,
386 // Device id of the "built-in" keyboard if there is one.
387 BUILT_IN_KEYBOARD_ID = 0,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800388 // First device id available for dynamic devices
389 END_RESERVED_ID = 1,
Prabir Pradhancae4b3a2019-02-05 18:51:32 -0800390};
391
Jeff Brown5912f952013-07-01 19:10:31 -0700392} // namespace android