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 std::string& filename, sp<KeyLayoutMap>* outMap) {
  40. outMap->clear();
  41. Tokenizer* tokenizer;
  42. status_t status = Tokenizer::open(String8(filename.c_str()), &tokenizer);
  43. if (status) {
  44. ALOGE("Error %d opening key layout map file %s.", status, filename.c_str());
  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 nullptr;
  103. }
  104. status_t KeyLayoutMap::findScanCodesForKey(
  105. int32_t keyCode, std::vector<int32_t>* outScanCodes) const {
  106. const size_t N = mKeysByScanCode.size();
  107. for (size_t i=0; i<N; i++) {
  108. if (mKeysByScanCode.valueAt(i).keyCode == keyCode) {
  109. outScanCodes->push_back(mKeysByScanCode.keyAt(i));
  110. }
  111. }
  112. return NO_ERROR;
  113. }
  114. status_t KeyLayoutMap::mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const {
  115. ssize_t index = mAxes.indexOfKey(scanCode);
  116. if (index < 0) {
  117. #if DEBUG_MAPPING
  118. ALOGD("mapAxis: scanCode=%d ~ Failed.", scanCode);
  119. #endif
  120. return NAME_NOT_FOUND;
  121. }
  122. *outAxisInfo = mAxes.valueAt(index);
  123. #if DEBUG_MAPPING
  124. ALOGD("mapAxis: scanCode=%d ~ Result mode=%d, axis=%d, highAxis=%d, "
  125. "splitValue=%d, flatOverride=%d.",
  126. scanCode,
  127. outAxisInfo->mode, outAxisInfo->axis, outAxisInfo->highAxis,
  128. outAxisInfo->splitValue, outAxisInfo->flatOverride);
  129. #endif
  130. return NO_ERROR;
  131. }
  132. status_t KeyLayoutMap::findScanCodeForLed(int32_t ledCode, int32_t* outScanCode) const {
  133. const size_t N = mLedsByScanCode.size();
  134. for (size_t i = 0; i < N; i++) {
  135. if (mLedsByScanCode.valueAt(i).ledCode == ledCode) {
  136. *outScanCode = mLedsByScanCode.keyAt(i);
  137. #if DEBUG_MAPPING
  138. ALOGD("findScanCodeForLed: ledCode=%d, scanCode=%d.", ledCode, *outScanCode);
  139. #endif
  140. return NO_ERROR;
  141. }
  142. }
  143. #if DEBUG_MAPPING
  144. ALOGD("findScanCodeForLed: ledCode=%d ~ Not found.", ledCode);
  145. #endif
  146. return NAME_NOT_FOUND;
  147. }
  148. status_t KeyLayoutMap::findUsageCodeForLed(int32_t ledCode, int32_t* outUsageCode) const {
  149. const size_t N = mLedsByUsageCode.size();
  150. for (size_t i = 0; i < N; i++) {
  151. if (mLedsByUsageCode.valueAt(i).ledCode == ledCode) {
  152. *outUsageCode = mLedsByUsageCode.keyAt(i);
  153. #if DEBUG_MAPPING
  154. ALOGD("findUsageForLed: ledCode=%d, usage=%x.", ledCode, *outUsageCode);
  155. #endif
  156. return NO_ERROR;
  157. }
  158. }
  159. #if DEBUG_MAPPING
  160. ALOGD("findUsageForLed: ledCode=%d ~ Not found.", ledCode);
  161. #endif
  162. return NAME_NOT_FOUND;
  163. }
  164. // --- KeyLayoutMap::Parser ---
  165. KeyLayoutMap::Parser::Parser(KeyLayoutMap* map, Tokenizer* tokenizer) :
  166. mMap(map), mTokenizer(tokenizer) {
  167. }
  168. KeyLayoutMap::Parser::~Parser() {
  169. }
  170. status_t KeyLayoutMap::Parser::parse() {
  171. while (!mTokenizer->isEof()) {
  172. #if DEBUG_PARSER
  173. ALOGD("Parsing %s: '%s'.", mTokenizer->getLocation().string(),
  174. mTokenizer->peekRemainderOfLine().string());
  175. #endif
  176. mTokenizer->skipDelimiters(WHITESPACE);
  177. if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
  178. String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
  179. if (keywordToken == "key") {
  180. mTokenizer->skipDelimiters(WHITESPACE);
  181. status_t status = parseKey();
  182. if (status) return status;
  183. } else if (keywordToken == "axis") {
  184. mTokenizer->skipDelimiters(WHITESPACE);
  185. status_t status = parseAxis();
  186. if (status) return status;
  187. } else if (keywordToken == "led") {
  188. mTokenizer->skipDelimiters(WHITESPACE);
  189. status_t status = parseLed();
  190. if (status) return status;
  191. } else {
  192. ALOGE("%s: Expected keyword, got '%s'.", mTokenizer->getLocation().string(),
  193. keywordToken.string());
  194. return BAD_VALUE;
  195. }
  196. mTokenizer->skipDelimiters(WHITESPACE);
  197. if (!mTokenizer->isEol() && mTokenizer->peekChar() != '#') {
  198. ALOGE("%s: Expected end of line or trailing comment, got '%s'.",
  199. mTokenizer->getLocation().string(),
  200. mTokenizer->peekRemainderOfLine().string());
  201. return BAD_VALUE;
  202. }
  203. }
  204. mTokenizer->nextLine();
  205. }
  206. return NO_ERROR;
  207. }
  208. status_t KeyLayoutMap::Parser::parseKey() {
  209. String8 codeToken = mTokenizer->nextToken(WHITESPACE);
  210. bool mapUsage = false;
  211. if (codeToken == "usage") {
  212. mapUsage = true;
  213. mTokenizer->skipDelimiters(WHITESPACE);
  214. codeToken = mTokenizer->nextToken(WHITESPACE);
  215. }
  216. char* end;
  217. int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
  218. if (*end) {
  219. ALOGE("%s: Expected key %s number, got '%s'.", mTokenizer->getLocation().string(),
  220. mapUsage ? "usage" : "scan code", codeToken.string());
  221. return BAD_VALUE;
  222. }
  223. KeyedVector<int32_t, Key>& map = mapUsage ? mMap->mKeysByUsageCode : mMap->mKeysByScanCode;
  224. if (map.indexOfKey(code) >= 0) {
  225. ALOGE("%s: Duplicate entry for key %s '%s'.", mTokenizer->getLocation().string(),
  226. mapUsage ? "usage" : "scan code", codeToken.string());
  227. return BAD_VALUE;
  228. }
  229. mTokenizer->skipDelimiters(WHITESPACE);
  230. String8 keyCodeToken = mTokenizer->nextToken(WHITESPACE);
  231. int32_t keyCode = getKeyCodeByLabel(keyCodeToken.string());
  232. if (!keyCode) {
  233. ALOGE("%s: Expected key code label, got '%s'.", mTokenizer->getLocation().string(),
  234. keyCodeToken.string());
  235. return BAD_VALUE;
  236. }
  237. uint32_t flags = 0;
  238. for (;;) {
  239. mTokenizer->skipDelimiters(WHITESPACE);
  240. if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') break;
  241. String8 flagToken = mTokenizer->nextToken(WHITESPACE);
  242. uint32_t flag = getKeyFlagByLabel(flagToken.string());
  243. if (!flag) {
  244. ALOGE("%s: Expected key flag label, got '%s'.", mTokenizer->getLocation().string(),
  245. flagToken.string());
  246. return BAD_VALUE;
  247. }
  248. if (flags & flag) {
  249. ALOGE("%s: Duplicate key flag '%s'.", mTokenizer->getLocation().string(),
  250. flagToken.string());
  251. return BAD_VALUE;
  252. }
  253. flags |= flag;
  254. }
  255. #if DEBUG_PARSER
  256. ALOGD("Parsed key %s: code=%d, keyCode=%d, flags=0x%08x.",
  257. mapUsage ? "usage" : "scan code", code, keyCode, flags);
  258. #endif
  259. Key key;
  260. key.keyCode = keyCode;
  261. key.flags = flags;
  262. map.add(code, key);
  263. return NO_ERROR;
  264. }
  265. status_t KeyLayoutMap::Parser::parseAxis() {
  266. String8 scanCodeToken = mTokenizer->nextToken(WHITESPACE);
  267. char* end;
  268. int32_t scanCode = int32_t(strtol(scanCodeToken.string(), &end, 0));
  269. if (*end) {
  270. ALOGE("%s: Expected axis scan code number, got '%s'.", mTokenizer->getLocation().string(),
  271. scanCodeToken.string());
  272. return BAD_VALUE;
  273. }
  274. if (mMap->mAxes.indexOfKey(scanCode) >= 0) {
  275. ALOGE("%s: Duplicate entry for axis scan code '%s'.", mTokenizer->getLocation().string(),
  276. scanCodeToken.string());
  277. return BAD_VALUE;
  278. }
  279. AxisInfo axisInfo;
  280. mTokenizer->skipDelimiters(WHITESPACE);
  281. String8 token = mTokenizer->nextToken(WHITESPACE);
  282. if (token == "invert") {
  283. axisInfo.mode = AxisInfo::MODE_INVERT;
  284. mTokenizer->skipDelimiters(WHITESPACE);
  285. String8 axisToken = mTokenizer->nextToken(WHITESPACE);
  286. axisInfo.axis = getAxisByLabel(axisToken.string());
  287. if (axisInfo.axis < 0) {
  288. ALOGE("%s: Expected inverted axis label, got '%s'.",
  289. mTokenizer->getLocation().string(), axisToken.string());
  290. return BAD_VALUE;
  291. }
  292. } else if (token == "split") {
  293. axisInfo.mode = AxisInfo::MODE_SPLIT;
  294. mTokenizer->skipDelimiters(WHITESPACE);
  295. String8 splitToken = mTokenizer->nextToken(WHITESPACE);
  296. axisInfo.splitValue = int32_t(strtol(splitToken.string(), &end, 0));
  297. if (*end) {
  298. ALOGE("%s: Expected split value, got '%s'.",
  299. mTokenizer->getLocation().string(), splitToken.string());
  300. return BAD_VALUE;
  301. }
  302. mTokenizer->skipDelimiters(WHITESPACE);
  303. String8 lowAxisToken = mTokenizer->nextToken(WHITESPACE);
  304. axisInfo.axis = getAxisByLabel(lowAxisToken.string());
  305. if (axisInfo.axis < 0) {
  306. ALOGE("%s: Expected low axis label, got '%s'.",
  307. mTokenizer->getLocation().string(), lowAxisToken.string());
  308. return BAD_VALUE;
  309. }
  310. mTokenizer->skipDelimiters(WHITESPACE);
  311. String8 highAxisToken = mTokenizer->nextToken(WHITESPACE);
  312. axisInfo.highAxis = getAxisByLabel(highAxisToken.string());
  313. if (axisInfo.highAxis < 0) {
  314. ALOGE("%s: Expected high axis label, got '%s'.",
  315. mTokenizer->getLocation().string(), highAxisToken.string());
  316. return BAD_VALUE;
  317. }
  318. } else {
  319. axisInfo.axis = getAxisByLabel(token.string());
  320. if (axisInfo.axis < 0) {
  321. ALOGE("%s: Expected axis label, 'split' or 'invert', got '%s'.",
  322. mTokenizer->getLocation().string(), token.string());
  323. return BAD_VALUE;
  324. }
  325. }
  326. for (;;) {
  327. mTokenizer->skipDelimiters(WHITESPACE);
  328. if (mTokenizer->isEol() || mTokenizer->peekChar() == '#') {
  329. break;
  330. }
  331. String8 keywordToken = mTokenizer->nextToken(WHITESPACE);
  332. if (keywordToken == "flat") {
  333. mTokenizer->skipDelimiters(WHITESPACE);
  334. String8 flatToken = mTokenizer->nextToken(WHITESPACE);
  335. axisInfo.flatOverride = int32_t(strtol(flatToken.string(), &end, 0));
  336. if (*end) {
  337. ALOGE("%s: Expected flat value, got '%s'.",
  338. mTokenizer->getLocation().string(), flatToken.string());
  339. return BAD_VALUE;
  340. }
  341. } else {
  342. ALOGE("%s: Expected keyword 'flat', got '%s'.",
  343. mTokenizer->getLocation().string(), keywordToken.string());
  344. return BAD_VALUE;
  345. }
  346. }
  347. #if DEBUG_PARSER
  348. ALOGD("Parsed axis: scanCode=%d, mode=%d, axis=%d, highAxis=%d, "
  349. "splitValue=%d, flatOverride=%d.",
  350. scanCode,
  351. axisInfo.mode, axisInfo.axis, axisInfo.highAxis,
  352. axisInfo.splitValue, axisInfo.flatOverride);
  353. #endif
  354. mMap->mAxes.add(scanCode, axisInfo);
  355. return NO_ERROR;
  356. }
  357. status_t KeyLayoutMap::Parser::parseLed() {
  358. String8 codeToken = mTokenizer->nextToken(WHITESPACE);
  359. bool mapUsage = false;
  360. if (codeToken == "usage") {
  361. mapUsage = true;
  362. mTokenizer->skipDelimiters(WHITESPACE);
  363. codeToken = mTokenizer->nextToken(WHITESPACE);
  364. }
  365. char* end;
  366. int32_t code = int32_t(strtol(codeToken.string(), &end, 0));
  367. if (*end) {
  368. ALOGE("%s: Expected led %s number, got '%s'.", mTokenizer->getLocation().string(),
  369. mapUsage ? "usage" : "scan code", codeToken.string());
  370. return BAD_VALUE;
  371. }
  372. KeyedVector<int32_t, Led>& map = mapUsage ? mMap->mLedsByUsageCode : mMap->mLedsByScanCode;
  373. if (map.indexOfKey(code) >= 0) {
  374. ALOGE("%s: Duplicate entry for led %s '%s'.", mTokenizer->getLocation().string(),
  375. mapUsage ? "usage" : "scan code", codeToken.string());
  376. return BAD_VALUE;
  377. }
  378. mTokenizer->skipDelimiters(WHITESPACE);
  379. String8 ledCodeToken = mTokenizer->nextToken(WHITESPACE);
  380. int32_t ledCode = getLedByLabel(ledCodeToken.string());
  381. if (ledCode < 0) {
  382. ALOGE("%s: Expected LED code label, got '%s'.", mTokenizer->getLocation().string(),
  383. ledCodeToken.string());
  384. return BAD_VALUE;
  385. }
  386. #if DEBUG_PARSER
  387. ALOGD("Parsed led %s: code=%d, ledCode=%d.",
  388. mapUsage ? "usage" : "scan code", code, ledCode);
  389. #endif
  390. Led led;
  391. led.ledCode = ledCode;
  392. map.add(code, led);
  393. return NO_ERROR;
  394. }
  395. };