blob: fb6c590b6e0acbddec3eb1a30b657c693275a007 [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#define LOG_TAG "InputDevice"
18
19#include <stdlib.h>
20#include <unistd.h>
21#include <ctype.h>
22
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010023#include <android-base/stringprintf.h>
Dominik Laskowski75788452021-02-09 18:51:25 -080024#include <ftl/enum.h>
Jeff Brown5912f952013-07-01 19:10:31 -070025#include <input/InputDevice.h>
Jaekyun Seok7ead73e2017-02-28 18:07:03 +090026#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070027
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010028using android::base::StringPrintf;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000029using android::hardware::input::InputDeviceCountryCode;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010030
Jeff Brown5912f952013-07-01 19:10:31 -070031namespace android {
32
33static const char* CONFIGURATION_FILE_DIR[] = {
34 "idc/",
35 "keylayout/",
36 "keychars/",
37};
38
39static const char* CONFIGURATION_FILE_EXTENSION[] = {
40 ".idc",
41 ".kl",
42 ".kcm",
43};
44
45static bool isValidNameChar(char ch) {
46 return isascii(ch) && (isdigit(ch) || isalpha(ch) || ch == '-' || ch == '_');
47}
48
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010049static void appendInputDeviceConfigurationFileRelativePath(std::string& path,
50 const std::string& name, InputDeviceConfigurationFileType type) {
Chris Ye1d927aa2020-07-04 18:22:41 -070051 path += CONFIGURATION_FILE_DIR[static_cast<int32_t>(type)];
Siarhei Vishniakou409ec1a2019-02-20 19:36:25 -060052 path += name;
Chris Ye1d927aa2020-07-04 18:22:41 -070053 path += CONFIGURATION_FILE_EXTENSION[static_cast<int32_t>(type)];
Jeff Brown5912f952013-07-01 19:10:31 -070054}
55
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010056std::string getInputDeviceConfigurationFilePathByDeviceIdentifier(
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070057 const InputDeviceIdentifier& deviceIdentifier, InputDeviceConfigurationFileType type,
58 const char* suffix) {
Jeff Brown5912f952013-07-01 19:10:31 -070059 if (deviceIdentifier.vendor !=0 && deviceIdentifier.product != 0) {
60 if (deviceIdentifier.version != 0) {
61 // Try vendor product version.
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070062 std::string versionPath =
63 getInputDeviceConfigurationFilePathByName(StringPrintf("Vendor_%04x_Product_%"
64 "04x_Version_%04x%s",
65 deviceIdentifier.vendor,
66 deviceIdentifier.product,
67 deviceIdentifier.version,
68 suffix),
69 type);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010070 if (!versionPath.empty()) {
Jeff Brown5912f952013-07-01 19:10:31 -070071 return versionPath;
72 }
73 }
74
75 // Try vendor product.
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070076 std::string productPath =
77 getInputDeviceConfigurationFilePathByName(StringPrintf("Vendor_%04x_Product_%04x%s",
78 deviceIdentifier.vendor,
79 deviceIdentifier.product,
80 suffix),
81 type);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010082 if (!productPath.empty()) {
Jeff Brown5912f952013-07-01 19:10:31 -070083 return productPath;
84 }
85 }
86
87 // Try device name.
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070088 return getInputDeviceConfigurationFilePathByName(deviceIdentifier.getCanonicalName() + suffix,
89 type);
Jeff Brown5912f952013-07-01 19:10:31 -070090}
91
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010092std::string getInputDeviceConfigurationFilePathByName(
93 const std::string& name, InputDeviceConfigurationFileType type) {
Jeff Brown5912f952013-07-01 19:10:31 -070094 // Search system repository.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010095 std::string path;
Jaekyun Seok7ead73e2017-02-28 18:07:03 +090096
Hongguang Chen1438ccb2021-04-30 09:48:49 -070097 // Treblized input device config files will be located /product/usr, /system_ext/usr,
98 // /odm/usr or /vendor/usr.
Daniel Norman54283d62021-10-08 16:31:03 -070099 // These files may also be in the com.android.input.config APEX.
100 const char* rootsForPartition[]{
101 "/product",
102 "/system_ext",
103 "/odm",
104 "/vendor",
105 "/apex/com.android.input.config/etc",
106 getenv("ANDROID_ROOT"),
107 };
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900108 for (size_t i = 0; i < size(rootsForPartition); i++) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100109 if (rootsForPartition[i] == nullptr) {
110 continue;
111 }
112 path = rootsForPartition[i];
113 path += "/usr/";
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900114 appendInputDeviceConfigurationFileRelativePath(path, name, type);
Jeff Brown5912f952013-07-01 19:10:31 -0700115#if DEBUG_PROBE
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900116 ALOGD("Probing for system provided input device configuration file: path='%s'",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100117 path.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700118#endif
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100119 if (!access(path.c_str(), R_OK)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700120#if DEBUG_PROBE
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900121 ALOGD("Found");
Jeff Brown5912f952013-07-01 19:10:31 -0700122#endif
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900123 return path;
124 }
Jeff Brown5912f952013-07-01 19:10:31 -0700125 }
126
127 // Search user repository.
128 // TODO Should only look here if not in safe mode.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100129 path = "";
130 char *androidData = getenv("ANDROID_DATA");
131 if (androidData != nullptr) {
132 path += androidData;
133 }
134 path += "/system/devices/";
Jeff Brown5912f952013-07-01 19:10:31 -0700135 appendInputDeviceConfigurationFileRelativePath(path, name, type);
136#if DEBUG_PROBE
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100137 ALOGD("Probing for system user input device configuration file: path='%s'", path.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700138#endif
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100139 if (!access(path.c_str(), R_OK)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700140#if DEBUG_PROBE
141 ALOGD("Found");
142#endif
143 return path;
144 }
145
146 // Not found.
147#if DEBUG_PROBE
148 ALOGD("Probe failed to find input device configuration file: name='%s', type=%d",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100149 name.c_str(), type);
Jeff Brown5912f952013-07-01 19:10:31 -0700150#endif
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100151 return "";
Jeff Brown5912f952013-07-01 19:10:31 -0700152}
153
Siarhei Vishniakoub45635c2019-02-20 19:22:09 -0600154// --- InputDeviceIdentifier
155
156std::string InputDeviceIdentifier::getCanonicalName() const {
157 std::string replacedName = name;
158 for (char& ch : replacedName) {
159 if (!isValidNameChar(ch)) {
160 ch = '_';
161 }
162 }
163 return replacedName;
164}
165
Jeff Brown5912f952013-07-01 19:10:31 -0700166
167// --- InputDeviceInfo ---
168
169InputDeviceInfo::InputDeviceInfo() {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100170 initialize(-1, 0, -1, InputDeviceIdentifier(), "", false, false);
Jeff Brown5912f952013-07-01 19:10:31 -0700171}
172
Chris Ye3a1e4462020-08-12 10:13:15 -0700173InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other)
174 : mId(other.mId),
175 mGeneration(other.mGeneration),
176 mControllerNumber(other.mControllerNumber),
177 mIdentifier(other.mIdentifier),
178 mAlias(other.mAlias),
179 mIsExternal(other.mIsExternal),
180 mHasMic(other.mHasMic),
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000181 mCountryCode(other.mCountryCode),
Zixuan Qufecb6062022-11-12 04:44:31 +0000182 mKeyboardLayoutInfo(other.mKeyboardLayoutInfo),
Chris Ye3a1e4462020-08-12 10:13:15 -0700183 mSources(other.mSources),
184 mKeyboardType(other.mKeyboardType),
185 mKeyCharacterMap(other.mKeyCharacterMap),
Prabir Pradhan167c2702022-09-14 00:37:24 +0000186 mSupportsUsi(other.mSupportsUsi),
Chris Ye3a1e4462020-08-12 10:13:15 -0700187 mHasVibrator(other.mHasVibrator),
Kim Low03ea0352020-11-06 12:45:07 -0800188 mHasBattery(other.mHasBattery),
Chris Ye3a1e4462020-08-12 10:13:15 -0700189 mHasButtonUnderPad(other.mHasButtonUnderPad),
Chris Yef59a2f42020-10-16 12:55:26 -0700190 mHasSensor(other.mHasSensor),
191 mMotionRanges(other.mMotionRanges),
Chris Ye3fdbfef2021-01-06 18:45:18 -0800192 mSensors(other.mSensors),
193 mLights(other.mLights) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700194
195InputDeviceInfo::~InputDeviceInfo() {
196}
197
Michael Wright0415d632013-07-17 13:23:26 -0700198void InputDeviceInfo::initialize(int32_t id, int32_t generation, int32_t controllerNumber,
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000199 const InputDeviceIdentifier& identifier, const std::string& alias,
200 bool isExternal, bool hasMic, InputDeviceCountryCode countryCode) {
Jeff Brown5912f952013-07-01 19:10:31 -0700201 mId = id;
202 mGeneration = generation;
Michael Wright0415d632013-07-17 13:23:26 -0700203 mControllerNumber = controllerNumber;
Jeff Brown5912f952013-07-01 19:10:31 -0700204 mIdentifier = identifier;
205 mAlias = alias;
206 mIsExternal = isExternal;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700207 mHasMic = hasMic;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000208 mCountryCode = countryCode;
Jeff Brown5912f952013-07-01 19:10:31 -0700209 mSources = 0;
210 mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE;
211 mHasVibrator = false;
Kim Low03ea0352020-11-06 12:45:07 -0800212 mHasBattery = false;
Michael Wright931fd6d2013-07-10 18:05:15 -0700213 mHasButtonUnderPad = false;
Chris Yef59a2f42020-10-16 12:55:26 -0700214 mHasSensor = false;
Prabir Pradhan167c2702022-09-14 00:37:24 +0000215 mSupportsUsi = false;
Jeff Brown5912f952013-07-01 19:10:31 -0700216 mMotionRanges.clear();
Chris Yef59a2f42020-10-16 12:55:26 -0700217 mSensors.clear();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800218 mLights.clear();
Jeff Brown5912f952013-07-01 19:10:31 -0700219}
220
221const InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange(
222 int32_t axis, uint32_t source) const {
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800223 for (const MotionRange& range : mMotionRanges) {
224 if (range.axis == axis && isFromSource(range.source, source)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700225 return &range;
226 }
227 }
Yi Kong5bed83b2018-07-17 12:53:47 -0700228 return nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700229}
230
231void InputDeviceInfo::addSource(uint32_t source) {
232 mSources |= source;
233}
234
235void InputDeviceInfo::addMotionRange(int32_t axis, uint32_t source, float min, float max,
236 float flat, float fuzz, float resolution) {
237 MotionRange range = { axis, source, min, max, flat, fuzz, resolution };
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800238 mMotionRanges.push_back(range);
Jeff Brown5912f952013-07-01 19:10:31 -0700239}
240
241void InputDeviceInfo::addMotionRange(const MotionRange& range) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800242 mMotionRanges.push_back(range);
Jeff Brown5912f952013-07-01 19:10:31 -0700243}
244
Chris Yef59a2f42020-10-16 12:55:26 -0700245void InputDeviceInfo::addSensorInfo(const InputDeviceSensorInfo& info) {
246 if (mSensors.find(info.type) != mSensors.end()) {
247 ALOGW("Sensor type %s already exists, will be replaced by new sensor added.",
Dominik Laskowski75788452021-02-09 18:51:25 -0800248 ftl::enum_string(info.type).c_str());
Chris Yef59a2f42020-10-16 12:55:26 -0700249 }
250 mSensors.insert_or_assign(info.type, info);
251}
252
Chris Yee2b1e5c2021-03-10 22:45:12 -0800253void InputDeviceInfo::addBatteryInfo(const InputDeviceBatteryInfo& info) {
254 if (mBatteries.find(info.id) != mBatteries.end()) {
255 ALOGW("Battery id %d already exists, will be replaced by new battery added.", info.id);
256 }
257 mBatteries.insert_or_assign(info.id, info);
258}
259
Chris Ye3fdbfef2021-01-06 18:45:18 -0800260void InputDeviceInfo::addLightInfo(const InputDeviceLightInfo& info) {
261 if (mLights.find(info.id) != mLights.end()) {
262 ALOGW("Light id %d already exists, will be replaced by new light added.", info.id);
263 }
264 mLights.insert_or_assign(info.id, info);
265}
266
Philip Junkerf8437962022-01-25 21:20:19 +0100267void InputDeviceInfo::setKeyboardType(int32_t keyboardType) {
268 static_assert(AINPUT_KEYBOARD_TYPE_NONE < AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
269 static_assert(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC < AINPUT_KEYBOARD_TYPE_ALPHABETIC);
270 // There can be multiple subdevices with different keyboard types, set it to the highest type
271 mKeyboardType = std::max(mKeyboardType, keyboardType);
272}
273
Zixuan Qufecb6062022-11-12 04:44:31 +0000274void InputDeviceInfo::setKeyboardLayoutInfo(KeyboardLayoutInfo layoutInfo) {
275 mKeyboardLayoutInfo = std::move(layoutInfo);
276}
277
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000278std::vector<InputDeviceSensorInfo> InputDeviceInfo::getSensors() {
279 std::vector<InputDeviceSensorInfo> infos;
280 infos.reserve(mSensors.size());
Chris Yef59a2f42020-10-16 12:55:26 -0700281 for (const auto& [type, info] : mSensors) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000282 infos.push_back(info);
Chris Yef59a2f42020-10-16 12:55:26 -0700283 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000284 return infos;
Chris Yef59a2f42020-10-16 12:55:26 -0700285}
286
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000287std::vector<InputDeviceLightInfo> InputDeviceInfo::getLights() {
288 std::vector<InputDeviceLightInfo> infos;
289 infos.reserve(mLights.size());
Chris Ye3fdbfef2021-01-06 18:45:18 -0800290 for (const auto& [id, info] : mLights) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000291 infos.push_back(info);
Chris Ye3fdbfef2021-01-06 18:45:18 -0800292 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000293 return infos;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800294}
295
Jeff Brown5912f952013-07-01 19:10:31 -0700296} // namespace android