Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1 | /* |
| 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 Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 24 | #include <input/InputDevice.h> |
Jaekyun Seok | 7ead73e | 2017-02-28 18:07:03 +0900 | [diff] [blame] | 25 | #include <input/InputEventLabels.h> |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 26 | #include <input/NamedEnum.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 27 | |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 28 | using android::base::StringPrintf; |
| 29 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | |
| 32 | static const char* CONFIGURATION_FILE_DIR[] = { |
| 33 | "idc/", |
| 34 | "keylayout/", |
| 35 | "keychars/", |
| 36 | }; |
| 37 | |
| 38 | static const char* CONFIGURATION_FILE_EXTENSION[] = { |
| 39 | ".idc", |
| 40 | ".kl", |
| 41 | ".kcm", |
| 42 | }; |
| 43 | |
| 44 | static bool isValidNameChar(char ch) { |
| 45 | return isascii(ch) && (isdigit(ch) || isalpha(ch) || ch == '-' || ch == '_'); |
| 46 | } |
| 47 | |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 48 | static void appendInputDeviceConfigurationFileRelativePath(std::string& path, |
| 49 | const std::string& name, InputDeviceConfigurationFileType type) { |
Chris Ye | 1d927aa | 2020-07-04 18:22:41 -0700 | [diff] [blame] | 50 | path += CONFIGURATION_FILE_DIR[static_cast<int32_t>(type)]; |
Siarhei Vishniakou | 409ec1a | 2019-02-20 19:36:25 -0600 | [diff] [blame] | 51 | path += name; |
Chris Ye | 1d927aa | 2020-07-04 18:22:41 -0700 | [diff] [blame] | 52 | path += CONFIGURATION_FILE_EXTENSION[static_cast<int32_t>(type)]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 55 | std::string getInputDeviceConfigurationFilePathByDeviceIdentifier( |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 56 | 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 Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 61 | std::string versionPath = getInputDeviceConfigurationFilePathByName( |
| 62 | StringPrintf("Vendor_%04x_Product_%04x_Version_%04x", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 63 | deviceIdentifier.vendor, deviceIdentifier.product, |
| 64 | deviceIdentifier.version), |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 65 | type); |
| 66 | if (!versionPath.empty()) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 67 | return versionPath; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Try vendor product. |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 72 | std::string productPath = getInputDeviceConfigurationFilePathByName( |
| 73 | StringPrintf("Vendor_%04x_Product_%04x", |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 74 | deviceIdentifier.vendor, deviceIdentifier.product), |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 75 | type); |
| 76 | if (!productPath.empty()) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 77 | return productPath; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Try device name. |
Siarhei Vishniakou | 409ec1a | 2019-02-20 19:36:25 -0600 | [diff] [blame] | 82 | return getInputDeviceConfigurationFilePathByName(deviceIdentifier.getCanonicalName(), type); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 85 | std::string getInputDeviceConfigurationFilePathByName( |
| 86 | const std::string& name, InputDeviceConfigurationFileType type) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 87 | // Search system repository. |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 88 | std::string path; |
Jaekyun Seok | 7ead73e | 2017-02-28 18:07:03 +0900 | [diff] [blame] | 89 | |
| 90 | // Treblized input device config files will be located /odm/usr or /vendor/usr. |
Frank Barchard | 9e94788 | 2017-03-06 11:17:52 -0800 | [diff] [blame] | 91 | const char *rootsForPartition[] {"/odm", "/vendor", getenv("ANDROID_ROOT")}; |
Jaekyun Seok | 7ead73e | 2017-02-28 18:07:03 +0900 | [diff] [blame] | 92 | for (size_t i = 0; i < size(rootsForPartition); i++) { |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 93 | if (rootsForPartition[i] == nullptr) { |
| 94 | continue; |
| 95 | } |
| 96 | path = rootsForPartition[i]; |
| 97 | path += "/usr/"; |
Jaekyun Seok | 7ead73e | 2017-02-28 18:07:03 +0900 | [diff] [blame] | 98 | appendInputDeviceConfigurationFileRelativePath(path, name, type); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 99 | #if DEBUG_PROBE |
Jaekyun Seok | 7ead73e | 2017-02-28 18:07:03 +0900 | [diff] [blame] | 100 | ALOGD("Probing for system provided input device configuration file: path='%s'", |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 101 | path.c_str()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 102 | #endif |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 103 | if (!access(path.c_str(), R_OK)) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 104 | #if DEBUG_PROBE |
Jaekyun Seok | 7ead73e | 2017-02-28 18:07:03 +0900 | [diff] [blame] | 105 | ALOGD("Found"); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 106 | #endif |
Jaekyun Seok | 7ead73e | 2017-02-28 18:07:03 +0900 | [diff] [blame] | 107 | return path; |
| 108 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // Search user repository. |
| 112 | // TODO Should only look here if not in safe mode. |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 113 | path = ""; |
| 114 | char *androidData = getenv("ANDROID_DATA"); |
| 115 | if (androidData != nullptr) { |
| 116 | path += androidData; |
| 117 | } |
| 118 | path += "/system/devices/"; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 119 | appendInputDeviceConfigurationFileRelativePath(path, name, type); |
| 120 | #if DEBUG_PROBE |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 121 | ALOGD("Probing for system user input device configuration file: path='%s'", path.c_str()); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 122 | #endif |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 123 | if (!access(path.c_str(), R_OK)) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 124 | #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 Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 133 | name.c_str(), type); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 134 | #endif |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 135 | return ""; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Siarhei Vishniakou | b45635c | 2019-02-20 19:22:09 -0600 | [diff] [blame] | 138 | // --- InputDeviceIdentifier |
| 139 | |
| 140 | std::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 Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 150 | |
| 151 | // --- InputDeviceInfo --- |
| 152 | |
| 153 | InputDeviceInfo::InputDeviceInfo() { |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 154 | initialize(-1, 0, -1, InputDeviceIdentifier(), "", false, false); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 157 | InputDeviceInfo::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 Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame^] | 169 | mHasBattery(other.mHasBattery), |
Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 170 | mHasButtonUnderPad(other.mHasButtonUnderPad), |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 171 | mHasSensor(other.mHasSensor), |
| 172 | mMotionRanges(other.mMotionRanges), |
| 173 | mSensors(other.mSensors) {} |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 174 | |
| 175 | InputDeviceInfo::~InputDeviceInfo() { |
| 176 | } |
| 177 | |
Michael Wright | 0415d63 | 2013-07-17 13:23:26 -0700 | [diff] [blame] | 178 | void InputDeviceInfo::initialize(int32_t id, int32_t generation, int32_t controllerNumber, |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 179 | const InputDeviceIdentifier& identifier, const std::string& alias, bool isExternal, |
Tim Kilbourn | 063ff53 | 2015-04-08 10:26:18 -0700 | [diff] [blame] | 180 | bool hasMic) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 181 | mId = id; |
| 182 | mGeneration = generation; |
Michael Wright | 0415d63 | 2013-07-17 13:23:26 -0700 | [diff] [blame] | 183 | mControllerNumber = controllerNumber; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 184 | mIdentifier = identifier; |
| 185 | mAlias = alias; |
| 186 | mIsExternal = isExternal; |
Tim Kilbourn | 063ff53 | 2015-04-08 10:26:18 -0700 | [diff] [blame] | 187 | mHasMic = hasMic; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 188 | mSources = 0; |
| 189 | mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE; |
| 190 | mHasVibrator = false; |
Kim Low | 03ea035 | 2020-11-06 12:45:07 -0800 | [diff] [blame^] | 191 | mHasBattery = false; |
Michael Wright | 931fd6d | 2013-07-10 18:05:15 -0700 | [diff] [blame] | 192 | mHasButtonUnderPad = false; |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 193 | mHasSensor = false; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 194 | mMotionRanges.clear(); |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 195 | mSensors.clear(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | const InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange( |
| 199 | int32_t axis, uint32_t source) const { |
| 200 | size_t numRanges = mMotionRanges.size(); |
| 201 | for (size_t i = 0; i < numRanges; i++) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 202 | const MotionRange& range = mMotionRanges[i]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 203 | if (range.axis == axis && range.source == source) { |
| 204 | return ⦥ |
| 205 | } |
| 206 | } |
Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 207 | return nullptr; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void InputDeviceInfo::addSource(uint32_t source) { |
| 211 | mSources |= source; |
| 212 | } |
| 213 | |
| 214 | void InputDeviceInfo::addMotionRange(int32_t axis, uint32_t source, float min, float max, |
| 215 | float flat, float fuzz, float resolution) { |
| 216 | MotionRange range = { axis, source, min, max, flat, fuzz, resolution }; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 217 | mMotionRanges.push_back(range); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void InputDeviceInfo::addMotionRange(const MotionRange& range) { |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 221 | mMotionRanges.push_back(range); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Chris Ye | f59a2f4 | 2020-10-16 12:55:26 -0700 | [diff] [blame] | 224 | void InputDeviceInfo::addSensorInfo(const InputDeviceSensorInfo& info) { |
| 225 | if (mSensors.find(info.type) != mSensors.end()) { |
| 226 | ALOGW("Sensor type %s already exists, will be replaced by new sensor added.", |
| 227 | NamedEnum::string(info.type).c_str()); |
| 228 | } |
| 229 | mSensors.insert_or_assign(info.type, info); |
| 230 | } |
| 231 | |
| 232 | const std::vector<InputDeviceSensorType> InputDeviceInfo::getSensorTypes() { |
| 233 | std::vector<InputDeviceSensorType> types; |
| 234 | for (const auto& [type, info] : mSensors) { |
| 235 | types.push_back(type); |
| 236 | } |
| 237 | return types; |
| 238 | } |
| 239 | |
| 240 | const InputDeviceSensorInfo* InputDeviceInfo::getSensorInfo(InputDeviceSensorType type) { |
| 241 | auto it = mSensors.find(type); |
| 242 | if (it == mSensors.end()) { |
| 243 | return nullptr; |
| 244 | } |
| 245 | return &it->second; |
| 246 | } |
| 247 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 248 | } // namespace android |