InputDevice.cpp revision 931fd6dee7891e94650353c09956ccf24b4dfff3
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* 29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2012 The Android Open Source Project 39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License. 69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at 79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and 149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License. 159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define LOG_TAG "InputDevice" 189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 19ac85f90466dd60d2af8ffc3942d503a0de606726Alan Viverette#include <stdlib.h> 20813d85b82cb7cbaa5dbe05496d1038caa17a1698Alan Viverette#include <unistd.h> 219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <ctype.h> 229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2309ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette#include <input/InputDevice.h> 2409ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette 257979388d4f7d5d9dbfcf7e1cc4709f8088c034aeChris Craiknamespace android { 2609ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette 2709ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverettestatic const char* CONFIGURATION_FILE_DIR[] = { 28a2992f44bc3787f37c3cad98f79dcfe67fb1aa81Alan Viverette "idc/", 2909ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette "keylayout/", 3009ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette "keychars/", 3109ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette}; 3209ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette 3309ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverettestatic const char* CONFIGURATION_FILE_EXTENSION[] = { 3452b999f0721b53e9c6e18a4bd664e89aeb65b2d5Alan Viverette ".idc", 359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project ".kl", 369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project ".kcm", 37611c1ff0de00b636ca0f9d5500c91d75b9c5257aAlan Viverette}; 389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic bool isValidNameChar(char ch) { 409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return isascii(ch) && (isdigit(ch) || isalpha(ch) || ch == '-' || ch == '_'); 419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic void appendInputDeviceConfigurationFileRelativePath(String8& path, 4409ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette const String8& name, InputDeviceConfigurationFileType type) { 4509ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette path.append(CONFIGURATION_FILE_DIR[type]); 4609ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette for (size_t i = 0; i < name.length(); i++) { 4709ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette char ch = name[i]; 4809ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette if (!isValidNameChar(ch)) { 4909ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette ch = '_'; 5009ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette } 5109ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette path.append(&ch, 1); 5209ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette } 5309ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette path.append(CONFIGURATION_FILE_EXTENSION[type]); 5409ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette} 5509ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette 5609ceac298cc12471e4575fe49f97766c1ed7b2a1Alan ViveretteString8 getInputDeviceConfigurationFilePathByDeviceIdentifier( 57776cf41d7c8f239d20397886f2120407396cfc87Yuichi Araki const InputDeviceIdentifier& deviceIdentifier, 58776cf41d7c8f239d20397886f2120407396cfc87Yuichi Araki InputDeviceConfigurationFileType type) { 59776cf41d7c8f239d20397886f2120407396cfc87Yuichi Araki if (deviceIdentifier.vendor !=0 && deviceIdentifier.product != 0) { 6009ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette if (deviceIdentifier.version != 0) { 6109ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette // Try vendor product version. 62dfe5c204403bc56c29bb36410574eab8b1950417Scott Main String8 versionPath(getInputDeviceConfigurationFilePathByName( 639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project String8::format("Vendor_%04x_Product_%04x_Version_%04x", 649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project deviceIdentifier.vendor, deviceIdentifier.product, 659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project deviceIdentifier.version), 669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project type)); 679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (!versionPath.isEmpty()) { 689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return versionPath; 699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Try vendor product. 73813d85b82cb7cbaa5dbe05496d1038caa17a1698Alan Viverette String8 productPath(getInputDeviceConfigurationFilePathByName( 749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project String8::format("Vendor_%04x_Product_%04x", 759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project deviceIdentifier.vendor, deviceIdentifier.product), 769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project type)); 779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (!productPath.isEmpty()) { 789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return productPath; 799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 8017cd4dfe3a05c2eddbcbc76066ff3b13fc3f2c8bAlan Viverette } 819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 8209ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette // Try device name. 839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return getInputDeviceConfigurationFilePathByName(deviceIdentifier.name, type); 849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 8509ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette 869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectString8 getInputDeviceConfigurationFilePathByName( 879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project const String8& name, InputDeviceConfigurationFileType type) { 889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Search system repository. 8917cd4dfe3a05c2eddbcbc76066ff3b13fc3f2c8bAlan Viverette String8 path; 9009ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette path.setTo(getenv("ANDROID_ROOT")); 919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project path.append("/usr/"); 929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project appendInputDeviceConfigurationFileRelativePath(path, name, type); 9309ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette#if DEBUG_PROBE 949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project ALOGD("Probing for system provided input device configuration file: path='%s'", path.string()); 959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif 969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (!access(path.string(), R_OK)) { 979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#if DEBUG_PROBE 989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project ALOGD("Found"); 999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif 10009ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette return path; 1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Search user repository. 1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // TODO Should only look here if not in safe mode. 1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project path.setTo(getenv("ANDROID_DATA")); 1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project path.append("/system/devices/"); 1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project appendInputDeviceConfigurationFileRelativePath(path, name, type); 10809ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette#if DEBUG_PROBE 1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project ALOGD("Probing for system user input device configuration file: path='%s'", path.string()); 11009ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette#endif 1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (!access(path.string(), R_OK)) { 11209ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette#if DEBUG_PROBE 1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project ALOGD("Found"); 1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif 1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return path; 1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 11809ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette // Not found. 1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#if DEBUG_PROBE 12009ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette ALOGD("Probe failed to find input device configuration file: name='%s', type=%d", 1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project name.string(), type); 1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif 1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return String8(); 1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// --- InputDeviceInfo --- 1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectInputDeviceInfo::InputDeviceInfo() { 1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project initialize(-1, -1, InputDeviceIdentifier(), String8(), false); 1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 13309ceac298cc12471e4575fe49f97766c1ed7b2a1Alan ViveretteInputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other) : 1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mId(other.mId), mGeneration(other.mGeneration), mIdentifier(other.mIdentifier), 1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mAlias(other.mAlias), mIsExternal(other.mIsExternal), mSources(other.mSources), 13609ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette mKeyboardType(other.mKeyboardType), 13709ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette mKeyCharacterMap(other.mKeyCharacterMap), 13809ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette mHasVibrator(other.mHasVibrator), 13909ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette mHasButtonUnderPad(other.mHasButtonUnderPad), 14009ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette mMotionRanges(other.mMotionRanges) { 1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectInputDeviceInfo::~InputDeviceInfo() { 1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid InputDeviceInfo::initialize(int32_t id, int32_t generation, 1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project const InputDeviceIdentifier& identifier, const String8& alias, bool isExternal) { 1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mId = id; 1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mGeneration = generation; 1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mIdentifier = identifier; 1519891e1fce5f29d0421d34aa481037417bd70853dChet Haase mAlias = alias; 1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mIsExternal = isExternal; 15309ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette mSources = 0; 1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE; 15509ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette mHasVibrator = false; 15609ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette mHasButtonUnderPad = false; 1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mMotionRanges.clear(); 1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectconst InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange( 1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int32_t axis, uint32_t source) const { 1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project size_t numRanges = mMotionRanges.size(); 1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project for (size_t i = 0; i < numRanges; i++) { 1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project const MotionRange& range = mMotionRanges.itemAt(i); 1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (range.axis == axis && range.source == source) { 1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return ⦥ 1679891e1fce5f29d0421d34aa481037417bd70853dChet Haase } 1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 16909ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette return NULL; 1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 17209ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverettevoid InputDeviceInfo::addSource(uint32_t source) { 1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mSources |= source; 1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid InputDeviceInfo::addMotionRange(int32_t axis, uint32_t source, float min, float max, 1779891e1fce5f29d0421d34aa481037417bd70853dChet Haase float flat, float fuzz, float resolution) { 1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project MotionRange range = { axis, source, min, max, flat, fuzz, resolution }; 17909ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverette mMotionRanges.add(range); 1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 18209ceac298cc12471e4575fe49f97766c1ed7b2a1Alan Viverettevoid InputDeviceInfo::addMotionRange(const MotionRange& range) { 1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mMotionRanges.add(range); 1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} // namespace android 1879891e1fce5f29d0421d34aa481037417bd70853dChet Haase