diff options
-rw-r--r-- | include/input/KeyLayoutMap.h | 13 | ||||
-rw-r--r-- | include/input/Keyboard.h | 2 | ||||
-rw-r--r-- | libs/input/KeyLayoutMap.cpp | 61 | ||||
-rw-r--r-- | libs/input/Keyboard.cpp | 8 | ||||
-rw-r--r-- | services/inputflinger/host/Android.bp | 1 |
5 files changed, 57 insertions, 28 deletions
diff --git a/include/input/KeyLayoutMap.h b/include/input/KeyLayoutMap.h index 26f35012e2..872dd45c73 100644 --- a/include/input/KeyLayoutMap.h +++ b/include/input/KeyLayoutMap.h @@ -17,11 +17,12 @@ #ifndef _LIBINPUT_KEY_LAYOUT_MAP_H #define _LIBINPUT_KEY_LAYOUT_MAP_H +#include <android-base/result.h> #include <stdint.h> #include <utils/Errors.h> #include <utils/KeyedVector.h> -#include <utils/Tokenizer.h> #include <utils/RefBase.h> +#include <utils/Tokenizer.h> namespace android { @@ -60,9 +61,12 @@ struct AxisInfo { * * This object is immutable after it has been loaded. */ -class KeyLayoutMap : public RefBase { +class KeyLayoutMap { public: - static status_t load(const std::string& filename, sp<KeyLayoutMap>* outMap); + static base::Result<std::shared_ptr<KeyLayoutMap>> load(const std::string& filename); + static base::Result<std::shared_ptr<KeyLayoutMap>> load(Tokenizer* tokenizer); + static base::Result<std::shared_ptr<KeyLayoutMap>> loadContents(const std::string& filename, + const char* contents); status_t mapKey(int32_t scanCode, int32_t usageCode, int32_t* outKeyCode, uint32_t* outFlags) const; @@ -71,8 +75,8 @@ public: status_t findUsageCodeForLed(int32_t ledCode, int32_t* outUsageCode) const; status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const; + const std::string getLoadFileName() const; -protected: virtual ~KeyLayoutMap(); private: @@ -91,6 +95,7 @@ private: KeyedVector<int32_t, AxisInfo> mAxes; KeyedVector<int32_t, Led> mLedsByScanCode; KeyedVector<int32_t, Led> mLedsByUsageCode; + std::string mLoadFileName; KeyLayoutMap(); diff --git a/include/input/Keyboard.h b/include/input/Keyboard.h index 92da10c056..4c56a694b4 100644 --- a/include/input/Keyboard.h +++ b/include/input/Keyboard.h @@ -34,7 +34,7 @@ class KeyCharacterMap; class KeyMap { public: std::string keyLayoutFile; - sp<KeyLayoutMap> keyLayoutMap; + std::shared_ptr<KeyLayoutMap> keyLayoutMap; std::string keyCharacterMapFile; sp<KeyCharacterMap> keyCharacterMap; diff --git a/libs/input/KeyLayoutMap.cpp b/libs/input/KeyLayoutMap.cpp index 9c399b3fa1..16ce48aca1 100644 --- a/libs/input/KeyLayoutMap.cpp +++ b/libs/input/KeyLayoutMap.cpp @@ -49,37 +49,60 @@ KeyLayoutMap::KeyLayoutMap() { KeyLayoutMap::~KeyLayoutMap() { } -status_t KeyLayoutMap::load(const std::string& filename, sp<KeyLayoutMap>* outMap) { - outMap->clear(); +base::Result<std::shared_ptr<KeyLayoutMap>> KeyLayoutMap::loadContents(const std::string& filename, + const char* contents) { + Tokenizer* tokenizer; + status_t status = Tokenizer::fromContents(String8(filename.c_str()), contents, &tokenizer); + if (status) { + ALOGE("Error %d opening key layout map.", status); + return Errorf("Error {} opening key layout map file {}.", status, filename.c_str()); + } + std::unique_ptr<Tokenizer> t(tokenizer); + auto ret = load(t.get()); + if (ret) { + (*ret)->mLoadFileName = filename; + } + return ret; +} +base::Result<std::shared_ptr<KeyLayoutMap>> KeyLayoutMap::load(const std::string& filename) { Tokenizer* tokenizer; status_t status = Tokenizer::open(String8(filename.c_str()), &tokenizer); if (status) { ALOGE("Error %d opening key layout map file %s.", status, filename.c_str()); + return Errorf("Error {} opening key layout map file {}.", status, filename.c_str()); + } + std::unique_ptr<Tokenizer> t(tokenizer); + auto ret = load(t.get()); + if (ret) { + (*ret)->mLoadFileName = filename; + } + return ret; +} + +base::Result<std::shared_ptr<KeyLayoutMap>> KeyLayoutMap::load(Tokenizer* tokenizer) { + std::shared_ptr<KeyLayoutMap> map = std::shared_ptr<KeyLayoutMap>(new KeyLayoutMap()); + status_t status = OK; + if (!map.get()) { + ALOGE("Error allocating key layout map."); + return Errorf("Error allocating key layout map."); } else { - sp<KeyLayoutMap> map = new KeyLayoutMap(); - if (!map.get()) { - ALOGE("Error allocating key layout map."); - status = NO_MEMORY; - } else { #if DEBUG_PARSER_PERFORMANCE - nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC); + nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC); #endif - Parser parser(map.get(), tokenizer); - status = parser.parse(); + Parser parser(map.get(), tokenizer); + status = parser.parse(); #if DEBUG_PARSER_PERFORMANCE - nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime; - ALOGD("Parsed key layout map file '%s' %d lines in %0.3fms.", - tokenizer->getFilename().string(), tokenizer->getLineNumber(), - elapsedTime / 1000000.0); + nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime; + ALOGD("Parsed key layout map file '%s' %d lines in %0.3fms.", + tokenizer->getFilename().string(), tokenizer->getLineNumber(), + elapsedTime / 1000000.0); #endif - if (!status) { - *outMap = map; - } + if (!status) { + return std::move(map); } - delete tokenizer; } - return status; + return Errorf("Load KeyLayoutMap failed {}.", status); } status_t KeyLayoutMap::mapKey(int32_t scanCode, int32_t usageCode, diff --git a/libs/input/Keyboard.cpp b/libs/input/Keyboard.cpp index 25025f2963..1a64a9ca1e 100644 --- a/libs/input/Keyboard.cpp +++ b/libs/input/Keyboard.cpp @@ -110,11 +110,11 @@ status_t KeyMap::loadKeyLayout(const InputDeviceIdentifier& deviceIdentifier, return NAME_NOT_FOUND; } - status_t status = KeyLayoutMap::load(path, &keyLayoutMap); - if (status) { - return status; + base::Result<std::shared_ptr<KeyLayoutMap>> ret = KeyLayoutMap::load(path); + if (!ret) { + return ret.error().code(); } - + keyLayoutMap = *ret; keyLayoutFile = path; return OK; } diff --git a/services/inputflinger/host/Android.bp b/services/inputflinger/host/Android.bp index 3496a24d47..9e797e4862 100644 --- a/services/inputflinger/host/Android.bp +++ b/services/inputflinger/host/Android.bp @@ -23,6 +23,7 @@ cc_library_shared { header_libs: ["jni_headers"], shared_libs: [ + "libbase", "libbinder", "libcrypto", "libcutils", |