diff options
author | Roland Reichwein <mail@reichwein.it> | 2025-01-06 11:41:05 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2025-01-06 11:41:05 +0100 |
commit | aa210ed8a27387882fc7fd0dcd7cf961a9adc88f (patch) | |
tree | 9a0a3ac1cd0b209161e81c6f6eacc12fdd87e351 | |
parent | 8609722958b89ce71e8336e3254fc5a88912a0e8 (diff) |
Fix touchpad event input reading
-rw-r--r-- | Touchpad.cpp | 35 | ||||
-rw-r--r-- | Touchpad.h | 4 |
2 files changed, 28 insertions, 11 deletions
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) { @@ -2,6 +2,8 @@ #include <libevdev/libevdev.h> +#include <optional> + class Touchpad { public: @@ -20,5 +22,7 @@ private: libevdev* m_dev = nullptr; int m_fd; bool m_valid; + + std::optional<input_event> m_ev; }; |