Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3e78decd authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

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
parent d008ac8d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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;
+20 −35
Original line number Diff line number Diff line
@@ -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();
        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.");
            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;
            }
        return nullptr;
    }
        delete tokenizer;

    Parser parser(map.get(), tokenizer.get());
    status = parser.parse();
    if (status != OK) {
        return nullptr;
    }
    return status;

    return map;
}


+6 −6
Original line number Diff line number Diff line
@@ -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) {
+2 −2
Original line number Diff line number Diff line
@@ -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);