diff options
author | Chris Ye <lzye@google.com> | 2020-08-18 12:50:12 -0700 |
---|---|---|
committer | Chris Ye <lzye@google.com> | 2020-08-28 22:13:25 -0700 |
commit | 1abffbd101f77f100789dc7fb8b33f729bb64dc9 (patch) | |
tree | b9383e94b8ddff48dfa831e14df094ffa258863b /libs/input/KeyLayoutMap.cpp | |
parent | 6ab8a005235a0270afb507bb9349abc13276fb79 (diff) |
Move KeyLayoutMap from RefBase to shared_ptr.
Move KeyLayoutMap from inheriting RefBase and use shared_ptr
to store in owner class like KeyMap.
Bug: 160010896
Test: atest inputflinger, atest libinput_tests
Change-Id: I565e07bdc501af644df5ebb8388fce10af185bce
Diffstat (limited to 'libs/input/KeyLayoutMap.cpp')
-rw-r--r-- | libs/input/KeyLayoutMap.cpp | 61 |
1 files changed, 42 insertions, 19 deletions
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, |