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

Commit 1abffbd1 authored by Chris Ye's avatar Chris Ye
Browse files

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
parent 6ab8a005
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -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();

+1 −1
Original line number Diff line number Diff line
@@ -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;
+42 −19
Original line number Diff line number Diff line
@@ -49,18 +49,43 @@ 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());
    } else {
        sp<KeyLayoutMap> map = new KeyLayoutMap();
        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.");
            status = NO_MEMORY;
        return Errorf("Error allocating key layout map.");
    } else {
#if DEBUG_PARSER_PERFORMANCE
        nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
@@ -74,12 +99,10 @@ status_t KeyLayoutMap::load(const std::string& filename, sp<KeyLayoutMap>* outMa
              elapsedTime / 1000000.0);
#endif
        if (!status) {
                *outMap = map;
            }
            return std::move(map);
        }
        delete tokenizer;
    }
    return status;
    return Errorf("Load KeyLayoutMap failed {}.", status);
}

status_t KeyLayoutMap::mapKey(int32_t scanCode, int32_t usageCode,
+4 −4
Original line number Diff line number Diff line
@@ -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;
}
+1 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ cc_library_shared {

    header_libs: ["jni_headers"],
    shared_libs: [
        "libbase",
        "libbinder",
        "libcrypto",
        "libcutils",