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

Commit 2f13e022 authored by Prabir Pradhan's avatar Prabir Pradhan
Browse files

InputEventLabels: Support lookup of evdev event codes by label

Bug: 293327283
Test: atest CtsInputTestCases
Change-Id: I846e1dc1eb450ea92d35ac7e403b06e2a029f25b
parent 1030142f
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -69,6 +69,12 @@ public:

    static EvdevEventLabel getLinuxEvdevLabel(int32_t type, int32_t code, int32_t value);

    static std::optional<int> getLinuxEvdevEventTypeByLabel(const char* label);

    static std::optional<int> getLinuxEvdevEventCodeByLabel(int32_t type, const char* label);

    static std::optional<int> getLinuxEvdevInputPropByLabel(const char* label);

private:
    InputEventLookup();

+22 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@

#include <linux/input-event-codes.h>
#include <linux/input.h>
#include <strings.h>

#define DEFINE_KEYCODE(key) { #key, AKEYCODE_##key }
#define DEFINE_AXIS(axis) { #axis, AMOTION_EVENT_AXIS_##axis }
@@ -523,6 +524,14 @@ std::string getLabel(const label* labels, int value) {
    return labels->name != nullptr ? labels->name : std::to_string(value);
}

std::optional<int> getValue(const label* labels, const char* searchLabel) {
    if (labels == nullptr) return {};
    while (labels->name != nullptr && ::strcasecmp(labels->name, searchLabel) != 0) {
        labels++;
    }
    return labels->name != nullptr ? std::make_optional(labels->value) : std::nullopt;
}

const label* getCodeLabelsForType(int32_t type) {
    switch (type) {
        case EV_SYN:
@@ -572,4 +581,17 @@ EvdevEventLabel InputEventLookup::getLinuxEvdevLabel(int32_t type, int32_t code,
    };
}

std::optional<int> InputEventLookup::getLinuxEvdevEventTypeByLabel(const char* label) {
    return getValue(ev_labels, label);
}

std::optional<int> InputEventLookup::getLinuxEvdevEventCodeByLabel(int32_t type,
                                                                   const char* label) {
    return getValue(getCodeLabelsForType(type), label);
}

std::optional<int> InputEventLookup::getLinuxEvdevInputPropByLabel(const char* label) {
    return getValue(input_prop_labels, label);
}

} // namespace android