widget/gonk/libui/InputDevice.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/gonk/libui/InputDevice.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,184 @@
     1.4 +/*
     1.5 + * Copyright (C) 2012 The Android Open Source Project
     1.6 + *
     1.7 + * Licensed under the Apache License, Version 2.0 (the "License");
     1.8 + * you may not use this file except in compliance with the License.
     1.9 + * You may obtain a copy of the License at
    1.10 + *
    1.11 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.12 + *
    1.13 + * Unless required by applicable law or agreed to in writing, software
    1.14 + * distributed under the License is distributed on an "AS IS" BASIS,
    1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.16 + * See the License for the specific language governing permissions and
    1.17 + * limitations under the License.
    1.18 + */
    1.19 +
    1.20 +#define LOG_TAG "InputDevice"
    1.21 +
    1.22 +#include <stdlib.h>
    1.23 +#include <unistd.h>
    1.24 +#include <ctype.h>
    1.25 +
    1.26 +#include "InputDevice.h"
    1.27 +
    1.28 +namespace android {
    1.29 +
    1.30 +static const char* CONFIGURATION_FILE_DIR[] = {
    1.31 +        "idc/",
    1.32 +        "keylayout/",
    1.33 +        "keychars/",
    1.34 +};
    1.35 +
    1.36 +static const char* CONFIGURATION_FILE_EXTENSION[] = {
    1.37 +        ".idc",
    1.38 +        ".kl",
    1.39 +        ".kcm",
    1.40 +};
    1.41 +
    1.42 +static bool isValidNameChar(char ch) {
    1.43 +    return isascii(ch) && (isdigit(ch) || isalpha(ch) || ch == '-' || ch == '_');
    1.44 +}
    1.45 +
    1.46 +static void appendInputDeviceConfigurationFileRelativePath(String8& path,
    1.47 +        const String8& name, InputDeviceConfigurationFileType type) {
    1.48 +    path.append(CONFIGURATION_FILE_DIR[type]);
    1.49 +    for (size_t i = 0; i < name.length(); i++) {
    1.50 +        char ch = name[i];
    1.51 +        if (!isValidNameChar(ch)) {
    1.52 +            ch = '_';
    1.53 +        }
    1.54 +        path.append(&ch, 1);
    1.55 +    }
    1.56 +    path.append(CONFIGURATION_FILE_EXTENSION[type]);
    1.57 +}
    1.58 +
    1.59 +String8 getInputDeviceConfigurationFilePathByDeviceIdentifier(
    1.60 +        const InputDeviceIdentifier& deviceIdentifier,
    1.61 +        InputDeviceConfigurationFileType type) {
    1.62 +    if (deviceIdentifier.vendor !=0 && deviceIdentifier.product != 0) {
    1.63 +        if (deviceIdentifier.version != 0) {
    1.64 +            // Try vendor product version.
    1.65 +            String8 versionPath(getInputDeviceConfigurationFilePathByName(
    1.66 +                    String8::format("Vendor_%04x_Product_%04x_Version_%04x",
    1.67 +                            deviceIdentifier.vendor, deviceIdentifier.product,
    1.68 +                            deviceIdentifier.version),
    1.69 +                    type));
    1.70 +            if (!versionPath.isEmpty()) {
    1.71 +                return versionPath;
    1.72 +            }
    1.73 +        }
    1.74 +
    1.75 +        // Try vendor product.
    1.76 +        String8 productPath(getInputDeviceConfigurationFilePathByName(
    1.77 +                String8::format("Vendor_%04x_Product_%04x",
    1.78 +                        deviceIdentifier.vendor, deviceIdentifier.product),
    1.79 +                type));
    1.80 +        if (!productPath.isEmpty()) {
    1.81 +            return productPath;
    1.82 +        }
    1.83 +    }
    1.84 +
    1.85 +    // Try device name.
    1.86 +    return getInputDeviceConfigurationFilePathByName(deviceIdentifier.name, type);
    1.87 +}
    1.88 +
    1.89 +String8 getInputDeviceConfigurationFilePathByName(
    1.90 +        const String8& name, InputDeviceConfigurationFileType type) {
    1.91 +    // Search system repository.
    1.92 +    String8 path;
    1.93 +    path.setTo(getenv("ANDROID_ROOT"));
    1.94 +    path.append("/usr/");
    1.95 +    appendInputDeviceConfigurationFileRelativePath(path, name, type);
    1.96 +#if DEBUG_PROBE
    1.97 +    ALOGD("Probing for system provided input device configuration file: path='%s'", path.string());
    1.98 +#endif
    1.99 +    if (!access(path.string(), R_OK)) {
   1.100 +#if DEBUG_PROBE
   1.101 +        ALOGD("Found");
   1.102 +#endif
   1.103 +        return path;
   1.104 +    }
   1.105 +
   1.106 +    // Search user repository.
   1.107 +    // TODO Should only look here if not in safe mode.
   1.108 +    path.setTo(getenv("ANDROID_DATA"));
   1.109 +    path.append("/system/devices/");
   1.110 +    appendInputDeviceConfigurationFileRelativePath(path, name, type);
   1.111 +#if DEBUG_PROBE
   1.112 +    ALOGD("Probing for system user input device configuration file: path='%s'", path.string());
   1.113 +#endif
   1.114 +    if (!access(path.string(), R_OK)) {
   1.115 +#if DEBUG_PROBE
   1.116 +        ALOGD("Found");
   1.117 +#endif
   1.118 +        return path;
   1.119 +    }
   1.120 +
   1.121 +    // Not found.
   1.122 +#if DEBUG_PROBE
   1.123 +    ALOGD("Probe failed to find input device configuration file: name='%s', type=%d",
   1.124 +            name.string(), type);
   1.125 +#endif
   1.126 +    return String8();
   1.127 +}
   1.128 +
   1.129 +
   1.130 +// --- InputDeviceInfo ---
   1.131 +
   1.132 +InputDeviceInfo::InputDeviceInfo() {
   1.133 +    initialize(-1, -1, InputDeviceIdentifier(), String8(), false);
   1.134 +}
   1.135 +
   1.136 +InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other) :
   1.137 +        mId(other.mId), mGeneration(other.mGeneration), mIdentifier(other.mIdentifier),
   1.138 +        mAlias(other.mAlias), mIsExternal(other.mIsExternal), mSources(other.mSources),
   1.139 +        mKeyboardType(other.mKeyboardType),
   1.140 +        mKeyCharacterMap(other.mKeyCharacterMap),
   1.141 +        mHasVibrator(other.mHasVibrator),
   1.142 +        mMotionRanges(other.mMotionRanges) {
   1.143 +}
   1.144 +
   1.145 +InputDeviceInfo::~InputDeviceInfo() {
   1.146 +}
   1.147 +
   1.148 +void InputDeviceInfo::initialize(int32_t id, int32_t generation,
   1.149 +        const InputDeviceIdentifier& identifier, const String8& alias, bool isExternal) {
   1.150 +    mId = id;
   1.151 +    mGeneration = generation;
   1.152 +    mIdentifier = identifier;
   1.153 +    mAlias = alias;
   1.154 +    mIsExternal = isExternal;
   1.155 +    mSources = 0;
   1.156 +    mKeyboardType = AINPUT_KEYBOARD_TYPE_NONE;
   1.157 +    mHasVibrator = false;
   1.158 +    mMotionRanges.clear();
   1.159 +}
   1.160 +
   1.161 +const InputDeviceInfo::MotionRange* InputDeviceInfo::getMotionRange(
   1.162 +        int32_t axis, uint32_t source) const {
   1.163 +    size_t numRanges = mMotionRanges.size();
   1.164 +    for (size_t i = 0; i < numRanges; i++) {
   1.165 +        const MotionRange& range = mMotionRanges.itemAt(i);
   1.166 +        if (range.axis == axis && range.source == source) {
   1.167 +            return &range;
   1.168 +        }
   1.169 +    }
   1.170 +    return NULL;
   1.171 +}
   1.172 +
   1.173 +void InputDeviceInfo::addSource(uint32_t source) {
   1.174 +    mSources |= source;
   1.175 +}
   1.176 +
   1.177 +void InputDeviceInfo::addMotionRange(int32_t axis, uint32_t source, float min, float max,
   1.178 +        float flat, float fuzz, float resolution) {
   1.179 +    MotionRange range = { axis, source, min, max, flat, fuzz, resolution };
   1.180 +    mMotionRanges.add(range);
   1.181 +}
   1.182 +
   1.183 +void InputDeviceInfo::addMotionRange(const MotionRange& range) {
   1.184 +    mMotionRanges.add(range);
   1.185 +}
   1.186 +
   1.187 +} // namespace android

mercurial