blob: 778c4539fa22d84464d25fbf5defd6b9aa1fd5d6 [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>
Jeff Brown5912f952013-07-01 19:10:31 -070026
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010027using android::base::StringPrintf;
28
Jeff Brown5912f952013-07-01 19:10:31 -070029namespace android {
30
31static const char* CONFIGURATION_FILE_DIR[] = {
32 "idc/",
33 "keylayout/",
34 "keychars/",
35};
36
37static const char* CONFIGURATION_FILE_EXTENSION[] = {
38 ".idc",
39 ".kl",
40 ".kcm",
41};
42
43static bool isValidNameChar(char ch) {
44 return isascii(ch) && (isdigit(ch) || isalpha(ch) || ch == '-' || ch == '_');
45}
46
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010047static void appendInputDeviceConfigurationFileRelativePath(std::string& path,
48 const std::string& name, InputDeviceConfigurationFileType type) {
Jeff Brown5912f952013-07-01 19:10:31 -070049 path.append(CONFIGURATION_FILE_DIR[type]);
50 for (size_t i = 0; i < name.length(); i++) {
51 char ch = name[i];
52 if (!isValidNameChar(ch)) {
53 ch = '_';
54 }
55 path.append(&ch, 1);
56 }
57 path.append(CONFIGURATION_FILE_EXTENSION[type]);
58}
59
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010060std::string getInputDeviceConfigurationFilePathByDeviceIdentifier(
Jeff Brown5912f952013-07-01 19:10:31 -070061 const InputDeviceIdentifier& deviceIdentifier,
62 InputDeviceConfigurationFileType type) {
63 if (deviceIdentifier.vendor !=0 && deviceIdentifier.product != 0) {
64 if (deviceIdentifier.version != 0) {
65 // Try vendor product version.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010066 std::string versionPath = getInputDeviceConfigurationFilePathByName(
67 StringPrintf("Vendor_%04x_Product_%04x_Version_%04x",
Jeff Brown5912f952013-07-01 19:10:31 -070068 deviceIdentifier.vendor, deviceIdentifier.product,
69 deviceIdentifier.version),
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010070 type);
71 if (!versionPath.empty()) {
Jeff Brown5912f952013-07-01 19:10:31 -070072 return versionPath;
73 }
74 }
75
76 // Try vendor product.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010077 std::string productPath = getInputDeviceConfigurationFilePathByName(
78 StringPrintf("Vendor_%04x_Product_%04x",
Jeff Brown5912f952013-07-01 19:10:31 -070079 deviceIdentifier.vendor, deviceIdentifier.product),
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010080 type);
81 if (!productPath.empty()) {
Jeff Brown5912f952013-07-01 19:10:31 -070082 return productPath;
83 }
84 }
85
86 // Try device name.
87 return getInputDeviceConfigurationFilePathByName(deviceIdentifier.name, type);
88}
89
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010090std::string getInputDeviceConfigurationFilePathByName(
91 const std::string& name, InputDeviceConfigurationFileType type) {
Jeff Brown5912f952013-07-01 19:10:31 -070092 // Search system repository.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010093 std::string path;
Jaekyun Seok7ead73e2017-02-28 18:07:03 +090094
95 // Treblized input device config files will be located /odm/usr or /vendor/usr.
Frank Barchard9e947882017-03-06 11:17:52 -080096 const char *rootsForPartition[] {"/odm", "/vendor", getenv("ANDROID_ROOT")};
Jaekyun Seok7ead73e2017-02-28 18:07:03 +090097 for (size_t i = 0; i < size(rootsForPartition); i++) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010098 if (rootsForPartition[i] == nullptr) {
99 continue;
100 }
101 path = rootsForPartition[i];
102 path += "/usr/";
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900103 appendInputDeviceConfigurationFileRelativePath(path, name, type);
Jeff Brown5912f952013-07-01 19:10:31 -0700104#if DEBUG_PROBE
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900105 ALOGD("Probing for system provided input device configuration file: path='%s'",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100106 path.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700107#endif
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100108 if (!access(path.c_str(), R_OK)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700109#if DEBUG_PROBE
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900110 ALOGD("Found");
Jeff Brown5912f952013-07-01 19:10:31 -0700111#endif
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900112 return path;
113 }
Jeff Brown5912f952013-07-01 19:10:31 -0700114 }
115
116 // Search user repository.
117 // TODO Should only look here if not in safe mode.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100118 path = "";
119 char *androidData = getenv("ANDROID_DATA");
120 if (androidData != nullptr) {
121 path += androidData;
122 }
123 path += "/system/devices/";
Jeff Brown5912f952013-07-01 19:10:31 -0700124 appendInputDeviceConfigurationFileRelativePath(path, name, type);
125#if DEBUG_PROBE
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100126 ALOGD("Probing for system user input device configuration file: path='%s'", path.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700127#endif
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100128 if (!access(path.c_str(), R_OK)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700129#if DEBUG_PROBE
130 ALOGD("Found");
131#endif
132 return path;
133 }
134
135 // Not found.
136#if DEBUG_PROBE
137 ALOGD("Probe failed to find input device configuration file: name='%s', type=%d",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100138 name.c_str(), type);
Jeff Brown5912f952013-07-01 19:10:31 -0700139#endif
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100140 return "";
Jeff Brown5912f952013-07-01 19:10:31 -0700141}
142
143
144// --- InputDeviceInfo ---
145
146InputDeviceInfo::InputDeviceInfo() {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100147 initialize(-1, 0, -1, InputDeviceIdentifier(), "", false, false);
Jeff Brown5912f952013-07-01 19:10:31 -0700148}
149
150InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other) :
Michael Wright0415d632013-07-17 13:23:26 -0700151 mId(other.mId), mGeneration(other.mGeneration), mControllerNumber(other.mControllerNumber),
152 mIdentifier(other.mIdentifier), mAlias(other.mAlias), mIsExternal(other.mIsExternal),
Tim Kilbourn063ff532015-04-08 10:26:18 -0700153 mHasMic(other.mHasMic), mSources(other.mSources),
154 mKeyboardType(other.mKeyboardType), mKeyCharacterMap(other.mKeyCharacterMap),
155 mHasVibrator(other.mHasVibrator), mHasButtonUnderPad(other.mHasButtonUnderPad),
156 mMotionRanges(other.mMotionRanges) {
Jeff Brown5912f952013-07-01 19:10:31 -0700157}
158
159InputDeviceInfo::~InputDeviceInfo() {
160}
161
Michael Wright0415d632013-07-17 13:23:26 -0700162void InputDeviceInfo::initialize(int32_t id, int32_t generation, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100163 const InputDeviceIdentifier& identifier, const std::string& alias, bool isExternal,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700164 bool hasMic) {
Jeff Brown5912f952013-07-01 19:10:31 -0700165 mId = id;
166 mGeneration = generation;
Michael Wright0415d632013-07-17 13:23:26 -0700167 mControllerNumber = controllerNumber;
Jeff Brown5912f952013-07-01 19:10:31 -0700168 mIdentifier = identifier;
169 mAlias = alias;
170 mIsExternal = isExternal;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700171 mHasMic = hasMic;
Jeff Brown5912f952013-07-01 19:10:31 -0700172 mSources = 0;
173 mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE;
174 mHasVibrator = false;
Michael Wright931fd6d2013-07-10 18:05:15 -0700175 mHasButtonUnderPad = false;
Jeff Brown5912f952013-07-01 19:10:31 -0700176 mMotionRanges.clear();
177}
178
179const InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange(
180 int32_t axis, uint32_t source) const {
181 size_t numRanges = mMotionRanges.size();
182 for (size_t i = 0; i < numRanges; i++) {
183 const MotionRange& range = mMotionRanges.itemAt(i);
184 if (range.axis == axis && range.source == source) {
185 return &range;
186 }
187 }
Yi Kong5bed83b2018-07-17 12:53:47 -0700188 return nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700189}
190
191void InputDeviceInfo::addSource(uint32_t source) {
192 mSources |= source;
193}
194
195void InputDeviceInfo::addMotionRange(int32_t axis, uint32_t source, float min, float max,
196 float flat, float fuzz, float resolution) {
197 MotionRange range = { axis, source, min, max, flat, fuzz, resolution };
198 mMotionRanges.add(range);
199}
200
201void InputDeviceInfo::addMotionRange(const MotionRange& range) {
202 mMotionRanges.add(range);
203}
204
205} // namespace android