blob: 31027b64768430dd98895eecbfe7164afe0c75a8 [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
90 // Treblized input device config files will be located /odm/usr or /vendor/usr.
Frank Barchard9e947882017-03-06 11:17:52 -080091 const char *rootsForPartition[] {"/odm", "/vendor", getenv("ANDROID_ROOT")};
Jaekyun Seok7ead73e2017-02-28 18:07:03 +090092 for (size_t i = 0; i < size(rootsForPartition); i++) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010093 if (rootsForPartition[i] == nullptr) {
94 continue;
95 }
96 path = rootsForPartition[i];
97 path += "/usr/";
Jaekyun Seok7ead73e2017-02-28 18:07:03 +090098 appendInputDeviceConfigurationFileRelativePath(path, name, type);
Jeff Brown5912f952013-07-01 19:10:31 -070099#if DEBUG_PROBE
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900100 ALOGD("Probing for system provided input device configuration file: path='%s'",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100101 path.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700102#endif
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100103 if (!access(path.c_str(), R_OK)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700104#if DEBUG_PROBE
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900105 ALOGD("Found");
Jeff Brown5912f952013-07-01 19:10:31 -0700106#endif
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900107 return path;
108 }
Jeff Brown5912f952013-07-01 19:10:31 -0700109 }
110
111 // Search user repository.
112 // TODO Should only look here if not in safe mode.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100113 path = "";
114 char *androidData = getenv("ANDROID_DATA");
115 if (androidData != nullptr) {
116 path += androidData;
117 }
118 path += "/system/devices/";
Jeff Brown5912f952013-07-01 19:10:31 -0700119 appendInputDeviceConfigurationFileRelativePath(path, name, type);
120#if DEBUG_PROBE
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100121 ALOGD("Probing for system user input device configuration file: path='%s'", path.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700122#endif
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100123 if (!access(path.c_str(), R_OK)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700124#if DEBUG_PROBE
125 ALOGD("Found");
126#endif
127 return path;
128 }
129
130 // Not found.
131#if DEBUG_PROBE
132 ALOGD("Probe failed to find input device configuration file: name='%s', type=%d",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100133 name.c_str(), type);
Jeff Brown5912f952013-07-01 19:10:31 -0700134#endif
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100135 return "";
Jeff Brown5912f952013-07-01 19:10:31 -0700136}
137
Siarhei Vishniakoub45635c2019-02-20 19:22:09 -0600138// --- InputDeviceIdentifier
139
140std::string InputDeviceIdentifier::getCanonicalName() const {
141 std::string replacedName = name;
142 for (char& ch : replacedName) {
143 if (!isValidNameChar(ch)) {
144 ch = '_';
145 }
146 }
147 return replacedName;
148}
149
Jeff Brown5912f952013-07-01 19:10:31 -0700150
151// --- InputDeviceInfo ---
152
153InputDeviceInfo::InputDeviceInfo() {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100154 initialize(-1, 0, -1, InputDeviceIdentifier(), "", false, false);
Jeff Brown5912f952013-07-01 19:10:31 -0700155}
156
Chris Ye3a1e4462020-08-12 10:13:15 -0700157InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other)
158 : mId(other.mId),
159 mGeneration(other.mGeneration),
160 mControllerNumber(other.mControllerNumber),
161 mIdentifier(other.mIdentifier),
162 mAlias(other.mAlias),
163 mIsExternal(other.mIsExternal),
164 mHasMic(other.mHasMic),
165 mSources(other.mSources),
166 mKeyboardType(other.mKeyboardType),
167 mKeyCharacterMap(other.mKeyCharacterMap),
168 mHasVibrator(other.mHasVibrator),
Kim Low03ea0352020-11-06 12:45:07 -0800169 mHasBattery(other.mHasBattery),
Chris Ye3a1e4462020-08-12 10:13:15 -0700170 mHasButtonUnderPad(other.mHasButtonUnderPad),
Chris Yef59a2f42020-10-16 12:55:26 -0700171 mHasSensor(other.mHasSensor),
172 mMotionRanges(other.mMotionRanges),
Chris Ye3fdbfef2021-01-06 18:45:18 -0800173 mSensors(other.mSensors),
174 mLights(other.mLights) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700175
176InputDeviceInfo::~InputDeviceInfo() {
177}
178
Michael Wright0415d632013-07-17 13:23:26 -0700179void InputDeviceInfo::initialize(int32_t id, int32_t generation, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100180 const InputDeviceIdentifier& identifier, const std::string& alias, bool isExternal,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700181 bool hasMic) {
Jeff Brown5912f952013-07-01 19:10:31 -0700182 mId = id;
183 mGeneration = generation;
Michael Wright0415d632013-07-17 13:23:26 -0700184 mControllerNumber = controllerNumber;
Jeff Brown5912f952013-07-01 19:10:31 -0700185 mIdentifier = identifier;
186 mAlias = alias;
187 mIsExternal = isExternal;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700188 mHasMic = hasMic;
Jeff Brown5912f952013-07-01 19:10:31 -0700189 mSources = 0;
190 mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE;
191 mHasVibrator = false;
Kim Low03ea0352020-11-06 12:45:07 -0800192 mHasBattery = false;
Michael Wright931fd6d2013-07-10 18:05:15 -0700193 mHasButtonUnderPad = false;
Chris Yef59a2f42020-10-16 12:55:26 -0700194 mHasSensor = false;
Jeff Brown5912f952013-07-01 19:10:31 -0700195 mMotionRanges.clear();
Chris Yef59a2f42020-10-16 12:55:26 -0700196 mSensors.clear();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800197 mLights.clear();
Jeff Brown5912f952013-07-01 19:10:31 -0700198}
199
200const InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange(
201 int32_t axis, uint32_t source) const {
202 size_t numRanges = mMotionRanges.size();
203 for (size_t i = 0; i < numRanges; i++) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800204 const MotionRange& range = mMotionRanges[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700205 if (range.axis == axis && range.source == source) {
206 return &range;
207 }
208 }
Yi Kong5bed83b2018-07-17 12:53:47 -0700209 return nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700210}
211
212void InputDeviceInfo::addSource(uint32_t source) {
213 mSources |= source;
214}
215
216void InputDeviceInfo::addMotionRange(int32_t axis, uint32_t source, float min, float max,
217 float flat, float fuzz, float resolution) {
218 MotionRange range = { axis, source, min, max, flat, fuzz, resolution };
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800219 mMotionRanges.push_back(range);
Jeff Brown5912f952013-07-01 19:10:31 -0700220}
221
222void InputDeviceInfo::addMotionRange(const MotionRange& range) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800223 mMotionRanges.push_back(range);
Jeff Brown5912f952013-07-01 19:10:31 -0700224}
225
Chris Yef59a2f42020-10-16 12:55:26 -0700226void InputDeviceInfo::addSensorInfo(const InputDeviceSensorInfo& info) {
227 if (mSensors.find(info.type) != mSensors.end()) {
228 ALOGW("Sensor type %s already exists, will be replaced by new sensor added.",
229 NamedEnum::string(info.type).c_str());
230 }
231 mSensors.insert_or_assign(info.type, info);
232}
233
Chris Yee2b1e5c2021-03-10 22:45:12 -0800234void InputDeviceInfo::addBatteryInfo(const InputDeviceBatteryInfo& info) {
235 if (mBatteries.find(info.id) != mBatteries.end()) {
236 ALOGW("Battery id %d already exists, will be replaced by new battery added.", info.id);
237 }
238 mBatteries.insert_or_assign(info.id, info);
239}
240
Chris Ye3fdbfef2021-01-06 18:45:18 -0800241void InputDeviceInfo::addLightInfo(const InputDeviceLightInfo& info) {
242 if (mLights.find(info.id) != mLights.end()) {
243 ALOGW("Light id %d already exists, will be replaced by new light added.", info.id);
244 }
245 mLights.insert_or_assign(info.id, info);
246}
247
Chris Yef59a2f42020-10-16 12:55:26 -0700248const std::vector<InputDeviceSensorType> InputDeviceInfo::getSensorTypes() {
249 std::vector<InputDeviceSensorType> types;
250 for (const auto& [type, info] : mSensors) {
251 types.push_back(type);
252 }
253 return types;
254}
255
256const InputDeviceSensorInfo* InputDeviceInfo::getSensorInfo(InputDeviceSensorType type) {
257 auto it = mSensors.find(type);
258 if (it == mSensors.end()) {
259 return nullptr;
260 }
261 return &it->second;
262}
263
Chris Ye3fdbfef2021-01-06 18:45:18 -0800264const std::vector<int32_t> InputDeviceInfo::getLightIds() {
265 std::vector<int32_t> ids;
266 for (const auto& [id, info] : mLights) {
267 ids.push_back(id);
268 }
269 return ids;
270}
271
272const InputDeviceLightInfo* InputDeviceInfo::getLightInfo(int32_t id) {
273 auto it = mLights.find(id);
274 if (it == mLights.end()) {
275 return nullptr;
276 }
277 return &it->second;
278}
279
Jeff Brown5912f952013-07-01 19:10:31 -0700280} // namespace android