blob: 4db9e06d8da87faddd9bc1694d46bc60795afd50 [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) {
Siarhei Vishniakou409ec1a2019-02-20 19:36:25 -060049 path += CONFIGURATION_FILE_DIR[type];
50 path += name;
51 path += CONFIGURATION_FILE_EXTENSION[type];
Jeff Brown5912f952013-07-01 19:10:31 -070052}
53
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010054std::string getInputDeviceConfigurationFilePathByDeviceIdentifier(
Jeff Brown5912f952013-07-01 19:10:31 -070055 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 Vishniakouec8f7252018-07-06 11:19:32 +010060 std::string versionPath = getInputDeviceConfigurationFilePathByName(
61 StringPrintf("Vendor_%04x_Product_%04x_Version_%04x",
Jeff Brown5912f952013-07-01 19:10:31 -070062 deviceIdentifier.vendor, deviceIdentifier.product,
63 deviceIdentifier.version),
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010064 type);
65 if (!versionPath.empty()) {
Jeff Brown5912f952013-07-01 19:10:31 -070066 return versionPath;
67 }
68 }
69
70 // Try vendor product.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010071 std::string productPath = getInputDeviceConfigurationFilePathByName(
72 StringPrintf("Vendor_%04x_Product_%04x",
Jeff Brown5912f952013-07-01 19:10:31 -070073 deviceIdentifier.vendor, deviceIdentifier.product),
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010074 type);
75 if (!productPath.empty()) {
Jeff Brown5912f952013-07-01 19:10:31 -070076 return productPath;
77 }
78 }
79
80 // Try device name.
Siarhei Vishniakou409ec1a2019-02-20 19:36:25 -060081 return getInputDeviceConfigurationFilePathByName(deviceIdentifier.getCanonicalName(), type);
Jeff Brown5912f952013-07-01 19:10:31 -070082}
83
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010084std::string getInputDeviceConfigurationFilePathByName(
85 const std::string& name, InputDeviceConfigurationFileType type) {
Jeff Brown5912f952013-07-01 19:10:31 -070086 // Search system repository.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010087 std::string path;
Jaekyun Seok7ead73e2017-02-28 18:07:03 +090088
89 // Treblized input device config files will be located /odm/usr or /vendor/usr.
Frank Barchard9e947882017-03-06 11:17:52 -080090 const char *rootsForPartition[] {"/odm", "/vendor", getenv("ANDROID_ROOT")};
Jaekyun Seok7ead73e2017-02-28 18:07:03 +090091 for (size_t i = 0; i < size(rootsForPartition); i++) {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010092 if (rootsForPartition[i] == nullptr) {
93 continue;
94 }
95 path = rootsForPartition[i];
96 path += "/usr/";
Jaekyun Seok7ead73e2017-02-28 18:07:03 +090097 appendInputDeviceConfigurationFileRelativePath(path, name, type);
Jeff Brown5912f952013-07-01 19:10:31 -070098#if DEBUG_PROBE
Jaekyun Seok7ead73e2017-02-28 18:07:03 +090099 ALOGD("Probing for system provided input device configuration file: path='%s'",
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100100 path.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700101#endif
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100102 if (!access(path.c_str(), R_OK)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700103#if DEBUG_PROBE
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900104 ALOGD("Found");
Jeff Brown5912f952013-07-01 19:10:31 -0700105#endif
Jaekyun Seok7ead73e2017-02-28 18:07:03 +0900106 return path;
107 }
Jeff Brown5912f952013-07-01 19:10:31 -0700108 }
109
110 // Search user repository.
111 // TODO Should only look here if not in safe mode.
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100112 path = "";
113 char *androidData = getenv("ANDROID_DATA");
114 if (androidData != nullptr) {
115 path += androidData;
116 }
117 path += "/system/devices/";
Jeff Brown5912f952013-07-01 19:10:31 -0700118 appendInputDeviceConfigurationFileRelativePath(path, name, type);
119#if DEBUG_PROBE
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100120 ALOGD("Probing for system user input device configuration file: path='%s'", 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
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 Vishniakouec8f7252018-07-06 11:19:32 +0100132 name.c_str(), type);
Jeff Brown5912f952013-07-01 19:10:31 -0700133#endif
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100134 return "";
Jeff Brown5912f952013-07-01 19:10:31 -0700135}
136
Siarhei Vishniakoub45635c2019-02-20 19:22:09 -0600137// --- InputDeviceIdentifier
138
139std::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 Brown5912f952013-07-01 19:10:31 -0700149
150// --- InputDeviceInfo ---
151
152InputDeviceInfo::InputDeviceInfo() {
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100153 initialize(-1, 0, -1, InputDeviceIdentifier(), "", false, false);
Jeff Brown5912f952013-07-01 19:10:31 -0700154}
155
156InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other) :
Michael Wright0415d632013-07-17 13:23:26 -0700157 mId(other.mId), mGeneration(other.mGeneration), mControllerNumber(other.mControllerNumber),
158 mIdentifier(other.mIdentifier), mAlias(other.mAlias), mIsExternal(other.mIsExternal),
Tim Kilbourn063ff532015-04-08 10:26:18 -0700159 mHasMic(other.mHasMic), mSources(other.mSources),
160 mKeyboardType(other.mKeyboardType), mKeyCharacterMap(other.mKeyCharacterMap),
161 mHasVibrator(other.mHasVibrator), mHasButtonUnderPad(other.mHasButtonUnderPad),
162 mMotionRanges(other.mMotionRanges) {
Jeff Brown5912f952013-07-01 19:10:31 -0700163}
164
165InputDeviceInfo::~InputDeviceInfo() {
166}
167
Michael Wright0415d632013-07-17 13:23:26 -0700168void InputDeviceInfo::initialize(int32_t id, int32_t generation, int32_t controllerNumber,
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100169 const InputDeviceIdentifier& identifier, const std::string& alias, bool isExternal,
Tim Kilbourn063ff532015-04-08 10:26:18 -0700170 bool hasMic) {
Jeff Brown5912f952013-07-01 19:10:31 -0700171 mId = id;
172 mGeneration = generation;
Michael Wright0415d632013-07-17 13:23:26 -0700173 mControllerNumber = controllerNumber;
Jeff Brown5912f952013-07-01 19:10:31 -0700174 mIdentifier = identifier;
175 mAlias = alias;
176 mIsExternal = isExternal;
Tim Kilbourn063ff532015-04-08 10:26:18 -0700177 mHasMic = hasMic;
Jeff Brown5912f952013-07-01 19:10:31 -0700178 mSources = 0;
179 mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE;
180 mHasVibrator = false;
Michael Wright931fd6d2013-07-10 18:05:15 -0700181 mHasButtonUnderPad = false;
Jeff Brown5912f952013-07-01 19:10:31 -0700182 mMotionRanges.clear();
183}
184
185const InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange(
186 int32_t axis, uint32_t source) const {
187 size_t numRanges = mMotionRanges.size();
188 for (size_t i = 0; i < numRanges; i++) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800189 const MotionRange& range = mMotionRanges[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700190 if (range.axis == axis && range.source == source) {
191 return &range;
192 }
193 }
Yi Kong5bed83b2018-07-17 12:53:47 -0700194 return nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700195}
196
197void InputDeviceInfo::addSource(uint32_t source) {
198 mSources |= source;
199}
200
201void InputDeviceInfo::addMotionRange(int32_t axis, uint32_t source, float min, float max,
202 float flat, float fuzz, float resolution) {
203 MotionRange range = { axis, source, min, max, flat, fuzz, resolution };
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800204 mMotionRanges.push_back(range);
Jeff Brown5912f952013-07-01 19:10:31 -0700205}
206
207void InputDeviceInfo::addMotionRange(const MotionRange& range) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800208 mMotionRanges.push_back(range);
Jeff Brown5912f952013-07-01 19:10:31 -0700209}
210
211} // namespace android