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