linput

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

commit 9e028dfe858c373590da5d8dfe2f7c430aece07c
parent c708f344c2cc36322185eeb05b815a76c4f0c4c0
Author: Artem Kobets <artem@akobets.xyz>
Date:   Sun, 19 Apr 2020 12:27:41 +0300

differentiate left/right modifiers

Diffstat:
Mconfig.def.h | 28++++++++++++++++++++--------
Mlinput.1 | 7+++----
Mlinput.c | 120++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
3 files changed, 106 insertions(+), 49 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -19,11 +19,24 @@ static struct EventRule { }; enum { - MOD_ANY = 0x01, - MOD_SHIFT = 0x02, - MOD_CTRL = 0x04, - MOD_SUPER = 0x08, - MOD_ALT = 0x10 + /* any modifiers */ + MOD_ANY = 0x0001, + MOD_LEFTSHIFT = 0x0002, + MOD_RIGHTSHIFT = 0x0004, + /* left or right shift */ + MOD_SHIFT = 0x0008, + MOD_LEFTCTRL = 0x0010, + MOD_RIGHTCTRL = 0x0020, + /* left or right ctrl */ + MOD_CTRL = 0x0040, + MOD_LEFTSUPER = 0x0080, + MOD_RIGHTSUPER = 0x0100, + /* left or right super */ + MOD_SUPER = 0x0200, + MOD_LEFTALT = 0x0400, + MOD_RIGHTALT = 0x0800, + /* left or right alt */ + MOD_ALT = 0x1000 } HotkeyMod; enum { @@ -37,9 +50,8 @@ enum { 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) - * are ignored, specify them in mod_mask instead. */ + /* see <linux/input-event-codes.h> for complete list + * (KEY_* constants) */ int keys[HOTKEY_MAX_KEYS + 1]; int event_mask; } hotkeys[] = { diff --git a/linput.1 b/linput.1 @@ -1,4 +1,4 @@ -.TH LINPUT 1 2020-04-15 +.TH LINPUT 1 2020-04-19 .SH NAME linput \- listen to input events .SH SYNOPSIS @@ -20,9 +20,8 @@ are simple rules that listen to a specific event. .B Hotkeys are rules specifically for keyboard events, which match -familiar hotkey behavior. You can specify a -modifier mask (Ctrl, Alt, etc.), a list of regular keys, and an event mask -(trigger on press, release or hold). +familiar hotkey behavior. You can specify a modifier mask (Ctrl, Alt, etc.), +a list of regular keys, and an event mask (trigger on press, release or hold). linput is customized through editing config.h file and recompiling source code. diff --git a/linput.c b/linput.c @@ -24,7 +24,6 @@ typedef struct node { } fd_node; static int is_mod(int key); -static int get_mod_mask(); static int has_key(const struct HotkeyRule *hotkey, int key); static int is_key_ignored(int key); static int is_match_hotkey_event(const struct HotkeyRule *hotkey, int key); @@ -69,39 +68,6 @@ is_mod(int key) } int -get_mod_mask() -{ - int mod_mask = 0; - - if ( - key_state[KEY_LEFTSHIFT] != 0 || - key_state[KEY_RIGHTSHIFT] != 0 - ) { - mod_mask |= MOD_SHIFT; - } - if ( - key_state[KEY_LEFTCTRL] != 0 || - key_state[KEY_RIGHTCTRL] != 0 - ) { - mod_mask |= MOD_CTRL; - } - if ( - key_state[KEY_LEFTMETA] != 0 || - key_state[KEY_RIGHTMETA] != 0 - ) { - mod_mask |= MOD_SUPER; - } - if ( - key_state[KEY_LEFTALT] != 0 || - key_state[KEY_RIGHTALT] != 0 - ) { - mod_mask |= MOD_ALT; - } - - return mod_mask; -} - -int has_key(const struct HotkeyRule *hotkey, int key) { const int *p; @@ -144,10 +110,90 @@ is_match_hotkey_event(const struct HotkeyRule *hotkey, int key) int is_match_hotkey_mod(const struct HotkeyRule *hotkey) { - if (hotkey->mod_mask & MOD_ANY) + int mod_mask; + + mod_mask = hotkey->mod_mask; + + if (mod_mask & MOD_ANY) return 1; - else - return get_mod_mask() == hotkey->mod_mask; + + if ( + (mod_mask & MOD_LEFTSHIFT) && + key_state[KEY_LEFTSHIFT] == 0 + ) + return 0; + + if ( + (mod_mask & MOD_RIGHTSHIFT) && + key_state[KEY_RIGHTSHIFT] == 0 + ) + return 0; + + if ( + (mod_mask & MOD_SHIFT) && + (key_state[KEY_LEFTSHIFT] == 0 && + key_state[KEY_RIGHTSHIFT] == 0) + ) + return 0; + + if ( + (mod_mask & MOD_LEFTCTRL) && + key_state[KEY_LEFTCTRL] == 0 + ) + return 0; + + if ( + (mod_mask & MOD_RIGHTCTRL) && + key_state[KEY_RIGHTCTRL] == 0 + ) + return 0; + + if ( + (mod_mask & MOD_CTRL) && + (key_state[KEY_LEFTCTRL] == 0 && + key_state[KEY_RIGHTCTRL] == 0) + ) + return 0; + + if ( + (mod_mask & MOD_LEFTSUPER) && + key_state[KEY_LEFTMETA] == 0 + ) + return 0; + + if ( + (mod_mask & MOD_RIGHTSUPER) && + key_state[KEY_RIGHTMETA] == 0 + ) + return 0; + + if ( + (mod_mask & MOD_SUPER) && + (key_state[KEY_LEFTMETA] == 0 && + key_state[KEY_RIGHTMETA] == 0) + ) + return 0; + + if ( + (mod_mask & MOD_LEFTALT) && + key_state[KEY_LEFTALT] == 0 + ) + return 0; + + if ( + (mod_mask & MOD_RIGHTALT) && + key_state[KEY_RIGHTALT] == 0 + ) + return 0; + + if ( + (mod_mask & MOD_ALT) && + (key_state[KEY_LEFTALT] == 0 && + key_state[KEY_RIGHTALT] == 0) + ) + return 0; + + return 1; } int