blob: 4a6f66e0586f0170a6c912643bf0f95026693014 [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
Harry Cuttsf20b6ba2024-09-12 18:11:44 +000023#include <android-base/logging.h>
Jooyung Han70932db2024-02-23 14:02:45 +090024#include <android-base/properties.h>
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010025#include <android-base/stringprintf.h>
Dominik Laskowski75788452021-02-09 18:51:25 -080026#include <ftl/enum.h>
Jeff Brown5912f952013-07-01 19:10:31 -070027#include <input/InputDevice.h>
Jaekyun Seok7ead73e2017-02-28 18:07:03 +090028#include <input/InputEventLabels.h>
Jeff Brown5912f952013-07-01 19:10:31 -070029
Jooyung Han70932db2024-02-23 14:02:45 +090030using android::base::GetProperty;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010031using android::base::StringPrintf;
32
Jeff Brown5912f952013-07-01 19:10:31 -070033namespace android {
34
Harry Cuttsf20b6ba2024-09-12 18:11:44 +000035// Set to true to log detailed debugging messages about IDC file probing.
36static constexpr bool DEBUG_PROBE = false;
37
Jeff Brown5912f952013-07-01 19:10:31 -070038static const char* CONFIGURATION_FILE_DIR[] = {
39 "idc/",
40 "keylayout/",
41 "keychars/",
42};
43
44static const char* CONFIGURATION_FILE_EXTENSION[] = {
45 ".idc",
46 ".kl",
47 ".kcm",
48};
49
50static bool isValidNameChar(char ch) {
51 return isascii(ch) && (isdigit(ch) || isalpha(ch) || ch == '-' || ch == '_');
52}
53
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010054static void appendInputDeviceConfigurationFileRelativePath(std::string& path,
55 const std::string& name, InputDeviceConfigurationFileType type) {
Chris Ye1d927aa2020-07-04 18:22:41 -070056 path += CONFIGURATION_FILE_DIR[static_cast<int32_t>(type)];
Siarhei Vishniakou409ec1a2019-02-20 19:36:25 -060057 path += name;
Chris Ye1d927aa2020-07-04 18:22:41 -070058 path += CONFIGURATION_FILE_EXTENSION[static_cast<int32_t>(type)];
Jeff Brown5912f952013-07-01 19:10:31 -070059}
60
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010061std::string getInputDeviceConfigurationFilePathByDeviceIdentifier(
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070062 const InputDeviceIdentifier& deviceIdentifier, InputDeviceConfigurationFileType type,
63 const char* suffix) {
Jeff Brown5912f952013-07-01 19:10:31 -070064 if (deviceIdentifier.vendor !=0 && deviceIdentifier.product != 0) {
65 if (deviceIdentifier.version != 0) {
66 // Try vendor product version.
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070067 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);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010075 if (!versionPath.empty()) {
Jeff Brown5912f952013-07-01 19:10:31 -070076 return versionPath;
77 }
78 }
79
80 // Try vendor product.
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070081 std::string productPath =
82 getInputDeviceConfigurationFilePathByName(StringPrintf("Vendor_%04x_Product_%04x%s",
83 deviceIdentifier.vendor,
84 deviceIdentifier.product,
85 suffix),
86 type);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010087 if (!productPath.empty()) {
Jeff Brown5912f952013-07-01 19:10:31 -070088 return productPath;
89 }
90 }
91
92 // Try device name.
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070093 return getInputDeviceConfigurationFilePathByName(deviceIdentifier.getCanonicalName() + suffix,
94 type);
Jeff Brown5912f952013-07-01 19:10:31 -070095}
96
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010097std::string getInputDeviceConfigurationFilePathByName(
98 const std::string& name, InputDeviceConfigurationFileType type) {
Jeff Brown5912f952013-07-01 19:10:31 -070099 // Search system repository.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100100 std::string path;
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900101
Hongguang Chen1438ccb2021-04-30 09:48:49 -0700102 // Treblized input device config files will be located /product/usr, /system_ext/usr,
103 // /odm/usr or /vendor/usr.
Jooyung Han70932db2024-02-23 14:02:45 +0900104 std::vector<std::string> pathPrefixes{
105 "/product/usr/",
106 "/system_ext/usr/",
107 "/odm/usr/",
108 "/vendor/usr/",
Daniel Norman54283d62021-10-08 16:31:03 -0700109 };
Jooyung Han70932db2024-02-23 14:02:45 +0900110 // 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;
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900120 appendInputDeviceConfigurationFileRelativePath(path, name, type);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100121 if (!access(path.c_str(), R_OK)) {
Harry Cuttsf20b6ba2024-09-12 18:11:44 +0000122 LOG_IF(INFO, DEBUG_PROBE)
123 << "Found system-provided input device configuration file at " << path;
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900124 return path;
Harry Cuttsb6321852024-09-12 18:49:39 +0000125 } 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.";
Harry Cuttsf20b6ba2024-09-12 18:11:44 +0000129 } else {
130 LOG_IF(ERROR, DEBUG_PROBE)
131 << "Didn't find system-provided input device configuration file at " << path
132 << ": " << strerror(errno);
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900133 }
Jeff Brown5912f952013-07-01 19:10:31 -0700134 }
135
136 // Search user repository.
137 // TODO Should only look here if not in safe mode.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100138 path = "";
139 char *androidData = getenv("ANDROID_DATA");
140 if (androidData != nullptr) {
141 path += androidData;
142 }
143 path += "/system/devices/";
Jeff Brown5912f952013-07-01 19:10:31 -0700144 appendInputDeviceConfigurationFileRelativePath(path, name, type);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100145 if (!access(path.c_str(), R_OK)) {
Harry Cuttsf20b6ba2024-09-12 18:11:44 +0000146 LOG_IF(INFO, DEBUG_PROBE) << "Found system user input device configuration file at "
147 << path;
Jeff Brown5912f952013-07-01 19:10:31 -0700148 return path;
Harry Cuttsb6321852024-09-12 18:49:39 +0000149 } 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.";
Harry Cuttsf20b6ba2024-09-12 18:11:44 +0000153 } else {
154 LOG_IF(ERROR, DEBUG_PROBE) << "Didn't find system user input device configuration file at "
155 << path << ": " << strerror(errno);
Jeff Brown5912f952013-07-01 19:10:31 -0700156 }
157
158 // Not found.
Harry Cuttsf20b6ba2024-09-12 18:11:44 +0000159 LOG_IF(INFO, DEBUG_PROBE) << "Probe failed to find input device configuration file with name '"
160 << name << "' and type " << ftl::enum_string(type);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100161 return "";
Jeff Brown5912f952013-07-01 19:10:31 -0700162}
163
Siarhei Vishniakoub45635c2019-02-20 19:22:09 -0600164// --- InputDeviceIdentifier
165
166std::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
Jeff Brown5912f952013-07-01 19:10:31 -0700176
177// --- InputDeviceInfo ---
178
179InputDeviceInfo::InputDeviceInfo() {
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700180 initialize(-1, 0, -1, InputDeviceIdentifier(), "", false, false, ui::LogicalDisplayId::INVALID);
Jeff Brown5912f952013-07-01 19:10:31 -0700181}
182
Chris Ye3a1e4462020-08-12 10:13:15 -0700183InputDeviceInfo::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),
Zixuan Qufecb6062022-11-12 04:44:31 +0000191 mKeyboardLayoutInfo(other.mKeyboardLayoutInfo),
Chris Ye3a1e4462020-08-12 10:13:15 -0700192 mSources(other.mSources),
193 mKeyboardType(other.mKeyboardType),
Linnan Lid78dd9b2024-10-15 15:50:01 +0000194 mKeyCharacterMap(other.mKeyCharacterMap
195 ? std::make_unique<KeyCharacterMap>(*other.mKeyCharacterMap)
196 : nullptr),
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000197 mUsiVersion(other.mUsiVersion),
198 mAssociatedDisplayId(other.mAssociatedDisplayId),
Linnan Li48f80da2024-04-22 18:38:16 +0000199 mEnabled(other.mEnabled),
Chris Ye3a1e4462020-08-12 10:13:15 -0700200 mHasVibrator(other.mHasVibrator),
Kim Low03ea0352020-11-06 12:45:07 -0800201 mHasBattery(other.mHasBattery),
Chris Ye3a1e4462020-08-12 10:13:15 -0700202 mHasButtonUnderPad(other.mHasButtonUnderPad),
Chris Yef59a2f42020-10-16 12:55:26 -0700203 mHasSensor(other.mHasSensor),
204 mMotionRanges(other.mMotionRanges),
Chris Ye3fdbfef2021-01-06 18:45:18 -0800205 mSensors(other.mSensors),
Yeabkal Wubshitb1b96db2024-01-24 12:47:00 -0800206 mLights(other.mLights),
207 mViewBehavior(other.mViewBehavior) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700208
Linnan Lid78dd9b2024-10-15 15:50:01 +0000209InputDeviceInfo& 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
Jeff Brown5912f952013-07-01 19:10:31 -0700237InputDeviceInfo::~InputDeviceInfo() {
238}
239
Michael Wright0415d632013-07-17 13:23:26 -0700240void InputDeviceInfo::initialize(int32_t id, int32_t generation, int32_t controllerNumber,
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000241 const InputDeviceIdentifier& identifier, const std::string& alias,
Linnan Li13bf76a2024-05-05 19:18:02 +0800242 bool isExternal, bool hasMic,
243 ui::LogicalDisplayId associatedDisplayId,
Linnan Li48f80da2024-04-22 18:38:16 +0000244 InputDeviceViewBehavior viewBehavior, bool enabled) {
Jeff Brown5912f952013-07-01 19:10:31 -0700245 mId = id;
246 mGeneration = generation;
Michael Wright0415d632013-07-17 13:23:26 -0700247 mControllerNumber = controllerNumber;
Jeff Brown5912f952013-07-01 19:10:31 -0700248 mIdentifier = identifier;
249 mAlias = alias;
250 mIsExternal = isExternal;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700251 mHasMic = hasMic;
Jeff Brown5912f952013-07-01 19:10:31 -0700252 mSources = 0;
253 mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE;
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000254 mAssociatedDisplayId = associatedDisplayId;
Linnan Li48f80da2024-04-22 18:38:16 +0000255 mEnabled = enabled;
Jeff Brown5912f952013-07-01 19:10:31 -0700256 mHasVibrator = false;
Kim Low03ea0352020-11-06 12:45:07 -0800257 mHasBattery = false;
Michael Wright931fd6d2013-07-10 18:05:15 -0700258 mHasButtonUnderPad = false;
Chris Yef59a2f42020-10-16 12:55:26 -0700259 mHasSensor = false;
Yeabkal Wubshitb1b96db2024-01-24 12:47:00 -0800260 mViewBehavior = viewBehavior;
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000261 mUsiVersion.reset();
Jeff Brown5912f952013-07-01 19:10:31 -0700262 mMotionRanges.clear();
Chris Yef59a2f42020-10-16 12:55:26 -0700263 mSensors.clear();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800264 mLights.clear();
Jeff Brown5912f952013-07-01 19:10:31 -0700265}
266
267const InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange(
268 int32_t axis, uint32_t source) const {
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800269 for (const MotionRange& range : mMotionRanges) {
270 if (range.axis == axis && isFromSource(range.source, source)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700271 return &range;
272 }
273 }
Yi Kong5bed83b2018-07-17 12:53:47 -0700274 return nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700275}
276
277void InputDeviceInfo::addSource(uint32_t source) {
278 mSources |= source;
279}
280
281void 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 };
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800284 mMotionRanges.push_back(range);
Jeff Brown5912f952013-07-01 19:10:31 -0700285}
286
287void InputDeviceInfo::addMotionRange(const MotionRange& range) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800288 mMotionRanges.push_back(range);
Jeff Brown5912f952013-07-01 19:10:31 -0700289}
290
Chris Yef59a2f42020-10-16 12:55:26 -0700291void 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.",
Dominik Laskowski75788452021-02-09 18:51:25 -0800294 ftl::enum_string(info.type).c_str());
Chris Yef59a2f42020-10-16 12:55:26 -0700295 }
296 mSensors.insert_or_assign(info.type, info);
297}
298
Chris Yee2b1e5c2021-03-10 22:45:12 -0800299void 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
Chris Ye3fdbfef2021-01-06 18:45:18 -0800306void 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
Philip Junkerf8437962022-01-25 21:20:19 +0100313void InputDeviceInfo::setKeyboardType(int32_t keyboardType) {
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +0000314 mKeyboardType = keyboardType;
Philip Junkerf8437962022-01-25 21:20:19 +0100315}
316
Zixuan Qufecb6062022-11-12 04:44:31 +0000317void InputDeviceInfo::setKeyboardLayoutInfo(KeyboardLayoutInfo layoutInfo) {
318 mKeyboardLayoutInfo = std::move(layoutInfo);
319}
320
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000321std::vector<InputDeviceSensorInfo> InputDeviceInfo::getSensors() {
322 std::vector<InputDeviceSensorInfo> infos;
323 infos.reserve(mSensors.size());
Chris Yef59a2f42020-10-16 12:55:26 -0700324 for (const auto& [type, info] : mSensors) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000325 infos.push_back(info);
Chris Yef59a2f42020-10-16 12:55:26 -0700326 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000327 return infos;
Chris Yef59a2f42020-10-16 12:55:26 -0700328}
329
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000330std::vector<InputDeviceLightInfo> InputDeviceInfo::getLights() {
331 std::vector<InputDeviceLightInfo> infos;
332 infos.reserve(mLights.size());
Chris Ye3fdbfef2021-01-06 18:45:18 -0800333 for (const auto& [id, info] : mLights) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000334 infos.push_back(info);
Chris Ye3fdbfef2021-01-06 18:45:18 -0800335 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000336 return infos;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800337}
338
Jeff Brown5912f952013-07-01 19:10:31 -0700339} // namespace android