michael@0: /* michael@0: * Copyright (C) 2010 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #define LOG_TAG "Keyboard" michael@0: #include "cutils_log.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "Keyboard.h" michael@0: #include "KeycodeLabels.h" michael@0: #include "KeyLayoutMap.h" michael@0: #include "KeyCharacterMap.h" michael@0: #include "InputDevice.h" michael@0: #include michael@0: #include michael@0: michael@0: namespace android { michael@0: michael@0: // --- KeyMap --- michael@0: michael@0: KeyMap::KeyMap() { michael@0: } michael@0: michael@0: KeyMap::~KeyMap() { michael@0: } michael@0: michael@0: status_t KeyMap::load(const InputDeviceIdentifier& deviceIdenfifier, michael@0: const PropertyMap* deviceConfiguration) { michael@0: // Use the configured key layout if available. michael@0: if (deviceConfiguration) { michael@0: String8 keyLayoutName; michael@0: if (deviceConfiguration->tryGetProperty(String8("keyboard.layout"), michael@0: keyLayoutName)) { michael@0: status_t status = loadKeyLayout(deviceIdenfifier, keyLayoutName); michael@0: if (status == NAME_NOT_FOUND) { michael@0: ALOGE("Configuration for keyboard device '%s' requested keyboard layout '%s' but " michael@0: "it was not found.", michael@0: deviceIdenfifier.name.string(), keyLayoutName.string()); michael@0: } michael@0: } michael@0: michael@0: String8 keyCharacterMapName; michael@0: if (deviceConfiguration->tryGetProperty(String8("keyboard.characterMap"), michael@0: keyCharacterMapName)) { michael@0: status_t status = loadKeyCharacterMap(deviceIdenfifier, keyCharacterMapName); michael@0: if (status == NAME_NOT_FOUND) { michael@0: ALOGE("Configuration for keyboard device '%s' requested keyboard character " michael@0: "map '%s' but it was not found.", michael@0: deviceIdenfifier.name.string(), keyLayoutName.string()); michael@0: } michael@0: } michael@0: michael@0: if (isComplete()) { michael@0: return OK; michael@0: } michael@0: } michael@0: michael@0: // Try searching by device identifier. michael@0: if (probeKeyMap(deviceIdenfifier, String8::empty())) { michael@0: return OK; michael@0: } michael@0: michael@0: // Fall back on the Generic key map. michael@0: // TODO Apply some additional heuristics here to figure out what kind of michael@0: // generic key map to use (US English, etc.) for typical external keyboards. michael@0: if (probeKeyMap(deviceIdenfifier, String8("Generic"))) { michael@0: return OK; michael@0: } michael@0: michael@0: // Try the Virtual key map as a last resort. michael@0: if (probeKeyMap(deviceIdenfifier, String8("Virtual"))) { michael@0: return OK; michael@0: } michael@0: michael@0: // Give up! michael@0: ALOGE("Could not determine key map for device '%s' and no default key maps were found!", michael@0: deviceIdenfifier.name.string()); michael@0: return NAME_NOT_FOUND; michael@0: } michael@0: michael@0: bool KeyMap::probeKeyMap(const InputDeviceIdentifier& deviceIdentifier, michael@0: const String8& keyMapName) { michael@0: if (!haveKeyLayout()) { michael@0: loadKeyLayout(deviceIdentifier, keyMapName); michael@0: } michael@0: if (!haveKeyCharacterMap()) { michael@0: loadKeyCharacterMap(deviceIdentifier, keyMapName); michael@0: } michael@0: return isComplete(); michael@0: } michael@0: michael@0: status_t KeyMap::loadKeyLayout(const InputDeviceIdentifier& deviceIdentifier, michael@0: const String8& name) { michael@0: String8 path(getPath(deviceIdentifier, name, michael@0: INPUT_DEVICE_CONFIGURATION_FILE_TYPE_KEY_LAYOUT)); michael@0: if (path.isEmpty()) { michael@0: return NAME_NOT_FOUND; michael@0: } michael@0: michael@0: status_t status = KeyLayoutMap::load(path, &keyLayoutMap); michael@0: if (status) { michael@0: return status; michael@0: } michael@0: michael@0: keyLayoutFile.setTo(path); michael@0: return OK; michael@0: } michael@0: michael@0: status_t KeyMap::loadKeyCharacterMap(const InputDeviceIdentifier& deviceIdentifier, michael@0: const String8& name) { michael@0: String8 path(getPath(deviceIdentifier, name, michael@0: INPUT_DEVICE_CONFIGURATION_FILE_TYPE_KEY_CHARACTER_MAP)); michael@0: if (path.isEmpty()) { michael@0: return NAME_NOT_FOUND; michael@0: } michael@0: michael@0: status_t status = KeyCharacterMap::load(path, michael@0: KeyCharacterMap::FORMAT_BASE, &keyCharacterMap); michael@0: if (status) { michael@0: return status; michael@0: } michael@0: michael@0: keyCharacterMapFile.setTo(path); michael@0: return OK; michael@0: } michael@0: michael@0: String8 KeyMap::getPath(const InputDeviceIdentifier& deviceIdentifier, michael@0: const String8& name, InputDeviceConfigurationFileType type) { michael@0: return name.isEmpty() michael@0: ? getInputDeviceConfigurationFilePathByDeviceIdentifier(deviceIdentifier, type) michael@0: : getInputDeviceConfigurationFilePathByName(name, type); michael@0: } michael@0: michael@0: michael@0: // --- Global functions --- michael@0: michael@0: bool isEligibleBuiltInKeyboard(const InputDeviceIdentifier& deviceIdentifier, michael@0: const PropertyMap* deviceConfiguration, const KeyMap* keyMap) { michael@0: if (!keyMap->haveKeyCharacterMap() michael@0: || keyMap->keyCharacterMap->getKeyboardType() michael@0: == KeyCharacterMap::KEYBOARD_TYPE_SPECIAL_FUNCTION) { michael@0: return false; michael@0: } michael@0: michael@0: if (deviceConfiguration) { michael@0: bool builtIn = false; michael@0: if (deviceConfiguration->tryGetProperty(String8("keyboard.builtIn"), builtIn) michael@0: && builtIn) { michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: return strstr(deviceIdentifier.name.string(), "-keypad"); michael@0: } michael@0: michael@0: static int lookupValueByLabel(const char* literal, const KeycodeLabel *list) { michael@0: while (list->literal) { michael@0: if (strcmp(literal, list->literal) == 0) { michael@0: return list->value; michael@0: } michael@0: list++; michael@0: } michael@0: return list->value; michael@0: } michael@0: michael@0: static const char* lookupLabelByValue(int value, const KeycodeLabel *list) { michael@0: while (list->literal) { michael@0: if (list->value == value) { michael@0: return list->literal; michael@0: } michael@0: list++; michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: int32_t getKeyCodeByLabel(const char* label) { michael@0: return int32_t(lookupValueByLabel(label, KEYCODES)); michael@0: } michael@0: michael@0: uint32_t getKeyFlagByLabel(const char* label) { michael@0: return uint32_t(lookupValueByLabel(label, FLAGS)); michael@0: } michael@0: michael@0: int32_t getAxisByLabel(const char* label) { michael@0: return int32_t(lookupValueByLabel(label, AXES)); michael@0: } michael@0: michael@0: const char* getAxisLabel(int32_t axisId) { michael@0: return lookupLabelByValue(axisId, AXES); michael@0: } michael@0: michael@0: static int32_t setEphemeralMetaState(int32_t mask, bool down, int32_t oldMetaState) { michael@0: int32_t newMetaState; michael@0: if (down) { michael@0: newMetaState = oldMetaState | mask; michael@0: } else { michael@0: newMetaState = oldMetaState & michael@0: ~(mask | AMETA_ALT_ON | AMETA_SHIFT_ON | AMETA_CTRL_ON | AMETA_META_ON); michael@0: } michael@0: michael@0: if (newMetaState & (AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON)) { michael@0: newMetaState |= AMETA_ALT_ON; michael@0: } michael@0: michael@0: if (newMetaState & (AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON)) { michael@0: newMetaState |= AMETA_SHIFT_ON; michael@0: } michael@0: michael@0: if (newMetaState & (AMETA_CTRL_LEFT_ON | AMETA_CTRL_RIGHT_ON)) { michael@0: newMetaState |= AMETA_CTRL_ON; michael@0: } michael@0: michael@0: if (newMetaState & (AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON)) { michael@0: newMetaState |= AMETA_META_ON; michael@0: } michael@0: return newMetaState; michael@0: } michael@0: michael@0: static int32_t toggleLockedMetaState(int32_t mask, bool down, int32_t oldMetaState) { michael@0: if (down) { michael@0: return oldMetaState; michael@0: } else { michael@0: return oldMetaState ^ mask; michael@0: } michael@0: } michael@0: michael@0: int32_t updateMetaState(int32_t keyCode, bool down, int32_t oldMetaState) { michael@0: int32_t mask; michael@0: switch (keyCode) { michael@0: case AKEYCODE_ALT_LEFT: michael@0: return setEphemeralMetaState(AMETA_ALT_LEFT_ON, down, oldMetaState); michael@0: case AKEYCODE_ALT_RIGHT: michael@0: return setEphemeralMetaState(AMETA_ALT_RIGHT_ON, down, oldMetaState); michael@0: case AKEYCODE_SHIFT_LEFT: michael@0: return setEphemeralMetaState(AMETA_SHIFT_LEFT_ON, down, oldMetaState); michael@0: case AKEYCODE_SHIFT_RIGHT: michael@0: return setEphemeralMetaState(AMETA_SHIFT_RIGHT_ON, down, oldMetaState); michael@0: case AKEYCODE_SYM: michael@0: return setEphemeralMetaState(AMETA_SYM_ON, down, oldMetaState); michael@0: case AKEYCODE_FUNCTION: michael@0: return setEphemeralMetaState(AMETA_FUNCTION_ON, down, oldMetaState); michael@0: case AKEYCODE_CTRL_LEFT: michael@0: return setEphemeralMetaState(AMETA_CTRL_LEFT_ON, down, oldMetaState); michael@0: case AKEYCODE_CTRL_RIGHT: michael@0: return setEphemeralMetaState(AMETA_CTRL_RIGHT_ON, down, oldMetaState); michael@0: case AKEYCODE_META_LEFT: michael@0: return setEphemeralMetaState(AMETA_META_LEFT_ON, down, oldMetaState); michael@0: case AKEYCODE_META_RIGHT: michael@0: return setEphemeralMetaState(AMETA_META_RIGHT_ON, down, oldMetaState); michael@0: case AKEYCODE_CAPS_LOCK: michael@0: return toggleLockedMetaState(AMETA_CAPS_LOCK_ON, down, oldMetaState); michael@0: case AKEYCODE_NUM_LOCK: michael@0: return toggleLockedMetaState(AMETA_NUM_LOCK_ON, down, oldMetaState); michael@0: case AKEYCODE_SCROLL_LOCK: michael@0: return toggleLockedMetaState(AMETA_SCROLL_LOCK_ON, down, oldMetaState); michael@0: default: michael@0: return oldMetaState; michael@0: } michael@0: } michael@0: michael@0: bool isMetaKey(int32_t keyCode) { michael@0: switch (keyCode) { michael@0: case AKEYCODE_ALT_LEFT: michael@0: case AKEYCODE_ALT_RIGHT: michael@0: case AKEYCODE_SHIFT_LEFT: michael@0: case AKEYCODE_SHIFT_RIGHT: michael@0: case AKEYCODE_SYM: michael@0: case AKEYCODE_FUNCTION: michael@0: case AKEYCODE_CTRL_LEFT: michael@0: case AKEYCODE_CTRL_RIGHT: michael@0: case AKEYCODE_META_LEFT: michael@0: case AKEYCODE_META_RIGHT: michael@0: case AKEYCODE_CAPS_LOCK: michael@0: case AKEYCODE_NUM_LOCK: michael@0: case AKEYCODE_SCROLL_LOCK: michael@0: return true; michael@0: default: michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: michael@0: } // namespace android