linput

Listen to input events
git clone git://git.akobets.xyz/linput
Log | Files | Refs | README | LICENSE

commit c708f344c2cc36322185eeb05b815a76c4f0c4c0
parent fc82c318c1b8b302b3fb6248b12389b9caa9391e
Author: Artem Kobets <artem@akobets.xyz>
Date:   Sat, 18 Apr 2020 21:19:43 +0300

pass entire cmd instead of script name

Diffstat:
A.gitignore | 1+
Mconfig.def.h | 27+++++++++++++++------------
Mlinput.1 | 10+++-------
Mlinput.c | 22++++++++++------------
4 files changed, 29 insertions(+), 31 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +linput diff --git a/config.def.h b/config.def.h @@ -1,8 +1,11 @@ -/* default script name */ -static char *script = "/etc/linput/handler"; +static char *cmd_sleep[] = { "sudo", "zzz", NULL }; +static char *cmd_openterminal[] = { "st", NULL }; +static char *cmd_openbrowser[] = { "chromium", NULL }; +static char *cmd_volumeup[] = { "amixer", "-qM", "sset", "Master", "2%+", NULL }; +static char *cmd_volumedown[] = { "amixer", "-qM", "sset", "Master", "2%-", NULL }; -static const struct EventRule { - const char *name; +static struct EventRule { + char **cmd; /* see <linux/input-event-codes.h> * for type/code/value available values */ int type; @@ -10,7 +13,7 @@ static const struct EventRule { int value; } events[] = { { - "sleep", + cmd_sleep, EV_SW, SW_LID, 1 } }; @@ -31,8 +34,8 @@ enum { #define HOTKEY_MAX_KEYS 20 -static const struct HotkeyRule { - const char *name; +static struct HotkeyRule { + char **cmd; int mod_mask; /* see <linux/input-event-codes.h> for complete list (KEY_* constants) * modifiers (e.g. KEY_RIGHTSHIFT, KEY_LEFTSHIFT) @@ -41,25 +44,25 @@ static const struct HotkeyRule { int event_mask; } hotkeys[] = { { - "open-terminal", + cmd_openterminal, MOD_SUPER, { KEY_C, 0 }, ON_PRESS, }, { - "open-browser", + cmd_openbrowser, MOD_SUPER, - { KEY_E, KEY_B, 0 }, + { KEY_E, KEY_1, 0 }, ON_PRESS }, { - "volume-up", + cmd_volumeup, 0, { KEY_VOLUMEUP, 0 }, ON_PRESS | ON_HOLD }, { - "volume-down", + cmd_volumedown, 0, { KEY_VOLUMEDOWN, 0 } , ON_PRESS | ON_HOLD diff --git a/linput.1 b/linput.1 @@ -3,10 +3,10 @@ linput \- listen to input events .SH SYNOPSIS .B linput -[-v] [-s script] +[-v] .SH DESCRIPTION .B linput -is an input event listener that runs a given script when a +is an input event listener that runs a given command when a certain sequence of events has occurred. It reads event data from /dev/input/event* files. @@ -29,11 +29,7 @@ linput is customized through editing config.h file and recompiling source code. .SH OPTIONS .TP .B \-v -display version and exit. -.TP -.B \-s script -set script name to execute. Can be a full pathname, or a script -in your shell PATH. Overrides default in config.h. +display version. .SH AUTHOR Artem Kobets <artem@akobets.xyz> diff --git a/linput.c b/linput.c @@ -30,7 +30,7 @@ static int is_key_ignored(int key); static int is_match_hotkey_event(const struct HotkeyRule *hotkey, int key); static int is_match_hotkey_mod(const struct HotkeyRule *hotkey); static int is_hotkey_active(const struct HotkeyRule *hotkey, int last_key); -static void run(const char *name); +static void run(char **cmd); static void add_fd(int fd); static void remove_fd(int fd); @@ -184,15 +184,16 @@ is_hotkey_active(const struct HotkeyRule *hotkey, int last_key) } void -run(const char *name) +run(char **cmd) { switch (fork()) { case -1: die("fork: %s\n", strerror(errno)); break; case 0: - execlp(script, script, name, (char *) NULL); - fprintf(stderr, "execlp %s: %s\n", script, strerror(errno)); + if (cmd != NULL && cmd[0] != NULL) + execvp(cmd[0], cmd); + fprintf(stderr, "execvp %s: %s\n", cmd[0], strerror(errno)); _exit(EXIT_FAILURE); break; } @@ -363,14 +364,14 @@ handle_input(int fd) for (i = 0; i < LENGTH(events); i++) { struct EventRule event = events[i]; - if (event.name == NULL) continue; + if (event.cmd == NULL) continue; if ( event.type == buf.type && event.code == buf.code && event.value == buf.value ) - run(event.name); + run(event.cmd); } if (buf.type == EV_KEY) { @@ -379,10 +380,10 @@ handle_input(int fd) for (i = 0; i < LENGTH(hotkeys); i++) { struct HotkeyRule hotkey = hotkeys[i]; - if (hotkey.name == NULL) continue; + if (hotkey.cmd == NULL) continue; if (is_hotkey_active(&hotkey, buf.code)) - run(hotkey.name); + run(hotkey.cmd); } } } @@ -452,7 +453,7 @@ die(const char *fmt, ...) void usage() { - die("usage: %s [-v] [-s script]\n", argv0); + die("usage: %s [-v]\n", argv0); } @@ -464,9 +465,6 @@ main(int argc, char **argv) int res; ARGBEGIN { - case 's': - script = EARGF(usage()); - break; case 'v': puts("linput "VERSION); exit(EXIT_FAILURE);