blob: 2bd7bd2004b3949ee81814712521351454ba77d7 [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
17#ifndef _LIBINPUT_INPUT_DEVICE_H
18#define _LIBINPUT_INPUT_DEVICE_H
19
Chris Yef59a2f42020-10-16 12:55:26 -070020#include <android/sensor.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
26namespace android {
27
28/*
29 * Identifies a device.
30 */
31struct InputDeviceIdentifier {
32 inline InputDeviceIdentifier() :
33 bus(0), vendor(0), product(0), version(0) {
34 }
35
36 // Information provided by the kernel.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010037 std::string name;
38 std::string location;
39 std::string uniqueId;
Jeff Brown5912f952013-07-01 19:10:31 -070040 uint16_t bus;
41 uint16_t vendor;
42 uint16_t product;
43 uint16_t version;
44
45 // A composite input device descriptor string that uniquely identifies the device
46 // even across reboots or reconnections. The value of this field is used by
47 // upper layers of the input system to associate settings with individual devices.
48 // It is hashed from whatever kernel provided information is available.
49 // Ideally, the way this value is computed should not change between Android releases
50 // because that would invalidate persistent settings that rely on it.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010051 std::string descriptor;
RoboErikec2a15a2013-12-19 11:54:29 -080052
53 // A value added to uniquely identify a device in the absence of a unique id. This
54 // is intended to be a minimum way to distinguish from other active devices and may
55 // reuse values that are not associated with an input anymore.
56 uint16_t nonce;
Siarhei Vishniakoub45635c2019-02-20 19:22:09 -060057
58 /**
59 * Return InputDeviceIdentifier.name that has been adjusted as follows:
60 * - all characters besides alphanumerics, dash,
61 * and underscore have been replaced with underscores.
62 * This helps in situations where a file that matches the device name is needed,
63 * while conforming to the filename limitations.
64 */
65 std::string getCanonicalName() const;
Jeff Brown5912f952013-07-01 19:10:31 -070066};
67
Chris Yef59a2f42020-10-16 12:55:26 -070068/* Types of input device sensors. Keep sync with core/java/android/hardware/Sensor.java */
69enum class InputDeviceSensorType : int32_t {
70 ACCELEROMETER = ASENSOR_TYPE_ACCELEROMETER,
71 MAGNETIC_FIELD = ASENSOR_TYPE_MAGNETIC_FIELD,
72 ORIENTATION = 3,
73 GYROSCOPE = ASENSOR_TYPE_GYROSCOPE,
74 LIGHT = ASENSOR_TYPE_LIGHT,
75 PRESSURE = ASENSOR_TYPE_PRESSURE,
76 TEMPERATURE = 7,
77 PROXIMITY = ASENSOR_TYPE_PROXIMITY,
78 GRAVITY = ASENSOR_TYPE_GRAVITY,
79 LINEAR_ACCELERATION = ASENSOR_TYPE_LINEAR_ACCELERATION,
80 ROTATION_VECTOR = ASENSOR_TYPE_ROTATION_VECTOR,
81 RELATIVE_HUMIDITY = ASENSOR_TYPE_RELATIVE_HUMIDITY,
82 AMBIENT_TEMPERATURE = ASENSOR_TYPE_AMBIENT_TEMPERATURE,
83 MAGNETIC_FIELD_UNCALIBRATED = ASENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED,
84 GAME_ROTATION_VECTOR = ASENSOR_TYPE_GAME_ROTATION_VECTOR,
85 GYROSCOPE_UNCALIBRATED = ASENSOR_TYPE_GYROSCOPE_UNCALIBRATED,
86 SIGNIFICANT_MOTION = ASENSOR_TYPE_SIGNIFICANT_MOTION,
87};
88
89enum class InputDeviceSensorAccuracy : int32_t {
90 ACCURACY_NONE = 0,
91 ACCURACY_LOW = 1,
92 ACCURACY_MEDIUM = 2,
93 ACCURACY_HIGH = 3,
94};
95
96enum class InputDeviceSensorReportingMode : int32_t {
97 CONTINUOUS = 0,
98 ON_CHANGE = 1,
99 ONE_SHOT = 2,
100 SPECIAL_TRIGGER = 3,
101};
102
103struct InputDeviceSensorInfo {
104 explicit InputDeviceSensorInfo(std::string name, std::string vendor, int32_t version,
105 InputDeviceSensorType type, InputDeviceSensorAccuracy accuracy,
106 float maxRange, float resolution, float power, int32_t minDelay,
107 int32_t fifoReservedEventCount, int32_t fifoMaxEventCount,
108 std::string stringType, int32_t maxDelay, int32_t flags,
109 int32_t id)
110 : name(name),
111 vendor(vendor),
112 version(version),
113 type(type),
114 accuracy(accuracy),
115 maxRange(maxRange),
116 resolution(resolution),
117 power(power),
118 minDelay(minDelay),
119 fifoReservedEventCount(fifoReservedEventCount),
120 fifoMaxEventCount(fifoMaxEventCount),
121 stringType(stringType),
122 maxDelay(maxDelay),
123 flags(flags),
124 id(id) {}
125 // Name string of the sensor.
126 std::string name;
127 // Vendor string of this sensor.
128 std::string vendor;
129 // Version of the sensor's module.
130 int32_t version;
131 // Generic type of this sensor.
132 InputDeviceSensorType type;
133 // The current accuracy of sensor event.
134 InputDeviceSensorAccuracy accuracy;
135 // Maximum range of the sensor in the sensor's unit.
136 float maxRange;
137 // Resolution of the sensor in the sensor's unit.
138 float resolution;
139 // The power in mA used by this sensor while in use.
140 float power;
141 // The minimum delay allowed between two events in microsecond or zero if this sensor only
142 // returns a value when the data it's measuring changes.
143 int32_t minDelay;
144 // Number of events reserved for this sensor in the batch mode FIFO.
145 int32_t fifoReservedEventCount;
146 // Maximum number of events of this sensor that could be batched.
147 int32_t fifoMaxEventCount;
148 // The type of this sensor as a string.
149 std::string stringType;
150 // The delay between two sensor events corresponding to the lowest frequency that this sensor
151 // supports.
152 int32_t maxDelay;
153 // Sensor flags
154 int32_t flags;
155 // Sensor id, same as the input device ID it belongs to.
156 int32_t id;
157};
158
Jeff Brown5912f952013-07-01 19:10:31 -0700159/*
160 * Describes the characteristics and capabilities of an input device.
161 */
162class InputDeviceInfo {
163public:
164 InputDeviceInfo();
165 InputDeviceInfo(const InputDeviceInfo& other);
166 ~InputDeviceInfo();
167
168 struct MotionRange {
169 int32_t axis;
170 uint32_t source;
171 float min;
172 float max;
173 float flat;
174 float fuzz;
175 float resolution;
176 };
177
Michael Wright0415d632013-07-17 13:23:26 -0700178 void initialize(int32_t id, int32_t generation, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100179 const InputDeviceIdentifier& identifier, const std::string& alias, bool isExternal,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700180 bool hasMic);
Jeff Brown5912f952013-07-01 19:10:31 -0700181
182 inline int32_t getId() const { return mId; }
Michael Wright0415d632013-07-17 13:23:26 -0700183 inline int32_t getControllerNumber() const { return mControllerNumber; }
Jeff Brown5912f952013-07-01 19:10:31 -0700184 inline int32_t getGeneration() const { return mGeneration; }
185 inline const InputDeviceIdentifier& getIdentifier() const { return mIdentifier; }
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100186 inline const std::string& getAlias() const { return mAlias; }
187 inline const std::string& getDisplayName() const {
188 return mAlias.empty() ? mIdentifier.name : mAlias;
Jeff Brown5912f952013-07-01 19:10:31 -0700189 }
190 inline bool isExternal() const { return mIsExternal; }
Tim Kilbourn063ff532015-04-08 10:26:18 -0700191 inline bool hasMic() const { return mHasMic; }
Jeff Brown5912f952013-07-01 19:10:31 -0700192 inline uint32_t getSources() const { return mSources; }
193
194 const MotionRange* getMotionRange(int32_t axis, uint32_t source) const;
195
196 void addSource(uint32_t source);
197 void addMotionRange(int32_t axis, uint32_t source,
198 float min, float max, float flat, float fuzz, float resolution);
199 void addMotionRange(const MotionRange& range);
Chris Yef59a2f42020-10-16 12:55:26 -0700200 void addSensorInfo(const InputDeviceSensorInfo& info);
Jeff Brown5912f952013-07-01 19:10:31 -0700201
202 inline void setKeyboardType(int32_t keyboardType) { mKeyboardType = keyboardType; }
203 inline int32_t getKeyboardType() const { return mKeyboardType; }
204
Chris Ye3a1e4462020-08-12 10:13:15 -0700205 inline void setKeyCharacterMap(const std::shared_ptr<KeyCharacterMap> value) {
Jeff Brown5912f952013-07-01 19:10:31 -0700206 mKeyCharacterMap = value;
207 }
208
Chris Ye3a1e4462020-08-12 10:13:15 -0700209 inline const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const {
Jeff Brown5912f952013-07-01 19:10:31 -0700210 return mKeyCharacterMap;
211 }
212
213 inline void setVibrator(bool hasVibrator) { mHasVibrator = hasVibrator; }
214 inline bool hasVibrator() const { return mHasVibrator; }
215
Kim Low03ea0352020-11-06 12:45:07 -0800216 inline void setHasBattery(bool hasBattery) { mHasBattery = hasBattery; }
217 inline bool hasBattery() const { return mHasBattery; }
218
Michael Wright931fd6d2013-07-10 18:05:15 -0700219 inline void setButtonUnderPad(bool hasButton) { mHasButtonUnderPad = hasButton; }
220 inline bool hasButtonUnderPad() const { return mHasButtonUnderPad; }
221
Chris Yef59a2f42020-10-16 12:55:26 -0700222 inline void setHasSensor(bool hasSensor) { mHasSensor = hasSensor; }
223 inline bool hasSensor() const { return mHasSensor; }
224
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800225 inline const std::vector<MotionRange>& getMotionRanges() const {
Jeff Brown5912f952013-07-01 19:10:31 -0700226 return mMotionRanges;
227 }
228
Chris Yef59a2f42020-10-16 12:55:26 -0700229 const InputDeviceSensorInfo* getSensorInfo(InputDeviceSensorType type);
230
231 const std::vector<InputDeviceSensorType> getSensorTypes();
232
Jeff Brown5912f952013-07-01 19:10:31 -0700233private:
234 int32_t mId;
235 int32_t mGeneration;
Michael Wright0415d632013-07-17 13:23:26 -0700236 int32_t mControllerNumber;
Jeff Brown5912f952013-07-01 19:10:31 -0700237 InputDeviceIdentifier mIdentifier;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100238 std::string mAlias;
Jeff Brown5912f952013-07-01 19:10:31 -0700239 bool mIsExternal;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700240 bool mHasMic;
Jeff Brown5912f952013-07-01 19:10:31 -0700241 uint32_t mSources;
242 int32_t mKeyboardType;
Chris Ye3a1e4462020-08-12 10:13:15 -0700243 std::shared_ptr<KeyCharacterMap> mKeyCharacterMap;
Jeff Brown5912f952013-07-01 19:10:31 -0700244 bool mHasVibrator;
Kim Low03ea0352020-11-06 12:45:07 -0800245 bool mHasBattery;
Michael Wright931fd6d2013-07-10 18:05:15 -0700246 bool mHasButtonUnderPad;
Chris Yef59a2f42020-10-16 12:55:26 -0700247 bool mHasSensor;
Jeff Brown5912f952013-07-01 19:10:31 -0700248
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800249 std::vector<MotionRange> mMotionRanges;
Chris Yef59a2f42020-10-16 12:55:26 -0700250 std::unordered_map<InputDeviceSensorType, InputDeviceSensorInfo> mSensors;
Jeff Brown5912f952013-07-01 19:10:31 -0700251};
252
253/* Types of input device configuration files. */
Chris Ye1d927aa2020-07-04 18:22:41 -0700254enum class InputDeviceConfigurationFileType : int32_t {
255 CONFIGURATION = 0, /* .idc file */
256 KEY_LAYOUT = 1, /* .kl file */
257 KEY_CHARACTER_MAP = 2, /* .kcm file */
Jeff Brown5912f952013-07-01 19:10:31 -0700258};
259
260/*
261 * Gets the path of an input device configuration file, if one is available.
262 * Considers both system provided and user installed configuration files.
263 *
264 * The device identifier is used to construct several default configuration file
265 * names to try based on the device name, vendor, product, and version.
266 *
267 * Returns an empty string if not found.
268 */
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100269extern std::string getInputDeviceConfigurationFilePathByDeviceIdentifier(
Jeff Brown5912f952013-07-01 19:10:31 -0700270 const InputDeviceIdentifier& deviceIdentifier,
271 InputDeviceConfigurationFileType type);
272
273/*
274 * Gets the path of an input device configuration file, if one is available.
275 * Considers both system provided and user installed configuration files.
276 *
277 * The name is case-sensitive and is used to construct the filename to resolve.
278 * All characters except 'a'-'z', 'A'-'Z', '0'-'9', '-', and '_' are replaced by underscores.
279 *
280 * Returns an empty string if not found.
281 */
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100282extern std::string getInputDeviceConfigurationFilePathByName(
283 const std::string& name, InputDeviceConfigurationFileType type);
Jeff Brown5912f952013-07-01 19:10:31 -0700284
Prabir Pradhancae4b3a2019-02-05 18:51:32 -0800285enum ReservedInputDeviceId : int32_t {
286 // Device id of a special "virtual" keyboard that is always present.
287 VIRTUAL_KEYBOARD_ID = -1,
288 // Device id of the "built-in" keyboard if there is one.
289 BUILT_IN_KEYBOARD_ID = 0,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800290 // First device id available for dynamic devices
291 END_RESERVED_ID = 1,
Prabir Pradhancae4b3a2019-02-05 18:51:32 -0800292};
293
Jeff Brown5912f952013-07-01 19:10:31 -0700294} // namespace android
295
296#endif // _LIBINPUT_INPUT_DEVICE_H