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

Commit 353bf1f9 authored by Mark Salyzyn's avatar Mark Salyzyn
Browse files

init: switch from /dev/keychord to /dev/input/

Replace deprecated /dev/keychord driver with /dev/input/ interface.
Will restrict which nodes are active and relevant, and try to mask
out any unreferenced inputs with EVIOCSMASK if available.

Test: manual, boot, check registered chord works
Bug: 64114943
Change-Id: I2bbf84a6e472d720f02282e10d56795b75ac62d1
parent 8ae7375f
Loading
Loading
Loading
Loading
+3 −3
Original line number Original line Diff line number Diff line
@@ -354,8 +354,8 @@ static Result<Success> wait_for_coldboot_done_action(const BuiltinArguments& arg
    return Success();
    return Success();
}
}


static Result<Success> keychord_init_action(const BuiltinArguments& args) {
static Result<Success> KeychordInitAction(const BuiltinArguments& args) {
    keychord_init();
    KeychordInit();
    return Success();
    return Success();
}
}


@@ -772,7 +772,7 @@ int main(int argc, char** argv) {
    am.QueueBuiltinAction(MixHwrngIntoLinuxRngAction, "MixHwrngIntoLinuxRng");
    am.QueueBuiltinAction(MixHwrngIntoLinuxRngAction, "MixHwrngIntoLinuxRng");
    am.QueueBuiltinAction(SetMmapRndBitsAction, "SetMmapRndBits");
    am.QueueBuiltinAction(SetMmapRndBitsAction, "SetMmapRndBits");
    am.QueueBuiltinAction(SetKptrRestrictAction, "SetKptrRestrict");
    am.QueueBuiltinAction(SetKptrRestrictAction, "SetKptrRestrict");
    am.QueueBuiltinAction(keychord_init_action, "keychord_init");
    am.QueueBuiltinAction(KeychordInitAction, "KeychordInit");
    am.QueueBuiltinAction(console_init_action, "console_init");
    am.QueueBuiltinAction(console_init_action, "console_init");


    // Trigger all the boot actions to get us started.
    // Trigger all the boot actions to get us started.
+191 −55
Original line number Original line Diff line number Diff line
@@ -16,13 +16,20 @@


#include "keychords.h"
#include "keychords.h"


#include <dirent.h>
#include <fcntl.h>
#include <fcntl.h>
#include <stdlib.h>
#include <linux/input.h>
#include <sys/stat.h>
#include <sys/cdefs.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/types.h>
#include <linux/keychord.h>
#include <unistd.h>
#include <unistd.h>


#include <algorithm>
#include <functional>
#include <memory>
#include <string>
#include <vector>

#include <android-base/logging.h>
#include <android-base/logging.h>
#include <android-base/properties.h>
#include <android-base/properties.h>


@@ -31,51 +38,87 @@
namespace android {
namespace android {
namespace init {
namespace init {


static struct input_keychord *keychords = 0;
namespace {
static int keychords_count = 0;

static int keychords_length = 0;
int keychords_count;
static int keychord_fd = -1;


struct KeychordEntry {
void add_service_keycodes(Service* svc)
    const std::vector<int> keycodes;
{
    bool notified;
    struct input_keychord *keychord;
    int id;
    size_t i, size;


    KeychordEntry(const std::vector<int>& keycodes, int id)
    if (!svc->keycodes().empty()) {
        : keycodes(keycodes), notified(false), id(id) {}
        /* add a new keychord to the list */
};
        size = sizeof(*keychord) + svc->keycodes().size() * sizeof(keychord->keycodes[0]);

        keychords = (input_keychord*) realloc(keychords, keychords_length + size);
std::vector<KeychordEntry> keychord_entries;
        if (!keychords) {

            PLOG(ERROR) << "could not allocate keychords";
// Bit management
            keychords_length = 0;
class KeychordMask {
            keychords_count = 0;
  private:
            return;
    typedef unsigned int mask_t;
    std::vector<mask_t> bits;
    static constexpr size_t bits_per_byte = 8;

  public:
    explicit KeychordMask(size_t bit = 0) : bits((bit + sizeof(mask_t) - 1) / sizeof(mask_t), 0) {}

    void SetBit(size_t bit, bool value = true) {
        auto idx = bit / (bits_per_byte * sizeof(mask_t));
        if (idx >= bits.size()) return;
        if (value) {
            bits[idx] |= mask_t(1) << (bit % (bits_per_byte * sizeof(mask_t)));
        } else {
            bits[idx] &= ~(mask_t(1) << (bit % (bits_per_byte * sizeof(mask_t))));
        }
    }
    }


        keychord = (struct input_keychord *)((char *)keychords + keychords_length);
    bool GetBit(size_t bit) const {
        keychord->version = KEYCHORD_VERSION;
        auto idx = bit / (bits_per_byte * sizeof(mask_t));
        keychord->id = keychords_count + 1;
        return bits[idx] & (mask_t(1) << (bit % (bits_per_byte * sizeof(mask_t))));
        keychord->count = svc->keycodes().size();
    }
        svc->set_keychord_id(keychord->id);


        for (i = 0; i < svc->keycodes().size(); i++) {
    size_t bytesize() const { return bits.size() * sizeof(mask_t); }
            keychord->keycodes[i] = svc->keycodes()[i];
    void* data() { return bits.data(); }
    size_t size() const { return bits.size() * sizeof(mask_t) * bits_per_byte; }
    void resize(size_t bit) {
        auto idx = bit / (bits_per_byte * sizeof(mask_t));
        if (idx >= bits.size()) {
            bits.resize(idx + 1, 0);
        }
        }
        keychords_count++;
        keychords_length += size;
    }
    }

    operator bool() const {
        for (size_t i = 0; i < bits.size(); ++i) {
            if (bits[i]) return true;
        }
        return false;
    }
    }


static void handle_keychord() {
    KeychordMask operator&(const KeychordMask& rval) const {
    int ret;
        auto len = std::min(bits.size(), rval.bits.size());
    __u16 id;
        KeychordMask ret;
        ret.bits.resize(len);
        for (size_t i = 0; i < len; ++i) {
            ret.bits[i] = bits[i] & rval.bits[i];
        }
        return ret;
    }


    ret = read(keychord_fd, &id, sizeof(id));
    void operator|=(const KeychordMask& rval) {
    if (ret != sizeof(id)) {
        size_t len = rval.bits.size();
        PLOG(ERROR) << "could not read keychord id";
        bits.resize(len);
        return;
        for (size_t i = 0; i < len; ++i) {
            bits[i] |= rval.bits[i];
        }
    }
    }
};


KeychordMask keychord_current;

constexpr char kDevicePath[] = "/dev/input";

void HandleKeychord(int id) {
    // Only handle keychords if adb is enabled.
    // Only handle keychords if adb is enabled.
    std::string adb_enabled = android::base::GetProperty("init.svc.adbd", "");
    std::string adb_enabled = android::base::GetProperty("init.svc.adbd", "");
    if (adb_enabled == "running") {
    if (adb_enabled == "running") {
@@ -94,32 +137,125 @@ static void handle_keychord() {
    }
    }
}
}


void keychord_init() {
void KeychordLambdaCheck() {
    for (const auto& service : ServiceList::GetInstance()) {
    for (auto& e : keychord_entries) {
        add_service_keycodes(service.get());
        bool found = true;
        for (auto& code : e.keycodes) {
            if (!keychord_current.GetBit(code)) {
                e.notified = false;
                found = false;
                break;
            }
        }
        if (!found) continue;
        if (e.notified) continue;
        e.notified = true;
        HandleKeychord(e.id);
    }
}
}


    // Nothing to do if no services require keychords.
void KeychordLambdaHandler(int fd) {
    if (!keychords) {
    input_event event;
        return;
    auto res = TEMP_FAILURE_RETRY(::read(fd, &event, sizeof(event)));
    if ((res != sizeof(event)) || (event.type != EV_KEY)) return;
    keychord_current.SetBit(event.code, event.value);
    KeychordLambdaCheck();
}

bool KeychordGeteventEnable(int fd) {
    static bool EviocsmaskSupported = true;

    // Make sure it is an event channel, should pass this ioctl call
    int version;
    if (::ioctl(fd, EVIOCGVERSION, &version)) return false;

    if (EviocsmaskSupported) {
        KeychordMask mask(EV_KEY);
        mask.SetBit(EV_KEY);
        input_mask msg = {};
        msg.type = EV_SYN;
        msg.codes_size = mask.bytesize();
        msg.codes_ptr = reinterpret_cast<uintptr_t>(mask.data());
        if (::ioctl(fd, EVIOCSMASK, &msg) == -1) {
            PLOG(WARNING) << "EVIOCSMASK not supported";
            EviocsmaskSupported = false;
        }
    }

    KeychordMask mask;
    for (auto& e : keychord_entries) {
        for (auto& code : e.keycodes) {
            mask.resize(code);
            mask.SetBit(code);
        }
    }

    keychord_current.resize(mask.size());
    KeychordMask available(mask.size());
    auto res = ::ioctl(fd, EVIOCGBIT(EV_KEY, available.bytesize()), available.data());
    if (res == -1) return false;
    if (!(available & mask)) return false;

    if (EviocsmaskSupported) {
        input_mask msg = {};
        msg.type = EV_KEY;
        msg.codes_size = mask.bytesize();
        msg.codes_ptr = reinterpret_cast<uintptr_t>(mask.data());
        ::ioctl(fd, EVIOCSMASK, &msg);
    }
    }


    keychord_fd = TEMP_FAILURE_RETRY(open("/dev/keychord", O_RDWR | O_CLOEXEC));
    KeychordMask set(mask.size());
    if (keychord_fd == -1) {
    res = ::ioctl(fd, EVIOCGKEY(res), set.data());
        PLOG(ERROR) << "could not open /dev/keychord";
    if (res > 0) {
        keychord_current |= mask & available & set;
        KeychordLambdaCheck();
    }
    register_epoll_handler(fd, [fd]() { KeychordLambdaHandler(fd); });
    return true;
}

void GeteventOpenDevice(const std::string& device) {
    auto fd = TEMP_FAILURE_RETRY(::open(device.c_str(), O_RDWR | O_CLOEXEC));
    if (fd == -1) {
        PLOG(ERROR) << "Can not open " << device;
        return;
        return;
    }
    }
    if (!KeychordGeteventEnable(fd)) {
        ::close(fd);
    }
}

void GeteventOpenDevice() {
    std::unique_ptr<DIR, decltype(&closedir)> device(opendir(kDevicePath), closedir);
    if (!device) return;

    dirent* entry;
    while ((entry = readdir(device.get()))) {
        if (entry->d_name[0] == '.') continue;
        std::string devname(kDevicePath);
        devname += '/';
        devname += entry->d_name;
        GeteventOpenDevice(devname);
    }
}


    int ret = write(keychord_fd, keychords, keychords_length);
void AddServiceKeycodes(Service* svc) {
    if (ret != keychords_length) {
    if (svc->keycodes().empty()) return;
        PLOG(ERROR) << "could not configure /dev/keychord " << ret;
    for (auto& code : svc->keycodes()) {
        close(keychord_fd);
        if ((code < 0) || (code >= KEY_MAX)) return;
    }
    ++keychords_count;
    keychord_entries.emplace_back(KeychordEntry(svc->keycodes(), keychords_count));
    svc->set_keychord_id(keychords_count);
}
}


    free(keychords);
}  // namespace
    keychords = nullptr;


    register_epoll_handler(keychord_fd, handle_keychord);
void KeychordInit() {
    for (const auto& service : ServiceList::GetInstance()) {
        AddServiceKeycodes(service.get());
    }
    if (keychords_count) GeteventOpenDevice();
}
}


}  // namespace init
}  // namespace init
+1 −2
Original line number Original line Diff line number Diff line
@@ -22,8 +22,7 @@
namespace android {
namespace android {
namespace init {
namespace init {


void add_service_keycodes(Service* svc);
void KeychordInit();
void keychord_init();


}  // namespace init
}  // namespace init
}  // namespace android
}  // namespace android