#include "Touchpad.h" #include #include #include #include "log.h" Touchpad::Touchpad(): m_valid{}, m_ev{} { m_fd = open("/dev/input/event0", O_RDONLY | O_NONBLOCK); int rc = libevdev_new_from_fd(m_fd, &m_dev); if (rc < 0) { log_cout << fmt::format("Failed to init libevdev ({})\n", strerror(-rc)); return; } log_cout << fmt::format("Input device name: \"{}\"\n", libevdev_get_name(m_dev)); log_cout << fmt::format("Input device ID: bus {:x} vendor {:x} product {:x}\n", libevdev_get_id_bustype(m_dev), libevdev_get_id_vendor(m_dev), libevdev_get_id_product(m_dev)); if (!libevdev_has_event_type(m_dev, EV_REL) || !libevdev_has_event_code(m_dev, EV_KEY, BTN_LEFT) || !libevdev_has_event_code(m_dev, EV_KEY, BTN_RIGHT)) { log_cout << fmt::format("Not the correct device, ignoring.\n"); } else { m_valid = true; } } bool Touchpad::is_valid() { return m_valid; } bool Touchpad::event_available() { if (!m_valid) { return false; } if (m_ev.has_value()) { return true; } input_event ev{}; int rc = libevdev_next_event(m_dev, LIBEVDEV_READ_FLAG_NORMAL, &ev); if (rc != 0) { return false; } log_cout << fmt::format("Event: {} {} {}\n", libevdev_event_type_get_name(ev.type), libevdev_event_code_get_name(ev.type, ev.code), ev.value); m_ev = ev; return true; } input_event Touchpad::get_event() { if (m_ev.has_value()) { input_event result = *m_ev; m_ev.reset(); return result; } input_event ev{}; int rc = libevdev_next_event(m_dev, LIBEVDEV_READ_FLAG_NORMAL, &ev); if (rc != 0) { throw std::runtime_error("Touchpad::get_event() couldn't read event"); } log_cout << fmt::format("Event: {} {} {}\n", libevdev_event_type_get_name(ev.type), libevdev_event_code_get_name(ev.type, ev.code), ev.value); return ev; } bool Touchpad::event_is_button1(const input_event& ev) { return ev.type == EV_KEY && ev.code == BTN_LEFT && ev.value == 0; // Press down } bool Touchpad::event_is_button2(const input_event& ev) { return ev.type == EV_KEY && ev.code == BTN_RIGHT && ev.value == 0; // Press down }