blob: f4a15238cf851ee23b5519a4b1121a7a647cb770 [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
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000026#include "android/hardware/input/InputDeviceCountryCode.h"
27
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
60 /**
61 * Return InputDeviceIdentifier.name that has been adjusted as follows:
62 * - all characters besides alphanumerics, dash,
63 * and underscore have been replaced with underscores.
64 * This helps in situations where a file that matches the device name is needed,
65 * while conforming to the filename limitations.
66 */
67 std::string getCanonicalName() const;
Jeff Brown5912f952013-07-01 19:10:31 -070068};
69
Chris Yef59a2f42020-10-16 12:55:26 -070070/* Types of input device sensors. Keep sync with core/java/android/hardware/Sensor.java */
71enum class InputDeviceSensorType : int32_t {
72 ACCELEROMETER = ASENSOR_TYPE_ACCELEROMETER,
73 MAGNETIC_FIELD = ASENSOR_TYPE_MAGNETIC_FIELD,
74 ORIENTATION = 3,
75 GYROSCOPE = ASENSOR_TYPE_GYROSCOPE,
76 LIGHT = ASENSOR_TYPE_LIGHT,
77 PRESSURE = ASENSOR_TYPE_PRESSURE,
78 TEMPERATURE = 7,
79 PROXIMITY = ASENSOR_TYPE_PROXIMITY,
80 GRAVITY = ASENSOR_TYPE_GRAVITY,
81 LINEAR_ACCELERATION = ASENSOR_TYPE_LINEAR_ACCELERATION,
82 ROTATION_VECTOR = ASENSOR_TYPE_ROTATION_VECTOR,
83 RELATIVE_HUMIDITY = ASENSOR_TYPE_RELATIVE_HUMIDITY,
84 AMBIENT_TEMPERATURE = ASENSOR_TYPE_AMBIENT_TEMPERATURE,
85 MAGNETIC_FIELD_UNCALIBRATED = ASENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED,
86 GAME_ROTATION_VECTOR = ASENSOR_TYPE_GAME_ROTATION_VECTOR,
87 GYROSCOPE_UNCALIBRATED = ASENSOR_TYPE_GYROSCOPE_UNCALIBRATED,
88 SIGNIFICANT_MOTION = ASENSOR_TYPE_SIGNIFICANT_MOTION,
Dominik Laskowski75788452021-02-09 18:51:25 -080089
90 ftl_first = ACCELEROMETER,
91 ftl_last = SIGNIFICANT_MOTION
Chris Yef59a2f42020-10-16 12:55:26 -070092};
93
94enum class InputDeviceSensorAccuracy : int32_t {
95 ACCURACY_NONE = 0,
96 ACCURACY_LOW = 1,
97 ACCURACY_MEDIUM = 2,
98 ACCURACY_HIGH = 3,
99};
100
101enum class InputDeviceSensorReportingMode : int32_t {
102 CONTINUOUS = 0,
103 ON_CHANGE = 1,
104 ONE_SHOT = 2,
105 SPECIAL_TRIGGER = 3,
106};
107
Chris Ye3fdbfef2021-01-06 18:45:18 -0800108enum class InputDeviceLightType : int32_t {
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000109 INPUT = 0,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800110 PLAYER_ID = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000111 KEYBOARD_BACKLIGHT = 2,
Dominik Laskowski75788452021-02-09 18:51:25 -0800112
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000113 ftl_last = KEYBOARD_BACKLIGHT
114};
115
116enum class InputDeviceLightCapability : uint32_t {
117 /** Capability to change brightness of the light */
118 BRIGHTNESS = 0x00000001,
119 /** Capability to change color of the light */
120 RGB = 0x00000002,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800121};
122
Chris Yef59a2f42020-10-16 12:55:26 -0700123struct InputDeviceSensorInfo {
124 explicit InputDeviceSensorInfo(std::string name, std::string vendor, int32_t version,
125 InputDeviceSensorType type, InputDeviceSensorAccuracy accuracy,
126 float maxRange, float resolution, float power, int32_t minDelay,
127 int32_t fifoReservedEventCount, int32_t fifoMaxEventCount,
128 std::string stringType, int32_t maxDelay, int32_t flags,
129 int32_t id)
130 : name(name),
131 vendor(vendor),
132 version(version),
133 type(type),
134 accuracy(accuracy),
135 maxRange(maxRange),
136 resolution(resolution),
137 power(power),
138 minDelay(minDelay),
139 fifoReservedEventCount(fifoReservedEventCount),
140 fifoMaxEventCount(fifoMaxEventCount),
141 stringType(stringType),
142 maxDelay(maxDelay),
143 flags(flags),
144 id(id) {}
145 // Name string of the sensor.
146 std::string name;
147 // Vendor string of this sensor.
148 std::string vendor;
149 // Version of the sensor's module.
150 int32_t version;
151 // Generic type of this sensor.
152 InputDeviceSensorType type;
153 // The current accuracy of sensor event.
154 InputDeviceSensorAccuracy accuracy;
155 // Maximum range of the sensor in the sensor's unit.
156 float maxRange;
157 // Resolution of the sensor in the sensor's unit.
158 float resolution;
159 // The power in mA used by this sensor while in use.
160 float power;
161 // The minimum delay allowed between two events in microsecond or zero if this sensor only
162 // returns a value when the data it's measuring changes.
163 int32_t minDelay;
164 // Number of events reserved for this sensor in the batch mode FIFO.
165 int32_t fifoReservedEventCount;
166 // Maximum number of events of this sensor that could be batched.
167 int32_t fifoMaxEventCount;
168 // The type of this sensor as a string.
169 std::string stringType;
170 // The delay between two sensor events corresponding to the lowest frequency that this sensor
171 // supports.
172 int32_t maxDelay;
173 // Sensor flags
174 int32_t flags;
175 // Sensor id, same as the input device ID it belongs to.
176 int32_t id;
177};
178
Chris Ye3fdbfef2021-01-06 18:45:18 -0800179struct InputDeviceLightInfo {
180 explicit InputDeviceLightInfo(std::string name, int32_t id, InputDeviceLightType type,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000181 ftl::Flags<InputDeviceLightCapability> capabilityFlags,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800182 int32_t ordinal)
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000183 : name(name), id(id), type(type), capabilityFlags(capabilityFlags), ordinal(ordinal) {}
Chris Ye3fdbfef2021-01-06 18:45:18 -0800184 // Name string of the light.
185 std::string name;
186 // Light id
187 int32_t id;
188 // Type of the light.
189 InputDeviceLightType type;
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +0000190 // Light capabilities.
191 ftl::Flags<InputDeviceLightCapability> capabilityFlags;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800192 // Ordinal of the light
193 int32_t ordinal;
194};
195
Chris Yee2b1e5c2021-03-10 22:45:12 -0800196struct InputDeviceBatteryInfo {
197 explicit InputDeviceBatteryInfo(std::string name, int32_t id) : name(name), id(id) {}
198 // Name string of the battery.
199 std::string name;
200 // Battery id
201 int32_t id;
202};
203
Jeff Brown5912f952013-07-01 19:10:31 -0700204/*
205 * Describes the characteristics and capabilities of an input device.
206 */
207class InputDeviceInfo {
208public:
209 InputDeviceInfo();
210 InputDeviceInfo(const InputDeviceInfo& other);
211 ~InputDeviceInfo();
212
213 struct MotionRange {
214 int32_t axis;
215 uint32_t source;
216 float min;
217 float max;
218 float flat;
219 float fuzz;
220 float resolution;
221 };
222
Michael Wright0415d632013-07-17 13:23:26 -0700223 void initialize(int32_t id, int32_t generation, int32_t controllerNumber,
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000224 const InputDeviceIdentifier& identifier, const std::string& alias,
225 bool isExternal, bool hasMic,
226 hardware::input::InputDeviceCountryCode countryCode =
227 hardware::input::InputDeviceCountryCode::INVALID);
Jeff Brown5912f952013-07-01 19:10:31 -0700228
229 inline int32_t getId() const { return mId; }
Michael Wright0415d632013-07-17 13:23:26 -0700230 inline int32_t getControllerNumber() const { return mControllerNumber; }
Jeff Brown5912f952013-07-01 19:10:31 -0700231 inline int32_t getGeneration() const { return mGeneration; }
232 inline const InputDeviceIdentifier& getIdentifier() const { return mIdentifier; }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100233 inline const std::string& getAlias() const { return mAlias; }
234 inline const std::string& getDisplayName() const {
235 return mAlias.empty() ? mIdentifier.name : mAlias;
Jeff Brown5912f952013-07-01 19:10:31 -0700236 }
237 inline bool isExternal() const { return mIsExternal; }
Tim Kilbourn063ff532015-04-08 10:26:18 -0700238 inline bool hasMic() const { return mHasMic; }
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000239 inline hardware::input::InputDeviceCountryCode getCountryCode() const { return mCountryCode; }
Jeff Brown5912f952013-07-01 19:10:31 -0700240 inline uint32_t getSources() const { return mSources; }
241
242 const MotionRange* getMotionRange(int32_t axis, uint32_t source) const;
243
244 void addSource(uint32_t source);
245 void addMotionRange(int32_t axis, uint32_t source,
246 float min, float max, float flat, float fuzz, float resolution);
247 void addMotionRange(const MotionRange& range);
Chris Yef59a2f42020-10-16 12:55:26 -0700248 void addSensorInfo(const InputDeviceSensorInfo& info);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800249 void addBatteryInfo(const InputDeviceBatteryInfo& info);
Chris Ye3fdbfef2021-01-06 18:45:18 -0800250 void addLightInfo(const InputDeviceLightInfo& info);
Jeff Brown5912f952013-07-01 19:10:31 -0700251
Philip Junkerf8437962022-01-25 21:20:19 +0100252 void setKeyboardType(int32_t keyboardType);
Jeff Brown5912f952013-07-01 19:10:31 -0700253 inline int32_t getKeyboardType() const { return mKeyboardType; }
254
Chris Ye3a1e4462020-08-12 10:13:15 -0700255 inline void setKeyCharacterMap(const std::shared_ptr<KeyCharacterMap> value) {
Jeff Brown5912f952013-07-01 19:10:31 -0700256 mKeyCharacterMap = value;
257 }
258
Chris Ye3a1e4462020-08-12 10:13:15 -0700259 inline const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const {
Jeff Brown5912f952013-07-01 19:10:31 -0700260 return mKeyCharacterMap;
261 }
262
263 inline void setVibrator(bool hasVibrator) { mHasVibrator = hasVibrator; }
264 inline bool hasVibrator() const { return mHasVibrator; }
265
Kim Low03ea0352020-11-06 12:45:07 -0800266 inline void setHasBattery(bool hasBattery) { mHasBattery = hasBattery; }
267 inline bool hasBattery() const { return mHasBattery; }
268
Michael Wright931fd6d2013-07-10 18:05:15 -0700269 inline void setButtonUnderPad(bool hasButton) { mHasButtonUnderPad = hasButton; }
270 inline bool hasButtonUnderPad() const { return mHasButtonUnderPad; }
271
Chris Yef59a2f42020-10-16 12:55:26 -0700272 inline void setHasSensor(bool hasSensor) { mHasSensor = hasSensor; }
273 inline bool hasSensor() const { return mHasSensor; }
274
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800275 inline const std::vector<MotionRange>& getMotionRanges() const {
Jeff Brown5912f952013-07-01 19:10:31 -0700276 return mMotionRanges;
277 }
278
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000279 std::vector<InputDeviceSensorInfo> getSensors();
Chris Yef59a2f42020-10-16 12:55:26 -0700280
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000281 std::vector<InputDeviceLightInfo> getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800282
Prabir Pradhan167c2702022-09-14 00:37:24 +0000283 inline void setSupportsUsi(bool supportsUsi) { mSupportsUsi = supportsUsi; }
284 inline bool supportsUsi() const { return mSupportsUsi; }
285
Jeff Brown5912f952013-07-01 19:10:31 -0700286private:
287 int32_t mId;
288 int32_t mGeneration;
Michael Wright0415d632013-07-17 13:23:26 -0700289 int32_t mControllerNumber;
Jeff Brown5912f952013-07-01 19:10:31 -0700290 InputDeviceIdentifier mIdentifier;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100291 std::string mAlias;
Jeff Brown5912f952013-07-01 19:10:31 -0700292 bool mIsExternal;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700293 bool mHasMic;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000294 hardware::input::InputDeviceCountryCode mCountryCode;
Jeff Brown5912f952013-07-01 19:10:31 -0700295 uint32_t mSources;
296 int32_t mKeyboardType;
Chris Ye3a1e4462020-08-12 10:13:15 -0700297 std::shared_ptr<KeyCharacterMap> mKeyCharacterMap;
Prabir Pradhan167c2702022-09-14 00:37:24 +0000298 // Whether this device supports the Universal Stylus Initiative (USI) protocol for styluses.
299 bool mSupportsUsi;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000300
Jeff Brown5912f952013-07-01 19:10:31 -0700301 bool mHasVibrator;
Kim Low03ea0352020-11-06 12:45:07 -0800302 bool mHasBattery;
Michael Wright931fd6d2013-07-10 18:05:15 -0700303 bool mHasButtonUnderPad;
Chris Yef59a2f42020-10-16 12:55:26 -0700304 bool mHasSensor;
Jeff Brown5912f952013-07-01 19:10:31 -0700305
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800306 std::vector<MotionRange> mMotionRanges;
Chris Yef59a2f42020-10-16 12:55:26 -0700307 std::unordered_map<InputDeviceSensorType, InputDeviceSensorInfo> mSensors;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800308 /* Map from light ID to light info */
309 std::unordered_map<int32_t, InputDeviceLightInfo> mLights;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800310 /* Map from battery ID to battery info */
311 std::unordered_map<int32_t, InputDeviceBatteryInfo> mBatteries;
Jeff Brown5912f952013-07-01 19:10:31 -0700312};
313
314/* Types of input device configuration files. */
Chris Ye1d927aa2020-07-04 18:22:41 -0700315enum class InputDeviceConfigurationFileType : int32_t {
316 CONFIGURATION = 0, /* .idc file */
317 KEY_LAYOUT = 1, /* .kl file */
318 KEY_CHARACTER_MAP = 2, /* .kcm file */
Jeff Brown5912f952013-07-01 19:10:31 -0700319};
320
321/*
322 * Gets the path of an input device configuration file, if one is available.
323 * Considers both system provided and user installed configuration files.
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700324 * The optional suffix is appended to the end of the file name (before the
325 * extension).
Jeff Brown5912f952013-07-01 19:10:31 -0700326 *
327 * The device identifier is used to construct several default configuration file
328 * names to try based on the device name, vendor, product, and version.
329 *
330 * Returns an empty string if not found.
331 */
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100332extern std::string getInputDeviceConfigurationFilePathByDeviceIdentifier(
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700333 const InputDeviceIdentifier& deviceIdentifier, InputDeviceConfigurationFileType type,
334 const char* suffix = "");
Jeff Brown5912f952013-07-01 19:10:31 -0700335
336/*
337 * Gets the path of an input device configuration file, if one is available.
338 * Considers both system provided and user installed configuration files.
339 *
340 * The name is case-sensitive and is used to construct the filename to resolve.
341 * All characters except 'a'-'z', 'A'-'Z', '0'-'9', '-', and '_' are replaced by underscores.
342 *
343 * Returns an empty string if not found.
344 */
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100345extern std::string getInputDeviceConfigurationFilePathByName(
346 const std::string& name, InputDeviceConfigurationFileType type);
Jeff Brown5912f952013-07-01 19:10:31 -0700347
Prabir Pradhancae4b3a2019-02-05 18:51:32 -0800348enum ReservedInputDeviceId : int32_t {
349 // Device id of a special "virtual" keyboard that is always present.
350 VIRTUAL_KEYBOARD_ID = -1,
351 // Device id of the "built-in" keyboard if there is one.
352 BUILT_IN_KEYBOARD_ID = 0,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800353 // First device id available for dynamic devices
354 END_RESERVED_ID = 1,
Prabir Pradhancae4b3a2019-02-05 18:51:32 -0800355};
356
Jeff Brown5912f952013-07-01 19:10:31 -0700357} // namespace android