Skip to content

Commit 3e730fe

Browse files
author
okay
committed
[rmkit] re-open devices when we detect an invalid file descriptor
1 parent 3197799 commit 3e730fe

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/rmkit/input/input.cpy

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ namespace input:
5555

5656

5757

58-
void handle_event_fd():
58+
int handle_event_fd():
5959
int bytes = read(fd, ev_data, sizeof(input_event) * 64);
60+
if bytes == -1:
61+
debug "ERRNO", errno, strerror(errno)
6062
if bytes < sizeof(input_event) || bytes == -1:
61-
return
63+
return bytes
6264

6365
#ifndef DEV
6466
// in DEV mode we allow event coalescing between calls to read() for
@@ -88,6 +90,8 @@ namespace input:
8890
if !syn_dropped:
8991
event.update(ev_data[i])
9092

93+
return 0
94+
9195
class Input:
9296
private:
9397

@@ -104,8 +108,11 @@ namespace input:
104108
vector<SynKeyEvent> all_key_events
105109

106110
Input():
107-
FD_ZERO(&rdfs)
111+
open_devices()
108112

113+
void open_devices():
114+
close_devices()
115+
FD_ZERO(&rdfs)
109116
// dev only
110117
// used by remarkable
111118
#ifdef REMARKABLE
@@ -141,11 +148,16 @@ namespace input:
141148
self.has_stylus = supports_stylus()
142149
return
143150

151+
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)
144158

145159
~Input():
146-
close(self.touch.fd)
147-
close(self.wacom.fd)
148-
close(self.button.fd)
160+
close_devices()
149161

150162
void open_device(string fname):
151163
fd := open(fname.c_str(), O_RDWR)
@@ -267,13 +279,17 @@ namespace input:
267279
else:
268280
retval = select(max_fd, &rdfs_cp, NULL, NULL, NULL)
269281

282+
270283
if retval > 0:
271284
if FD_ISSET(self.wacom.fd, &rdfs_cp):
272-
self.wacom.handle_event_fd()
285+
if self.wacom.handle_event_fd() < 0:
286+
self.open_devices()
273287
if FD_ISSET(self.touch.fd, &rdfs_cp):
274-
self.touch.handle_event_fd()
288+
if self.touch.handle_event_fd() < 0:
289+
self.open_devices()
275290
if FD_ISSET(self.button.fd, &rdfs_cp):
276-
self.button.handle_event_fd()
291+
if self.button.handle_event_fd() < 0:
292+
self.open_devices()
277293
if FD_ISSET(input::ipc_fd[0], &rdfs_cp):
278294
self.handle_ipc()
279295

0 commit comments

Comments
 (0)