blob: 415080d15d567e2e6c066290d807e86bcc59d5c3 [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>
Jeff Brown5912f952013-07-01 19:10:31 -070020#include <input/Input.h>
21#include <input/KeyCharacterMap.h>
Chris Yef59a2f42020-10-16 12:55:26 -070022#include <unordered_map>
Arthur Hung7c3ae9c2019-03-11 11:23:03 +080023#include <vector>
Jeff Brown5912f952013-07-01 19:10:31 -070024
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000025#include "android/hardware/input/InputDeviceCountryCode.h"
26
Jeff Brown5912f952013-07-01 19:10:31 -070027namespace android {
28
29/*
30 * Identifies a device.
31 */
32struct InputDeviceIdentifier {
33 inline InputDeviceIdentifier() :
34 bus(0), vendor(0), product(0), version(0) {
35 }
36
37 // Information provided by the kernel.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010038 std::string name;
39 std::string location;
40 std::string uniqueId;
Jeff Brown5912f952013-07-01 19:10:31 -070041 uint16_t bus;
42 uint16_t vendor;
43 uint16_t product;
44 uint16_t version;
45
46 // A composite input device descriptor string that uniquely identifies the device
47 // even across reboots or reconnections. The value of this field is used by
48 // upper layers of the input system to associate settings with individual devices.
49 // It is hashed from whatever kernel provided information is available.
50 // Ideally, the way this value is computed should not change between Android releases
51 // because that would invalidate persistent settings that rely on it.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010052 std::string descriptor;
RoboErikec2a15a2013-12-19 11:54:29 -080053
54 // A value added to uniquely identify a device in the absence of a unique id. This
55 // is intended to be a minimum way to distinguish from other active devices and may
56 // reuse values that are not associated with an input anymore.
57 uint16_t nonce;
Siarhei Vishniakoub45635c2019-02-20 19:22:09 -060058
59 /**
60 * Return InputDeviceIdentifier.name that has been adjusted as follows:
61 * - all characters besides alphanumerics, dash,
62 * and underscore have been replaced with underscores.
63 * This helps in situations where a file that matches the device name is needed,
64 * while conforming to the filename limitations.
65 */
66 std::string getCanonicalName() const;
Jeff Brown5912f952013-07-01 19:10:31 -070067};
68
Chris Yef59a2f42020-10-16 12:55:26 -070069/* Types of input device sensors. Keep sync with core/java/android/hardware/Sensor.java */
70enum class InputDeviceSensorType : int32_t {
71 ACCELEROMETER = ASENSOR_TYPE_ACCELEROMETER,
72 MAGNETIC_FIELD = ASENSOR_TYPE_MAGNETIC_FIELD,
73 ORIENTATION = 3,
74 GYROSCOPE = ASENSOR_TYPE_GYROSCOPE,
75 LIGHT = ASENSOR_TYPE_LIGHT,
76 PRESSURE = ASENSOR_TYPE_PRESSURE,
77 TEMPERATURE = 7,
78 PROXIMITY = ASENSOR_TYPE_PROXIMITY,
79 GRAVITY = ASENSOR_TYPE_GRAVITY,
80 LINEAR_ACCELERATION = ASENSOR_TYPE_LINEAR_ACCELERATION,
81 ROTATION_VECTOR = ASENSOR_TYPE_ROTATION_VECTOR,
82 RELATIVE_HUMIDITY = ASENSOR_TYPE_RELATIVE_HUMIDITY,
83 AMBIENT_TEMPERATURE = ASENSOR_TYPE_AMBIENT_TEMPERATURE,
84 MAGNETIC_FIELD_UNCALIBRATED = ASENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED,
85 GAME_ROTATION_VECTOR = ASENSOR_TYPE_GAME_ROTATION_VECTOR,
86 GYROSCOPE_UNCALIBRATED = ASENSOR_TYPE_GYROSCOPE_UNCALIBRATED,
87 SIGNIFICANT_MOTION = ASENSOR_TYPE_SIGNIFICANT_MOTION,
Dominik Laskowski75788452021-02-09 18:51:25 -080088
89 ftl_first = ACCELEROMETER,
90 ftl_last = SIGNIFICANT_MOTION
Chris Yef59a2f42020-10-16 12:55:26 -070091};
92
93enum class InputDeviceSensorAccuracy : int32_t {
94 ACCURACY_NONE = 0,
95 ACCURACY_LOW = 1,
96 ACCURACY_MEDIUM = 2,
97 ACCURACY_HIGH = 3,
98};
99
100enum class InputDeviceSensorReportingMode : int32_t {
101 CONTINUOUS = 0,
102 ON_CHANGE = 1,
103 ONE_SHOT = 2,
104 SPECIAL_TRIGGER = 3,
105};
106
Chris Ye3fdbfef2021-01-06 18:45:18 -0800107enum class InputDeviceLightType : int32_t {
Chris Ye85758332021-05-16 23:05:17 -0700108 MONO = 0,
Chris Ye3fdbfef2021-01-06 18:45:18 -0800109 PLAYER_ID = 1,
110 RGB = 2,
111 MULTI_COLOR = 3,
Dominik Laskowski75788452021-02-09 18:51:25 -0800112
113 ftl_last = MULTI_COLOR
Chris Ye3fdbfef2021-01-06 18:45:18 -0800114};
115
Chris Yef59a2f42020-10-16 12:55:26 -0700116struct InputDeviceSensorInfo {
117 explicit InputDeviceSensorInfo(std::string name, std::string vendor, int32_t version,
118 InputDeviceSensorType type, InputDeviceSensorAccuracy accuracy,
119 float maxRange, float resolution, float power, int32_t minDelay,
120 int32_t fifoReservedEventCount, int32_t fifoMaxEventCount,
121 std::string stringType, int32_t maxDelay, int32_t flags,
122 int32_t id)
123 : name(name),
124 vendor(vendor),
125 version(version),
126 type(type),
127 accuracy(accuracy),
128 maxRange(maxRange),
129 resolution(resolution),
130 power(power),
131 minDelay(minDelay),
132 fifoReservedEventCount(fifoReservedEventCount),
133 fifoMaxEventCount(fifoMaxEventCount),
134 stringType(stringType),
135 maxDelay(maxDelay),
136 flags(flags),
137 id(id) {}
138 // Name string of the sensor.
139 std::string name;
140 // Vendor string of this sensor.
141 std::string vendor;
142 // Version of the sensor's module.
143 int32_t version;
144 // Generic type of this sensor.
145 InputDeviceSensorType type;
146 // The current accuracy of sensor event.
147 InputDeviceSensorAccuracy accuracy;
148 // Maximum range of the sensor in the sensor's unit.
149 float maxRange;
150 // Resolution of the sensor in the sensor's unit.
151 float resolution;
152 // The power in mA used by this sensor while in use.
153 float power;
154 // The minimum delay allowed between two events in microsecond or zero if this sensor only
155 // returns a value when the data it's measuring changes.
156 int32_t minDelay;
157 // Number of events reserved for this sensor in the batch mode FIFO.
158 int32_t fifoReservedEventCount;
159 // Maximum number of events of this sensor that could be batched.
160 int32_t fifoMaxEventCount;
161 // The type of this sensor as a string.
162 std::string stringType;
163 // The delay between two sensor events corresponding to the lowest frequency that this sensor
164 // supports.
165 int32_t maxDelay;
166 // Sensor flags
167 int32_t flags;
168 // Sensor id, same as the input device ID it belongs to.
169 int32_t id;
170};
171
Chris Ye3fdbfef2021-01-06 18:45:18 -0800172struct InputDeviceLightInfo {
173 explicit InputDeviceLightInfo(std::string name, int32_t id, InputDeviceLightType type,
174 int32_t ordinal)
175 : name(name), id(id), type(type), ordinal(ordinal) {}
176 // Name string of the light.
177 std::string name;
178 // Light id
179 int32_t id;
180 // Type of the light.
181 InputDeviceLightType type;
182 // Ordinal of the light
183 int32_t ordinal;
184};
185
Chris Yee2b1e5c2021-03-10 22:45:12 -0800186struct InputDeviceBatteryInfo {
187 explicit InputDeviceBatteryInfo(std::string name, int32_t id) : name(name), id(id) {}
188 // Name string of the battery.
189 std::string name;
190 // Battery id
191 int32_t id;
192};
193
Jeff Brown5912f952013-07-01 19:10:31 -0700194/*
195 * Describes the characteristics and capabilities of an input device.
196 */
197class InputDeviceInfo {
198public:
199 InputDeviceInfo();
200 InputDeviceInfo(const InputDeviceInfo& other);
201 ~InputDeviceInfo();
202
203 struct MotionRange {
204 int32_t axis;
205 uint32_t source;
206 float min;
207 float max;
208 float flat;
209 float fuzz;
210 float resolution;
211 };
212
Michael Wright0415d632013-07-17 13:23:26 -0700213 void initialize(int32_t id, int32_t generation, int32_t controllerNumber,
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000214 const InputDeviceIdentifier& identifier, const std::string& alias,
215 bool isExternal, bool hasMic,
216 hardware::input::InputDeviceCountryCode countryCode =
217 hardware::input::InputDeviceCountryCode::INVALID);
Jeff Brown5912f952013-07-01 19:10:31 -0700218
219 inline int32_t getId() const { return mId; }
Michael Wright0415d632013-07-17 13:23:26 -0700220 inline int32_t getControllerNumber() const { return mControllerNumber; }
Jeff Brown5912f952013-07-01 19:10:31 -0700221 inline int32_t getGeneration() const { return mGeneration; }
222 inline const InputDeviceIdentifier& getIdentifier() const { return mIdentifier; }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100223 inline const std::string& getAlias() const { return mAlias; }
224 inline const std::string& getDisplayName() const {
225 return mAlias.empty() ? mIdentifier.name : mAlias;
Jeff Brown5912f952013-07-01 19:10:31 -0700226 }
227 inline bool isExternal() const { return mIsExternal; }
Tim Kilbourn063ff532015-04-08 10:26:18 -0700228 inline bool hasMic() const { return mHasMic; }
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000229 inline hardware::input::InputDeviceCountryCode getCountryCode() const { return mCountryCode; }
Jeff Brown5912f952013-07-01 19:10:31 -0700230 inline uint32_t getSources() const { return mSources; }
231
232 const MotionRange* getMotionRange(int32_t axis, uint32_t source) const;
233
234 void addSource(uint32_t source);
235 void addMotionRange(int32_t axis, uint32_t source,
236 float min, float max, float flat, float fuzz, float resolution);
237 void addMotionRange(const MotionRange& range);
Chris Yef59a2f42020-10-16 12:55:26 -0700238 void addSensorInfo(const InputDeviceSensorInfo& info);
Chris Yee2b1e5c2021-03-10 22:45:12 -0800239 void addBatteryInfo(const InputDeviceBatteryInfo& info);
Chris Ye3fdbfef2021-01-06 18:45:18 -0800240 void addLightInfo(const InputDeviceLightInfo& info);
Jeff Brown5912f952013-07-01 19:10:31 -0700241
Philip Junkerf8437962022-01-25 21:20:19 +0100242 void setKeyboardType(int32_t keyboardType);
Jeff Brown5912f952013-07-01 19:10:31 -0700243 inline int32_t getKeyboardType() const { return mKeyboardType; }
244
Chris Ye3a1e4462020-08-12 10:13:15 -0700245 inline void setKeyCharacterMap(const std::shared_ptr<KeyCharacterMap> value) {
Jeff Brown5912f952013-07-01 19:10:31 -0700246 mKeyCharacterMap = value;
247 }
248
Chris Ye3a1e4462020-08-12 10:13:15 -0700249 inline const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const {
Jeff Brown5912f952013-07-01 19:10:31 -0700250 return mKeyCharacterMap;
251 }
252
253 inline void setVibrator(bool hasVibrator) { mHasVibrator = hasVibrator; }
254 inline bool hasVibrator() const { return mHasVibrator; }
255
Kim Low03ea0352020-11-06 12:45:07 -0800256 inline void setHasBattery(bool hasBattery) { mHasBattery = hasBattery; }
257 inline bool hasBattery() const { return mHasBattery; }
258
Michael Wright931fd6d2013-07-10 18:05:15 -0700259 inline void setButtonUnderPad(bool hasButton) { mHasButtonUnderPad = hasButton; }
260 inline bool hasButtonUnderPad() const { return mHasButtonUnderPad; }
261
Chris Yef59a2f42020-10-16 12:55:26 -0700262 inline void setHasSensor(bool hasSensor) { mHasSensor = hasSensor; }
263 inline bool hasSensor() const { return mHasSensor; }
264
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800265 inline const std::vector<MotionRange>& getMotionRanges() const {
Jeff Brown5912f952013-07-01 19:10:31 -0700266 return mMotionRanges;
267 }
268
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000269 std::vector<InputDeviceSensorInfo> getSensors();
Chris Yef59a2f42020-10-16 12:55:26 -0700270
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000271 std::vector<InputDeviceLightInfo> getLights();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800272
Jeff Brown5912f952013-07-01 19:10:31 -0700273private:
274 int32_t mId;
275 int32_t mGeneration;
Michael Wright0415d632013-07-17 13:23:26 -0700276 int32_t mControllerNumber;
Jeff Brown5912f952013-07-01 19:10:31 -0700277 InputDeviceIdentifier mIdentifier;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100278 std::string mAlias;
Jeff Brown5912f952013-07-01 19:10:31 -0700279 bool mIsExternal;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700280 bool mHasMic;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000281 hardware::input::InputDeviceCountryCode mCountryCode;
Jeff Brown5912f952013-07-01 19:10:31 -0700282 uint32_t mSources;
283 int32_t mKeyboardType;
Chris Ye3a1e4462020-08-12 10:13:15 -0700284 std::shared_ptr<KeyCharacterMap> mKeyCharacterMap;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000285
Jeff Brown5912f952013-07-01 19:10:31 -0700286 bool mHasVibrator;
Kim Low03ea0352020-11-06 12:45:07 -0800287 bool mHasBattery;
Michael Wright931fd6d2013-07-10 18:05:15 -0700288 bool mHasButtonUnderPad;
Chris Yef59a2f42020-10-16 12:55:26 -0700289 bool mHasSensor;
Jeff Brown5912f952013-07-01 19:10:31 -0700290
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800291 std::vector<MotionRange> mMotionRanges;
Chris Yef59a2f42020-10-16 12:55:26 -0700292 std::unordered_map<InputDeviceSensorType, InputDeviceSensorInfo> mSensors;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800293 /* Map from light ID to light info */
294 std::unordered_map<int32_t, InputDeviceLightInfo> mLights;
Chris Yee2b1e5c2021-03-10 22:45:12 -0800295 /* Map from battery ID to battery info */
296 std::unordered_map<int32_t, InputDeviceBatteryInfo> mBatteries;
Jeff Brown5912f952013-07-01 19:10:31 -0700297};
298
299/* Types of input device configuration files. */
Chris Ye1d927aa2020-07-04 18:22:41 -0700300enum class InputDeviceConfigurationFileType : int32_t {
301 CONFIGURATION = 0, /* .idc file */
302 KEY_LAYOUT = 1, /* .kl file */
303 KEY_CHARACTER_MAP = 2, /* .kcm file */
Jeff Brown5912f952013-07-01 19:10:31 -0700304};
305
306/*
307 * Gets the path of an input device configuration file, if one is available.
308 * Considers both system provided and user installed configuration files.
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700309 * The optional suffix is appended to the end of the file name (before the
310 * extension).
Jeff Brown5912f952013-07-01 19:10:31 -0700311 *
312 * The device identifier is used to construct several default configuration file
313 * names to try based on the device name, vendor, product, and version.
314 *
315 * Returns an empty string if not found.
316 */
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100317extern std::string getInputDeviceConfigurationFilePathByDeviceIdentifier(
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -0700318 const InputDeviceIdentifier& deviceIdentifier, InputDeviceConfigurationFileType type,
319 const char* suffix = "");
Jeff Brown5912f952013-07-01 19:10:31 -0700320
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.
324 *
325 * The name is case-sensitive and is used to construct the filename to resolve.
326 * All characters except 'a'-'z', 'A'-'Z', '0'-'9', '-', and '_' are replaced by underscores.
327 *
328 * Returns an empty string if not found.
329 */
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100330extern std::string getInputDeviceConfigurationFilePathByName(
331 const std::string& name, InputDeviceConfigurationFileType type);
Jeff Brown5912f952013-07-01 19:10:31 -0700332
Prabir Pradhancae4b3a2019-02-05 18:51:32 -0800333enum ReservedInputDeviceId : int32_t {
334 // Device id of a special "virtual" keyboard that is always present.
335 VIRTUAL_KEYBOARD_ID = -1,
336 // Device id of the "built-in" keyboard if there is one.
337 BUILT_IN_KEYBOARD_ID = 0,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800338 // First device id available for dynamic devices
339 END_RESERVED_ID = 1,
Prabir Pradhancae4b3a2019-02-05 18:51:32 -0800340};
341
Jeff Brown5912f952013-07-01 19:10:31 -0700342} // namespace android