Keyboard.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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);
  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.string(), keyLayoutName.string());
  45. }
  46. }
  47. String8 keyCharacterMapName;
  48. if (deviceConfiguration->tryGetProperty(String8("keyboard.characterMap"),
  49. keyCharacterMapName)) {
  50. status_t status = loadKeyCharacterMap(deviceIdenfifier, keyCharacterMapName);
  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.string(), keyLayoutName.string());
  55. }
  56. }
  57. if (isComplete()) {
  58. return OK;
  59. }
  60. }
  61. // Try searching by device identifier.
  62. if (probeKeyMap(deviceIdenfifier, String8::empty())) {
  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, String8("Generic"))) {
  69. return OK;
  70. }
  71. // Try the Virtual key map as a last resort.
  72. if (probeKeyMap(deviceIdenfifier, String8("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.string());
  78. return NAME_NOT_FOUND;
  79. }
  80. bool KeyMap::probeKeyMap(const InputDeviceIdentifier& deviceIdentifier,
  81. const String8& 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 String8& name) {
  92. String8 path(getPath(deviceIdentifier, name,
  93. INPUT_DEVICE_CONFIGURATION_FILE_TYPE_KEY_LAYOUT));
  94. if (path.isEmpty()) {
  95. return NAME_NOT_FOUND;
  96. }
  97. status_t status = KeyLayoutMap::load(path, &keyLayoutMap);
  98. if (status) {
  99. return status;
  100. }
  101. keyLayoutFile.setTo(path);
  102. return OK;
  103. }
  104. status_t KeyMap::loadKeyCharacterMap(const InputDeviceIdentifier& deviceIdentifier,
  105. const String8& name) {
  106. String8 path(getPath(deviceIdentifier, name,
  107. INPUT_DEVICE_CONFIGURATION_FILE_TYPE_KEY_CHARACTER_MAP));
  108. if (path.isEmpty()) {
  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.setTo(path);
  117. return OK;
  118. }
  119. String8 KeyMap::getPath(const InputDeviceIdentifier& deviceIdentifier,
  120. const String8& name, InputDeviceConfigurationFileType type) {
  121. return name.isEmpty()
  122. ? getInputDeviceConfigurationFilePathByDeviceIdentifier(deviceIdentifier, type)
  123. : getInputDeviceConfigurationFilePathByName(name, type);
  124. }
  125. // --- Global functions ---
  126. bool isEligibleBuiltInKeyboard(const InputDeviceIdentifier& deviceIdentifier,
  127. const PropertyMap* deviceConfiguration, const KeyMap* keyMap) {
  128. if (!keyMap->haveKeyCharacterMap()
  129. || keyMap->keyCharacterMap->getKeyboardType()
  130. == KeyCharacterMap::KEYBOARD_TYPE_SPECIAL_FUNCTION) {
  131. return false;
  132. }
  133. if (deviceConfiguration) {
  134. bool builtIn = false;
  135. if (deviceConfiguration->tryGetProperty(String8("keyboard.builtIn"), builtIn)
  136. && builtIn) {
  137. return true;
  138. }
  139. }
  140. return strstr(deviceIdentifier.name.string(), "-keypad");
  141. }
  142. static int32_t setEphemeralMetaState(int32_t mask, bool down, int32_t oldMetaState) {
  143. int32_t newMetaState;
  144. if (down) {
  145. newMetaState = oldMetaState | mask;
  146. } else {
  147. newMetaState = oldMetaState &
  148. ~(mask | AMETA_ALT_ON | AMETA_SHIFT_ON | AMETA_CTRL_ON | AMETA_META_ON);
  149. }
  150. return normalizeMetaState(newMetaState);
  151. }
  152. int32_t normalizeMetaState(int32_t oldMetaState) {
  153. int32_t newMetaState = oldMetaState;
  154. if (newMetaState & (AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON)) {
  155. newMetaState |= AMETA_ALT_ON;
  156. }
  157. if (newMetaState & (AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON)) {
  158. newMetaState |= AMETA_SHIFT_ON;
  159. }
  160. if (newMetaState & (AMETA_CTRL_LEFT_ON | AMETA_CTRL_RIGHT_ON)) {
  161. newMetaState |= AMETA_CTRL_ON;
  162. }
  163. if (newMetaState & (AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON)) {
  164. newMetaState |= AMETA_META_ON;
  165. }
  166. return newMetaState;
  167. }
  168. static int32_t toggleLockedMetaState(int32_t mask, bool down, int32_t oldMetaState) {
  169. if (down) {
  170. return oldMetaState;
  171. } else {
  172. return oldMetaState ^ mask;
  173. }
  174. }
  175. int32_t updateMetaState(int32_t keyCode, bool down, int32_t oldMetaState) {
  176. int32_t mask;
  177. switch (keyCode) {
  178. case AKEYCODE_ALT_LEFT:
  179. return setEphemeralMetaState(AMETA_ALT_LEFT_ON, down, oldMetaState);
  180. case AKEYCODE_ALT_RIGHT:
  181. return setEphemeralMetaState(AMETA_ALT_RIGHT_ON, down, oldMetaState);
  182. case AKEYCODE_SHIFT_LEFT:
  183. return setEphemeralMetaState(AMETA_SHIFT_LEFT_ON, down, oldMetaState);
  184. case AKEYCODE_SHIFT_RIGHT:
  185. return setEphemeralMetaState(AMETA_SHIFT_RIGHT_ON, down, oldMetaState);
  186. case AKEYCODE_SYM:
  187. return setEphemeralMetaState(AMETA_SYM_ON, down, oldMetaState);
  188. case AKEYCODE_FUNCTION:
  189. return setEphemeralMetaState(AMETA_FUNCTION_ON, down, oldMetaState);
  190. case AKEYCODE_CTRL_LEFT:
  191. return setEphemeralMetaState(AMETA_CTRL_LEFT_ON, down, oldMetaState);
  192. case AKEYCODE_CTRL_RIGHT:
  193. return setEphemeralMetaState(AMETA_CTRL_RIGHT_ON, down, oldMetaState);
  194. case AKEYCODE_META_LEFT:
  195. return setEphemeralMetaState(AMETA_META_LEFT_ON, down, oldMetaState);
  196. case AKEYCODE_META_RIGHT:
  197. return setEphemeralMetaState(AMETA_META_RIGHT_ON, down, oldMetaState);
  198. case AKEYCODE_CAPS_LOCK:
  199. return toggleLockedMetaState(AMETA_CAPS_LOCK_ON, down, oldMetaState);
  200. case AKEYCODE_NUM_LOCK:
  201. return toggleLockedMetaState(AMETA_NUM_LOCK_ON, down, oldMetaState);
  202. case AKEYCODE_SCROLL_LOCK:
  203. return toggleLockedMetaState(AMETA_SCROLL_LOCK_ON, down, oldMetaState);
  204. default:
  205. return oldMetaState;
  206. }
  207. }
  208. bool isMetaKey(int32_t keyCode) {
  209. switch (keyCode) {
  210. case AKEYCODE_ALT_LEFT:
  211. case AKEYCODE_ALT_RIGHT:
  212. case AKEYCODE_SHIFT_LEFT:
  213. case AKEYCODE_SHIFT_RIGHT:
  214. case AKEYCODE_SYM:
  215. case AKEYCODE_FUNCTION:
  216. case AKEYCODE_CTRL_LEFT:
  217. case AKEYCODE_CTRL_RIGHT:
  218. case AKEYCODE_META_LEFT:
  219. case AKEYCODE_META_RIGHT:
  220. case AKEYCODE_CAPS_LOCK:
  221. case AKEYCODE_NUM_LOCK:
  222. case AKEYCODE_SCROLL_LOCK:
  223. return true;
  224. default:
  225. return false;
  226. }
  227. }
  228. } // namespace android