• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef _LIBINPUT_INPUT_DEVICE_H
18 #define _LIBINPUT_INPUT_DEVICE_H
19 
20 #include <android/sensor.h>
21 #include <input/Input.h>
22 #include <input/KeyCharacterMap.h>
23 #include <unordered_map>
24 #include <vector>
25 
26 namespace android {
27 
28 /*
29  * Identifies a device.
30  */
31 struct InputDeviceIdentifier {
InputDeviceIdentifierInputDeviceIdentifier32     inline InputDeviceIdentifier() :
33             bus(0), vendor(0), product(0), version(0) {
34     }
35 
36     // Information provided by the kernel.
37     std::string name;
38     std::string location;
39     std::string uniqueId;
40     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.
51     std::string descriptor;
52 
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;
57 
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;
66 };
67 
68 /* Types of input device sensors. Keep sync with core/java/android/hardware/Sensor.java */
69 enum 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     ftl_first = ACCELEROMETER,
89     ftl_last = SIGNIFICANT_MOTION
90 };
91 
92 enum class InputDeviceSensorAccuracy : int32_t {
93     ACCURACY_NONE = 0,
94     ACCURACY_LOW = 1,
95     ACCURACY_MEDIUM = 2,
96     ACCURACY_HIGH = 3,
97 };
98 
99 enum class InputDeviceSensorReportingMode : int32_t {
100     CONTINUOUS = 0,
101     ON_CHANGE = 1,
102     ONE_SHOT = 2,
103     SPECIAL_TRIGGER = 3,
104 };
105 
106 enum class InputDeviceLightType : int32_t {
107     MONO = 0,
108     PLAYER_ID = 1,
109     RGB = 2,
110     MULTI_COLOR = 3,
111 
112     ftl_last = MULTI_COLOR
113 };
114 
115 struct InputDeviceSensorInfo {
InputDeviceSensorInfoInputDeviceSensorInfo116     explicit InputDeviceSensorInfo(std::string name, std::string vendor, int32_t version,
117                                    InputDeviceSensorType type, InputDeviceSensorAccuracy accuracy,
118                                    float maxRange, float resolution, float power, int32_t minDelay,
119                                    int32_t fifoReservedEventCount, int32_t fifoMaxEventCount,
120                                    std::string stringType, int32_t maxDelay, int32_t flags,
121                                    int32_t id)
122           : name(name),
123             vendor(vendor),
124             version(version),
125             type(type),
126             accuracy(accuracy),
127             maxRange(maxRange),
128             resolution(resolution),
129             power(power),
130             minDelay(minDelay),
131             fifoReservedEventCount(fifoReservedEventCount),
132             fifoMaxEventCount(fifoMaxEventCount),
133             stringType(stringType),
134             maxDelay(maxDelay),
135             flags(flags),
136             id(id) {}
137     // Name string of the sensor.
138     std::string name;
139     // Vendor string of this sensor.
140     std::string vendor;
141     // Version of the sensor's module.
142     int32_t version;
143     // Generic type of this sensor.
144     InputDeviceSensorType type;
145     // The current accuracy of sensor event.
146     InputDeviceSensorAccuracy accuracy;
147     // Maximum range of the sensor in the sensor's unit.
148     float maxRange;
149     // Resolution of the sensor in the sensor's unit.
150     float resolution;
151     // The power in mA used by this sensor while in use.
152     float power;
153     // The minimum delay allowed between two events in microsecond or zero if this sensor only
154     // returns a value when the data it's measuring changes.
155     int32_t minDelay;
156     // Number of events reserved for this sensor in the batch mode FIFO.
157     int32_t fifoReservedEventCount;
158     // Maximum number of events of this sensor that could be batched.
159     int32_t fifoMaxEventCount;
160     // The type of this sensor as a string.
161     std::string stringType;
162     // The delay between two sensor events corresponding to the lowest frequency that this sensor
163     // supports.
164     int32_t maxDelay;
165     // Sensor flags
166     int32_t flags;
167     // Sensor id, same as the input device ID it belongs to.
168     int32_t id;
169 };
170 
171 struct InputDeviceLightInfo {
InputDeviceLightInfoInputDeviceLightInfo172     explicit InputDeviceLightInfo(std::string name, int32_t id, InputDeviceLightType type,
173                                   int32_t ordinal)
174           : name(name), id(id), type(type), ordinal(ordinal) {}
175     // Name string of the light.
176     std::string name;
177     // Light id
178     int32_t id;
179     // Type of the light.
180     InputDeviceLightType type;
181     // Ordinal of the light
182     int32_t ordinal;
183 };
184 
185 struct InputDeviceBatteryInfo {
InputDeviceBatteryInfoInputDeviceBatteryInfo186     explicit InputDeviceBatteryInfo(std::string name, int32_t id) : name(name), id(id) {}
187     // Name string of the battery.
188     std::string name;
189     // Battery id
190     int32_t id;
191 };
192 
193 /*
194  * Describes the characteristics and capabilities of an input device.
195  */
196 class InputDeviceInfo {
197 public:
198     InputDeviceInfo();
199     InputDeviceInfo(const InputDeviceInfo& other);
200     ~InputDeviceInfo();
201 
202     struct MotionRange {
203         int32_t axis;
204         uint32_t source;
205         float min;
206         float max;
207         float flat;
208         float fuzz;
209         float resolution;
210     };
211 
212     void initialize(int32_t id, int32_t generation, int32_t controllerNumber,
213             const InputDeviceIdentifier& identifier, const std::string& alias, bool isExternal,
214             bool hasMic);
215 
getId()216     inline int32_t getId() const { return mId; }
getControllerNumber()217     inline int32_t getControllerNumber() const { return mControllerNumber; }
getGeneration()218     inline int32_t getGeneration() const { return mGeneration; }
getIdentifier()219     inline const InputDeviceIdentifier& getIdentifier() const { return mIdentifier; }
getAlias()220     inline const std::string& getAlias() const { return mAlias; }
getDisplayName()221     inline const std::string& getDisplayName() const {
222         return mAlias.empty() ? mIdentifier.name : mAlias;
223     }
isExternal()224     inline bool isExternal() const { return mIsExternal; }
hasMic()225     inline bool hasMic() const { return mHasMic; }
getSources()226     inline uint32_t getSources() const { return mSources; }
227 
228     const MotionRange* getMotionRange(int32_t axis, uint32_t source) const;
229 
230     void addSource(uint32_t source);
231     void addMotionRange(int32_t axis, uint32_t source,
232             float min, float max, float flat, float fuzz, float resolution);
233     void addMotionRange(const MotionRange& range);
234     void addSensorInfo(const InputDeviceSensorInfo& info);
235     void addBatteryInfo(const InputDeviceBatteryInfo& info);
236     void addLightInfo(const InputDeviceLightInfo& info);
237 
238     void setKeyboardType(int32_t keyboardType);
getKeyboardType()239     inline int32_t getKeyboardType() const { return mKeyboardType; }
240 
setKeyCharacterMap(const std::shared_ptr<KeyCharacterMap> value)241     inline void setKeyCharacterMap(const std::shared_ptr<KeyCharacterMap> value) {
242         mKeyCharacterMap = value;
243     }
244 
getKeyCharacterMap()245     inline const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const {
246         return mKeyCharacterMap;
247     }
248 
setVibrator(bool hasVibrator)249     inline void setVibrator(bool hasVibrator) { mHasVibrator = hasVibrator; }
hasVibrator()250     inline bool hasVibrator() const { return mHasVibrator; }
251 
setHasBattery(bool hasBattery)252     inline void setHasBattery(bool hasBattery) { mHasBattery = hasBattery; }
hasBattery()253     inline bool hasBattery() const { return mHasBattery; }
254 
setButtonUnderPad(bool hasButton)255     inline void setButtonUnderPad(bool hasButton) { mHasButtonUnderPad = hasButton; }
hasButtonUnderPad()256     inline bool hasButtonUnderPad() const { return mHasButtonUnderPad; }
257 
setHasSensor(bool hasSensor)258     inline void setHasSensor(bool hasSensor) { mHasSensor = hasSensor; }
hasSensor()259     inline bool hasSensor() const { return mHasSensor; }
260 
getMotionRanges()261     inline const std::vector<MotionRange>& getMotionRanges() const {
262         return mMotionRanges;
263     }
264 
265     std::vector<InputDeviceSensorInfo> getSensors();
266 
267     std::vector<InputDeviceLightInfo> getLights();
268 
269 private:
270     int32_t mId;
271     int32_t mGeneration;
272     int32_t mControllerNumber;
273     InputDeviceIdentifier mIdentifier;
274     std::string mAlias;
275     bool mIsExternal;
276     bool mHasMic;
277     uint32_t mSources;
278     int32_t mKeyboardType;
279     std::shared_ptr<KeyCharacterMap> mKeyCharacterMap;
280     bool mHasVibrator;
281     bool mHasBattery;
282     bool mHasButtonUnderPad;
283     bool mHasSensor;
284 
285     std::vector<MotionRange> mMotionRanges;
286     std::unordered_map<InputDeviceSensorType, InputDeviceSensorInfo> mSensors;
287     /* Map from light ID to light info */
288     std::unordered_map<int32_t, InputDeviceLightInfo> mLights;
289     /* Map from battery ID to battery info */
290     std::unordered_map<int32_t, InputDeviceBatteryInfo> mBatteries;
291 };
292 
293 /* Types of input device configuration files. */
294 enum class InputDeviceConfigurationFileType : int32_t {
295     CONFIGURATION = 0,     /* .idc file */
296     KEY_LAYOUT = 1,        /* .kl file */
297     KEY_CHARACTER_MAP = 2, /* .kcm file */
298 };
299 
300 /*
301  * Gets the path of an input device configuration file, if one is available.
302  * Considers both system provided and user installed configuration files.
303  * The optional suffix is appended to the end of the file name (before the
304  * extension).
305  *
306  * The device identifier is used to construct several default configuration file
307  * names to try based on the device name, vendor, product, and version.
308  *
309  * Returns an empty string if not found.
310  */
311 extern std::string getInputDeviceConfigurationFilePathByDeviceIdentifier(
312         const InputDeviceIdentifier& deviceIdentifier, InputDeviceConfigurationFileType type,
313         const char* suffix = "");
314 
315 /*
316  * Gets the path of an input device configuration file, if one is available.
317  * Considers both system provided and user installed configuration files.
318  *
319  * The name is case-sensitive and is used to construct the filename to resolve.
320  * All characters except 'a'-'z', 'A'-'Z', '0'-'9', '-', and '_' are replaced by underscores.
321  *
322  * Returns an empty string if not found.
323  */
324 extern std::string getInputDeviceConfigurationFilePathByName(
325         const std::string& name, InputDeviceConfigurationFileType type);
326 
327 enum ReservedInputDeviceId : int32_t {
328     // Device id of a special "virtual" keyboard that is always present.
329     VIRTUAL_KEYBOARD_ID = -1,
330     // Device id of the "built-in" keyboard if there is one.
331     BUILT_IN_KEYBOARD_ID = 0,
332     // First device id available for dynamic devices
333     END_RESERVED_ID = 1,
334 };
335 
336 } // namespace android
337 
338 #endif // _LIBINPUT_INPUT_DEVICE_H
339