1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
#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()
{
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)
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 && 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
}
|