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 <input/Input.h>
21 #include <input/KeyCharacterMap.h>
22 
23 namespace android {
24 
25 /*
26  * Identifies a device.
27  */
28 struct InputDeviceIdentifier {
InputDeviceIdentifierInputDeviceIdentifier29     inline InputDeviceIdentifier() :
30             bus(0), vendor(0), product(0), version(0) {
31     }
32 
33     // Information provided by the kernel.
34     String8 name;
35     String8 location;
36     String8 uniqueId;
37     uint16_t bus;
38     uint16_t vendor;
39     uint16_t product;
40     uint16_t version;
41 
42     // A composite input device descriptor string that uniquely identifies the device
43     // even across reboots or reconnections.  The value of this field is used by
44     // upper layers of the input system to associate settings with individual devices.
45     // It is hashed from whatever kernel provided information is available.
46     // Ideally, the way this value is computed should not change between Android releases
47     // because that would invalidate persistent settings that rely on it.
48     String8 descriptor;
49 
50     // A value added to uniquely identify a device in the absence of a unique id. This
51     // is intended to be a minimum way to distinguish from other active devices and may
52     // reuse values that are not associated with an input anymore.
53     uint16_t nonce;
54 };
55 
56 /*
57  * Describes the characteristics and capabilities of an input device.
58  */
59 class InputDeviceInfo {
60 public:
61     InputDeviceInfo();
62     InputDeviceInfo(const InputDeviceInfo& other);
63     ~InputDeviceInfo();
64 
65     struct MotionRange {
66         int32_t axis;
67         uint32_t source;
68         float min;
69         float max;
70         float flat;
71         float fuzz;
72         float resolution;
73     };
74 
75     void initialize(int32_t id, int32_t generation, int32_t controllerNumber,
76             const InputDeviceIdentifier& identifier, const String8& alias, bool isExternal,
77             bool hasMic);
78 
getId()79     inline int32_t getId() const { return mId; }
getControllerNumber()80     inline int32_t getControllerNumber() const { return mControllerNumber; }
getGeneration()81     inline int32_t getGeneration() const { return mGeneration; }
getIdentifier()82     inline const InputDeviceIdentifier& getIdentifier() const { return mIdentifier; }
getAlias()83     inline const String8& getAlias() const { return mAlias; }
getDisplayName()84     inline const String8& getDisplayName() const {
85         return mAlias.isEmpty() ? mIdentifier.name : mAlias;
86     }
isExternal()87     inline bool isExternal() const { return mIsExternal; }
hasMic()88     inline bool hasMic() const { return mHasMic; }
getSources()89     inline uint32_t getSources() const { return mSources; }
90 
91     const MotionRange* getMotionRange(int32_t axis, uint32_t source) const;
92 
93     void addSource(uint32_t source);
94     void addMotionRange(int32_t axis, uint32_t source,
95             float min, float max, float flat, float fuzz, float resolution);
96     void addMotionRange(const MotionRange& range);
97 
setKeyboardType(int32_t keyboardType)98     inline void setKeyboardType(int32_t keyboardType) { mKeyboardType = keyboardType; }
getKeyboardType()99     inline int32_t getKeyboardType() const { return mKeyboardType; }
100 
setKeyCharacterMap(const sp<KeyCharacterMap> & value)101     inline void setKeyCharacterMap(const sp<KeyCharacterMap>& value) {
102         mKeyCharacterMap = value;
103     }
104 
getKeyCharacterMap()105     inline sp<KeyCharacterMap> getKeyCharacterMap() const {
106         return mKeyCharacterMap;
107     }
108 
setVibrator(bool hasVibrator)109     inline void setVibrator(bool hasVibrator) { mHasVibrator = hasVibrator; }
hasVibrator()110     inline bool hasVibrator() const { return mHasVibrator; }
111 
setButtonUnderPad(bool hasButton)112     inline void setButtonUnderPad(bool hasButton) { mHasButtonUnderPad = hasButton; }
hasButtonUnderPad()113     inline bool hasButtonUnderPad() const { return mHasButtonUnderPad; }
114 
getMotionRanges()115     inline const Vector<MotionRange>& getMotionRanges() const {
116         return mMotionRanges;
117     }
118 
119 private:
120     int32_t mId;
121     int32_t mGeneration;
122     int32_t mControllerNumber;
123     InputDeviceIdentifier mIdentifier;
124     String8 mAlias;
125     bool mIsExternal;
126     bool mHasMic;
127     uint32_t mSources;
128     int32_t mKeyboardType;
129     sp<KeyCharacterMap> mKeyCharacterMap;
130     bool mHasVibrator;
131     bool mHasButtonUnderPad;
132 
133     Vector<MotionRange> mMotionRanges;
134 };
135 
136 /* Types of input device configuration files. */
137 enum InputDeviceConfigurationFileType {
138     INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION = 0,     /* .idc file */
139     INPUT_DEVICE_CONFIGURATION_FILE_TYPE_KEY_LAYOUT = 1,        /* .kl file */
140     INPUT_DEVICE_CONFIGURATION_FILE_TYPE_KEY_CHARACTER_MAP = 2, /* .kcm file */
141 };
142 
143 /*
144  * Gets the path of an input device configuration file, if one is available.
145  * Considers both system provided and user installed configuration files.
146  *
147  * The device identifier is used to construct several default configuration file
148  * names to try based on the device name, vendor, product, and version.
149  *
150  * Returns an empty string if not found.
151  */
152 extern String8 getInputDeviceConfigurationFilePathByDeviceIdentifier(
153         const InputDeviceIdentifier& deviceIdentifier,
154         InputDeviceConfigurationFileType type);
155 
156 /*
157  * Gets the path of an input device configuration file, if one is available.
158  * Considers both system provided and user installed configuration files.
159  *
160  * The name is case-sensitive and is used to construct the filename to resolve.
161  * All characters except 'a'-'z', 'A'-'Z', '0'-'9', '-', and '_' are replaced by underscores.
162  *
163  * Returns an empty string if not found.
164  */
165 extern String8 getInputDeviceConfigurationFilePathByName(
166         const String8& name, InputDeviceConfigurationFileType type);
167 
168 } // namespace android
169 
170 #endif // _LIBINPUT_INPUT_DEVICE_H
171