2
* Copyright (C) 2012 The Android Open Source Project
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
#define LOG_TAG "InputDevice"
23
#include <androidfw/InputDevice.h>
27
static const char* CONFIGURATION_FILE_DIR[] = {
33
static const char* CONFIGURATION_FILE_EXTENSION[] = {
39
static bool isValidNameChar(char ch) {
40
return isascii(ch) && (isdigit(ch) || isalpha(ch) || ch == '-' || ch == '_');
43
static void appendInputDeviceConfigurationFileRelativePath(String8& path,
44
const String8& name, InputDeviceConfigurationFileType type) {
45
path.append(CONFIGURATION_FILE_DIR[type]);
46
for (size_t i = 0; i < name.length(); i++) {
48
if (!isValidNameChar(ch)) {
53
path.append(CONFIGURATION_FILE_EXTENSION[type]);
56
String8 getInputDeviceConfigurationFilePathByDeviceIdentifier(
57
const InputDeviceIdentifier& deviceIdentifier,
58
InputDeviceConfigurationFileType type) {
59
if (deviceIdentifier.vendor !=0 && deviceIdentifier.product != 0) {
60
if (deviceIdentifier.version != 0) {
61
// Try vendor product version.
62
String8 versionPath(getInputDeviceConfigurationFilePathByName(
63
String8::format("Vendor_%04x_Product_%04x_Version_%04x",
64
deviceIdentifier.vendor, deviceIdentifier.product,
65
deviceIdentifier.version),
67
if (!versionPath.isEmpty()) {
72
// Try vendor product.
73
String8 productPath(getInputDeviceConfigurationFilePathByName(
74
String8::format("Vendor_%04x_Product_%04x",
75
deviceIdentifier.vendor, deviceIdentifier.product),
77
if (!productPath.isEmpty()) {
83
return getInputDeviceConfigurationFilePathByName(deviceIdentifier.name, type);
86
String8 getInputDeviceConfigurationFilePathByName(
87
const String8& name, InputDeviceConfigurationFileType type) {
88
// Search system repository.
90
path.setTo(getenv("ANDROID_ROOT"));
92
appendInputDeviceConfigurationFileRelativePath(path, name, type);
94
ALOGD("Probing for system provided input device configuration file: path='%s'", path.string());
96
if (!access(path.string(), R_OK)) {
103
// Search user repository.
104
// TODO Should only look here if not in safe mode.
105
path.setTo(getenv("ANDROID_DATA"));
106
path.append("/system/devices/");
107
appendInputDeviceConfigurationFileRelativePath(path, name, type);
109
ALOGD("Probing for system user input device configuration file: path='%s'", path.string());
111
if (!access(path.string(), R_OK)) {
120
ALOGD("Probe failed to find input device configuration file: name='%s', type=%d",
121
name.string(), type);
127
// --- InputDeviceInfo ---
129
InputDeviceInfo::InputDeviceInfo() {
130
initialize(-1, -1, InputDeviceIdentifier(), String8(), false);
133
InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other) :
134
mId(other.mId), mGeneration(other.mGeneration), mIdentifier(other.mIdentifier),
135
mAlias(other.mAlias), mIsExternal(other.mIsExternal), mSources(other.mSources),
136
mKeyboardType(other.mKeyboardType),
137
mKeyCharacterMap(other.mKeyCharacterMap),
138
mHasVibrator(other.mHasVibrator),
139
mMotionRanges(other.mMotionRanges) {
142
InputDeviceInfo::~InputDeviceInfo() {
145
void InputDeviceInfo::initialize(int32_t id, int32_t generation,
146
const InputDeviceIdentifier& identifier, const String8& alias, bool isExternal) {
148
mGeneration = generation;
149
mIdentifier = identifier;
151
mIsExternal = isExternal;
153
mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE;
154
mHasVibrator = false;
155
mMotionRanges.clear();
158
const InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange(
159
int32_t axis, uint32_t source) const {
160
size_t numRanges = mMotionRanges.size();
161
for (size_t i = 0; i < numRanges; i++) {
162
const MotionRange& range = mMotionRanges.itemAt(i);
163
if (range.axis == axis && range.source == source) {
170
void InputDeviceInfo::addSource(uint32_t source) {
174
void InputDeviceInfo::addMotionRange(int32_t axis, uint32_t source, float min, float max,
175
float flat, float fuzz) {
176
MotionRange range = { axis, source, min, max, flat, fuzz };
177
mMotionRanges.add(range);
180
void InputDeviceInfo::addMotionRange(const MotionRange& range) {
181
mMotionRanges.add(range);
184
} // namespace android