Skip to content

Commit 13290f6

Browse files
author
okay
committed
[rmkit] refactor input reopening on ENODEV
1 parent d2e0387 commit 13290f6

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

src/rmkit/input/input.cpy

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ namespace input:
2222
extern int ipc_fd[2] = { -1, -1 };
2323
extern bool CRASH_ON_BAD_DEVICE = (getenv("RMKIT_CRASH_ON_BAD_DEVICE") != NULL)
2424

25+
class IInputClass:
26+
public:
27+
int fd = 0
28+
bool reopen = false
29+
2530
template<class T, class EV>
26-
class InputClass:
31+
class InputClass : public IInputClass:
2732
public:
28-
int fd
2933
input_event ev_data[64]
3034
T prev_ev, event
3135
vector<T> events
@@ -60,6 +64,8 @@ namespace input:
6064
if bytes == -1:
6165
debug "ERRNO", errno, strerror(errno)
6266
if bytes < sizeof(input_event) || bytes == -1:
67+
if errno == ENODEV:
68+
self.reopen = true
6369
return bytes
6470

6571
#ifndef DEV
@@ -149,12 +155,10 @@ namespace input:
149155
return
150156

151157
void close_devices():
152-
if self.touch.fd:
153-
close(self.touch.fd)
154-
if self.wacom.fd:
155-
close(self.wacom.fd)
156-
if self.button.fd:
157-
close(self.button.fd)
158+
vector<IInputClass> fds = { self.touch, self.wacom, self.button}
159+
for auto in : fds:
160+
if in.fd > 0:
161+
close(in.fd)
158162

159163
~Input():
160164
close_devices()
@@ -261,6 +265,19 @@ namespace input:
261265
for auto fd : { self.touch.fd, self.wacom.fd, self.button.fd }:
262266
ioctl(fd, EVIOCGRAB, false)
263267

268+
void check_reopen():
269+
vector<IInputClass*> inputs = { &self.wacom, &self.touch, &self.button }
270+
needs_reopen := false
271+
for auto in : inputs:
272+
if in->reopen:
273+
needs_reopen = true
274+
break
275+
276+
if needs_reopen:
277+
self.open_devices()
278+
279+
for auto in : inputs:
280+
in->reopen = false
264281

265282
void listen_all(long timeout_ms = 0):
266283
fd_set rdfs_cp
@@ -284,17 +301,16 @@ namespace input:
284301
// and we only re-open the specific device that fail
285302
if retval > 0:
286303
if FD_ISSET(self.wacom.fd, &rdfs_cp):
287-
if self.wacom.handle_event_fd() < 0 and errno == ENODEV:
288-
self.open_devices()
304+
self.wacom.handle_event_fd()
289305
if FD_ISSET(self.touch.fd, &rdfs_cp):
290-
if self.touch.handle_event_fd() < 0 and errno == ENODEV:
291-
self.open_devices()
306+
self.touch.handle_event_fd()
292307
if FD_ISSET(self.button.fd, &rdfs_cp):
293-
if self.button.handle_event_fd() < 0 and errno == ENODEV:
294-
self.open_devices()
308+
self.button.handle_event_fd()
295309
if FD_ISSET(input::ipc_fd[0], &rdfs_cp):
296310
self.handle_ipc()
297311

312+
self.check_reopen()
313+
298314
for auto ev : self.wacom.events:
299315
self.all_motion_events.push_back(self.wacom.marshal(ev))
300316

0 commit comments

Comments
 (0)