diff options
author | 2019-02-20 16:21:46 -0600 | |
---|---|---|
committer | 2019-02-20 19:04:12 -0600 | |
commit | 3e78decd5e6fe3590da3e91680aae51d43c62d5f (patch) | |
tree | 9fc6c4e78abf1edf50a56505509e32f9225d5604 | |
parent | d008ac8d41cefba53508306f6dda0a5ba14bb312 (diff) |
Refactor VirtualKeyMap
Currently, there's a lot of new and delete going on in VirtualKeyMap.
Simplify this by using unique_ptr instead.
Bug: 113575658
Test: atest libinput_tests inputflinger_tests
Change-Id: Ib2a68ae23a4300b0e2cf72902371f4b9604cfee5
-rw-r--r-- | include/input/VirtualKeyMap.h | 2 | ||||
-rw-r--r-- | libs/input/VirtualKeyMap.cpp | 55 | ||||
-rw-r--r-- | services/inputflinger/EventHub.cpp | 12 | ||||
-rw-r--r-- | services/inputflinger/EventHub.h | 4 |
4 files changed, 29 insertions, 44 deletions
diff --git a/include/input/VirtualKeyMap.h b/include/input/VirtualKeyMap.h index 24e0e0ed9e..4f7cfb6b75 100644 --- a/include/input/VirtualKeyMap.h +++ b/include/input/VirtualKeyMap.h @@ -49,7 +49,7 @@ class VirtualKeyMap { public: ~VirtualKeyMap(); - static status_t load(const std::string& filename, VirtualKeyMap** outMap); + static std::unique_ptr<VirtualKeyMap> load(const std::string& filename); inline const Vector<VirtualKeyDefinition>& getVirtualKeys() const { return mVirtualKeys; diff --git a/libs/input/VirtualKeyMap.cpp b/libs/input/VirtualKeyMap.cpp index 3ec53bf5a0..624f152996 100644 --- a/libs/input/VirtualKeyMap.cpp +++ b/libs/input/VirtualKeyMap.cpp @@ -28,10 +28,6 @@ // Enables debug output for the parser. #define DEBUG_PARSER 0 -// Enables debug output for parser performance. -#define DEBUG_PARSER_PERFORMANCE 0 - - namespace android { static const char* WHITESPACE = " \t\r"; @@ -46,39 +42,28 @@ VirtualKeyMap::VirtualKeyMap() { VirtualKeyMap::~VirtualKeyMap() { } -status_t VirtualKeyMap::load(const std::string& filename, VirtualKeyMap** outMap) { - *outMap = nullptr; - - Tokenizer* tokenizer; - status_t status = Tokenizer::open(String8(filename.c_str()), &tokenizer); - if (status) { +std::unique_ptr<VirtualKeyMap> VirtualKeyMap::load(const std::string& filename) { + Tokenizer* t; + status_t status = Tokenizer::open(String8(filename.c_str()), &t); + if (status != OK) { ALOGE("Error %d opening virtual key map file %s.", status, filename.c_str()); - } else { - VirtualKeyMap* map = new VirtualKeyMap(); - if (!map) { - ALOGE("Error allocating virtual key map."); - status = NO_MEMORY; - } else { -#if DEBUG_PARSER_PERFORMANCE - nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC); -#endif - Parser parser(map, tokenizer); - status = parser.parse(); -#if DEBUG_PARSER_PERFORMANCE - nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime; - ALOGD("Parsed key character map file '%s' %d lines in %0.3fms.", - tokenizer->getFilename().string(), tokenizer->getLineNumber(), - elapsedTime / 1000000.0); -#endif - if (status) { - delete map; - } else { - *outMap = map; - } - } - delete tokenizer; + return nullptr; + } + std::unique_ptr<Tokenizer> tokenizer(t); + // Using 'new' to access a non-public constructor + std::unique_ptr<VirtualKeyMap> map(new VirtualKeyMap()); + if (!map) { + ALOGE("Error allocating virtual key map."); + return nullptr; } - return status; + + Parser parser(map.get(), tokenizer.get()); + status = parser.parse(); + if (status != OK) { + return nullptr; + } + + return map; } diff --git a/services/inputflinger/EventHub.cpp b/services/inputflinger/EventHub.cpp index c13bac6a12..4735643934 100644 --- a/services/inputflinger/EventHub.cpp +++ b/services/inputflinger/EventHub.cpp @@ -202,7 +202,6 @@ EventHub::Device::Device(int fd, int32_t id, const std::string& path, EventHub::Device::~Device() { close(); delete configuration; - delete virtualKeyMap; } void EventHub::Device::close() { @@ -1364,8 +1363,8 @@ status_t EventHub::openDeviceLocked(const char* devicePath) { if ((device->classes & INPUT_DEVICE_CLASS_TOUCH)) { // Load the virtual keys for the touch screen, if any. // We do this now so that we can make sure to load the keymap if necessary. - status_t status = loadVirtualKeyMapLocked(device); - if (!status) { + bool success = loadVirtualKeyMapLocked(device); + if (success) { device->classes |= INPUT_DEVICE_CLASS_KEYBOARD; } } @@ -1614,15 +1613,16 @@ void EventHub::loadConfigurationLocked(Device* device) { } } -status_t EventHub::loadVirtualKeyMapLocked(Device* device) { +bool EventHub::loadVirtualKeyMapLocked(Device* device) { // The virtual key map is supplied by the kernel as a system board property file. std::string path; path += "/sys/board_properties/virtualkeys."; path += device->identifier.name; if (access(path.c_str(), R_OK)) { - return NAME_NOT_FOUND; + return false; } - return VirtualKeyMap::load(path, &device->virtualKeyMap); + device->virtualKeyMap = VirtualKeyMap::load(path); + return device->virtualKeyMap != nullptr; } status_t EventHub::loadKeyMapLocked(Device* device) { diff --git a/services/inputflinger/EventHub.h b/services/inputflinger/EventHub.h index d176648b04..44f7b3715a 100644 --- a/services/inputflinger/EventHub.h +++ b/services/inputflinger/EventHub.h @@ -345,7 +345,7 @@ private: std::string configurationFile; PropertyMap* configuration; - VirtualKeyMap* virtualKeyMap; + std::unique_ptr<VirtualKeyMap> virtualKeyMap; KeyMap keyMap; sp<KeyCharacterMap> overlayKeyMap; @@ -416,7 +416,7 @@ private: bool hasKeycodeLocked(Device* device, int keycode) const; void loadConfigurationLocked(Device* device); - status_t loadVirtualKeyMapLocked(Device* device); + bool loadVirtualKeyMapLocked(Device* device); status_t loadKeyMapLocked(Device* device); bool isExternalDeviceLocked(Device* device); |