4
4
#include < fcntl.h>
5
5
#include < functional>
6
6
#include < inputtino/result.hpp>
7
+ #include < iostream>
7
8
#include < linux/uhid.h>
8
9
#include < memory>
9
10
#include < poll.h>
@@ -50,7 +51,7 @@ static inputtino::Result<bool> uhid_write(int fd, const struct uhid_event *ev) {
50
51
class Device {
51
52
private:
52
53
Device (std::shared_ptr<std::thread> ev_thread, std::shared_ptr<ThreadState> state)
53
- : ev_thread(std::move(ev_thread)), state(std::move(state)){};
54
+ : ev_thread(std::move(ev_thread)), state(std::move(state)) {};
54
55
std::shared_ptr<std::thread> ev_thread;
55
56
std::shared_ptr<ThreadState> state;
56
57
std::shared_ptr<std::function<void (const uhid_event &ev, int fd)>> on_event;
@@ -81,7 +82,7 @@ class Device {
81
82
82
83
~Device () {
83
84
if (state) {
84
- struct uhid_event ev {};
85
+ struct uhid_event ev{};
85
86
ev.type = UHID_DESTROY;
86
87
uhid_write (state->fd , &ev);
87
88
@@ -100,6 +101,8 @@ static void set_c_str(const std::string &str, unsigned char *c_str) {
100
101
c_str[str.length ()] = 0 ;
101
102
}
102
103
104
+ constexpr int UHID_POLL_TIMEOUT = 500 ; // ms
105
+
103
106
inputtino::Result<Device> Device::create (const DeviceDefinition &definition,
104
107
const std::function<void (const uhid_event &ev, int fd)> &on_event) {
105
108
@@ -124,30 +127,28 @@ inputtino::Result<Device> Device::create(const DeviceDefinition &definition,
124
127
state->fd = fd;
125
128
state->on_event = on_event;
126
129
auto thread = std::make_shared<std::thread>([state]() {
127
- ssize_t ret;
128
- struct pollfd pfds[1 ];
129
- pfds[0 ].fd = state->fd ;
130
- pfds[0 ].events = POLLIN;
130
+ std::array<pollfd, 1 > pfds = {pollfd{.fd = state->fd , .events = POLLIN}};
131
+ int poll_rs = 0 ;
131
132
132
133
while (!state->stop_repeat_thread ) {
133
- ret = poll (pfds, 1 , - 1 );
134
- if (ret < 0 ) {
135
- fprintf (stderr, " Cannot poll for fds: %m \n " ) ;
134
+ poll_rs = poll (pfds. data (), pfds. size (), UHID_POLL_TIMEOUT );
135
+ if (poll_rs < 0 ) {
136
+ std::cerr << " Failed polling uhid fd; ret= " << strerror (errno) << std::endl ;
136
137
break ;
137
138
}
138
139
if (pfds[0 ].revents & POLLHUP) {
139
- fprintf (stderr, " Received HUP on uhid-cdev\n " ) ;
140
+ std::cerr << " HUP on uhid-cdev" << std::endl ;
140
141
break ;
141
142
}
142
143
if (pfds[0 ].revents & POLLIN) {
143
- struct uhid_event ev {};
144
- ret = read (state->fd , &ev, sizeof (ev));
144
+ struct uhid_event ev{};
145
+ auto ret = read (state->fd , &ev, sizeof (ev));
145
146
if (ret == 0 ) {
146
- fprintf (stderr, " Read HUP on uhid-cdev\n " ) ;
147
+ std::cerr << " Read HUP on uhid-cdev" << std::endl ;
147
148
} else if (ret < 0 ) {
148
- fprintf (stderr, " Cannot read uhid-cdev: %m \n " ) ;
149
+ std::cerr << " Cannot read uhid-cdev: " << strerror (errno) << std::endl ;
149
150
} else if (ret != sizeof (ev)) {
150
- fprintf (stderr, " Invalid size read from uhid-dev: %zd != %zu \n " , ret, sizeof (ev)) ;
151
+ std::cerr << " Invalid size read from uhid-dev" << ret << " != " << sizeof (ev) << std::endl ;
151
152
} else {
152
153
if (state->on_event ) {
153
154
state->on_event (ev, state->fd );
0 commit comments