KeyLayoutMap.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Copyright (C) 2008 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 "KeyLayoutMap"
  17. #include <stdlib.h>
  18. #include <android/keycodes.h>
  19. #include <input/InputEventLabels.h>
  20. #include <input/Keyboard.h>
  21. #include <input/KeyLayoutMap.h>
  22. #include <utils/Log.h>
  23. #include <utils/Errors.h>
  24. #include <utils/Tokenizer.h>
  25. #include <utils/Timers.h>
  26. // Enables debug output for the parser.
  27. #define DEBUG_PARSER 0
  28. // Enables debug output for parser performance.
  29. #define DEBUG_PARSER_PERFORMANCE 0
  30. // Enables debug output for mapping.
  31. #define DEBUG_MAPPING 0
  32. namespace android {
  33. static const char* WHITESPACE = " \t\r";
  34. // --- KeyLayoutMap ---
  35. KeyLayoutMap::KeyLayoutMap() {
  36. }
  37. KeyLayoutMap::~KeyLayoutMap() {
  38. }
  39. status_t KeyLayoutMap::load(const String8& filename, sp<KeyLayoutMap>* outMap) {
  40. outMap->clear();
  41. Tokenizer* tokenizer;
  42. status_t status = Tokenizer::open(filename, &tokenizer);
  43. if (status) {
  44. ALOGE("Error %d opening key layout map file %s.", status, filename.string());
  45. } else {
  46. sp<KeyLayoutMap> map = new KeyLayoutMap();
  47. if (!map.get()) {
  48. ALOGE("Error allocating key layout map.");
  49. status = NO_MEMORY;
  50. } else {
  51. #if DEBUG_PARSER_PERFORMANCE
  52. nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
  53. #endif
  54. Parser parser(map.get(), tokenizer);
  55. status = parser.parse();
  56. #if DEBUG_PARSER_PERFORMANCE
  57. nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime;
  58. ALOGD("Parsed key layout map file '%s' %d lines in %0.3fms.",
  59. tokenizer->getFilename().string(), tokenizer->getLineNumber(),
  60. elapsedTime / 1000000.0);
  61. #endif
  62. if (!status) {
  63. *outMap = map;
  64. }
  65. }
  66. delete tokenizer;
  67. }
  68. return status;
  69. }
  70. status_t KeyLayoutMap::mapKey(int32_t scanCode, int32_t usageCode,
  71. int32_t* outKeyCode, uint32_t* outFlags) const {
  72. const Key* key = getKey(scanCode, usageCode);
  73. if (!key) {
  74. #if DEBUG_MAPPING
  75. ALOGD("mapKey: scanCode=%d, usageCode=0x%08x ~ Failed.", scanCode, usageCode);
  76. #endif
  77. *outKeyCode = AKEYCODE_UNKNOWN;
  78. *outFlags = 0;
  79. return NAME_NOT_FOUND;
  80. }
  81. *outKeyCode = key->keyCode;
  82. *outFlags = key->flags;
  83. #if DEBUG_MAPPING
  84. ALOGD("mapKey: scanCode=%d, usageCode=0x%08x ~ Result keyCode=%d, outFlags=0x%08x.",
  85. scanCode, usageCode, *outKeyCode, *outFlags);
  86. #endif
  87. return NO_ERROR;
  88. }
  89. const KeyLayoutMap::Key* KeyLayoutMap::getKey(int32_t scanCode, int32_t usageCode) const {
  90. if (usageCode) {
  91. ssize_t index = mKeysByUsageCode.indexOfKey(usageCode);
  92. if (index >= 0) {
  93. return &mKeysByUsageCode.valueAt(index);
  94. }
  95. }
  96. if (scanCode) {
  97. ssize_t index = mKeysByScanCode.indexOfKey(scanCode);
  98. if (index >= 0) {
  99. return &mKeysByScanCode.valueAt(index);
  100. }
  101. }
  102. return NULL;
  103. }
  104. status_t KeyLayoutMap::findScanCodesForKey(int32_t keyCode, Vector<int32_t>* outScanCodes) const {
  105. const size_t N = mKeysByScanCode.size();
  106. for (size_t i=0; i<N; i++) {
  107. if (mKeysByScanCode.valueAt(i).keyCode == keyCode) {
  108. outScanCodes->add(mKeysByScanCode.keyAt(i));
  109. }
  110. }
  111. return NO_ERROR;
  112. }
  113. status_t KeyLayoutMap::mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const {
  114. ssize_t index = mAxes.indexOfKey(scanCode);
  115. if (index < 0) {
  116. #if DEBUG_MAPPING
  117. ALOGD("mapAxis: scanCode=%d ~ Failed.", scanCode);
  118. #endif
  119. return NAME_NOT_FOUND;
  120. }
  121. *outAxisInfo = mAxes.valueAt(index);
  122. #if DEBUG_MAPPING
  123. ALOGD("mapAxis: scanCode=%d ~ Result mode=%d, axis=%d, highAxis=%d, "
  124. "splitValue=%d, flatOverride=%d.",
  125. scanCode,
  126. outAxisInfo->mode, outAxisInfo->axis, outAxisInfo->highAxis,
  127. outAxisInfo->splitValue, outAxisInfo->flatOverride);
  128. #endif
  129. return NO_ERROR;
  130. }
  131. status_t KeyLayoutMap::findScanCodeForLed(int32_t ledCode, int32_t* outScanCode) const {
  132. const size_t N = mLedsByScanCode.size();
  133. for (size_t i = 0; i < N; i++) {
  134. if (mLedsByScanCode.valueAt(i).ledCode == ledCode) {
  135. *outScanCode = mLedsByScanCode.keyAt(i);
  136. #if DEBUG_MAPPING
  137. ALOGD("findScanCodeForLed: ledCode=%d, scanCode=%d.", ledCode, *outScanCode);
  138. #endif
  139. return NO_ERROR;
  140. }
  141. }
  142. #if DEBUG_MAPPING
  143. ALOGD("findScanCodeForLed: ledCode=%d ~ Not found.", ledCode);
  144. #endif
  145. return NAME_NOT_FOUND;
  146. }
  147. status_t KeyLayoutMap::findUsageCodeForLed(int32_t ledCode, int32_t* outUsageCode) const {
  148. const size_t N = mLedsByUsageCode.size();
  149. for (size_t i = 0; i < N; i++) {
  150. if (mLedsByUsageCode.valueAt(i).ledCode == ledCode) {
  151. *outUsageCode = mLedsByUsageCode.keyAt(i);
  152. #if DEBUG_MAPPING
  153. ALOGD("findUsageForLed: ledCode=%d, usage=%x.", ledCode, *outUsageCode);
  154. #endif
  155. return NO_ERROR;
  156. }
  157. }
  158. #if DEBUG_MAPPING
  159. ALOGD("findUsageForLed: ledCode=%d ~ Not found.", ledCode);
  160. #endif
  161. return NAME_NOT_FOUND;
  162. }
  163. // --- KeyLayoutMap::Parser ---
  164. KeyLayoutMap::Parser::Parser(KeyLayoutMap* map, Tokenizer* tokenizer) :
  165. mMap(map), mTokenizer(tokenizer) {
  166. }
  167. KeyLayoutMap::Parser::~Parser() {
  168. }
  169. status_t KeyLayoutMap::Parser::parse() {
  170. while (!mTokenizer->isEof()) {
  171. #if DEBUG_PARSER
  172. ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(),
  173. mTokenizer->peekRemainderOfLine().string());
  174. #endif
  175. mTokenizer->skipDelimiters(WHITESPACE);
  176. if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
  177. String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
  178. if (keywordToken == "key") {
  179. mTokenizer->skipDelimiters(WHITESPACE);
  180. status_t status = parseKey();
  181. if (status) return status;
  182. } else if (keywordToken == "axis") {
  183. mTokenizer->skipDelimiters(WHITESPACE);
  184. status_t status = parseAxis();
  185. if (status) return status;
  186. } else if (keywordToken == "led") {
  187. mTokenizer->skipDelimiters(WHITESPACE);
  188. status_t status = parseLed();
  189. if (status) return status;
  190. } else {
  191. ALOGE("%s: Expected keyword, got '%s'.", mTokenizer->getLocation().string(),
  192. keywordToken.string());
  193. return BAD_VALUE;
  194. }
  195. mTokenizer->skipDelimiters(WHITESPACE);
  196. if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
  197. ALOGE("%s: Expected end of line or trailing comment, got '%s'.",
  198. mTokenizer->getLocation().string(),
  199. mTokenizer->peekRemainderOfLine().string());
  200. return BAD_VALUE;
  201. }
  202. }
  203. mTokenizer->nextLine();
  204. }
  205. return NO_ERROR;
  206. }
  207. status_t KeyLayoutMap::Parser::parseKey() {
  208. String8 codeToken = mTokenizer->nextToken(WHITESPACE);
  209. bool mapUsage = false;
  210. if (codeToken == "usage") {
  211. mapUsage = true;
  212. mTokenizer->skipDelimiters(WHITESPACE);
  213. codeToken = mTokenizer->nextToken(WHITESPACE);
  214. }
  215. char* end;
  216. int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
  217. if (*end) {
  218. ALOGE("%s: Expected key %s number, got '%s'.", mTokenizer->getLocation().string(),
  219. mapUsage ? "usage" : "scan code", codeToken.string());
  220. return BAD_VALUE;
  221. }
  222. KeyedVector<int32_t, Key>& map = mapUsage ? mMap->mKeysByUsageCode : mMap->mKeysByScanCode;
  223. if (map.indexOfKey(code) >= 0) {
  224. ALOGE("%s: Duplicate entry for key %s '%s'.", mTokenizer->getLocation().string(),
  225. mapUsage ? "usage" : "scan code", codeToken.string());
  226. return BAD_VALUE;
  227. }
  228. mTokenizer->skipDelimiters(WHITESPACE);
  229. String8 keyCodeToken = mTokenizer->nextToken(WHITESPACE);
  230. int32_t keyCode = getKeyCodeByLabel(keyCodeToken.string());
  231. if (!keyCode) {
  232. ALOGE("%s: Expected key code label, got '%s'.", mTokenizer->getLocation().string(),
  233. keyCodeToken.string());
  234. return BAD_VALUE;
  235. }
  236. uint32_t flags = 0;
  237. for (;;) {
  238. mTokenizer->skipDelimiters(WHITESPACE);
  239. if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') break;
  240. String8 flagToken = mTokenizer->nextToken(WHITESPACE);
  241. uint32_t flag = getKeyFlagByLabel(flagToken.string());
  242. if (!flag) {
  243. ALOGE("%s: Expected key flag label, got '%s'.", mTokenizer->getLocation().string(),
  244. flagToken.string());
  245. return BAD_VALUE;
  246. }
  247. if (flags & flag) {
  248. ALOGE("%s: Duplicate key flag '%s'.", mTokenizer->getLocation().string(),
  249. flagToken.string());
  250. return BAD_VALUE;
  251. }
  252. flags |= flag;
  253. }
  254. #if DEBUG_PARSER
  255. ALOGD("Parsed key %s: code=%d, keyCode=%d, flags=0x%08x.",
  256. mapUsage ? "usage" : "scan code", code, keyCode, flags);
  257. #endif
  258. Key key;
  259. key.keyCode = keyCode;
  260. key.flags = flags;
  261. map.add(code, key);
  262. return NO_ERROR;
  263. }
  264. status_t KeyLayoutMap::Parser::parseAxis() {
  265. String8 scanCodeToken = mTokenizer->nextToken(WHITESPACE);
  266. char* end;
  267. int32_t scanCode = int32_t(strtol(scanCodeToken.string(), &end, 0));
  268. if (*end) {
  269. ALOGE("%s: Expected axis scan code number, got '%s'.", mTokenizer->getLocation().string(),
  270. scanCodeToken.string());
  271. return BAD_VALUE;
  272. }
  273. if (mMap->mAxes.indexOfKey(scanCode) >= 0) {
  274. ALOGE("%s: Duplicate entry for axis scan code '%s'.", mTokenizer->getLocation().string(),
  275. scanCodeToken.string());
  276. return BAD_VALUE;
  277. }
  278. AxisInfo axisInfo;
  279. mTokenizer->skipDelimiters(WHITESPACE);
  280. String8 token = mTokenizer->nextToken(WHITESPACE);
  281. if (token == "invert") {
  282. axisInfo.mode = AxisInfo::MODE_INVERT;
  283. mTokenizer->skipDelimiters(WHITESPACE);
  284. String8 axisToken = mTokenizer->nextToken(WHITESPACE);
  285. axisInfo.axis = getAxisByLabel(axisToken.string());
  286. if (axisInfo.axis < 0) {
  287. ALOGE("%s: Expected inverted axis label, got '%s'.",
  288. mTokenizer->getLocation().string(), axisToken.string());
  289. return BAD_VALUE;
  290. }
  291. } else if (token == "split") {
  292. axisInfo.mode = AxisInfo::MODE_SPLIT;
  293. mTokenizer->skipDelimiters(WHITESPACE);
  294. String8 splitToken = mTokenizer->nextToken(WHITESPACE);
  295. axisInfo.splitValue = int32_t(strtol(splitToken.string(), &end, 0));
  296. if (*end) {
  297. ALOGE("%s: Expected split value, got '%s'.",
  298. mTokenizer->getLocation().string(), splitToken.string());
  299. return BAD_VALUE;
  300. }
  301. mTokenizer->skipDelimiters(WHITESPACE);
  302. String8 lowAxisToken = mTokenizer->nextToken(WHITESPACE);
  303. axisInfo.axis = getAxisByLabel(lowAxisToken.string());
  304. if (axisInfo.axis < 0) {
  305. ALOGE("%s: Expected low axis label, got '%s'.",
  306. mTokenizer->getLocation().string(), lowAxisToken.string());
  307. return BAD_VALUE;
  308. }
  309. mTokenizer->skipDelimiters(WHITESPACE);
  310. String8 highAxisToken = mTokenizer->nextToken(WHITESPACE);
  311. axisInfo.highAxis = getAxisByLabel(highAxisToken.string());
  312. if (axisInfo.highAxis < 0) {
  313. ALOGE("%s: Expected high axis label, got '%s'.",
  314. mTokenizer->getLocation().string(), highAxisToken.string());
  315. return BAD_VALUE;
  316. }
  317. } else {
  318. axisInfo.axis = getAxisByLabel(token.string());
  319. if (axisInfo.axis < 0) {
  320. ALOGE("%s: Expected axis label, 'split' or 'invert', got '%s'.",
  321. mTokenizer->getLocation().string(), token.string());
  322. return BAD_VALUE;
  323. }
  324. }
  325. for (;;) {
  326. mTokenizer->skipDelimiters(WHITESPACE);
  327. if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') {
  328. break;
  329. }
  330. String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
  331. if (keywordToken == "flat") {
  332. mTokenizer->skipDelimiters(WHITESPACE);
  333. String8 flatToken = mTokenizer->nextToken(WHITESPACE);
  334. axisInfo.flatOverride = int32_t(strtol(flatToken.string(), &end, 0));
  335. if (*end) {
  336. ALOGE("%s: Expected flat value, got '%s'.",
  337. mTokenizer->getLocation().string(), flatToken.string());
  338. return BAD_VALUE;
  339. }
  340. } else {
  341. ALOGE("%s: Expected keyword 'flat', got '%s'.",
  342. mTokenizer->getLocation().string(), keywordToken.string());
  343. return BAD_VALUE;
  344. }
  345. }
  346. #if DEBUG_PARSER
  347. ALOGD("Parsed axis: scanCode=%d, mode=%d, axis=%d, highAxis=%d, "
  348. "splitValue=%d, flatOverride=%d.",
  349. scanCode,
  350. axisInfo.mode, axisInfo.axis, axisInfo.highAxis,
  351. axisInfo.splitValue, axisInfo.flatOverride);
  352. #endif
  353. mMap->mAxes.add(scanCode, axisInfo);
  354. return NO_ERROR;
  355. }
  356. status_t KeyLayoutMap::Parser::parseLed() {
  357. String8 codeToken = mTokenizer->nextToken(WHITESPACE);
  358. bool mapUsage = false;
  359. if (codeToken == "usage") {
  360. mapUsage = true;
  361. mTokenizer->skipDelimiters(WHITESPACE);
  362. codeToken = mTokenizer->nextToken(WHITESPACE);
  363. }
  364. char* end;
  365. int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
  366. if (*end) {
  367. ALOGE("%s: Expected led %s number, got '%s'.", mTokenizer->getLocation().string(),
  368. mapUsage ? "usage" : "scan code", codeToken.string());
  369. return BAD_VALUE;
  370. }
  371. KeyedVector<int32_t, Led>& map = mapUsage ? mMap->mLedsByUsageCode : mMap->mLedsByScanCode;
  372. if (map.indexOfKey(code) >= 0) {
  373. ALOGE("%s: Duplicate entry for led %s '%s'.", mTokenizer->getLocation().string(),
  374. mapUsage ? "usage" : "scan code", codeToken.string());
  375. return BAD_VALUE;
  376. }
  377. mTokenizer->skipDelimiters(WHITESPACE);
  378. String8 ledCodeToken = mTokenizer->nextToken(WHITESPACE);
  379. int32_t ledCode = getLedByLabel(ledCodeToken.string());
  380. if (ledCode < 0) {
  381. ALOGE("%s: Expected LED code label, got '%s'.", mTokenizer->getLocation().string(),
  382. ledCodeToken.string());
  383. return BAD_VALUE;
  384. }
  385. #if DEBUG_PARSER
  386. ALOGD("Parsed led %s: code=%d, ledCode=%d.",
  387. mapUsage ? "usage" : "scan code", code, ledCode);
  388. #endif
  389. Led led;
  390. led.ledCode = ledCode;
  391. map.add(code, led);
  392. return NO_ERROR;
  393. }
  394. };