xref: /aosp_15_r20/frameworks/native/libs/input/InputDevice.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
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 #define LOG_TAG "InputDevice"
18 
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <ctype.h>
22 
23 #include <android-base/logging.h>
24 #include <android-base/properties.h>
25 #include <android-base/stringprintf.h>
26 #include <ftl/enum.h>
27 #include <input/InputDevice.h>
28 #include <input/InputEventLabels.h>
29 
30 using android::base::GetProperty;
31 using android::base::StringPrintf;
32 
33 namespace android {
34 
35 // Set to true to log detailed debugging messages about IDC file probing.
36 static constexpr bool DEBUG_PROBE = false;
37 
38 static const char* CONFIGURATION_FILE_DIR[] = {
39         "idc/",
40         "keylayout/",
41         "keychars/",
42 };
43 
44 static const char* CONFIGURATION_FILE_EXTENSION[] = {
45         ".idc",
46         ".kl",
47         ".kcm",
48 };
49 
isValidNameChar(char ch)50 static bool isValidNameChar(char ch) {
51     return isascii(ch) && (isdigit(ch) || isalpha(ch) || ch == '-' || ch == '_');
52 }
53 
appendInputDeviceConfigurationFileRelativePath(std::string & path,const std::string & name,InputDeviceConfigurationFileType type)54 static void appendInputDeviceConfigurationFileRelativePath(std::string& path,
55         const std::string& name, InputDeviceConfigurationFileType type) {
56     path += CONFIGURATION_FILE_DIR[static_cast<int32_t>(type)];
57     path += name;
58     path += CONFIGURATION_FILE_EXTENSION[static_cast<int32_t>(type)];
59 }
60 
getInputDeviceConfigurationFilePathByDeviceIdentifier(const InputDeviceIdentifier & deviceIdentifier,InputDeviceConfigurationFileType type,const char * suffix)61 std::string getInputDeviceConfigurationFilePathByDeviceIdentifier(
62         const InputDeviceIdentifier& deviceIdentifier, InputDeviceConfigurationFileType type,
63         const char* suffix) {
64     if (deviceIdentifier.vendor !=0 && deviceIdentifier.product != 0) {
65         if (deviceIdentifier.version != 0) {
66             // Try vendor product version.
67             std::string versionPath =
68                     getInputDeviceConfigurationFilePathByName(StringPrintf("Vendor_%04x_Product_%"
69                                                                            "04x_Version_%04x%s",
70                                                                            deviceIdentifier.vendor,
71                                                                            deviceIdentifier.product,
72                                                                            deviceIdentifier.version,
73                                                                            suffix),
74                                                               type);
75             if (!versionPath.empty()) {
76                 return versionPath;
77             }
78         }
79 
80         // Try vendor product.
81         std::string productPath =
82                 getInputDeviceConfigurationFilePathByName(StringPrintf("Vendor_%04x_Product_%04x%s",
83                                                                        deviceIdentifier.vendor,
84                                                                        deviceIdentifier.product,
85                                                                        suffix),
86                                                           type);
87         if (!productPath.empty()) {
88             return productPath;
89         }
90     }
91 
92     // Try device name.
93     return getInputDeviceConfigurationFilePathByName(deviceIdentifier.getCanonicalName() + suffix,
94                                                      type);
95 }
96 
getInputDeviceConfigurationFilePathByName(const std::string & name,InputDeviceConfigurationFileType type)97 std::string getInputDeviceConfigurationFilePathByName(
98         const std::string& name, InputDeviceConfigurationFileType type) {
99     // Search system repository.
100     std::string path;
101 
102     // Treblized input device config files will be located /product/usr, /system_ext/usr,
103     // /odm/usr or /vendor/usr.
104     std::vector<std::string> pathPrefixes{
105             "/product/usr/",
106             "/system_ext/usr/",
107             "/odm/usr/",
108             "/vendor/usr/",
109     };
110     // These files may also be in the APEX pointed by input_device.config_file.apex sysprop.
111     if (auto apex = GetProperty("input_device.config_file.apex", ""); !apex.empty()) {
112         pathPrefixes.push_back("/apex/" + apex + "/etc/usr/");
113     }
114     // ANDROID_ROOT may not be set on host
115     if (auto android_root = getenv("ANDROID_ROOT"); android_root != nullptr) {
116         pathPrefixes.push_back(std::string(android_root) + "/usr/");
117     }
118     for (const auto& prefix : pathPrefixes) {
119         path = prefix;
120         appendInputDeviceConfigurationFileRelativePath(path, name, type);
121         if (!access(path.c_str(), R_OK)) {
122             LOG_IF(INFO, DEBUG_PROBE)
123                     << "Found system-provided input device configuration file at " << path;
124             return path;
125         } else if (errno != ENOENT) {
126             LOG(WARNING) << "Couldn't find a system-provided input device configuration file at "
127                          << path << " due to error " << errno << " (" << strerror(errno)
128                          << "); there may be an IDC file there that cannot be loaded.";
129         } else {
130             LOG_IF(ERROR, DEBUG_PROBE)
131                     << "Didn't find system-provided input device configuration file at " << path
132                     << ": " << strerror(errno);
133         }
134     }
135 
136     // Search user repository.
137     // TODO Should only look here if not in safe mode.
138     path = "";
139     char *androidData = getenv("ANDROID_DATA");
140     if (androidData != nullptr) {
141         path += androidData;
142     }
143     path += "/system/devices/";
144     appendInputDeviceConfigurationFileRelativePath(path, name, type);
145     if (!access(path.c_str(), R_OK)) {
146         LOG_IF(INFO, DEBUG_PROBE) << "Found system user input device configuration file at "
147                                   << path;
148         return path;
149     } else if (errno != ENOENT) {
150         LOG(WARNING) << "Couldn't find a system user input device configuration file at " << path
151                      << " due to error " << errno << " (" << strerror(errno)
152                      << "); there may be an IDC file there that cannot be loaded.";
153     } else {
154         LOG_IF(ERROR, DEBUG_PROBE) << "Didn't find system user input device configuration file at "
155                                    << path << ": " << strerror(errno);
156     }
157 
158     // Not found.
159     LOG_IF(INFO, DEBUG_PROBE) << "Probe failed to find input device configuration file with name '"
160                               << name << "' and type " << ftl::enum_string(type);
161     return "";
162 }
163 
164 // --- InputDeviceIdentifier
165 
getCanonicalName() const166 std::string InputDeviceIdentifier::getCanonicalName() const {
167     std::string replacedName = name;
168     for (char& ch : replacedName) {
169         if (!isValidNameChar(ch)) {
170             ch = '_';
171         }
172     }
173     return replacedName;
174 }
175 
176 
177 // --- InputDeviceInfo ---
178 
InputDeviceInfo()179 InputDeviceInfo::InputDeviceInfo() {
180     initialize(-1, 0, -1, InputDeviceIdentifier(), "", false, false, ui::LogicalDisplayId::INVALID);
181 }
182 
InputDeviceInfo(const InputDeviceInfo & other)183 InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other)
184       : mId(other.mId),
185         mGeneration(other.mGeneration),
186         mControllerNumber(other.mControllerNumber),
187         mIdentifier(other.mIdentifier),
188         mAlias(other.mAlias),
189         mIsExternal(other.mIsExternal),
190         mHasMic(other.mHasMic),
191         mKeyboardLayoutInfo(other.mKeyboardLayoutInfo),
192         mSources(other.mSources),
193         mKeyboardType(other.mKeyboardType),
194         mKeyCharacterMap(other.mKeyCharacterMap
195                                  ? std::make_unique<KeyCharacterMap>(*other.mKeyCharacterMap)
196                                  : nullptr),
197         mUsiVersion(other.mUsiVersion),
198         mAssociatedDisplayId(other.mAssociatedDisplayId),
199         mEnabled(other.mEnabled),
200         mHasVibrator(other.mHasVibrator),
201         mHasBattery(other.mHasBattery),
202         mHasButtonUnderPad(other.mHasButtonUnderPad),
203         mHasSensor(other.mHasSensor),
204         mMotionRanges(other.mMotionRanges),
205         mSensors(other.mSensors),
206         mLights(other.mLights),
207         mViewBehavior(other.mViewBehavior) {}
208 
operator =(const InputDeviceInfo & other)209 InputDeviceInfo& InputDeviceInfo::operator=(const InputDeviceInfo& other) {
210     mId = other.mId;
211     mGeneration = other.mGeneration;
212     mControllerNumber = other.mControllerNumber;
213     mIdentifier = other.mIdentifier;
214     mAlias = other.mAlias;
215     mIsExternal = other.mIsExternal;
216     mHasMic = other.mHasMic;
217     mKeyboardLayoutInfo = other.mKeyboardLayoutInfo;
218     mSources = other.mSources;
219     mKeyboardType = other.mKeyboardType;
220     mKeyCharacterMap = other.mKeyCharacterMap
221             ? std::make_unique<KeyCharacterMap>(*other.mKeyCharacterMap)
222             : nullptr;
223     mUsiVersion = other.mUsiVersion;
224     mAssociatedDisplayId = other.mAssociatedDisplayId;
225     mEnabled = other.mEnabled;
226     mHasVibrator = other.mHasVibrator;
227     mHasBattery = other.mHasBattery;
228     mHasButtonUnderPad = other.mHasButtonUnderPad;
229     mHasSensor = other.mHasSensor;
230     mMotionRanges = other.mMotionRanges;
231     mSensors = other.mSensors;
232     mLights = other.mLights;
233     mViewBehavior = other.mViewBehavior;
234     return *this;
235 }
236 
~InputDeviceInfo()237 InputDeviceInfo::~InputDeviceInfo() {
238 }
239 
initialize(int32_t id,int32_t generation,int32_t controllerNumber,const InputDeviceIdentifier & identifier,const std::string & alias,bool isExternal,bool hasMic,ui::LogicalDisplayId associatedDisplayId,InputDeviceViewBehavior viewBehavior,bool enabled)240 void InputDeviceInfo::initialize(int32_t id, int32_t generation, int32_t controllerNumber,
241                                  const InputDeviceIdentifier& identifier, const std::string& alias,
242                                  bool isExternal, bool hasMic,
243                                  ui::LogicalDisplayId associatedDisplayId,
244                                  InputDeviceViewBehavior viewBehavior, bool enabled) {
245     mId = id;
246     mGeneration = generation;
247     mControllerNumber = controllerNumber;
248     mIdentifier = identifier;
249     mAlias = alias;
250     mIsExternal = isExternal;
251     mHasMic = hasMic;
252     mSources = 0;
253     mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE;
254     mAssociatedDisplayId = associatedDisplayId;
255     mEnabled = enabled;
256     mHasVibrator = false;
257     mHasBattery = false;
258     mHasButtonUnderPad = false;
259     mHasSensor = false;
260     mViewBehavior = viewBehavior;
261     mUsiVersion.reset();
262     mMotionRanges.clear();
263     mSensors.clear();
264     mLights.clear();
265 }
266 
getMotionRange(int32_t axis,uint32_t source) const267 const InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange(
268         int32_t axis, uint32_t source) const {
269     for (const MotionRange& range : mMotionRanges) {
270         if (range.axis == axis && isFromSource(range.source, source)) {
271             return &range;
272         }
273     }
274     return nullptr;
275 }
276 
addSource(uint32_t source)277 void InputDeviceInfo::addSource(uint32_t source) {
278     mSources |= source;
279 }
280 
addMotionRange(int32_t axis,uint32_t source,float min,float max,float flat,float fuzz,float resolution)281 void InputDeviceInfo::addMotionRange(int32_t axis, uint32_t source, float min, float max,
282         float flat, float fuzz, float resolution) {
283     MotionRange range = { axis, source, min, max, flat, fuzz, resolution };
284     mMotionRanges.push_back(range);
285 }
286 
addMotionRange(const MotionRange & range)287 void InputDeviceInfo::addMotionRange(const MotionRange& range) {
288     mMotionRanges.push_back(range);
289 }
290 
addSensorInfo(const InputDeviceSensorInfo & info)291 void InputDeviceInfo::addSensorInfo(const InputDeviceSensorInfo& info) {
292     if (mSensors.find(info.type) != mSensors.end()) {
293         ALOGW("Sensor type %s already exists, will be replaced by new sensor added.",
294               ftl::enum_string(info.type).c_str());
295     }
296     mSensors.insert_or_assign(info.type, info);
297 }
298 
addBatteryInfo(const InputDeviceBatteryInfo & info)299 void InputDeviceInfo::addBatteryInfo(const InputDeviceBatteryInfo& info) {
300     if (mBatteries.find(info.id) != mBatteries.end()) {
301         ALOGW("Battery id %d already exists, will be replaced by new battery added.", info.id);
302     }
303     mBatteries.insert_or_assign(info.id, info);
304 }
305 
addLightInfo(const InputDeviceLightInfo & info)306 void InputDeviceInfo::addLightInfo(const InputDeviceLightInfo& info) {
307     if (mLights.find(info.id) != mLights.end()) {
308         ALOGW("Light id %d already exists, will be replaced by new light added.", info.id);
309     }
310     mLights.insert_or_assign(info.id, info);
311 }
312 
setKeyboardType(int32_t keyboardType)313 void InputDeviceInfo::setKeyboardType(int32_t keyboardType) {
314     mKeyboardType = keyboardType;
315 }
316 
setKeyboardLayoutInfo(KeyboardLayoutInfo layoutInfo)317 void InputDeviceInfo::setKeyboardLayoutInfo(KeyboardLayoutInfo layoutInfo) {
318     mKeyboardLayoutInfo = std::move(layoutInfo);
319 }
320 
getSensors()321 std::vector<InputDeviceSensorInfo> InputDeviceInfo::getSensors() {
322     std::vector<InputDeviceSensorInfo> infos;
323     infos.reserve(mSensors.size());
324     for (const auto& [type, info] : mSensors) {
325         infos.push_back(info);
326     }
327     return infos;
328 }
329 
getLights()330 std::vector<InputDeviceLightInfo> InputDeviceInfo::getLights() {
331     std::vector<InputDeviceLightInfo> infos;
332     infos.reserve(mLights.size());
333     for (const auto& [id, info] : mLights) {
334         infos.push_back(info);
335     }
336     return infos;
337 }
338 
339 } // namespace android
340