diff options
author | Roland Reichwein <mail@reichwein.it> | 2025-01-06 00:16:49 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2025-01-06 00:16:49 +0100 |
commit | c072b661564e1ce3f0f01f1b6ddba0266fbb47b6 (patch) | |
tree | 84f44481827db46618911d1298a2fb60292618a2 /Touchpad.cpp | |
parent | b2f0cb564a296653c83b0a64c0f9a3de0a2f0d5e (diff) |
Fixed missing Touchpad
Diffstat (limited to 'Touchpad.cpp')
-rw-r--r-- | Touchpad.cpp | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/Touchpad.cpp b/Touchpad.cpp new file mode 100644 index 0000000..073fbde --- /dev/null +++ b/Touchpad.cpp @@ -0,0 +1,74 @@ +#include "Touchpad.h" + +#include <fmt/format.h> +#include <sys/poll.h> +#include <fcntl.h> + +#include "log.h" + +Touchpad::Touchpad(): m_valid{} +{ + 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() +{ + 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) + return true; + else + log_cout << "Error: Touchpad::event_available()" << std::endl; + return false; +} + +input_event Touchpad::get_event() +{ + 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; +} + +bool Touchpad::event_is_button2(const input_event& ev) +{ + return ev.type == EV_KEY && ev.code == BTN_RIGHT; +} |