diff options
Diffstat (limited to 'tools/gsr-global-hotkeys')
-rw-r--r-- | tools/gsr-global-hotkeys/README.md | 21 | ||||
-rw-r--r-- | tools/gsr-global-hotkeys/keyboard_event.c | 57 | ||||
-rw-r--r-- | tools/gsr-global-hotkeys/keys.c | 21 | ||||
-rw-r--r-- | tools/gsr-global-hotkeys/keys.h | 10 |
4 files changed, 89 insertions, 20 deletions
diff --git a/tools/gsr-global-hotkeys/README.md b/tools/gsr-global-hotkeys/README.md new file mode 100644 index 0000000..8744107 --- /dev/null +++ b/tools/gsr-global-hotkeys/README.md @@ -0,0 +1,21 @@ +# About +Global hotkeys for X11 and all Wayland compositors by using linux device api. Keyboards are grabbed and only the non-hotkey keys are passed through to the system. +The program accepts text commands as input. Run the program with the option `--virtual` to only grab virtual devices. This is useful when using keyboard input mapping software such as +kanata, otherwise kanata may fail to launch or this program may fail to launch. +# Commands +## Bind +To add a key send `bind <action> <keycode+keycode+...><newline>` to the programs stdin, for example: +``` +bind show_hide 56+44 + +``` +which will bind alt+z. When alt+z is pressed the program will output `show_hide` (and a newline) to stdout. +The program only accepts one key for each keybind command but accepts a multiple modifier keys. +The keybinding requires at least one modifier key (ctrl, alt, super or shift) and a key to be used. +The keycodes are values from `<linux/input-event-codes.h>` linux api header (which is the same as X11 keycode value minus 8). +## Unbind +To unbind all keys send `unbind_all<newline>` to the programs stdin, for example: +``` +unbind_all + +```
\ No newline at end of file diff --git a/tools/gsr-global-hotkeys/keyboard_event.c b/tools/gsr-global-hotkeys/keyboard_event.c index b8d94fd..6973d4b 100644 --- a/tools/gsr-global-hotkeys/keyboard_event.c +++ b/tools/gsr-global-hotkeys/keyboard_event.c @@ -1,4 +1,5 @@ #include "keyboard_event.h" +#include "keys.h" /* C stdlib */ #include <stdio.h> @@ -81,19 +82,19 @@ static void keyboard_event_fetch_update_key_states(keyboard_event *self, event_e } } -static void keyboard_event_process_key_state_change(keyboard_event *self, struct input_event event, event_extra_data *extra_data, int fd) { - if(event.type != EV_KEY) +static void keyboard_event_process_key_state_change(keyboard_event *self, const struct input_event *event, event_extra_data *extra_data, int fd) { + if(event->type != EV_KEY) return; - if(!extra_data->key_states || event.code >= KEY_STATES_SIZE * 8) + if(!extra_data->key_states || event->code >= KEY_STATES_SIZE * 8) return; - const unsigned int byte_index = event.code / 8; - const unsigned char bit_index = event.code % 8; + const unsigned int byte_index = event->code / 8; + const unsigned char bit_index = event->code % 8; unsigned char key_byte_state = extra_data->key_states[byte_index]; const bool prev_key_pressed = (key_byte_state & (1 << bit_index)) != KEY_RELEASE; - if(event.value == KEY_RELEASE) { + if(event->value == KEY_RELEASE) { key_byte_state &= ~(1 << bit_index); if(prev_key_pressed) --extra_data->num_keys_pressed; @@ -171,8 +172,8 @@ static void keyboard_event_process_input_event_data(keyboard_event *self, event_ //fprintf(stderr, "fd: %d, type: %d, pressed %d, value: %d\n", fd, event.type, event.code, event.value); //} - if(event.type == EV_KEY) { - keyboard_event_process_key_state_change(self, event, extra_data, fd); + if(event.type == EV_KEY && is_keyboard_key(event.code)) { + keyboard_event_process_key_state_change(self, &event, extra_data, fd); const uint32_t modifier_bit = keycode_to_modifier_bit(event.code); if(modifier_bit == 0) { if(keyboard_event_on_key_pressed(self, &event, self->modifier_button_states)) @@ -270,7 +271,8 @@ static bool keyboard_event_try_add_device_if_keyboard(keyboard_event *self, cons if(dev_input_id == -1) return false; - if(self->grab_type == KEYBOARD_GRAB_TYPE_VIRTUAL && !dev_input_is_virtual(dev_input_id)) + const bool is_virtual_device = dev_input_is_virtual(dev_input_id); + if(self->grab_type == KEYBOARD_GRAB_TYPE_VIRTUAL && !is_virtual_device) return false; if(keyboard_event_has_event_with_dev_input_fd(self, dev_input_id)) @@ -286,7 +288,7 @@ static bool keyboard_event_try_add_device_if_keyboard(keyboard_event *self, cons unsigned long evbit = 0; ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), &evbit); - const bool is_keyboard = evbit & (1 << EV_KEY); + const bool is_keyboard = (evbit & (1 << EV_SYN)) && (evbit & (1 << EV_KEY)); if(is_keyboard && strcmp(device_name, GSR_UI_VIRTUAL_KEYBOARD_NAME) != 0) { unsigned char key_bits[KEY_MAX/8 + 1] = {0}; @@ -297,7 +299,7 @@ static bool keyboard_event_try_add_device_if_keyboard(keyboard_event *self, cons //const bool supports_touch_events = key_bits[BTN_TOUCH/8] & (1 << (BTN_TOUCH % 8)); const bool supports_joystick_events = key_bits[BTN_JOYSTICK/8] & (1 << (BTN_JOYSTICK % 8)); const bool supports_wheel_events = key_bits[BTN_WHEEL/8] & (1 << (BTN_WHEEL % 8)); - if(supports_key_events && !supports_mouse_events && !supports_joystick_events && !supports_wheel_events) { + if(supports_key_events && (is_virtual_device || (!supports_joystick_events && !supports_wheel_events))) { unsigned char *key_states = calloc(1, KEY_STATES_SIZE); if(key_states && self->num_event_polls < MAX_EVENT_POLLS) { //fprintf(stderr, "%s (%s) supports key inputs\n", dev_input_filepath, device_name); @@ -314,9 +316,16 @@ static bool keyboard_event_try_add_device_if_keyboard(keyboard_event *self, cons .num_keys_pressed = 0 }; - keyboard_event_fetch_update_key_states(self, &self->event_extra_data[self->num_event_polls], fd); - if(self->event_extra_data[self->num_event_polls].num_keys_pressed > 0) - fprintf(stderr, "Info: device not grabbed yet because some keys are still being pressed: /dev/input/event%d\n", dev_input_id); + if(supports_mouse_events || supports_joystick_events || supports_wheel_events) { + fprintf(stderr, "Info: device not grabbed yet because it might be a mouse: /dev/input/event%d\n", dev_input_id); + fsync(fd); + if(ioctl(fd, EVIOCGKEY(KEY_STATES_SIZE), self->event_extra_data[self->num_event_polls].key_states) == -1) + fprintf(stderr, "Warning: failed to fetch key states for device: /dev/input/event%d\n", dev_input_id); + } else { + keyboard_event_fetch_update_key_states(self, &self->event_extra_data[self->num_event_polls], fd); + if(self->event_extra_data[self->num_event_polls].num_keys_pressed > 0) + fprintf(stderr, "Info: device not grabbed yet because some keys are still being pressed: /dev/input/event%d\n", dev_input_id); + } ++self->num_event_polls; return true; @@ -389,14 +398,21 @@ static int setup_virtual_keyboard_input(const char *name) { success &= (ioctl(fd, UI_SET_EVBIT, EV_SYN) != -1); success &= (ioctl(fd, UI_SET_EVBIT, EV_MSC) != -1); success &= (ioctl(fd, UI_SET_EVBIT, EV_KEY) != -1); + success &= (ioctl(fd, UI_SET_EVBIT, EV_REP) != -1); + success &= (ioctl(fd, UI_SET_EVBIT, EV_REL) != -1); + success &= (ioctl(fd, UI_SET_EVBIT, EV_LED) != -1); + + success &= (ioctl(fd, UI_SET_MSCBIT, MSC_SCAN) != -1); for(int i = 1; i < KEY_MAX; ++i) { - success &= (ioctl(fd, UI_SET_KEYBIT, i) != -1); + if(is_keyboard_key(i) || is_mouse_button(i)) + success &= (ioctl(fd, UI_SET_KEYBIT, i) != -1); + } + for(int i = 0; i < REL_MAX; ++i) { + success &= (ioctl(fd, UI_SET_RELBIT, i) != -1); + } + for(int i = 0; i < LED_MAX; ++i) { + success &= (ioctl(fd, UI_SET_LEDBIT, i) != -1); } - - success &= (ioctl(fd, UI_SET_EVBIT, EV_REL) != -1); - success &= (ioctl(fd, UI_SET_RELBIT, REL_X) != -1); - success &= (ioctl(fd, UI_SET_RELBIT, REL_Y) != -1); - success &= (ioctl(fd, UI_SET_RELBIT, REL_Z) != -1); // success &= (ioctl(fd, UI_SET_EVBIT, EV_ABS) != -1); // success &= (ioctl(fd, UI_SET_ABSBIT, ABS_X) != -1); @@ -507,6 +523,7 @@ void keyboard_event_deinit(keyboard_event *self) { self->num_global_hotkeys = 0; if(self->uinput_fd > 0) { + ioctl(self->uinput_fd, UI_DEV_DESTROY); close(self->uinput_fd); self->uinput_fd = -1; } diff --git a/tools/gsr-global-hotkeys/keys.c b/tools/gsr-global-hotkeys/keys.c new file mode 100644 index 0000000..3b8fc8a --- /dev/null +++ b/tools/gsr-global-hotkeys/keys.c @@ -0,0 +1,21 @@ +#include "keys.h" +#include <linux/input-event-codes.h> + +bool is_keyboard_key(uint32_t keycode) { + return (keycode >= KEY_ESC && keycode <= KEY_KPDOT) + || (keycode >= KEY_ZENKAKUHANKAKU && keycode <= KEY_F24) + || (keycode >= KEY_PLAYCD && keycode <= KEY_MICMUTE) + || (keycode >= KEY_OK && keycode <= KEY_IMAGES) + || (keycode >= KEY_DEL_EOL && keycode <= KEY_DEL_LINE) + || (keycode >= KEY_FN && keycode <= KEY_FN_B) + || (keycode >= KEY_BRL_DOT1 && keycode <= KEY_BRL_DOT10) + || (keycode >= KEY_NUMERIC_0 && keycode <= KEY_LIGHTS_TOGGLE) + || (keycode == KEY_ALS_TOGGLE) + || (keycode >= KEY_BUTTONCONFIG && keycode <= KEY_VOICECOMMAND) + || (keycode >= KEY_BRIGHTNESS_MIN && keycode <= KEY_BRIGHTNESS_MAX) + || (keycode >= KEY_KBDINPUTASSIST_PREV && keycode <= KEY_ONSCREEN_KEYBOARD); +} + +bool is_mouse_button(uint32_t keycode) { + return (keycode >= BTN_MOUSE && keycode <= BTN_TASK); +} diff --git a/tools/gsr-global-hotkeys/keys.h b/tools/gsr-global-hotkeys/keys.h new file mode 100644 index 0000000..4f31882 --- /dev/null +++ b/tools/gsr-global-hotkeys/keys.h @@ -0,0 +1,10 @@ +#ifndef KEYS_H +#define KEYS_H + +#include <stdbool.h> +#include <stdint.h> + +bool is_keyboard_key(uint32_t keycode); +bool is_mouse_button(uint32_t keycode); + +#endif /* KEYS_H */ |