From 74bdd2e7ceabd3c9e74ccf7c2e6bd3dae27ca497 Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Thu, 17 Oct 2013 17:35:53 -0700 Subject: [PATCH] Parse LED mappings in keylayout files Change-Id: I6bc3760341868015f054d9866e2616a312da94e0 --- include/input/Input.h | 25 +++++++++++ include/input/KeyLayoutMap.h | 10 +++++ include/input/Keyboard.h | 10 ++++- include/input/KeycodeLabels.h | 22 ++++++++++ libs/input/KeyLayoutMap.cpp | 83 ++++++++++++++++++++++++++++++++++- libs/input/Keyboard.cpp | 4 ++ 6 files changed, 150 insertions(+), 4 deletions(-) diff --git a/include/input/Input.h b/include/input/Input.h index e77807634..600c10eef 100644 --- a/include/input/Input.h +++ b/include/input/Input.h @@ -65,6 +65,31 @@ enum { AINPUT_SOURCE_SWITCH = 0x80000000, }; +enum { + /** + * Constants for LEDs. Hidden from the API since we don't actually expose a way to interact + * with LEDs to developers + * + * NOTE: If you add LEDs here, you must also add them to KeycodeLabels.h + */ + + ALED_NUM_LOCK = 0x00, + ALED_CAPS_LOCK = 0x01, + ALED_SCROLL_LOCK = 0x02, + ALED_COMPOSE = 0x03, + ALED_KANA = 0x04, + ALED_SLEEP = 0x05, + ALED_SUSPEND = 0x06, + ALED_MUTE = 0x07, + ALED_MISC = 0x08, + ALED_MAIL = 0x09, + ALED_CHARGING = 0x0a, + ALED_CONTROLLER_1 = 0x10, + ALED_CONTROLLER_2 = 0x11, + ALED_CONTROLLER_3 = 0x12, + ALED_CONTROLLER_4 = 0x13, +}; + /* * SystemUiVisibility constants from View. */ diff --git a/include/input/KeyLayoutMap.h b/include/input/KeyLayoutMap.h index eec11cf13..1e8de7173 100644 --- a/include/input/KeyLayoutMap.h +++ b/include/input/KeyLayoutMap.h @@ -67,6 +67,8 @@ public: status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t* outKeyCode, uint32_t* outFlags) const; status_t findScanCodesForKey(int32_t keyCode, Vector* outScanCodes) const; + status_t findScanCodeForLed(int32_t ledCode, int32_t* outScanCode) const; + status_t findUsageCodeForLed(int32_t ledCode, int32_t* outUsageCode) const; status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const; @@ -79,9 +81,16 @@ private: uint32_t flags; }; + struct Led { + int32_t ledCode; + }; + + KeyedVector mKeysByScanCode; KeyedVector mKeysByUsageCode; KeyedVector mAxes; + KeyedVector mLedsByScanCode; + KeyedVector mLedsByUsageCode; KeyLayoutMap(); @@ -99,6 +108,7 @@ private: private: status_t parseKey(); status_t parseAxis(); + status_t parseLed(); }; }; diff --git a/include/input/Keyboard.h b/include/input/Keyboard.h index 846cb0c83..25b2f07cf 100644 --- a/include/input/Keyboard.h +++ b/include/input/Keyboard.h @@ -94,17 +94,23 @@ extern int32_t getKeyCodeByLabel(const char* label); extern uint32_t getKeyFlagByLabel(const char* label); /** - * Gets a axis by its short form label, eg. "X". + * Gets an axis by its short form label, eg. "X". * Returns -1 if unknown. */ extern int32_t getAxisByLabel(const char* label); /** - * Gets a axis label by its id. + * Gets an axis label by its id. * Returns NULL if unknown. */ extern const char* getAxisLabel(int32_t axisId); +/** + * Gets an LED by its short form label, eg. "CAPS_LOCK". + * Returns -1 if unknown. + */ +extern int32_t getLedByLabel(const char* label); + /** * Updates a meta state field when a key is pressed or released. */ diff --git a/include/input/KeycodeLabels.h b/include/input/KeycodeLabels.h index c64c5d8da..19582e920 100644 --- a/include/input/KeycodeLabels.h +++ b/include/input/KeycodeLabels.h @@ -319,4 +319,26 @@ static const KeycodeLabel AXES[] = { { NULL, -1 } }; +static const KeycodeLabel LEDS[] = { + { "NUM_LOCK", 0x00 }, + { "CAPS_LOCK", 0x01 }, + { "SCROLL_LOCK", 0x02 }, + { "COMPOSE", 0x03 }, + { "KANA", 0x04 }, + { "SLEEP", 0x05 }, + { "SUSPEND", 0x06 }, + { "MUTE", 0x07 }, + { "MISC", 0x08 }, + { "MAIL", 0x09 }, + { "CHARGING", 0x0a }, + { "CONTROLLER_1", 0x10 }, + { "CONTROLLER_2", 0x11 }, + { "CONTROLLER_3", 0x12 }, + { "CONTROLLER_4", 0x13 }, + + // NOTE: If you add new LEDs here, you must also add them to Input.h + + { NULL, -1 } +}; + #endif // _LIBINPUT_KEYCODE_LABELS_H diff --git a/libs/input/KeyLayoutMap.cpp b/libs/input/KeyLayoutMap.cpp index 2f5494b70..0800a31d7 100644 --- a/libs/input/KeyLayoutMap.cpp +++ b/libs/input/KeyLayoutMap.cpp @@ -150,6 +150,40 @@ status_t KeyLayoutMap::mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const { return NO_ERROR; } +status_t KeyLayoutMap::findScanCodeForLed(int32_t ledCode, int32_t* outScanCode) const { + const size_t N = mLedsByScanCode.size(); + for (size_t i = 0; i < N; i++) { + if (mLedsByScanCode.valueAt(i).ledCode == ledCode) { + *outScanCode = mLedsByScanCode.keyAt(i); +#if DEBUG_MAPPING + ALOGD("findScanCodeForLed: ledCode=%d, scanCode=%d.", ledCode, *outScanCode); +#endif + return NO_ERROR; + } + } +#if DEBUG_MAPPING + ALOGD("findScanCodeForLed: ledCode=%d ~ Not found.", ledCode); +#endif + return NAME_NOT_FOUND; +} + +status_t KeyLayoutMap::findUsageCodeForLed(int32_t ledCode, int32_t* outUsageCode) const { + const size_t N = mLedsByUsageCode.size(); + for (size_t i = 0; i < N; i++) { + if (mLedsByUsageCode.valueAt(i).ledCode == ledCode) { + *outUsageCode = mLedsByUsageCode.keyAt(i); +#if DEBUG_MAPPING + ALOGD("findUsageForLed: ledCode=%d, usage=%x.", ledCode, *outUsageCode); +#endif + return NO_ERROR; + } + } +#if DEBUG_MAPPING + ALOGD("findUsageForLed: ledCode=%d ~ Not found.", ledCode); +#endif + return NAME_NOT_FOUND; +} + // --- KeyLayoutMap::Parser --- @@ -179,6 +213,10 @@ status_t KeyLayoutMap::Parser::parse() { mTokenizer->skipDelimiters(WHITESPACE); status_t status = parseAxis(); if (status) return status; + } else if (keywordToken == "led") { + mTokenizer->skipDelimiters(WHITESPACE); + status_t status = parseLed(); + if (status) return status; } else { ALOGE("%s: Expected keyword, got '%s'.", mTokenizer->getLocation().string(), keywordToken.string()); @@ -215,8 +253,7 @@ status_t KeyLayoutMap::Parser::parseKey() { mapUsage ? "usage" : "scan code", codeToken.string()); return BAD_VALUE; } - KeyedVector& map = - mapUsage ? mMap->mKeysByUsageCode : mMap->mKeysByScanCode; + KeyedVector& map = mapUsage ? mMap->mKeysByUsageCode : mMap->mKeysByScanCode; if (map.indexOfKey(code) >= 0) { ALOGE("%s: Duplicate entry for key %s '%s'.", mTokenizer->getLocation().string(), mapUsage ? "usage" : "scan code", codeToken.string()); @@ -364,4 +401,46 @@ status_t KeyLayoutMap::Parser::parseAxis() { return NO_ERROR; } +status_t KeyLayoutMap::Parser::parseLed() { + String8 codeToken = mTokenizer->nextToken(WHITESPACE); + bool mapUsage = false; + if (codeToken == "usage") { + mapUsage = true; + mTokenizer->skipDelimiters(WHITESPACE); + codeToken = mTokenizer->nextToken(WHITESPACE); + } + char* end; + int32_t code = int32_t(strtol(codeToken.string(), &end, 0)); + if (*end) { + ALOGE("%s: Expected led %s number, got '%s'.", mTokenizer->getLocation().string(), + mapUsage ? "usage" : "scan code", codeToken.string()); + return BAD_VALUE; + } + + KeyedVector& map = mapUsage ? mMap->mLedsByUsageCode : mMap->mLedsByScanCode; + if (map.indexOfKey(code) >= 0) { + ALOGE("%s: Duplicate entry for led %s '%s'.", mTokenizer->getLocation().string(), + mapUsage ? "usage" : "scan code", codeToken.string()); + return BAD_VALUE; + } + + mTokenizer->skipDelimiters(WHITESPACE); + String8 ledCodeToken = mTokenizer->nextToken(WHITESPACE); + int32_t ledCode = getLedByLabel(ledCodeToken.string()); + if (ledCode < 0) { + ALOGE("%s: Expected LED code label, got '%s'.", mTokenizer->getLocation().string(), + ledCodeToken.string()); + return BAD_VALUE; + } + +#if DEBUG_PARSER + ALOGD("Parsed led %s: code=%d, ledCode=%d.", + mapUsage ? "usage" : "scan code", code, ledCode); +#endif + + Led led; + led.ledCode = ledCode; + map.add(code, led); + return NO_ERROR; +} }; diff --git a/libs/input/Keyboard.cpp b/libs/input/Keyboard.cpp index b6551ee76..7d4ac9216 100644 --- a/libs/input/Keyboard.cpp +++ b/libs/input/Keyboard.cpp @@ -203,6 +203,10 @@ const char* getAxisLabel(int32_t axisId) { return lookupLabelByValue(axisId, AXES); } +int32_t getLedByLabel(const char* label) { + return int32_t(lookupValueByLabel(label, LEDS)); +} + static int32_t setEphemeralMetaState(int32_t mask, bool down, int32_t oldMetaState) { int32_t newMetaState; if (down) {