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

Commit 23fcab4c authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Initialize InputEventLookup in a static function" into udc-qpr-dev

parents 61649950 ffe62933
Loading
Loading
Loading
Loading
+22 −6
Original line number Diff line number Diff line
@@ -40,7 +40,16 @@ struct EvdevEventLabel {
//   then you must add it to InputEventLabels.cpp.

class InputEventLookup {
    /**
     * This class is not purely static, but uses a singleton pattern in order to delay the
     * initialization of the maps that it contains. If it were purely static, the maps could be
     * created early, and would cause sanitizers to report memory leaks.
     */
public:
    InputEventLookup(InputEventLookup& other) = delete;

    void operator=(const InputEventLookup&) = delete;

    static std::optional<int> lookupValueByLabel(const std::unordered_map<std::string, int>& map,
                                                 const char* literal);

@@ -61,17 +70,24 @@ public:
    static EvdevEventLabel getLinuxEvdevLabel(int32_t type, int32_t code, int32_t value);

private:
    static const std::unordered_map<std::string, int> KEYCODES;
    InputEventLookup();

    static const InputEventLookup& get() {
        static InputEventLookup sLookup;
        return sLookup;
    }

    const std::unordered_map<std::string, int> KEYCODES;

    static const std::vector<InputEventLabel> KEY_NAMES;
    const std::vector<InputEventLabel> KEY_NAMES;

    static const std::unordered_map<std::string, int> AXES;
    const std::unordered_map<std::string, int> AXES;

    static const std::vector<InputEventLabel> AXES_NAMES;
    const std::vector<InputEventLabel> AXES_NAMES;

    static const std::unordered_map<std::string, int> LEDS;
    const std::unordered_map<std::string, int> LEDS;

    static const std::unordered_map<std::string, int> FLAGS;
    const std::unordered_map<std::string, int> FLAGS;
};

} // namespace android
+20 −17
Original line number Diff line number Diff line
@@ -434,17 +434,14 @@ namespace android {
// clang-format on

// --- InputEventLookup ---
const std::unordered_map<std::string, int> InputEventLookup::KEYCODES = {KEYCODES_SEQUENCE};

const std::vector<InputEventLabel> InputEventLookup::KEY_NAMES = {KEYCODES_SEQUENCE};

const std::unordered_map<std::string, int> InputEventLookup::AXES = {AXES_SEQUENCE};

const std::vector<InputEventLabel> InputEventLookup::AXES_NAMES = {AXES_SEQUENCE};

const std::unordered_map<std::string, int> InputEventLookup::LEDS = {LEDS_SEQUENCE};

const std::unordered_map<std::string, int> InputEventLookup::FLAGS = {FLAGS_SEQUENCE};
InputEventLookup::InputEventLookup()
      : KEYCODES({KEYCODES_SEQUENCE}),
        KEY_NAMES({KEYCODES_SEQUENCE}),
        AXES({AXES_SEQUENCE}),
        AXES_NAMES({AXES_SEQUENCE}),
        LEDS({LEDS_SEQUENCE}),
        FLAGS({FLAGS_SEQUENCE}) {}

std::optional<int> InputEventLookup::lookupValueByLabel(
        const std::unordered_map<std::string, int>& map, const char* literal) {
@@ -462,30 +459,36 @@ const char* InputEventLookup::lookupLabelByValue(const std::vector<InputEventLab
}

std::optional<int> InputEventLookup::getKeyCodeByLabel(const char* label) {
    return lookupValueByLabel(KEYCODES, label);
    const auto& self = get();
    return self.lookupValueByLabel(self.KEYCODES, label);
}

const char* InputEventLookup::getLabelByKeyCode(int32_t keyCode) {
    if (keyCode >= 0 && static_cast<size_t>(keyCode) < KEYCODES.size()) {
        return lookupLabelByValue(KEY_NAMES, keyCode);
    const auto& self = get();
    if (keyCode >= 0 && static_cast<size_t>(keyCode) < self.KEYCODES.size()) {
        return get().lookupLabelByValue(self.KEY_NAMES, keyCode);
    }
    return nullptr;
}

std::optional<int> InputEventLookup::getKeyFlagByLabel(const char* label) {
    return lookupValueByLabel(FLAGS, label);
    const auto& self = get();
    return lookupValueByLabel(self.FLAGS, label);
}

std::optional<int> InputEventLookup::getAxisByLabel(const char* label) {
    return lookupValueByLabel(AXES, label);
    const auto& self = get();
    return lookupValueByLabel(self.AXES, label);
}

const char* InputEventLookup::getAxisLabel(int32_t axisId) {
    return lookupLabelByValue(AXES_NAMES, axisId);
    const auto& self = get();
    return lookupLabelByValue(self.AXES_NAMES, axisId);
}

std::optional<int> InputEventLookup::getLedByLabel(const char* label) {
    return lookupValueByLabel(LEDS, label);
    const auto& self = get();
    return lookupValueByLabel(self.LEDS, label);
}

namespace {