Keyboard.cpp 8.7 KB

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