From aa210ed8a27387882fc7fd0dcd7cf961a9adc88f Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Mon, 6 Jan 2025 11:41:05 +0100 Subject: Fix touchpad event input reading --- Touchpad.cpp | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'Touchpad.cpp') diff --git a/Touchpad.cpp b/Touchpad.cpp index 5fe6bf4..3989df7 100644 --- a/Touchpad.cpp +++ b/Touchpad.cpp @@ -6,7 +6,7 @@ #include "log.h" -Touchpad::Touchpad(): m_valid{} +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); @@ -39,21 +39,34 @@ bool Touchpad::event_available() if (!m_valid) { return false; } - struct pollfd fds{}; - fds.fd = m_fd; - fds.events = POLLIN; - int ret = poll(&fds, 1, 0); - if (ret == 0) - return false; - else if (ret == 1) + + if (m_ev.has_value()) { return true; - else - log_cout << "Error: Touchpad::event_available()" << std::endl; - return false; + } + + 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) { -- cgit v1.2.3