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

Commit 3a1e4468 authored by Chris Ye's avatar Chris Ye
Browse files

Move KeyCharacterMap from RefBase to shared_ptr.

Move KeyCharacterMap from RefBase and make it shared_ptr in EventHub
device when loaded from file.
A shared_ptr of KeyCharacterMap is returned by EventHub getKeyCharacterMap
to be shared in InputdeviceInfo.
Remove extra KeyCharacterMap in EventHub to save memory sapce.

Bug: 160010896
Test: atest inputflinger, atest libinput_tests

Change-Id: Ibb317ea0684e6af6e7a6d808f816fc4a05c7b421
parent 1abffbd1
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -108,11 +108,11 @@ public:
    inline void setKeyboardType(int32_t keyboardType) { mKeyboardType = keyboardType; }
    inline int32_t getKeyboardType() const { return mKeyboardType; }

    inline void setKeyCharacterMap(const sp<KeyCharacterMap>& value) {
    inline void setKeyCharacterMap(const std::shared_ptr<KeyCharacterMap> value) {
        mKeyCharacterMap = value;
    }

    inline sp<KeyCharacterMap> getKeyCharacterMap() const {
    inline const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap() const {
        return mKeyCharacterMap;
    }

@@ -136,7 +136,7 @@ private:
    bool mHasMic;
    uint32_t mSources;
    int32_t mKeyboardType;
    sp<KeyCharacterMap> mKeyCharacterMap;
    std::shared_ptr<KeyCharacterMap> mKeyCharacterMap;
    bool mHasVibrator;
    bool mHasButtonUnderPad;

+15 −16
Original line number Diff line number Diff line
@@ -23,12 +23,12 @@
#include <binder/IBinder.h>
#endif

#include <android-base/result.h>
#include <input/Input.h>
#include <utils/Errors.h>
#include <utils/KeyedVector.h>
#include <utils/Tokenizer.h>
#include <utils/Unicode.h>
#include <utils/RefBase.h>

// Maximum number of keys supported by KeyCharacterMaps
#define MAX_KEYS 8192
@@ -42,7 +42,7 @@ namespace android {
 *
 * This object is immutable after it has been loaded.
 */
class KeyCharacterMap : public RefBase {
class KeyCharacterMap {
public:
    enum KeyboardType {
        KEYBOARD_TYPE_UNKNOWN = 0,
@@ -74,18 +74,18 @@ public:
    };

    /* Loads a key character map from a file. */
    static status_t load(const std::string& filename, Format format, sp<KeyCharacterMap>* outMap);
    static base::Result<std::shared_ptr<KeyCharacterMap>> load(const std::string& filename,
                                                               Format format);

    /* Loads a key character map from its string contents. */
    static status_t loadContents(const std::string& filename,
            const char* contents, Format format, sp<KeyCharacterMap>* outMap);
    static base::Result<std::shared_ptr<KeyCharacterMap>> loadContents(const std::string& filename,
                                                                       const char* contents,
                                                                       Format format);

    /* Combines a base key character map and an overlay. */
    static sp<KeyCharacterMap> combine(const sp<KeyCharacterMap>& base,
            const sp<KeyCharacterMap>& overlay);
    const std::string getLoadFileName() const;

    /* Returns an empty key character map. */
    static sp<KeyCharacterMap> empty();
    /* Combines this key character map with an overlay. */
    void combine(const KeyCharacterMap& overlay);

    /* Gets the keyboard type. */
    int32_t getKeyboardType() const;
@@ -136,13 +136,14 @@ public:

#ifdef __ANDROID__
    /* Reads a key map from a parcel. */
    static sp<KeyCharacterMap> readFromParcel(Parcel* parcel);
    static std::shared_ptr<KeyCharacterMap> readFromParcel(Parcel* parcel);

    /* Writes a key map to a parcel. */
    void writeToParcel(Parcel* parcel) const;
#endif

protected:
    KeyCharacterMap(const KeyCharacterMap& other);

    virtual ~KeyCharacterMap();

private:
@@ -224,16 +225,14 @@ private:
        status_t parseCharacterLiteral(char16_t* outCharacter);
    };

    static sp<KeyCharacterMap> sEmpty;

    KeyedVector<int32_t, Key*> mKeys;
    int mType;
    std::string mLoadFileName;

    KeyedVector<int32_t, int32_t> mKeysByScanCode;
    KeyedVector<int32_t, int32_t> mKeysByUsageCode;

    KeyCharacterMap();
    KeyCharacterMap(const KeyCharacterMap& other);

    bool getKey(int32_t keyCode, const Key** outKey) const;
    bool getKeyBehavior(int32_t keyCode, int32_t metaState,
@@ -242,7 +241,7 @@ private:

    bool findKey(char16_t ch, int32_t* outKeyCode, int32_t* outMetaState) const;

    static status_t load(Tokenizer* tokenizer, Format format, sp<KeyCharacterMap>* outMap);
    static base::Result<std::shared_ptr<KeyCharacterMap>> load(Tokenizer* tokenizer, Format format);

    static void addKey(Vector<KeyEvent>& outEvents,
            int32_t deviceId, int32_t keyCode, int32_t metaState, bool down, nsecs_t time);
+1 −1
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ public:
    std::shared_ptr<KeyLayoutMap> keyLayoutMap;

    std::string keyCharacterMapFile;
    sp<KeyCharacterMap> keyCharacterMap;
    std::shared_ptr<KeyCharacterMap> keyCharacterMap;

    KeyMap();
    ~KeyMap();
+14 −8
Original line number Diff line number Diff line
@@ -153,14 +153,20 @@ InputDeviceInfo::InputDeviceInfo() {
    initialize(-1, 0, -1, InputDeviceIdentifier(), "", false, false);
}

InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other) :
        mId(other.mId), mGeneration(other.mGeneration), mControllerNumber(other.mControllerNumber),
        mIdentifier(other.mIdentifier), mAlias(other.mAlias), mIsExternal(other.mIsExternal),
        mHasMic(other.mHasMic), mSources(other.mSources),
        mKeyboardType(other.mKeyboardType), mKeyCharacterMap(other.mKeyCharacterMap),
        mHasVibrator(other.mHasVibrator), mHasButtonUnderPad(other.mHasButtonUnderPad),
        mMotionRanges(other.mMotionRanges) {
}
InputDeviceInfo::InputDeviceInfo(const InputDeviceInfo& other)
      : mId(other.mId),
        mGeneration(other.mGeneration),
        mControllerNumber(other.mControllerNumber),
        mIdentifier(other.mIdentifier),
        mAlias(other.mAlias),
        mIsExternal(other.mIsExternal),
        mHasMic(other.mHasMic),
        mSources(other.mSources),
        mKeyboardType(other.mKeyboardType),
        mKeyCharacterMap(other.mKeyCharacterMap),
        mHasVibrator(other.mHasVibrator),
        mHasButtonUnderPad(other.mHasButtonUnderPad),
        mMotionRanges(other.mMotionRanges) {}

InputDeviceInfo::~InputDeviceInfo() {
}
+66 −68
Original line number Diff line number Diff line
@@ -85,14 +85,13 @@ static String8 toString(const char16_t* chars, size_t numChars) {

// --- KeyCharacterMap ---

sp<KeyCharacterMap> KeyCharacterMap::sEmpty = new KeyCharacterMap();

KeyCharacterMap::KeyCharacterMap() :
    mType(KEYBOARD_TYPE_UNKNOWN) {
}

KeyCharacterMap::KeyCharacterMap(const KeyCharacterMap& other) :
    RefBase(), mType(other.mType), mKeysByScanCode(other.mKeysByScanCode),
KeyCharacterMap::KeyCharacterMap(const KeyCharacterMap& other)
      : mType(other.mType),
        mKeysByScanCode(other.mKeysByScanCode),
        mKeysByUsageCode(other.mKeysByUsageCode) {
    for (size_t i = 0; i < other.mKeys.size(); i++) {
        mKeys.add(other.mKeys.keyAt(i), new Key(*other.mKeys.valueAt(i)));
@@ -106,44 +105,45 @@ KeyCharacterMap::~KeyCharacterMap() {
    }
}

status_t KeyCharacterMap::load(const std::string& filename,
        Format format, sp<KeyCharacterMap>* outMap) {
    outMap->clear();

base::Result<std::shared_ptr<KeyCharacterMap>> KeyCharacterMap::load(const std::string& filename,
                                                                     Format format) {
    Tokenizer* tokenizer;
    status_t status = Tokenizer::open(String8(filename.c_str()), &tokenizer);
    if (status) {
        ALOGE("Error %d opening key character map file %s.", status, filename.c_str());
    } else {
        status = load(tokenizer, format, outMap);
        delete tokenizer;
        return Errorf("Error {} opening key character map file {}.", status, filename.c_str());
    }
    return status;
    std::unique_ptr<Tokenizer> t(tokenizer);
    auto ret = load(t.get(), format);
    if (ret) {
        (*ret)->mLoadFileName = filename;
    }
    return ret;
}

status_t KeyCharacterMap::loadContents(const std::string& filename, const char* contents,
        Format format, sp<KeyCharacterMap>* outMap) {
    outMap->clear();

base::Result<std::shared_ptr<KeyCharacterMap>> KeyCharacterMap::loadContents(
        const std::string& filename, const char* contents, Format format) {
    Tokenizer* tokenizer;
    status_t status = Tokenizer::fromContents(String8(filename.c_str()), contents, &tokenizer);
    if (status) {
        ALOGE("Error %d opening key character map.", status);
    } else {
        status = load(tokenizer, format, outMap);
        delete tokenizer;
        return Errorf("Error {} opening key character map.", status);
    }
    return status;
    std::unique_ptr<Tokenizer> t(tokenizer);
    auto ret = load(t.get(), format);
    if (ret) {
        (*ret)->mLoadFileName = filename;
    }
    return ret;
}

status_t KeyCharacterMap::load(Tokenizer* tokenizer,
        Format format, sp<KeyCharacterMap>* outMap) {
base::Result<std::shared_ptr<KeyCharacterMap>> KeyCharacterMap::load(Tokenizer* tokenizer,
                                                                     Format format) {
    status_t status = OK;
    sp<KeyCharacterMap> map = new KeyCharacterMap();
    std::shared_ptr<KeyCharacterMap> map = std::shared_ptr<KeyCharacterMap>(new KeyCharacterMap());
    if (!map.get()) {
        ALOGE("Error allocating key character map.");
        status = NO_MEMORY;
    } else {
        return Errorf("Error allocating key character map.");
    }
#if DEBUG_PARSER_PERFORMANCE
    nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
#endif
@@ -152,58 +152,48 @@ status_t KeyCharacterMap::load(Tokenizer* tokenizer,
#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);
          tokenizer->getFilename().string(), tokenizer->getLineNumber(), elapsedTime / 1000000.0);
#endif
        if (!status) {
            *outMap = map;
        }
    }
    return status;
    if (status == OK) {
        return map;
    }

sp<KeyCharacterMap> KeyCharacterMap::combine(const sp<KeyCharacterMap>& base,
        const sp<KeyCharacterMap>& overlay) {
    if (overlay == nullptr) {
        return base;
    }
    if (base == nullptr) {
        return overlay;
    return Errorf("Load KeyCharacterMap failed {}.", status);
}

    sp<KeyCharacterMap> map = new KeyCharacterMap(*base.get());
    for (size_t i = 0; i < overlay->mKeys.size(); i++) {
        int32_t keyCode = overlay->mKeys.keyAt(i);
        Key* key = overlay->mKeys.valueAt(i);
        ssize_t oldIndex = map->mKeys.indexOfKey(keyCode);
void KeyCharacterMap::combine(const KeyCharacterMap& overlay) {
    for (size_t i = 0; i < overlay.mKeys.size(); i++) {
        int32_t keyCode = overlay.mKeys.keyAt(i);
        Key* key = overlay.mKeys.valueAt(i);
        ssize_t oldIndex = mKeys.indexOfKey(keyCode);
        if (oldIndex >= 0) {
            delete map->mKeys.valueAt(oldIndex);
            map->mKeys.editValueAt(oldIndex) = new Key(*key);
            delete mKeys.valueAt(oldIndex);
            mKeys.editValueAt(oldIndex) = new Key(*key);
        } else {
            map->mKeys.add(keyCode, new Key(*key));
            mKeys.add(keyCode, new Key(*key));
        }
    }

    for (size_t i = 0; i < overlay->mKeysByScanCode.size(); i++) {
        map->mKeysByScanCode.replaceValueFor(overlay->mKeysByScanCode.keyAt(i),
                overlay->mKeysByScanCode.valueAt(i));
    for (size_t i = 0; i < overlay.mKeysByScanCode.size(); i++) {
        mKeysByScanCode.replaceValueFor(overlay.mKeysByScanCode.keyAt(i),
                                        overlay.mKeysByScanCode.valueAt(i));
    }

    for (size_t i = 0; i < overlay->mKeysByUsageCode.size(); i++) {
        map->mKeysByUsageCode.replaceValueFor(overlay->mKeysByUsageCode.keyAt(i),
                overlay->mKeysByUsageCode.valueAt(i));
    }
    return map;
    for (size_t i = 0; i < overlay.mKeysByUsageCode.size(); i++) {
        mKeysByUsageCode.replaceValueFor(overlay.mKeysByUsageCode.keyAt(i),
                                         overlay.mKeysByUsageCode.valueAt(i));
    }

sp<KeyCharacterMap> KeyCharacterMap::empty() {
    return sEmpty;
    mLoadFileName = overlay.mLoadFileName;
}

int32_t KeyCharacterMap::getKeyboardType() const {
    return mType;
}

const std::string KeyCharacterMap::getLoadFileName() const {
    return mLoadFileName;
}

char16_t KeyCharacterMap::getDisplayLabel(int32_t keyCode) const {
    char16_t result = 0;
    const Key* key;
@@ -600,8 +590,12 @@ void KeyCharacterMap::addLockedMetaKey(Vector<KeyEvent>& outEvents,
}

#ifdef __ANDROID__
sp<KeyCharacterMap> KeyCharacterMap::readFromParcel(Parcel* parcel) {
    sp<KeyCharacterMap> map = new KeyCharacterMap();
std::shared_ptr<KeyCharacterMap> KeyCharacterMap::readFromParcel(Parcel* parcel) {
    if (parcel == nullptr) {
        ALOGE("%s: Null parcel", __func__);
        return nullptr;
    }
    std::shared_ptr<KeyCharacterMap> map = std::shared_ptr<KeyCharacterMap>(new KeyCharacterMap());
    map->mType = parcel->readInt32();
    size_t numKeys = parcel->readInt32();
    if (parcel->errorCheck()) {
@@ -656,6 +650,10 @@ sp<KeyCharacterMap> KeyCharacterMap::readFromParcel(Parcel* parcel) {
}

void KeyCharacterMap::writeToParcel(Parcel* parcel) const {
    if (parcel == nullptr) {
        ALOGE("%s: Null parcel", __func__);
        return;
    }
    parcel->writeInt32(mType);

    size_t numKeys = mKeys.size();
Loading