hc
2023-11-06 36f0949ef9854b82a9a3154d970da4e3b8d12a61
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
From 740cfb20ae7e386391fb6528a8765e5827c9781c Mon Sep 17 00:00:00 2001
From: Jeffy Chen <jeffy.chen@rock-chips.com>
Date: Wed, 26 May 2021 07:10:47 +0800
Subject: [PATCH] Support sync events
 
Wait for child process when handling sync events(start with '*').
 
For example:
[Keys]
*POWER:1      = /etc/power-key.sh press
*POWER:0      = /etc/power-key.sh release
 
Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
---
 input-event-daemon.c | 23 +++++++++++++++++++----
 input-event-daemon.h |  4 +++-
 2 files changed, 22 insertions(+), 5 deletions(-)
 
diff --git a/input-event-daemon.c b/input-event-daemon.c
index 4bf2a33..e1528a6 100644
--- a/input-event-daemon.c
+++ b/input-event-daemon.c
@@ -233,7 +233,7 @@ static int idle_event_parse(unsigned long idle) {
 
             fprintf(stderr, "  exec     : \"%s\"\n\n", fired_idle_event->exec);
         }
-        daemon_exec(fired_idle_event->exec);
+        daemon_exec(fired_idle_event->exec, 0);
     }
 
     return (fired_idle_event != NULL);
@@ -371,7 +371,7 @@ static void input_parse_event(struct input_event *event, const char *src) {
             fired_key_event = key_event_parse(event->code, event->value, src);
 
             if(fired_key_event != NULL) {
-                daemon_exec(fired_key_event->exec);
+                daemon_exec(fired_key_event->exec, fired_key_event->sync);
             }
             break;
         case EV_SW:
@@ -379,7 +379,7 @@ static void input_parse_event(struct input_event *event, const char *src) {
                 switch_event_parse(event->code, event->value, src);
 
             if(fired_switch_event != NULL) {
-                daemon_exec(fired_switch_event->exec);
+                daemon_exec(fired_switch_event->exec, fired_switch_event->sync);
             }
 
             break;
@@ -549,6 +549,12 @@ static const char *config_key_event(char *shortcut, char *exec) {
     strsep(&value, ":");
     shortcut = config_trim_string(shortcut);
 
+    new_key_event->sync = 0;
+    if (shortcut[0] == '*') {
+        new_key_event->sync = 1;
+        shortcut++;
+    }
+
     if((code = strrchr(shortcut, '+')) != NULL) {
         *code = '\0';
         code = config_trim_string(code+1);
@@ -650,6 +656,12 @@ static const char *config_switch_event(char *switchcode, char *exec) {
     code = config_trim_string(code);
     value = config_trim_string(value);
 
+    new_switch_event->sync = 0;
+    if (code[0] == '*') {
+        new_switch_event->sync = 1;
+        code++;
+    }
+
     new_switch_event->code = strdup(code);
     new_switch_event->value = atoi(value);
     new_switch_event->exec = strdup(exec);
@@ -783,8 +795,9 @@ void daemon_start_listener() {
     }
 }
 
-static void daemon_exec(const char *command) {
+static void daemon_exec(const char *command, int sync) {
     pid_t pid = fork();
+
     if(pid == 0) {
         const char *args[] = {
             "sh", "-c", command, NULL
@@ -810,6 +823,8 @@ static void daemon_exec(const char *command) {
         _exit(127);
     } else if(pid < 0) {
         perror(PROGRAM": fork()");
+    } else if (sync) {
+        waitpid(pid, NULL, 0);
     }
 
     return;
diff --git a/input-event-daemon.h b/input-event-daemon.h
index 7cab13d..4a6944b 100644
--- a/input-event-daemon.h
+++ b/input-event-daemon.h
@@ -41,6 +41,7 @@ typedef struct key_event {
     const char *modifiers[MAX_MODIFIERS];
     size_t     modifier_n;
     const char *exec;
+    int sync;
 } key_event_t;
 
 
@@ -53,6 +54,7 @@ typedef struct switch_event {
     const char *code;
     signed int value;
     const char *exec;
+    int sync;
 } switch_event_t;
 
 /**
@@ -109,7 +111,7 @@ static char         *config_trim_string(char *str);
 
 void        daemon_init();
 void        daemon_start_listener();
-static void daemon_exec(const char *command);
+static void daemon_exec(const char *command, int sync);
 void        daemon_clean();
 static void daemon_print_help();
 static void daemon_print_version();
-- 
2.20.1