Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2012 The Android Open Source Project |
michael@0 | 3 | * |
michael@0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 5 | * you may not use this file except in compliance with the License. |
michael@0 | 6 | * You may obtain a copy of the License at |
michael@0 | 7 | * |
michael@0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 9 | * |
michael@0 | 10 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 13 | * See the License for the specific language governing permissions and |
michael@0 | 14 | * limitations under the License. |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | #define LOG_TAG "InputDevice" |
michael@0 | 18 | |
michael@0 | 19 | #include <stdlib.h> |
michael@0 | 20 | #include <unistd.h> |
michael@0 | 21 | #include <ctype.h> |
michael@0 | 22 | |
michael@0 | 23 | #include "InputDevice.h" |
michael@0 | 24 | |
michael@0 | 25 | namespace android { |
michael@0 | 26 | |
michael@0 | 27 | static const char* CONFIGURATION_FILE_DIR[] = { |
michael@0 | 28 | "idc/", |
michael@0 | 29 | "keylayout/", |
michael@0 | 30 | "keychars/", |
michael@0 | 31 | }; |
michael@0 | 32 | |
michael@0 | 33 | static const char* CONFIGURATION_FILE_EXTENSION[] = { |
michael@0 | 34 | ".idc", |
michael@0 | 35 | ".kl", |
michael@0 | 36 | ".kcm", |
michael@0 | 37 | }; |
michael@0 | 38 | |
michael@0 | 39 | static bool isValidNameChar(char ch) { |
michael@0 | 40 | return isascii(ch) && (isdigit(ch) || isalpha(ch) || ch == '-' || ch == '_'); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | static void appendInputDeviceConfigurationFileRelativePath(String8& path, |
michael@0 | 44 | const String8& name, InputDeviceConfigurationFileType type) { |
michael@0 | 45 | path.append(CONFIGURATION_FILE_DIR[type]); |
michael@0 | 46 | for (size_t i = 0; i < name.length(); i++) { |
michael@0 | 47 | char ch = name[i]; |
michael@0 | 48 | if (!isValidNameChar(ch)) { |
michael@0 | 49 | ch = '_'; |
michael@0 | 50 | } |
michael@0 | 51 | path.append(&ch, 1); |
michael@0 | 52 | } |
michael@0 | 53 | path.append(CONFIGURATION_FILE_EXTENSION[type]); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | String8 getInputDeviceConfigurationFilePathByDeviceIdentifier( |
michael@0 | 57 | const InputDeviceIdentifier& deviceIdentifier, |
michael@0 | 58 | InputDeviceConfigurationFileType type) { |
michael@0 | 59 | if (deviceIdentifier.vendor !=0 && deviceIdentifier.product != 0) { |
michael@0 | 60 | if (deviceIdentifier.version != 0) { |
michael@0 | 61 | // Try vendor product version. |
michael@0 | 62 | String8 versionPath(getInputDeviceConfigurationFilePathByName( |
michael@0 | 63 | String8::format("Vendor_%04x_Product_%04x_Version_%04x", |
michael@0 | 64 | deviceIdentifier.vendor, deviceIdentifier.product, |
michael@0 | 65 | deviceIdentifier.version), |
michael@0 | 66 | type)); |
michael@0 | 67 | if (!versionPath.isEmpty()) { |
michael@0 | 68 | return versionPath; |
michael@0 | 69 | } |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | // Try vendor product. |
michael@0 | 73 | String8 productPath(getInputDeviceConfigurationFilePathByName( |
michael@0 | 74 | String8::format("Vendor_%04x_Product_%04x", |
michael@0 | 75 | deviceIdentifier.vendor, deviceIdentifier.product), |
michael@0 | 76 | type)); |
michael@0 | 77 | if (!productPath.isEmpty()) { |
michael@0 | 78 | return productPath; |
michael@0 | 79 | } |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | // Try device name. |
michael@0 | 83 | return getInputDeviceConfigurationFilePathByName(deviceIdentifier.name, type); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | String8 getInputDeviceConfigurationFilePathByName( |
michael@0 | 87 | const String8& name, InputDeviceConfigurationFileType type) { |
michael@0 | 88 | // Search system repository. |
michael@0 | 89 | String8 path; |
michael@0 | 90 | path.setTo(getenv("ANDROID_ROOT")); |
michael@0 | 91 | path.append("/usr/"); |
michael@0 | 92 | appendInputDeviceConfigurationFileRelativePath(path, name, type); |
michael@0 | 93 | #if DEBUG_PROBE |
michael@0 | 94 | ALOGD("Probing for system provided input device configuration file: path='%s'", path.string()); |
michael@0 | 95 | #endif |
michael@0 | 96 | if (!access(path.string(), R_OK)) { |
michael@0 | 97 | #if DEBUG_PROBE |
michael@0 | 98 | ALOGD("Found"); |
michael@0 | 99 | #endif |
michael@0 | 100 | return path; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | // Search user repository. |
michael@0 | 104 | // TODO Should only look here if not in safe mode. |
michael@0 | 105 | path.setTo(getenv("ANDROID_DATA")); |
michael@0 | 106 | path.append("/system/devices/"); |
michael@0 | 107 | appendInputDeviceConfigurationFileRelativePath(path, name, type); |
michael@0 | 108 | #if DEBUG_PROBE |
michael@0 | 109 | ALOGD("Probing for system user input device configuration file: path='%s'", path.string()); |
michael@0 | 110 | #endif |
michael@0 | 111 | if (!access(path.string(), R_OK)) { |
michael@0 | 112 | #if DEBUG_PROBE |
michael@0 | 113 | ALOGD("Found"); |
michael@0 | 114 | #endif |
michael@0 | 115 | return path; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | // Not found. |
michael@0 | 119 | #if DEBUG_PROBE |
michael@0 | 120 | ALOGD("Probe failed to find input device configuration file: name='%s', type=%d", |
michael@0 | 121 | name.string(), type); |
michael@0 | 122 | #endif |
michael@0 | 123 | return String8(); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | |
michael@0 | 127 | // --- InputDeviceInfo --- |
michael@0 | 128 | |
michael@0 | 129 | InputDeviceInfo::InputDeviceInfo() { |
michael@0 | 130 | initialize(-1, -1, InputDeviceIdentifier(), String8(), false); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other) : |
michael@0 | 134 | mId(other.mId), mGeneration(other.mGeneration), mIdentifier(other.mIdentifier), |
michael@0 | 135 | mAlias(other.mAlias), mIsExternal(other.mIsExternal), mSources(other.mSources), |
michael@0 | 136 | mKeyboardType(other.mKeyboardType), |
michael@0 | 137 | mKeyCharacterMap(other.mKeyCharacterMap), |
michael@0 | 138 | mHasVibrator(other.mHasVibrator), |
michael@0 | 139 | mMotionRanges(other.mMotionRanges) { |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | InputDeviceInfo::~InputDeviceInfo() { |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | void InputDeviceInfo::initialize(int32_t id, int32_t generation, |
michael@0 | 146 | const InputDeviceIdentifier& identifier, const String8& alias, bool isExternal) { |
michael@0 | 147 | mId = id; |
michael@0 | 148 | mGeneration = generation; |
michael@0 | 149 | mIdentifier = identifier; |
michael@0 | 150 | mAlias = alias; |
michael@0 | 151 | mIsExternal = isExternal; |
michael@0 | 152 | mSources = 0; |
michael@0 | 153 | mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE; |
michael@0 | 154 | mHasVibrator = false; |
michael@0 | 155 | mMotionRanges.clear(); |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | const InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange( |
michael@0 | 159 | int32_t axis, uint32_t source) const { |
michael@0 | 160 | size_t numRanges = mMotionRanges.size(); |
michael@0 | 161 | for (size_t i = 0; i < numRanges; i++) { |
michael@0 | 162 | const MotionRange& range = mMotionRanges.itemAt(i); |
michael@0 | 163 | if (range.axis == axis && range.source == source) { |
michael@0 | 164 | return ⦥ |
michael@0 | 165 | } |
michael@0 | 166 | } |
michael@0 | 167 | return NULL; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | void InputDeviceInfo::addSource(uint32_t source) { |
michael@0 | 171 | mSources |= source; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | void InputDeviceInfo::addMotionRange(int32_t axis, uint32_t source, float min, float max, |
michael@0 | 175 | float flat, float fuzz, float resolution) { |
michael@0 | 176 | MotionRange range = { axis, source, min, max, flat, fuzz, resolution }; |
michael@0 | 177 | mMotionRanges.add(range); |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | void InputDeviceInfo::addMotionRange(const MotionRange& range) { |
michael@0 | 181 | mMotionRanges.add(range); |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | } // namespace android |