1*4882a593SmuzhiyunFrom 740cfb20ae7e386391fb6528a8765e5827c9781c Mon Sep 17 00:00:00 2001 2*4882a593SmuzhiyunFrom: Jeffy Chen <jeffy.chen@rock-chips.com> 3*4882a593SmuzhiyunDate: Wed, 26 May 2021 07:10:47 +0800 4*4882a593SmuzhiyunSubject: [PATCH] Support sync events 5*4882a593Smuzhiyun 6*4882a593SmuzhiyunWait for child process when handling sync events(start with '*'). 7*4882a593Smuzhiyun 8*4882a593SmuzhiyunFor example: 9*4882a593Smuzhiyun[Keys] 10*4882a593Smuzhiyun*POWER:1 = /etc/power-key.sh press 11*4882a593Smuzhiyun*POWER:0 = /etc/power-key.sh release 12*4882a593Smuzhiyun 13*4882a593SmuzhiyunSigned-off-by: Jeffy Chen <jeffy.chen@rock-chips.com> 14*4882a593Smuzhiyun--- 15*4882a593Smuzhiyun input-event-daemon.c | 23 +++++++++++++++++++---- 16*4882a593Smuzhiyun input-event-daemon.h | 4 +++- 17*4882a593Smuzhiyun 2 files changed, 22 insertions(+), 5 deletions(-) 18*4882a593Smuzhiyun 19*4882a593Smuzhiyundiff --git a/input-event-daemon.c b/input-event-daemon.c 20*4882a593Smuzhiyunindex 4bf2a33..e1528a6 100644 21*4882a593Smuzhiyun--- a/input-event-daemon.c 22*4882a593Smuzhiyun+++ b/input-event-daemon.c 23*4882a593Smuzhiyun@@ -233,7 +233,7 @@ static int idle_event_parse(unsigned long idle) { 24*4882a593Smuzhiyun 25*4882a593Smuzhiyun fprintf(stderr, " exec : \"%s\"\n\n", fired_idle_event->exec); 26*4882a593Smuzhiyun } 27*4882a593Smuzhiyun- daemon_exec(fired_idle_event->exec); 28*4882a593Smuzhiyun+ daemon_exec(fired_idle_event->exec, 0); 29*4882a593Smuzhiyun } 30*4882a593Smuzhiyun 31*4882a593Smuzhiyun return (fired_idle_event != NULL); 32*4882a593Smuzhiyun@@ -371,7 +371,7 @@ static void input_parse_event(struct input_event *event, const char *src) { 33*4882a593Smuzhiyun fired_key_event = key_event_parse(event->code, event->value, src); 34*4882a593Smuzhiyun 35*4882a593Smuzhiyun if(fired_key_event != NULL) { 36*4882a593Smuzhiyun- daemon_exec(fired_key_event->exec); 37*4882a593Smuzhiyun+ daemon_exec(fired_key_event->exec, fired_key_event->sync); 38*4882a593Smuzhiyun } 39*4882a593Smuzhiyun break; 40*4882a593Smuzhiyun case EV_SW: 41*4882a593Smuzhiyun@@ -379,7 +379,7 @@ static void input_parse_event(struct input_event *event, const char *src) { 42*4882a593Smuzhiyun switch_event_parse(event->code, event->value, src); 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun if(fired_switch_event != NULL) { 45*4882a593Smuzhiyun- daemon_exec(fired_switch_event->exec); 46*4882a593Smuzhiyun+ daemon_exec(fired_switch_event->exec, fired_switch_event->sync); 47*4882a593Smuzhiyun } 48*4882a593Smuzhiyun 49*4882a593Smuzhiyun break; 50*4882a593Smuzhiyun@@ -549,6 +549,12 @@ static const char *config_key_event(char *shortcut, char *exec) { 51*4882a593Smuzhiyun strsep(&value, ":"); 52*4882a593Smuzhiyun shortcut = config_trim_string(shortcut); 53*4882a593Smuzhiyun 54*4882a593Smuzhiyun+ new_key_event->sync = 0; 55*4882a593Smuzhiyun+ if (shortcut[0] == '*') { 56*4882a593Smuzhiyun+ new_key_event->sync = 1; 57*4882a593Smuzhiyun+ shortcut++; 58*4882a593Smuzhiyun+ } 59*4882a593Smuzhiyun+ 60*4882a593Smuzhiyun if((code = strrchr(shortcut, '+')) != NULL) { 61*4882a593Smuzhiyun *code = '\0'; 62*4882a593Smuzhiyun code = config_trim_string(code+1); 63*4882a593Smuzhiyun@@ -650,6 +656,12 @@ static const char *config_switch_event(char *switchcode, char *exec) { 64*4882a593Smuzhiyun code = config_trim_string(code); 65*4882a593Smuzhiyun value = config_trim_string(value); 66*4882a593Smuzhiyun 67*4882a593Smuzhiyun+ new_switch_event->sync = 0; 68*4882a593Smuzhiyun+ if (code[0] == '*') { 69*4882a593Smuzhiyun+ new_switch_event->sync = 1; 70*4882a593Smuzhiyun+ code++; 71*4882a593Smuzhiyun+ } 72*4882a593Smuzhiyun+ 73*4882a593Smuzhiyun new_switch_event->code = strdup(code); 74*4882a593Smuzhiyun new_switch_event->value = atoi(value); 75*4882a593Smuzhiyun new_switch_event->exec = strdup(exec); 76*4882a593Smuzhiyun@@ -783,8 +795,9 @@ void daemon_start_listener() { 77*4882a593Smuzhiyun } 78*4882a593Smuzhiyun } 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun-static void daemon_exec(const char *command) { 81*4882a593Smuzhiyun+static void daemon_exec(const char *command, int sync) { 82*4882a593Smuzhiyun pid_t pid = fork(); 83*4882a593Smuzhiyun+ 84*4882a593Smuzhiyun if(pid == 0) { 85*4882a593Smuzhiyun const char *args[] = { 86*4882a593Smuzhiyun "sh", "-c", command, NULL 87*4882a593Smuzhiyun@@ -810,6 +823,8 @@ static void daemon_exec(const char *command) { 88*4882a593Smuzhiyun _exit(127); 89*4882a593Smuzhiyun } else if(pid < 0) { 90*4882a593Smuzhiyun perror(PROGRAM": fork()"); 91*4882a593Smuzhiyun+ } else if (sync) { 92*4882a593Smuzhiyun+ waitpid(pid, NULL, 0); 93*4882a593Smuzhiyun } 94*4882a593Smuzhiyun 95*4882a593Smuzhiyun return; 96*4882a593Smuzhiyundiff --git a/input-event-daemon.h b/input-event-daemon.h 97*4882a593Smuzhiyunindex 7cab13d..4a6944b 100644 98*4882a593Smuzhiyun--- a/input-event-daemon.h 99*4882a593Smuzhiyun+++ b/input-event-daemon.h 100*4882a593Smuzhiyun@@ -41,6 +41,7 @@ typedef struct key_event { 101*4882a593Smuzhiyun const char *modifiers[MAX_MODIFIERS]; 102*4882a593Smuzhiyun size_t modifier_n; 103*4882a593Smuzhiyun const char *exec; 104*4882a593Smuzhiyun+ int sync; 105*4882a593Smuzhiyun } key_event_t; 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun 108*4882a593Smuzhiyun@@ -53,6 +54,7 @@ typedef struct switch_event { 109*4882a593Smuzhiyun const char *code; 110*4882a593Smuzhiyun signed int value; 111*4882a593Smuzhiyun const char *exec; 112*4882a593Smuzhiyun+ int sync; 113*4882a593Smuzhiyun } switch_event_t; 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun /** 116*4882a593Smuzhiyun@@ -109,7 +111,7 @@ static char *config_trim_string(char *str); 117*4882a593Smuzhiyun 118*4882a593Smuzhiyun void daemon_init(); 119*4882a593Smuzhiyun void daemon_start_listener(); 120*4882a593Smuzhiyun-static void daemon_exec(const char *command); 121*4882a593Smuzhiyun+static void daemon_exec(const char *command, int sync); 122*4882a593Smuzhiyun void daemon_clean(); 123*4882a593Smuzhiyun static void daemon_print_help(); 124*4882a593Smuzhiyun static void daemon_print_version(); 125*4882a593Smuzhiyun-- 126*4882a593Smuzhiyun2.20.1 127*4882a593Smuzhiyun 128