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