| 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> | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 26 |  | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 27 | using android::base::StringPrintf; | 
|  | 28 |  | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 29 | namespace android { | 
|  | 30 |  | 
|  | 31 | static const char* CONFIGURATION_FILE_DIR[] = { | 
|  | 32 | "idc/", | 
|  | 33 | "keylayout/", | 
|  | 34 | "keychars/", | 
|  | 35 | }; | 
|  | 36 |  | 
|  | 37 | static const char* CONFIGURATION_FILE_EXTENSION[] = { | 
|  | 38 | ".idc", | 
|  | 39 | ".kl", | 
|  | 40 | ".kcm", | 
|  | 41 | }; | 
|  | 42 |  | 
|  | 43 | static bool isValidNameChar(char ch) { | 
|  | 44 | return isascii(ch) && (isdigit(ch) || isalpha(ch) || ch == '-' || ch == '_'); | 
|  | 45 | } | 
|  | 46 |  | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 47 | static void appendInputDeviceConfigurationFileRelativePath(std::string& path, | 
|  | 48 | const std::string& name, InputDeviceConfigurationFileType type) { | 
| Chris Ye | 1d927aa | 2020-07-04 18:22:41 -0700 | [diff] [blame] | 49 | path += CONFIGURATION_FILE_DIR[static_cast<int32_t>(type)]; | 
| Siarhei Vishniakou | 409ec1a | 2019-02-20 19:36:25 -0600 | [diff] [blame] | 50 | path += name; | 
| Chris Ye | 1d927aa | 2020-07-04 18:22:41 -0700 | [diff] [blame] | 51 | path += CONFIGURATION_FILE_EXTENSION[static_cast<int32_t>(type)]; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 52 | } | 
|  | 53 |  | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 54 | std::string getInputDeviceConfigurationFilePathByDeviceIdentifier( | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 55 | const InputDeviceIdentifier& deviceIdentifier, | 
|  | 56 | InputDeviceConfigurationFileType type) { | 
|  | 57 | if (deviceIdentifier.vendor !=0 && deviceIdentifier.product != 0) { | 
|  | 58 | if (deviceIdentifier.version != 0) { | 
|  | 59 | // Try vendor product version. | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 60 | std::string versionPath = getInputDeviceConfigurationFilePathByName( | 
|  | 61 | StringPrintf("Vendor_%04x_Product_%04x_Version_%04x", | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 62 | deviceIdentifier.vendor, deviceIdentifier.product, | 
|  | 63 | deviceIdentifier.version), | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 64 | type); | 
|  | 65 | if (!versionPath.empty()) { | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 66 | return versionPath; | 
|  | 67 | } | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | // Try vendor product. | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 71 | std::string productPath = getInputDeviceConfigurationFilePathByName( | 
|  | 72 | StringPrintf("Vendor_%04x_Product_%04x", | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 73 | deviceIdentifier.vendor, deviceIdentifier.product), | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 74 | type); | 
|  | 75 | if (!productPath.empty()) { | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 76 | return productPath; | 
|  | 77 | } | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | // Try device name. | 
| Siarhei Vishniakou | 409ec1a | 2019-02-20 19:36:25 -0600 | [diff] [blame] | 81 | return getInputDeviceConfigurationFilePathByName(deviceIdentifier.getCanonicalName(), type); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 84 | std::string getInputDeviceConfigurationFilePathByName( | 
|  | 85 | const std::string& name, InputDeviceConfigurationFileType type) { | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 86 | // Search system repository. | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 87 | std::string path; | 
| Jaekyun Seok | 7ead73e | 2017-02-28 18:07:03 +0900 | [diff] [blame] | 88 |  | 
|  | 89 | // 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] | 90 | const char *rootsForPartition[] {"/odm", "/vendor", getenv("ANDROID_ROOT")}; | 
| Jaekyun Seok | 7ead73e | 2017-02-28 18:07:03 +0900 | [diff] [blame] | 91 | for (size_t i = 0; i < size(rootsForPartition); i++) { | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 92 | if (rootsForPartition[i] == nullptr) { | 
|  | 93 | continue; | 
|  | 94 | } | 
|  | 95 | path = rootsForPartition[i]; | 
|  | 96 | path += "/usr/"; | 
| Jaekyun Seok | 7ead73e | 2017-02-28 18:07:03 +0900 | [diff] [blame] | 97 | appendInputDeviceConfigurationFileRelativePath(path, name, type); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 98 | #if DEBUG_PROBE | 
| Jaekyun Seok | 7ead73e | 2017-02-28 18:07:03 +0900 | [diff] [blame] | 99 | ALOGD("Probing for system provided input device configuration file: path='%s'", | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 100 | path.c_str()); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 101 | #endif | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 102 | if (!access(path.c_str(), R_OK)) { | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 103 | #if DEBUG_PROBE | 
| Jaekyun Seok | 7ead73e | 2017-02-28 18:07:03 +0900 | [diff] [blame] | 104 | ALOGD("Found"); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 105 | #endif | 
| Jaekyun Seok | 7ead73e | 2017-02-28 18:07:03 +0900 | [diff] [blame] | 106 | return path; | 
|  | 107 | } | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 108 | } | 
|  | 109 |  | 
|  | 110 | // Search user repository. | 
|  | 111 | // TODO Should only look here if not in safe mode. | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 112 | path = ""; | 
|  | 113 | char *androidData = getenv("ANDROID_DATA"); | 
|  | 114 | if (androidData != nullptr) { | 
|  | 115 | path += androidData; | 
|  | 116 | } | 
|  | 117 | path += "/system/devices/"; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 118 | appendInputDeviceConfigurationFileRelativePath(path, name, type); | 
|  | 119 | #if DEBUG_PROBE | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 120 | 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] | 121 | #endif | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 122 | if (!access(path.c_str(), R_OK)) { | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 123 | #if DEBUG_PROBE | 
|  | 124 | ALOGD("Found"); | 
|  | 125 | #endif | 
|  | 126 | return path; | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | // Not found. | 
|  | 130 | #if DEBUG_PROBE | 
|  | 131 | 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] | 132 | name.c_str(), type); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 133 | #endif | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 134 | return ""; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
| Siarhei Vishniakou | b45635c | 2019-02-20 19:22:09 -0600 | [diff] [blame] | 137 | // --- InputDeviceIdentifier | 
|  | 138 |  | 
|  | 139 | std::string InputDeviceIdentifier::getCanonicalName() const { | 
|  | 140 | std::string replacedName = name; | 
|  | 141 | for (char& ch : replacedName) { | 
|  | 142 | if (!isValidNameChar(ch)) { | 
|  | 143 | ch = '_'; | 
|  | 144 | } | 
|  | 145 | } | 
|  | 146 | return replacedName; | 
|  | 147 | } | 
|  | 148 |  | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 149 |  | 
|  | 150 | // --- InputDeviceInfo --- | 
|  | 151 |  | 
|  | 152 | InputDeviceInfo::InputDeviceInfo() { | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 153 | initialize(-1, 0, -1, InputDeviceIdentifier(), "", false, false); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 154 | } | 
|  | 155 |  | 
| Chris Ye | 3a1e446 | 2020-08-12 10:13:15 -0700 | [diff] [blame] | 156 | InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other) | 
|  | 157 | : mId(other.mId), | 
|  | 158 | mGeneration(other.mGeneration), | 
|  | 159 | mControllerNumber(other.mControllerNumber), | 
|  | 160 | mIdentifier(other.mIdentifier), | 
|  | 161 | mAlias(other.mAlias), | 
|  | 162 | mIsExternal(other.mIsExternal), | 
|  | 163 | mHasMic(other.mHasMic), | 
|  | 164 | mSources(other.mSources), | 
|  | 165 | mKeyboardType(other.mKeyboardType), | 
|  | 166 | mKeyCharacterMap(other.mKeyCharacterMap), | 
|  | 167 | mHasVibrator(other.mHasVibrator), | 
|  | 168 | mHasButtonUnderPad(other.mHasButtonUnderPad), | 
|  | 169 | mMotionRanges(other.mMotionRanges) {} | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 170 |  | 
|  | 171 | InputDeviceInfo::~InputDeviceInfo() { | 
|  | 172 | } | 
|  | 173 |  | 
| Michael Wright | 0415d63 | 2013-07-17 13:23:26 -0700 | [diff] [blame] | 174 | void InputDeviceInfo::initialize(int32_t id, int32_t generation, int32_t controllerNumber, | 
| Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 175 | const InputDeviceIdentifier& identifier, const std::string& alias, bool isExternal, | 
| Tim Kilbourn | 063ff53 | 2015-04-08 10:26:18 -0700 | [diff] [blame] | 176 | bool hasMic) { | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 177 | mId = id; | 
|  | 178 | mGeneration = generation; | 
| Michael Wright | 0415d63 | 2013-07-17 13:23:26 -0700 | [diff] [blame] | 179 | mControllerNumber = controllerNumber; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 180 | mIdentifier = identifier; | 
|  | 181 | mAlias = alias; | 
|  | 182 | mIsExternal = isExternal; | 
| Tim Kilbourn | 063ff53 | 2015-04-08 10:26:18 -0700 | [diff] [blame] | 183 | mHasMic = hasMic; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 184 | mSources = 0; | 
|  | 185 | mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE; | 
|  | 186 | mHasVibrator = false; | 
| Michael Wright | 931fd6d | 2013-07-10 18:05:15 -0700 | [diff] [blame] | 187 | mHasButtonUnderPad = false; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 188 | mMotionRanges.clear(); | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | const InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange( | 
|  | 192 | int32_t axis, uint32_t source) const { | 
|  | 193 | size_t numRanges = mMotionRanges.size(); | 
|  | 194 | for (size_t i = 0; i < numRanges; i++) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 195 | const MotionRange& range = mMotionRanges[i]; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 196 | if (range.axis == axis && range.source == source) { | 
|  | 197 | return ⦥ | 
|  | 198 | } | 
|  | 199 | } | 
| Yi Kong | 5bed83b | 2018-07-17 12:53:47 -0700 | [diff] [blame] | 200 | return nullptr; | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 201 | } | 
|  | 202 |  | 
|  | 203 | void InputDeviceInfo::addSource(uint32_t source) { | 
|  | 204 | mSources |= source; | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | void InputDeviceInfo::addMotionRange(int32_t axis, uint32_t source, float min, float max, | 
|  | 208 | float flat, float fuzz, float resolution) { | 
|  | 209 | MotionRange range = { axis, source, min, max, flat, fuzz, resolution }; | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 210 | mMotionRanges.push_back(range); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 211 | } | 
|  | 212 |  | 
|  | 213 | void InputDeviceInfo::addMotionRange(const MotionRange& range) { | 
| Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 214 | mMotionRanges.push_back(range); | 
| Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 215 | } | 
|  | 216 |  | 
|  | 217 | } // namespace android |