1From fca8f74afe7d96c37d1649350d2fda0b7f0f9151 Mon Sep 17 00:00:00 2001
2From: Jeffy Chen <jeffy.chen@rock-chips.com>
3Date: Mon, 13 May 2019 17:45:21 +0800
4Subject: [PATCH 3/7] Support dynamic remove devices when not available.
5
6It's the same behaviour as triggerhappy.
7
8Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
9---
10 input-event-daemon.c | 20 +++++++++++++++-----
11 1 file changed, 15 insertions(+), 5 deletions(-)
12
13diff --git a/input-event-daemon.c b/input-event-daemon.c
14index 646d00d..f124250 100644
15--- a/input-event-daemon.c
16+++ b/input-event-daemon.c
17@@ -690,19 +690,23 @@ void daemon_start_listener() {
18     signal(SIGCHLD, SIG_IGN);
19
20     FD_ZERO(&initial_fdset);
21+    fd_len = 0;
22     for(i=0; i < MAX_LISTENER && conf.listen[i] != NULL; i++) {
23         conf.listen_fd[i] = open(conf.listen[i], O_RDONLY);
24
25         if(conf.listen_fd[i] < 0) {
26             fprintf(stderr, PROGRAM": open(%s): %s\n",
27                 conf.listen[i], strerror(errno));
28-            exit(EXIT_FAILURE);
29+            conf.listen_fd[i] = 0;
30+            continue;
31+        }
32+        if(conf.verbose) {
33+            fprintf(stderr, PROGRAM": Adding device: %s...\n", conf.listen[i]);
34         }
35         FD_SET(conf.listen_fd[i], &initial_fdset);
36+        fd_len++;
37     }
38
39-    fd_len = i;
40-
41     if(fd_len == 0) {
42         fprintf(stderr, PROGRAM": no listener found!\n");
43         return;
44@@ -749,12 +753,18 @@ void daemon_start_listener() {
45             idle_time = 0;
46         }
47
48-        for(i=0; i<fd_len; i++) {
49+        for(i=0; i<MAX_LISTENER; i++) {
50             if(FD_ISSET(conf.listen_fd[i], &fdset)) {
51                 if(read(conf.listen_fd[i], &event, sizeof(event)) < 0) {
52                     fprintf(stderr, PROGRAM": read(%s): %s\n",
53                         conf.listen[i], strerror(errno));
54-                    break;
55+
56+                    /* read error? Remove the device! */
57+                    FD_CLR(conf.listen_fd[i], &initial_fdset);
58+                    close(conf.listen_fd[i]);
59+                    conf.listen_fd[i] = 0;
60+                    fd_len --;
61+                    continue;
62                 }
63                 input_parse_event(&event, conf.listen[i]);
64             }
65--
662.20.1
67
68