Home | History | Annotate | Download | only in input
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      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
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     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.
     15  */
     16 
     17 #define LOG_TAG "Keyboard"
     18 
     19 #include <stdlib.h>
     20 #include <unistd.h>
     21 #include <limits.h>
     22 
     23 #include <input/Keyboard.h>
     24 #include <input/InputEventLabels.h>
     25 #include <input/KeyLayoutMap.h>
     26 #include <input/KeyCharacterMap.h>
     27 #include <input/InputDevice.h>
     28 #include <utils/Errors.h>
     29 #include <utils/Log.h>
     30 
     31 namespace android {
     32 
     33 // --- KeyMap ---
     34 
     35 KeyMap::KeyMap() {
     36 }
     37 
     38 KeyMap::~KeyMap() {
     39 }
     40 
     41 status_t KeyMap::load(const InputDeviceIdentifier& deviceIdenfifier,
     42         const PropertyMap* deviceConfiguration) {
     43     // Use the configured key layout if available.
     44     if (deviceConfiguration) {
     45         String8 keyLayoutName;
     46         if (deviceConfiguration->tryGetProperty(String8("keyboard.layout"),
     47                 keyLayoutName)) {
     48             status_t status = loadKeyLayout(deviceIdenfifier, keyLayoutName);
     49             if (status == NAME_NOT_FOUND) {
     50                 ALOGE("Configuration for keyboard device '%s' requested keyboard layout '%s' but "
     51                         "it was not found.",
     52                         deviceIdenfifier.name.string(), keyLayoutName.string());
     53             }
     54         }
     55 
     56         String8 keyCharacterMapName;
     57         if (deviceConfiguration->tryGetProperty(String8("keyboard.characterMap"),
     58                 keyCharacterMapName)) {
     59             status_t status = loadKeyCharacterMap(deviceIdenfifier, keyCharacterMapName);
     60             if (status == NAME_NOT_FOUND) {
     61                 ALOGE("Configuration for keyboard device '%s' requested keyboard character "
     62                         "map '%s' but it was not found.",
     63                         deviceIdenfifier.name.string(), keyLayoutName.string());
     64             }
     65         }
     66 
     67         if (isComplete()) {
     68             return OK;
     69         }
     70     }
     71 
     72     // Try searching by device identifier.
     73     if (probeKeyMap(deviceIdenfifier, String8::empty())) {
     74         return OK;
     75     }
     76 
     77     // Fall back on the Generic key map.
     78     // TODO Apply some additional heuristics here to figure out what kind of
     79     //      generic key map to use (US English, etc.) for typical external keyboards.
     80     if (probeKeyMap(deviceIdenfifier, String8("Generic"))) {
     81         return OK;
     82     }
     83 
     84     // Try the Virtual key map as a last resort.
     85     if (probeKeyMap(deviceIdenfifier, String8("Virtual"))) {
     86         return OK;
     87     }
     88 
     89     // Give up!
     90     ALOGE("Could not determine key map for device '%s' and no default key maps were found!",
     91             deviceIdenfifier.name.string());
     92     return NAME_NOT_FOUND;
     93 }
     94 
     95 bool KeyMap::probeKeyMap(const InputDeviceIdentifier& deviceIdentifier,
     96         const String8& keyMapName) {
     97     if (!haveKeyLayout()) {
     98         loadKeyLayout(deviceIdentifier, keyMapName);
     99     }
    100     if (!haveKeyCharacterMap()) {
    101         loadKeyCharacterMap(deviceIdentifier, keyMapName);
    102     }
    103     return isComplete();
    104 }
    105 
    106 status_t KeyMap::loadKeyLayout(const InputDeviceIdentifier& deviceIdentifier,
    107         const String8& name) {
    108     String8 path(getPath(deviceIdentifier, name,
    109             INPUT_DEVICE_CONFIGURATION_FILE_TYPE_KEY_LAYOUT));
    110     if (path.isEmpty()) {
    111         return NAME_NOT_FOUND;
    112     }
    113 
    114     status_t status = KeyLayoutMap::load(path, &keyLayoutMap);
    115     if (status) {
    116         return status;
    117     }
    118 
    119     keyLayoutFile.setTo(path);
    120     return OK;
    121 }
    122 
    123 status_t KeyMap::loadKeyCharacterMap(const InputDeviceIdentifier& deviceIdentifier,
    124         const String8& name) {
    125     String8 path(getPath(deviceIdentifier, name,
    126             INPUT_DEVICE_CONFIGURATION_FILE_TYPE_KEY_CHARACTER_MAP));
    127     if (path.isEmpty()) {
    128         return NAME_NOT_FOUND;
    129     }
    130 
    131     status_t status = KeyCharacterMap::load(path,
    132             KeyCharacterMap::FORMAT_BASE, &keyCharacterMap);
    133     if (status) {
    134         return status;
    135     }
    136 
    137     keyCharacterMapFile.setTo(path);
    138     return OK;
    139 }
    140 
    141 String8 KeyMap::getPath(const InputDeviceIdentifier& deviceIdentifier,
    142         const String8& name, InputDeviceConfigurationFileType type) {
    143     return name.isEmpty()
    144             ? getInputDeviceConfigurationFilePathByDeviceIdentifier(deviceIdentifier, type)
    145             : getInputDeviceConfigurationFilePathByName(name, type);
    146 }
    147 
    148 
    149 // --- Global functions ---
    150 
    151 bool isEligibleBuiltInKeyboard(const InputDeviceIdentifier& deviceIdentifier,
    152         const PropertyMap* deviceConfiguration, const KeyMap* keyMap) {
    153     if (!keyMap->haveKeyCharacterMap()
    154             || keyMap->keyCharacterMap->getKeyboardType()
    155                     == KeyCharacterMap::KEYBOARD_TYPE_SPECIAL_FUNCTION) {
    156         return false;
    157     }
    158 
    159     if (deviceConfiguration) {
    160         bool builtIn = false;
    161         if (deviceConfiguration->tryGetProperty(String8("keyboard.builtIn"), builtIn)
    162                 && builtIn) {
    163             return true;
    164         }
    165     }
    166 
    167     return strstr(deviceIdentifier.name.string(), "-keypad");
    168 }
    169 
    170 static int32_t setEphemeralMetaState(int32_t mask, bool down, int32_t oldMetaState) {
    171     int32_t newMetaState;
    172     if (down) {
    173         newMetaState = oldMetaState | mask;
    174     } else {
    175         newMetaState = oldMetaState &
    176                 ~(mask | AMETA_ALT_ON | AMETA_SHIFT_ON | AMETA_CTRL_ON | AMETA_META_ON);
    177     }
    178 
    179     if (newMetaState & (AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON)) {
    180         newMetaState |= AMETA_ALT_ON;
    181     }
    182 
    183     if (newMetaState & (AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON)) {
    184         newMetaState |= AMETA_SHIFT_ON;
    185     }
    186 
    187     if (newMetaState & (AMETA_CTRL_LEFT_ON | AMETA_CTRL_RIGHT_ON)) {
    188         newMetaState |= AMETA_CTRL_ON;
    189     }
    190 
    191     if (newMetaState & (AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON)) {
    192         newMetaState |= AMETA_META_ON;
    193     }
    194     return newMetaState;
    195 }
    196 
    197 static int32_t toggleLockedMetaState(int32_t mask, bool down, int32_t oldMetaState) {
    198     if (down) {
    199         return oldMetaState;
    200     } else {
    201         return oldMetaState ^ mask;
    202     }
    203 }
    204 
    205 int32_t updateMetaState(int32_t keyCode, bool down, int32_t oldMetaState) {
    206     int32_t mask;
    207     switch (keyCode) {
    208     case AKEYCODE_ALT_LEFT:
    209         return setEphemeralMetaState(AMETA_ALT_LEFT_ON, down, oldMetaState);
    210     case AKEYCODE_ALT_RIGHT:
    211         return setEphemeralMetaState(AMETA_ALT_RIGHT_ON, down, oldMetaState);
    212     case AKEYCODE_SHIFT_LEFT:
    213         return setEphemeralMetaState(AMETA_SHIFT_LEFT_ON, down, oldMetaState);
    214     case AKEYCODE_SHIFT_RIGHT:
    215         return setEphemeralMetaState(AMETA_SHIFT_RIGHT_ON, down, oldMetaState);
    216     case AKEYCODE_SYM:
    217         return setEphemeralMetaState(AMETA_SYM_ON, down, oldMetaState);
    218     case AKEYCODE_FUNCTION:
    219         return setEphemeralMetaState(AMETA_FUNCTION_ON, down, oldMetaState);
    220     case AKEYCODE_CTRL_LEFT:
    221         return setEphemeralMetaState(AMETA_CTRL_LEFT_ON, down, oldMetaState);
    222     case AKEYCODE_CTRL_RIGHT:
    223         return setEphemeralMetaState(AMETA_CTRL_RIGHT_ON, down, oldMetaState);
    224     case AKEYCODE_META_LEFT:
    225         return setEphemeralMetaState(AMETA_META_LEFT_ON, down, oldMetaState);
    226     case AKEYCODE_META_RIGHT:
    227         return setEphemeralMetaState(AMETA_META_RIGHT_ON, down, oldMetaState);
    228     case AKEYCODE_CAPS_LOCK:
    229         return toggleLockedMetaState(AMETA_CAPS_LOCK_ON, down, oldMetaState);
    230     case AKEYCODE_NUM_LOCK:
    231         return toggleLockedMetaState(AMETA_NUM_LOCK_ON, down, oldMetaState);
    232     case AKEYCODE_SCROLL_LOCK:
    233         return toggleLockedMetaState(AMETA_SCROLL_LOCK_ON, down, oldMetaState);
    234     default:
    235         return oldMetaState;
    236     }
    237 }
    238 
    239 bool isMetaKey(int32_t keyCode) {
    240     switch (keyCode) {
    241     case AKEYCODE_ALT_LEFT:
    242     case AKEYCODE_ALT_RIGHT:
    243     case AKEYCODE_SHIFT_LEFT:
    244     case AKEYCODE_SHIFT_RIGHT:
    245     case AKEYCODE_SYM:
    246     case AKEYCODE_FUNCTION:
    247     case AKEYCODE_CTRL_LEFT:
    248     case AKEYCODE_CTRL_RIGHT:
    249     case AKEYCODE_META_LEFT:
    250     case AKEYCODE_META_RIGHT:
    251     case AKEYCODE_CAPS_LOCK:
    252     case AKEYCODE_NUM_LOCK:
    253     case AKEYCODE_SCROLL_LOCK:
    254         return true;
    255     default:
    256         return false;
    257     }
    258 }
    259 
    260 
    261 } // namespace android
    262