blob: fcbc16f9c7e731191d1a496495159940ce0ed021 [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>
Jeff Brown5912f952013-07-01 19:10:31 -070024#include <input/InputDevice.h>
Jaekyun Seok7ead73e2017-02-28 18:07:03 +090025#include <input/InputEventLabels.h>
Chris Yef59a2f42020-10-16 12:55:26 -070026#include <input/NamedEnum.h>
Jeff Brown5912f952013-07-01 19:10:31 -070027
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010028using android::base::StringPrintf;
29
Jeff Brown5912f952013-07-01 19:10:31 -070030namespace android {
31
32static const char* CONFIGURATION_FILE_DIR[] = {
33 "idc/",
34 "keylayout/",
35 "keychars/",
36};
37
38static const char* CONFIGURATION_FILE_EXTENSION[] = {
39 ".idc",
40 ".kl",
41 ".kcm",
42};
43
44static bool isValidNameChar(char ch) {
45 return isascii(ch) && (isdigit(ch) || isalpha(ch) || ch == '-' || ch == '_');
46}
47
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010048static void appendInputDeviceConfigurationFileRelativePath(std::string& path,
49 const std::string& name, InputDeviceConfigurationFileType type) {
Chris Ye1d927aa2020-07-04 18:22:41 -070050 path += CONFIGURATION_FILE_DIR[static_cast<int32_t>(type)];
Siarhei Vishniakou409ec1a2019-02-20 19:36:25 -060051 path += name;
Chris Ye1d927aa2020-07-04 18:22:41 -070052 path += CONFIGURATION_FILE_EXTENSION[static_cast<int32_t>(type)];
Jeff Brown5912f952013-07-01 19:10:31 -070053}
54
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010055std::string getInputDeviceConfigurationFilePathByDeviceIdentifier(
Jeff Brown5912f952013-07-01 19:10:31 -070056 const InputDeviceIdentifier& deviceIdentifier,
57 InputDeviceConfigurationFileType type) {
58 if (deviceIdentifier.vendor !=0 && deviceIdentifier.product != 0) {
59 if (deviceIdentifier.version != 0) {
60 // Try vendor product version.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010061 std::string versionPath = getInputDeviceConfigurationFilePathByName(
62 StringPrintf("Vendor_%04x_Product_%04x_Version_%04x",
Jeff Brown5912f952013-07-01 19:10:31 -070063 deviceIdentifier.vendor, deviceIdentifier.product,
64 deviceIdentifier.version),
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010065 type);
66 if (!versionPath.empty()) {
Jeff Brown5912f952013-07-01 19:10:31 -070067 return versionPath;
68 }
69 }
70
71 // Try vendor product.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010072 std::string productPath = getInputDeviceConfigurationFilePathByName(
73 StringPrintf("Vendor_%04x_Product_%04x",
Jeff Brown5912f952013-07-01 19:10:31 -070074 deviceIdentifier.vendor, deviceIdentifier.product),
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010075 type);
76 if (!productPath.empty()) {
Jeff Brown5912f952013-07-01 19:10:31 -070077 return productPath;
78 }
79 }
80
81 // Try device name.
Siarhei Vishniakou409ec1a2019-02-20 19:36:25 -060082 return getInputDeviceConfigurationFilePathByName(deviceIdentifier.getCanonicalName(), type);
Jeff Brown5912f952013-07-01 19:10:31 -070083}
84
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010085std::string getInputDeviceConfigurationFilePathByName(
86 const std::string& name, InputDeviceConfigurationFileType type) {
Jeff Brown5912f952013-07-01 19:10:31 -070087 // Search system repository.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010088 std::string path;
Jaekyun Seok7ead73e2017-02-28 18:07:03 +090089
Hongguang Chen1438ccb2021-04-30 09:48:49 -070090 // Treblized input device config files will be located /product/usr, /system_ext/usr,
91 // /odm/usr or /vendor/usr.
Daniel Norman54283d62021-10-08 16:31:03 -070092 // These files may also be in the com.android.input.config APEX.
93 const char* rootsForPartition[]{
94 "/product",
95 "/system_ext",
96 "/odm",
97 "/vendor",
98 "/apex/com.android.input.config/etc",
99 getenv("ANDROID_ROOT"),
100 };
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900101 for (size_t i = 0; i < size(rootsForPartition); i++) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100102 if (rootsForPartition[i] == nullptr) {
103 continue;
104 }
105 path = rootsForPartition[i];
106 path += "/usr/";
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900107 appendInputDeviceConfigurationFileRelativePath(path, name, type);
Jeff Brown5912f952013-07-01 19:10:31 -0700108#if DEBUG_PROBE
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900109 ALOGD("Probing for system provided input device configuration file: path='%s'",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100110 path.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700111#endif
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100112 if (!access(path.c_str(), R_OK)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700113#if DEBUG_PROBE
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900114 ALOGD("Found");
Jeff Brown5912f952013-07-01 19:10:31 -0700115#endif
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900116 return path;
117 }
Jeff Brown5912f952013-07-01 19:10:31 -0700118 }
119
120 // Search user repository.
121 // TODO Should only look here if not in safe mode.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100122 path = "";
123 char *androidData = getenv("ANDROID_DATA");
124 if (androidData != nullptr) {
125 path += androidData;
126 }
127 path += "/system/devices/";
Jeff Brown5912f952013-07-01 19:10:31 -0700128 appendInputDeviceConfigurationFileRelativePath(path, name, type);
129#if DEBUG_PROBE
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100130 ALOGD("Probing for system user input device configuration file: path='%s'", path.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700131#endif
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100132 if (!access(path.c_str(), R_OK)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700133#if DEBUG_PROBE
134 ALOGD("Found");
135#endif
136 return path;
137 }
138
139 // Not found.
140#if DEBUG_PROBE
141 ALOGD("Probe failed to find input device configuration file: name='%s', type=%d",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100142 name.c_str(), type);
Jeff Brown5912f952013-07-01 19:10:31 -0700143#endif
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100144 return "";
Jeff Brown5912f952013-07-01 19:10:31 -0700145}
146
Siarhei Vishniakoub45635c2019-02-20 19:22:09 -0600147// --- InputDeviceIdentifier
148
149std::string InputDeviceIdentifier::getCanonicalName() const {
150 std::string replacedName = name;
151 for (char& ch : replacedName) {
152 if (!isValidNameChar(ch)) {
153 ch = '_';
154 }
155 }
156 return replacedName;
157}
158
Jeff Brown5912f952013-07-01 19:10:31 -0700159
160// --- InputDeviceInfo ---
161
162InputDeviceInfo::InputDeviceInfo() {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100163 initialize(-1, 0, -1, InputDeviceIdentifier(), "", false, false);
Jeff Brown5912f952013-07-01 19:10:31 -0700164}
165
Chris Ye3a1e4462020-08-12 10:13:15 -0700166InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other)
167 : mId(other.mId),
168 mGeneration(other.mGeneration),
169 mControllerNumber(other.mControllerNumber),
170 mIdentifier(other.mIdentifier),
171 mAlias(other.mAlias),
172 mIsExternal(other.mIsExternal),
173 mHasMic(other.mHasMic),
174 mSources(other.mSources),
175 mKeyboardType(other.mKeyboardType),
176 mKeyCharacterMap(other.mKeyCharacterMap),
177 mHasVibrator(other.mHasVibrator),
Kim Low03ea0352020-11-06 12:45:07 -0800178 mHasBattery(other.mHasBattery),
Chris Ye3a1e4462020-08-12 10:13:15 -0700179 mHasButtonUnderPad(other.mHasButtonUnderPad),
Chris Yef59a2f42020-10-16 12:55:26 -0700180 mHasSensor(other.mHasSensor),
181 mMotionRanges(other.mMotionRanges),
Chris Ye3fdbfef2021-01-06 18:45:18 -0800182 mSensors(other.mSensors),
183 mLights(other.mLights) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700184
185InputDeviceInfo::~InputDeviceInfo() {
186}
187
Michael Wright0415d632013-07-17 13:23:26 -0700188void InputDeviceInfo::initialize(int32_t id, int32_t generation, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100189 const InputDeviceIdentifier& identifier, const std::string& alias, bool isExternal,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700190 bool hasMic) {
Jeff Brown5912f952013-07-01 19:10:31 -0700191 mId = id;
192 mGeneration = generation;
Michael Wright0415d632013-07-17 13:23:26 -0700193 mControllerNumber = controllerNumber;
Jeff Brown5912f952013-07-01 19:10:31 -0700194 mIdentifier = identifier;
195 mAlias = alias;
196 mIsExternal = isExternal;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700197 mHasMic = hasMic;
Jeff Brown5912f952013-07-01 19:10:31 -0700198 mSources = 0;
199 mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE;
200 mHasVibrator = false;
Kim Low03ea0352020-11-06 12:45:07 -0800201 mHasBattery = false;
Michael Wright931fd6d2013-07-10 18:05:15 -0700202 mHasButtonUnderPad = false;
Chris Yef59a2f42020-10-16 12:55:26 -0700203 mHasSensor = false;
Jeff Brown5912f952013-07-01 19:10:31 -0700204 mMotionRanges.clear();
Chris Yef59a2f42020-10-16 12:55:26 -0700205 mSensors.clear();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800206 mLights.clear();
Jeff Brown5912f952013-07-01 19:10:31 -0700207}
208
209const InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange(
210 int32_t axis, uint32_t source) const {
211 size_t numRanges = mMotionRanges.size();
212 for (size_t i = 0; i < numRanges; i++) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800213 const MotionRange& range = mMotionRanges[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700214 if (range.axis == axis && range.source == source) {
215 return &range;
216 }
217 }
Yi Kong5bed83b2018-07-17 12:53:47 -0700218 return nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700219}
220
221void InputDeviceInfo::addSource(uint32_t source) {
222 mSources |= source;
223}
224
225void InputDeviceInfo::addMotionRange(int32_t axis, uint32_t source, float min, float max,
226 float flat, float fuzz, float resolution) {
227 MotionRange range = { axis, source, min, max, flat, fuzz, resolution };
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800228 mMotionRanges.push_back(range);
Jeff Brown5912f952013-07-01 19:10:31 -0700229}
230
231void InputDeviceInfo::addMotionRange(const MotionRange& range) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800232 mMotionRanges.push_back(range);
Jeff Brown5912f952013-07-01 19:10:31 -0700233}
234
Chris Yef59a2f42020-10-16 12:55:26 -0700235void InputDeviceInfo::addSensorInfo(const InputDeviceSensorInfo& info) {
236 if (mSensors.find(info.type) != mSensors.end()) {
237 ALOGW("Sensor type %s already exists, will be replaced by new sensor added.",
238 NamedEnum::string(info.type).c_str());
239 }
240 mSensors.insert_or_assign(info.type, info);
241}
242
Chris Yee2b1e5c2021-03-10 22:45:12 -0800243void InputDeviceInfo::addBatteryInfo(const InputDeviceBatteryInfo& info) {
244 if (mBatteries.find(info.id) != mBatteries.end()) {
245 ALOGW("Battery id %d already exists, will be replaced by new battery added.", info.id);
246 }
247 mBatteries.insert_or_assign(info.id, info);
248}
249
Chris Ye3fdbfef2021-01-06 18:45:18 -0800250void InputDeviceInfo::addLightInfo(const InputDeviceLightInfo& info) {
251 if (mLights.find(info.id) != mLights.end()) {
252 ALOGW("Light id %d already exists, will be replaced by new light added.", info.id);
253 }
254 mLights.insert_or_assign(info.id, info);
255}
256
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000257std::vector<InputDeviceSensorInfo> InputDeviceInfo::getSensors() {
258 std::vector<InputDeviceSensorInfo> infos;
259 infos.reserve(mSensors.size());
Chris Yef59a2f42020-10-16 12:55:26 -0700260 for (const auto& [type, info] : mSensors) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000261 infos.push_back(info);
Chris Yef59a2f42020-10-16 12:55:26 -0700262 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000263 return infos;
Chris Yef59a2f42020-10-16 12:55:26 -0700264}
265
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000266std::vector<InputDeviceLightInfo> InputDeviceInfo::getLights() {
267 std::vector<InputDeviceLightInfo> infos;
268 infos.reserve(mLights.size());
Chris Ye3fdbfef2021-01-06 18:45:18 -0800269 for (const auto& [id, info] : mLights) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000270 infos.push_back(info);
Chris Ye3fdbfef2021-01-06 18:45:18 -0800271 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000272 return infos;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800273}
274
Jeff Brown5912f952013-07-01 19:10:31 -0700275} // namespace android