blob: 222647db56fb1c8a6d47ac0b3ef30d6fd2895b1c [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
Jooyung Han70932db2024-02-23 14:02:45 +090023#include <android-base/properties.h>
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010024#include <android-base/stringprintf.h>
Dominik Laskowski75788452021-02-09 18:51:25 -080025#include <ftl/enum.h>
Prabir Pradhane04ffaa2022-12-13 23:04:04 +000026#include <gui/constants.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
35static const char* CONFIGURATION_FILE_DIR[] = {
36 "idc/",
37 "keylayout/",
38 "keychars/",
39};
40
41static const char* CONFIGURATION_FILE_EXTENSION[] = {
42 ".idc",
43 ".kl",
44 ".kcm",
45};
46
47static bool isValidNameChar(char ch) {
48 return isascii(ch) && (isdigit(ch) || isalpha(ch) || ch == '-' || ch == '_');
49}
50
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010051static void appendInputDeviceConfigurationFileRelativePath(std::string& path,
52 const std::string& name, InputDeviceConfigurationFileType type) {
Chris Ye1d927aa2020-07-04 18:22:41 -070053 path += CONFIGURATION_FILE_DIR[static_cast<int32_t>(type)];
Siarhei Vishniakou409ec1a2019-02-20 19:36:25 -060054 path += name;
Chris Ye1d927aa2020-07-04 18:22:41 -070055 path += CONFIGURATION_FILE_EXTENSION[static_cast<int32_t>(type)];
Jeff Brown5912f952013-07-01 19:10:31 -070056}
57
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010058std::string getInputDeviceConfigurationFilePathByDeviceIdentifier(
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070059 const InputDeviceIdentifier& deviceIdentifier, InputDeviceConfigurationFileType type,
60 const char* suffix) {
Jeff Brown5912f952013-07-01 19:10:31 -070061 if (deviceIdentifier.vendor !=0 && deviceIdentifier.product != 0) {
62 if (deviceIdentifier.version != 0) {
63 // Try vendor product version.
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070064 std::string versionPath =
65 getInputDeviceConfigurationFilePathByName(StringPrintf("Vendor_%04x_Product_%"
66 "04x_Version_%04x%s",
67 deviceIdentifier.vendor,
68 deviceIdentifier.product,
69 deviceIdentifier.version,
70 suffix),
71 type);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010072 if (!versionPath.empty()) {
Jeff Brown5912f952013-07-01 19:10:31 -070073 return versionPath;
74 }
75 }
76
77 // Try vendor product.
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070078 std::string productPath =
79 getInputDeviceConfigurationFilePathByName(StringPrintf("Vendor_%04x_Product_%04x%s",
80 deviceIdentifier.vendor,
81 deviceIdentifier.product,
82 suffix),
83 type);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010084 if (!productPath.empty()) {
Jeff Brown5912f952013-07-01 19:10:31 -070085 return productPath;
86 }
87 }
88
89 // Try device name.
Siarhei Vishniakoua9fd82c2022-05-18 09:42:52 -070090 return getInputDeviceConfigurationFilePathByName(deviceIdentifier.getCanonicalName() + suffix,
91 type);
Jeff Brown5912f952013-07-01 19:10:31 -070092}
93
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010094std::string getInputDeviceConfigurationFilePathByName(
95 const std::string& name, InputDeviceConfigurationFileType type) {
Jeff Brown5912f952013-07-01 19:10:31 -070096 // Search system repository.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010097 std::string path;
Jaekyun Seok7ead73e2017-02-28 18:07:03 +090098
Hongguang Chen1438ccb2021-04-30 09:48:49 -070099 // Treblized input device config files will be located /product/usr, /system_ext/usr,
100 // /odm/usr or /vendor/usr.
Jooyung Han70932db2024-02-23 14:02:45 +0900101 std::vector<std::string> pathPrefixes{
102 "/product/usr/",
103 "/system_ext/usr/",
104 "/odm/usr/",
105 "/vendor/usr/",
Daniel Norman54283d62021-10-08 16:31:03 -0700106 };
Jooyung Han70932db2024-02-23 14:02:45 +0900107 // These files may also be in the APEX pointed by input_device.config_file.apex sysprop.
108 if (auto apex = GetProperty("input_device.config_file.apex", ""); !apex.empty()) {
109 pathPrefixes.push_back("/apex/" + apex + "/etc/usr/");
110 }
111 // ANDROID_ROOT may not be set on host
112 if (auto android_root = getenv("ANDROID_ROOT"); android_root != nullptr) {
113 pathPrefixes.push_back(std::string(android_root) + "/usr/");
114 }
115 for (const auto& prefix : pathPrefixes) {
116 path = prefix;
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900117 appendInputDeviceConfigurationFileRelativePath(path, name, type);
Jeff Brown5912f952013-07-01 19:10:31 -0700118#if DEBUG_PROBE
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900119 ALOGD("Probing for system provided input device configuration file: path='%s'",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100120 path.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700121#endif
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100122 if (!access(path.c_str(), R_OK)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700123#if DEBUG_PROBE
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900124 ALOGD("Found");
Jeff Brown5912f952013-07-01 19:10:31 -0700125#endif
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900126 return path;
127 }
Jeff Brown5912f952013-07-01 19:10:31 -0700128 }
129
130 // Search user repository.
131 // TODO Should only look here if not in safe mode.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100132 path = "";
133 char *androidData = getenv("ANDROID_DATA");
134 if (androidData != nullptr) {
135 path += androidData;
136 }
137 path += "/system/devices/";
Jeff Brown5912f952013-07-01 19:10:31 -0700138 appendInputDeviceConfigurationFileRelativePath(path, name, type);
139#if DEBUG_PROBE
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100140 ALOGD("Probing for system user input device configuration file: path='%s'", path.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700141#endif
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100142 if (!access(path.c_str(), R_OK)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700143#if DEBUG_PROBE
144 ALOGD("Found");
145#endif
146 return path;
147 }
148
149 // Not found.
150#if DEBUG_PROBE
151 ALOGD("Probe failed to find input device configuration file: name='%s', type=%d",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100152 name.c_str(), type);
Jeff Brown5912f952013-07-01 19:10:31 -0700153#endif
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100154 return "";
Jeff Brown5912f952013-07-01 19:10:31 -0700155}
156
Siarhei Vishniakoub45635c2019-02-20 19:22:09 -0600157// --- InputDeviceIdentifier
158
159std::string InputDeviceIdentifier::getCanonicalName() const {
160 std::string replacedName = name;
161 for (char& ch : replacedName) {
162 if (!isValidNameChar(ch)) {
163 ch = '_';
164 }
165 }
166 return replacedName;
167}
168
Jeff Brown5912f952013-07-01 19:10:31 -0700169
170// --- InputDeviceInfo ---
171
172InputDeviceInfo::InputDeviceInfo() {
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000173 initialize(-1, 0, -1, InputDeviceIdentifier(), "", false, false, ADISPLAY_ID_NONE);
Jeff Brown5912f952013-07-01 19:10:31 -0700174}
175
Chris Ye3a1e4462020-08-12 10:13:15 -0700176InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other)
177 : mId(other.mId),
178 mGeneration(other.mGeneration),
179 mControllerNumber(other.mControllerNumber),
180 mIdentifier(other.mIdentifier),
181 mAlias(other.mAlias),
182 mIsExternal(other.mIsExternal),
183 mHasMic(other.mHasMic),
Zixuan Qufecb6062022-11-12 04:44:31 +0000184 mKeyboardLayoutInfo(other.mKeyboardLayoutInfo),
Chris Ye3a1e4462020-08-12 10:13:15 -0700185 mSources(other.mSources),
186 mKeyboardType(other.mKeyboardType),
187 mKeyCharacterMap(other.mKeyCharacterMap),
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000188 mUsiVersion(other.mUsiVersion),
189 mAssociatedDisplayId(other.mAssociatedDisplayId),
Linnan Li48f80da2024-04-22 18:38:16 +0000190 mEnabled(other.mEnabled),
Chris Ye3a1e4462020-08-12 10:13:15 -0700191 mHasVibrator(other.mHasVibrator),
Kim Low03ea0352020-11-06 12:45:07 -0800192 mHasBattery(other.mHasBattery),
Chris Ye3a1e4462020-08-12 10:13:15 -0700193 mHasButtonUnderPad(other.mHasButtonUnderPad),
Chris Yef59a2f42020-10-16 12:55:26 -0700194 mHasSensor(other.mHasSensor),
195 mMotionRanges(other.mMotionRanges),
Chris Ye3fdbfef2021-01-06 18:45:18 -0800196 mSensors(other.mSensors),
Yeabkal Wubshitb1b96db2024-01-24 12:47:00 -0800197 mLights(other.mLights),
198 mViewBehavior(other.mViewBehavior) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700199
200InputDeviceInfo::~InputDeviceInfo() {
201}
202
Michael Wright0415d632013-07-17 13:23:26 -0700203void InputDeviceInfo::initialize(int32_t id, int32_t generation, int32_t controllerNumber,
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000204 const InputDeviceIdentifier& identifier, const std::string& alias,
Yeabkal Wubshitb1b96db2024-01-24 12:47:00 -0800205 bool isExternal, bool hasMic, int32_t associatedDisplayId,
Linnan Li48f80da2024-04-22 18:38:16 +0000206 InputDeviceViewBehavior viewBehavior, bool enabled) {
Jeff Brown5912f952013-07-01 19:10:31 -0700207 mId = id;
208 mGeneration = generation;
Michael Wright0415d632013-07-17 13:23:26 -0700209 mControllerNumber = controllerNumber;
Jeff Brown5912f952013-07-01 19:10:31 -0700210 mIdentifier = identifier;
211 mAlias = alias;
212 mIsExternal = isExternal;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700213 mHasMic = hasMic;
Jeff Brown5912f952013-07-01 19:10:31 -0700214 mSources = 0;
215 mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE;
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000216 mAssociatedDisplayId = associatedDisplayId;
Linnan Li48f80da2024-04-22 18:38:16 +0000217 mEnabled = enabled;
Jeff Brown5912f952013-07-01 19:10:31 -0700218 mHasVibrator = false;
Kim Low03ea0352020-11-06 12:45:07 -0800219 mHasBattery = false;
Michael Wright931fd6d2013-07-10 18:05:15 -0700220 mHasButtonUnderPad = false;
Chris Yef59a2f42020-10-16 12:55:26 -0700221 mHasSensor = false;
Yeabkal Wubshitb1b96db2024-01-24 12:47:00 -0800222 mViewBehavior = viewBehavior;
Prabir Pradhane04ffaa2022-12-13 23:04:04 +0000223 mUsiVersion.reset();
Jeff Brown5912f952013-07-01 19:10:31 -0700224 mMotionRanges.clear();
Chris Yef59a2f42020-10-16 12:55:26 -0700225 mSensors.clear();
Chris Ye3fdbfef2021-01-06 18:45:18 -0800226 mLights.clear();
Jeff Brown5912f952013-07-01 19:10:31 -0700227}
228
229const InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange(
230 int32_t axis, uint32_t source) const {
Siarhei Vishniakoud9489572021-11-12 20:08:38 -0800231 for (const MotionRange& range : mMotionRanges) {
232 if (range.axis == axis && isFromSource(range.source, source)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700233 return &range;
234 }
235 }
Yi Kong5bed83b2018-07-17 12:53:47 -0700236 return nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700237}
238
239void InputDeviceInfo::addSource(uint32_t source) {
240 mSources |= source;
241}
242
243void InputDeviceInfo::addMotionRange(int32_t axis, uint32_t source, float min, float max,
244 float flat, float fuzz, float resolution) {
245 MotionRange range = { axis, source, min, max, flat, fuzz, resolution };
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800246 mMotionRanges.push_back(range);
Jeff Brown5912f952013-07-01 19:10:31 -0700247}
248
249void InputDeviceInfo::addMotionRange(const MotionRange& range) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800250 mMotionRanges.push_back(range);
Jeff Brown5912f952013-07-01 19:10:31 -0700251}
252
Chris Yef59a2f42020-10-16 12:55:26 -0700253void InputDeviceInfo::addSensorInfo(const InputDeviceSensorInfo& info) {
254 if (mSensors.find(info.type) != mSensors.end()) {
255 ALOGW("Sensor type %s already exists, will be replaced by new sensor added.",
Dominik Laskowski75788452021-02-09 18:51:25 -0800256 ftl::enum_string(info.type).c_str());
Chris Yef59a2f42020-10-16 12:55:26 -0700257 }
258 mSensors.insert_or_assign(info.type, info);
259}
260
Chris Yee2b1e5c2021-03-10 22:45:12 -0800261void InputDeviceInfo::addBatteryInfo(const InputDeviceBatteryInfo& info) {
262 if (mBatteries.find(info.id) != mBatteries.end()) {
263 ALOGW("Battery id %d already exists, will be replaced by new battery added.", info.id);
264 }
265 mBatteries.insert_or_assign(info.id, info);
266}
267
Chris Ye3fdbfef2021-01-06 18:45:18 -0800268void InputDeviceInfo::addLightInfo(const InputDeviceLightInfo& info) {
269 if (mLights.find(info.id) != mLights.end()) {
270 ALOGW("Light id %d already exists, will be replaced by new light added.", info.id);
271 }
272 mLights.insert_or_assign(info.id, info);
273}
274
Philip Junkerf8437962022-01-25 21:20:19 +0100275void InputDeviceInfo::setKeyboardType(int32_t keyboardType) {
276 static_assert(AINPUT_KEYBOARD_TYPE_NONE < AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
277 static_assert(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC < AINPUT_KEYBOARD_TYPE_ALPHABETIC);
278 // There can be multiple subdevices with different keyboard types, set it to the highest type
279 mKeyboardType = std::max(mKeyboardType, keyboardType);
280}
281
Zixuan Qufecb6062022-11-12 04:44:31 +0000282void InputDeviceInfo::setKeyboardLayoutInfo(KeyboardLayoutInfo layoutInfo) {
283 mKeyboardLayoutInfo = std::move(layoutInfo);
284}
285
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000286std::vector<InputDeviceSensorInfo> InputDeviceInfo::getSensors() {
287 std::vector<InputDeviceSensorInfo> infos;
288 infos.reserve(mSensors.size());
Chris Yef59a2f42020-10-16 12:55:26 -0700289 for (const auto& [type, info] : mSensors) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000290 infos.push_back(info);
Chris Yef59a2f42020-10-16 12:55:26 -0700291 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000292 return infos;
Chris Yef59a2f42020-10-16 12:55:26 -0700293}
294
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000295std::vector<InputDeviceLightInfo> InputDeviceInfo::getLights() {
296 std::vector<InputDeviceLightInfo> infos;
297 infos.reserve(mLights.size());
Chris Ye3fdbfef2021-01-06 18:45:18 -0800298 for (const auto& [id, info] : mLights) {
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000299 infos.push_back(info);
Chris Ye3fdbfef2021-01-06 18:45:18 -0800300 }
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000301 return infos;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800302}
303
Jeff Brown5912f952013-07-01 19:10:31 -0700304} // namespace android