diff options
Diffstat (limited to 'tools/gsr-global-hotkeys')
-rw-r--r-- | tools/gsr-global-hotkeys/README.md | 27 | ||||
-rw-r--r-- | tools/gsr-global-hotkeys/hotplug.c | 11 | ||||
-rw-r--r-- | tools/gsr-global-hotkeys/keyboard_event.c | 542 | ||||
-rw-r--r-- | tools/gsr-global-hotkeys/keyboard_event.h | 60 | ||||
-rw-r--r-- | tools/gsr-global-hotkeys/keys.c | 21 | ||||
-rw-r--r-- | tools/gsr-global-hotkeys/keys.h | 10 | ||||
-rw-r--r-- | tools/gsr-global-hotkeys/main.c | 75 |
7 files changed, 570 insertions, 176 deletions
diff --git a/tools/gsr-global-hotkeys/README.md b/tools/gsr-global-hotkeys/README.md new file mode 100644 index 0000000..38585c1 --- /dev/null +++ b/tools/gsr-global-hotkeys/README.md @@ -0,0 +1,27 @@ +# 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 + +``` +## Exit +To close gsr-global-hotkeys send `exit<newline>` to the programs stdin, for example: +``` +exit + +```
\ No newline at end of file diff --git a/tools/gsr-global-hotkeys/hotplug.c b/tools/gsr-global-hotkeys/hotplug.c index ba3ef9c..2e8ca9f 100644 --- a/tools/gsr-global-hotkeys/hotplug.c +++ b/tools/gsr-global-hotkeys/hotplug.c @@ -22,7 +22,7 @@ bool hotplug_event_init(hotplug_event *self) { const int fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); if(fd == -1) - return false; /* Not root user */ + return false; if(bind(fd, (void*)&nls, sizeof(struct sockaddr_nl))) { close(fd); @@ -56,19 +56,22 @@ static void hotplug_event_parse_netlink_data(hotplug_event *self, const char *li if(strcmp(line, "SUBSYSTEM=input") == 0) self->subsystem_is_input = true; - if(self->subsystem_is_input && strncmp(line, "DEVNAME=", 8) == 0) + if(self->subsystem_is_input && strncmp(line, "DEVNAME=", 8) == 0) { callback(line+8, userdata); + self->event_is_add = false; + } } } /* Netlink uevent structure is documented here: https://web.archive.org/web/20160127215232/https://www.kernel.org/doc/pending/hotplug.txt */ void hotplug_event_process_event_data(hotplug_event *self, int fd, hotplug_device_added_callback callback, void *userdata) { - const int bytes_read = read(fd, self->event_data, sizeof(self->event_data)); - int data_index = 0; + const int bytes_read = read(fd, self->event_data, sizeof(self->event_data) - 1); if(bytes_read <= 0) return; + self->event_data[bytes_read] = '\0'; /* Hotplug data ends with a newline and a null terminator */ + int data_index = 0; while(data_index < bytes_read) { hotplug_event_parse_netlink_data(self, self->event_data + data_index, callback, userdata); data_index += strlen(self->event_data + data_index) + 1; /* Skip null terminator as well */ diff --git a/tools/gsr-global-hotkeys/keyboard_event.c b/tools/gsr-global-hotkeys/keyboard_event.c index 203dc00..4ff7f11 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> @@ -11,7 +12,7 @@ #include <fcntl.h> #include <unistd.h> #include <dirent.h> -#include <sys/poll.h> +#include <poll.h> /* LINUX */ #include <linux/input.h> @@ -68,7 +69,7 @@ static void keyboard_event_fetch_update_key_states(keyboard_event *self, event_e if(ioctl(fd, EVIOCGKEY(KEY_STATES_SIZE), extra_data->key_states) == -1) fprintf(stderr, "Warning: failed to fetch key states for device: /dev/input/event%d\n", extra_data->dev_input_id); - if(!keyboard_event_has_exclusive_grab(self) || extra_data->grabbed) + if(!keyboard_event_has_exclusive_grab(self) || extra_data->grabbed || extra_data->is_non_keyboard_device) return; extra_data->num_keys_pressed = keyboard_event_get_num_keys_pressed(extra_data->key_states); @@ -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; @@ -105,7 +106,7 @@ static void keyboard_event_process_key_state_change(keyboard_event *self, struct extra_data->key_states[byte_index] = key_byte_state; - if(!keyboard_event_has_exclusive_grab(self) || extra_data->grabbed) + if(!keyboard_event_has_exclusive_grab(self) || extra_data->grabbed || extra_data->is_non_keyboard_device) return; if(extra_data->num_keys_pressed == 0) { @@ -117,7 +118,65 @@ static void keyboard_event_process_key_state_change(keyboard_event *self, struct } } -static void keyboard_event_process_input_event_data(keyboard_event *self, event_extra_data *extra_data, int fd, key_callback callback, void *userdata) { +/* Return true if a global hotkey is assigned to the key combination */ +static bool keyboard_event_on_key_pressed(keyboard_event *self, const struct input_event *event, uint32_t modifiers) { + bool global_hotkey_match = false; + for(int i = 0; i < self->num_global_hotkeys; ++i) { + if(event->code == self->global_hotkeys[i].key && modifiers == self->global_hotkeys[i].modifiers) { + if(event->value == KEYBOARD_BUTTON_PRESSED) { + puts(self->global_hotkeys[i].action); + fflush(stdout); + } + global_hotkey_match = true; + } + } + return global_hotkey_match; +} + +static inline uint32_t set_bit(uint32_t value, uint32_t bit_flag, bool set) { + if(set) + return value | bit_flag; + else + return value & ~bit_flag; +} + +static uint32_t keycode_to_modifier_bit(uint32_t keycode) { + switch(keycode) { + case KEY_LEFTSHIFT: return KEYBOARD_MODKEY_LSHIFT; + case KEY_RIGHTSHIFT: return KEYBOARD_MODKEY_RSHIFT; + case KEY_LEFTCTRL: return KEYBOARD_MODKEY_LCTRL; + case KEY_RIGHTCTRL: return KEYBOARD_MODKEY_RCTRL; + case KEY_LEFTALT: return KEYBOARD_MODKEY_LALT; + case KEY_RIGHTALT: return KEYBOARD_MODKEY_RALT; + case KEY_LEFTMETA: return KEYBOARD_MODKEY_LSUPER; + case KEY_RIGHTMETA: return KEYBOARD_MODKEY_RSUPER; + } + return 0; +} + +/* Returns true if the state changed */ +static bool keyboard_event_set_key_presses_grabbed(const struct input_event *event, event_extra_data *extra_data) { + if(event->type != EV_KEY) + return false; + + if(!extra_data->key_presses_grabbed || event->code >= KEY_STATES_SIZE * 8) + return false; + + const unsigned int byte_index = event->code / 8; + const unsigned char bit_index = event->code % 8; + unsigned char key_byte_state = extra_data->key_presses_grabbed[byte_index]; + const bool prev_key_pressed = (key_byte_state & (1 << bit_index)) != KEY_RELEASE; + extra_data->key_presses_grabbed[byte_index] = set_bit(key_byte_state, bit_index, event->value >= 1); + + if(event->value == KEY_PRESS) + return !prev_key_pressed; + else if(event->value == KEY_RELEASE || event->value == KEY_REPEAT) + return prev_key_pressed; + + return false; +} + +static void keyboard_event_process_input_event_data(keyboard_event *self, event_extra_data *extra_data, int fd) { struct input_event event; if(read(fd, &event, sizeof(event)) != sizeof(event)) { fprintf(stderr, "Error: failed to read input event data\n"); @@ -134,59 +193,20 @@ 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); - - switch(event.code) { - case KEY_LEFTSHIFT: - self->lshift_button_state = event.value >= 1 ? KEYBOARD_BUTTON_PRESSED : KEYBOARD_BUTTON_RELEASED; - break; - case KEY_RIGHTSHIFT: - self->rshift_button_state = event.value >= 1 ? KEYBOARD_BUTTON_PRESSED : KEYBOARD_BUTTON_RELEASED; - break; - case KEY_LEFTCTRL: - self->lctrl_button_state = event.value >= 1 ? KEYBOARD_BUTTON_PRESSED : KEYBOARD_BUTTON_RELEASED; - break; - case KEY_RIGHTCTRL: - self->rctrl_button_state = event.value >= 1 ? KEYBOARD_BUTTON_PRESSED : KEYBOARD_BUTTON_RELEASED; - break; - case KEY_LEFTALT: - self->lalt_button_state = event.value >= 1 ? KEYBOARD_BUTTON_PRESSED : KEYBOARD_BUTTON_RELEASED; - break; - case KEY_RIGHTALT: - self->ralt_button_state = event.value >= 1 ? KEYBOARD_BUTTON_PRESSED : KEYBOARD_BUTTON_RELEASED; - break; - case KEY_LEFTMETA: - self->lmeta_button_state = event.value >= 1 ? KEYBOARD_BUTTON_PRESSED : KEYBOARD_BUTTON_RELEASED; - break; - case KEY_RIGHTMETA: - self->rmeta_button_state = event.value >= 1 ? KEYBOARD_BUTTON_PRESSED : KEYBOARD_BUTTON_RELEASED; - break; - default: { - const bool shift_pressed = self->lshift_button_state == KEYBOARD_BUTTON_PRESSED || self->rshift_button_state == KEYBOARD_BUTTON_PRESSED; - const bool ctrl_pressed = self->lctrl_button_state == KEYBOARD_BUTTON_PRESSED || self->rctrl_button_state == KEYBOARD_BUTTON_PRESSED; - const bool lalt_pressed = self->lalt_button_state == KEYBOARD_BUTTON_PRESSED; - const bool ralt_pressed = self->ralt_button_state == KEYBOARD_BUTTON_PRESSED; - const bool meta_pressed = self->lmeta_button_state == KEYBOARD_BUTTON_PRESSED || self->rmeta_button_state == KEYBOARD_BUTTON_PRESSED; - //fprintf(stderr, "pressed key: %d, state: %d, shift: %s, ctrl: %s, alt: %s, meta: %s\n", event.code, event.value, - // shift_pressed ? "yes" : "no", ctrl_pressed ? "yes" : "no", alt_pressed ? "yes" : "no", meta_pressed ? "yes" : "no"); - uint32_t modifiers = 0; - if(shift_pressed) - modifiers |= KEYBOARD_MODKEY_SHIFT; - if(ctrl_pressed) - modifiers |= KEYBOARD_MODKEY_CTRL; - if(lalt_pressed) - modifiers |= KEYBOARD_MODKEY_LALT; - if(ralt_pressed) - modifiers |= KEYBOARD_MODKEY_RALT; - if(meta_pressed) - modifiers |= KEYBOARD_MODKEY_SUPER; - - if(!callback(event.code, modifiers, event.value, userdata)) + const bool keyboard_key = is_keyboard_key(event.code); + if(event.type == EV_KEY && keyboard_key) { + 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)) { + if(keyboard_event_set_key_presses_grabbed(&event, extra_data)) + return; + } else if(event.value == KEY_RELEASE) { + if(keyboard_event_set_key_presses_grabbed(&event, extra_data)) return; - - break; } + } else { + self->modifier_button_states = set_bit(self->modifier_button_states, modifier_bit, event.value >= 1); } } @@ -195,6 +215,50 @@ static void keyboard_event_process_input_event_data(keyboard_event *self, event_ if(write(self->uinput_fd, &event, sizeof(event)) != sizeof(event)) fprintf(stderr, "Error: failed to write event data to virtual keyboard for exclusively grabbed device\n"); } + + if(!extra_data->is_possibly_non_keyboard_device) + return; + + /* TODO: What if some key is being pressed down while this is done? will it remain pressed down? */ + if(!extra_data->is_non_keyboard_device && (event.type == EV_REL || event.type == EV_ABS || (event.type == EV_KEY && !keyboard_key))) { + fprintf(stderr, "Info: device /dev/input/event%d is likely a non-keyboard device as it received a non-keyboard event. This device will be ignored\n", extra_data->dev_input_id); + extra_data->is_non_keyboard_device = true; + if(extra_data->grabbed) { + extra_data->grabbed = false; + ioctl(fd, EVIOCGRAB, 0); + fprintf(stderr, "Info: ungrabbed device: /dev/input/event%d\n", extra_data->dev_input_id); + } + } +} + +/* Retarded linux takes very long time to close /dev/input/eventN files, even though they are virtual and opened read-only */ +static void* keyboard_event_close_fds_callback(void *userdata) { + keyboard_event *self = userdata; + while(self->running) { + pthread_mutex_lock(&self->close_dev_input_mutex); + for(int i = 0; i < self->num_close_fds; ++i) { + close(self->close_fds[i]); + } + self->num_close_fds = 0; + pthread_mutex_unlock(&self->close_dev_input_mutex); + + usleep(100 * 1000); /* 100 milliseconds */ + } + return NULL; +} + +static bool keyboard_event_try_add_close_fd(keyboard_event *self, int fd) { + bool success = false; + pthread_mutex_lock(&self->close_dev_input_mutex); + if(self->num_close_fds < MAX_CLOSE_FDS) { + self->close_fds[self->num_close_fds] = fd; + ++self->num_close_fds; + success = true; + } else { + success = false; + } + pthread_mutex_unlock(&self->close_dev_input_mutex); + return success; } /* Returns -1 if invalid format. Expected |dev_input_filepath| to be in format /dev/input/eventN */ @@ -243,12 +307,53 @@ static bool dev_input_is_virtual(int dev_input_id) { return is_virtual; } +static inline bool supports_key(unsigned char *key_bits, unsigned int key) { + return key_bits[key/8] & (1 << (key % 8)); +} + +static bool supports_keyboard_keys(unsigned char *key_bits) { + const int keys[2] = { KEY_A, KEY_ESC }; + for(int i = 0; i < 2; ++i) { + if(supports_key(key_bits, keys[i])) + return true; + } + return false; +} + +static bool supports_mouse_keys(unsigned char *key_bits) { + const int keys[2] = { BTN_MOUSE, BTN_LEFT }; + for(int i = 0; i < 2; ++i) { + if(supports_key(key_bits, keys[i])) + return true; + } + return false; +} + +static bool supports_joystick_keys(unsigned char *key_bits) { + const int keys[9] = { BTN_JOYSTICK, BTN_A, BTN_B, BTN_X, BTN_Y, BTN_SELECT, BTN_START, BTN_SELECT, BTN_TRIGGER_HAPPY1 }; + for(int i = 0; i < 9; ++i) { + if(supports_key(key_bits, keys[i])) + return true; + } + return false; +} + +static bool supports_wheel_keys(unsigned char *key_bits) { + const int keys[2] = { BTN_WHEEL, BTN_GEAR_DOWN }; + for(int i = 0; i < 2; ++i) { + if(supports_key(key_bits, keys[i])) + return true; + } + return false; +} + static bool keyboard_event_try_add_device_if_keyboard(keyboard_event *self, const char *dev_input_filepath) { const int dev_input_id = get_dev_input_id_from_filepath(dev_input_filepath); 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)) @@ -264,20 +369,21 @@ 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}; ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)), &key_bits); - const bool supports_key_events = key_bits[KEY_A/8] & (1 << (KEY_A % 8)); - const bool supports_mouse_events = key_bits[BTN_MOUSE/8] & (1 << (BTN_MOUSE % 8)); - //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) { + const bool supports_key_events = supports_keyboard_keys(key_bits); + const bool supports_mouse_events = supports_mouse_keys(key_bits); + const bool supports_joystick_events = supports_joystick_keys(key_bits); + const bool supports_wheel_events = supports_wheel_keys(key_bits); + + 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) { + unsigned char *key_presses_grabbed = calloc(1, KEY_STATES_SIZE); + if(key_states && key_presses_grabbed && self->num_event_polls < MAX_EVENT_POLLS) { //fprintf(stderr, "%s (%s) supports key inputs\n", dev_input_filepath, device_name); self->event_polls[self->num_event_polls] = (struct pollfd) { .fd = fd, @@ -289,22 +395,36 @@ static bool keyboard_event_try_add_device_if_keyboard(keyboard_event *self, cons .dev_input_id = dev_input_id, .grabbed = false, .key_states = key_states, + .key_presses_grabbed = key_presses_grabbed, .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) { + self->event_extra_data[self->num_event_polls].is_possibly_non_keyboard_device = true; + 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; } else { fprintf(stderr, "Warning: the maximum number of keyboard devices have been registered. The newly added keyboard will be ignored\n"); + free(key_states); + free(key_presses_grabbed); } } } - close(fd); + if(!keyboard_event_try_add_close_fd(self, fd)) { + fprintf(stderr, "Error: failed to add immediately, closing now\n"); + close(fd); + } return false; } @@ -336,9 +456,12 @@ static void keyboard_event_remove_event(keyboard_event *self, int index) { if(index < 0 || index >= self->num_event_polls) return; - ioctl(self->event_polls[index].fd, EVIOCGRAB, 0); - close(self->event_polls[index].fd); + if(self->event_polls[index].fd > 0) { + ioctl(self->event_polls[index].fd, EVIOCGRAB, 0); + close(self->event_polls[index].fd); + } free(self->event_extra_data[index].key_states); + free(self->event_extra_data[index].key_presses_grabbed); for(int i = index + 1; i < self->num_event_polls; ++i) { self->event_polls[i - 1] = self->event_polls[i]; @@ -364,14 +487,22 @@ 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); + // TODO: Check for joystick button? if we accidentally grab joystick + if(is_keyboard_key(i) || is_mouse_button(i)) + success &= (ioctl(fd, UI_SET_KEYBIT, 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); + 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_ABS) != -1); // success &= (ioctl(fd, UI_SET_ABSBIT, ABS_X) != -1); @@ -406,11 +537,19 @@ static int setup_virtual_keyboard_input(const char *name) { return fd; } -bool keyboard_event_init(keyboard_event *self, bool poll_stdout_error, bool exclusive_grab, keyboard_grab_type grab_type) { +bool keyboard_event_init(keyboard_event *self, bool exclusive_grab, keyboard_grab_type grab_type) { memset(self, 0, sizeof(*self)); - self->stdout_event_index = -1; + self->stdin_event_index = -1; self->hotplug_event_index = -1; self->grab_type = grab_type; + self->running = true; + + pthread_mutex_init(&self->close_dev_input_mutex, NULL); + if(pthread_create(&self->close_dev_input_fds_thread, NULL, keyboard_event_close_fds_callback, self) != 0) { + self->close_dev_input_fds_thread = 0; + fprintf(stderr, "Error: failed to create close fds thread\n"); + return false; + } if(exclusive_grab) { self->uinput_fd = setup_virtual_keyboard_input(GSR_UI_VIRTUAL_KEYBOARD_NAME); @@ -418,23 +557,22 @@ bool keyboard_event_init(keyboard_event *self, bool poll_stdout_error, bool excl fprintf(stderr, "Warning: failed to setup virtual keyboard input for exclusive grab. The focused application will receive keys used for global hotkeys\n"); } - if(poll_stdout_error) { - self->event_polls[self->num_event_polls] = (struct pollfd) { - .fd = STDOUT_FILENO, - .events = 0, - .revents = 0 - }; + self->event_polls[self->num_event_polls] = (struct pollfd) { + .fd = STDIN_FILENO, + .events = POLLIN, + .revents = 0 + }; - self->event_extra_data[self->num_event_polls] = (event_extra_data) { - .dev_input_id = -1, - .grabbed = false, - .key_states = NULL, - .num_keys_pressed = 0 - }; + self->event_extra_data[self->num_event_polls] = (event_extra_data) { + .dev_input_id = -1, + .grabbed = false, + .key_states = NULL, + .key_presses_grabbed = NULL, + .num_keys_pressed = 0 + }; - self->stdout_event_index = self->num_event_polls; - ++self->num_event_polls; - } + self->stdin_event_index = self->num_event_polls; + ++self->num_event_polls; if(hotplug_event_init(&self->hotplug_ev)) { self->event_polls[self->num_event_polls] = (struct pollfd) { @@ -447,6 +585,7 @@ bool keyboard_event_init(keyboard_event *self, bool poll_stdout_error, bool excl .dev_input_id = -1, .grabbed = false, .key_states = NULL, + .key_presses_grabbed = NULL, .num_keys_pressed = 0 }; @@ -468,35 +607,218 @@ bool keyboard_event_init(keyboard_event *self, bool poll_stdout_error, bool excl } void keyboard_event_deinit(keyboard_event *self) { + self->running = false; + + for(int i = 0; i < self->num_global_hotkeys; ++i) { + free(self->global_hotkeys[i].action); + } + 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; } for(int i = 0; i < self->num_event_polls; ++i) { - ioctl(self->event_polls[i].fd, EVIOCGRAB, 0); - close(self->event_polls[i].fd); + if(self->event_polls[i].fd > 0) { + ioctl(self->event_polls[i].fd, EVIOCGRAB, 0); + close(self->event_polls[i].fd); + } free(self->event_extra_data[i].key_states); + free(self->event_extra_data[i].key_presses_grabbed); } self->num_event_polls = 0; hotplug_event_deinit(&self->hotplug_ev); + + if(self->close_dev_input_fds_thread > 0) { + pthread_join(self->close_dev_input_fds_thread, NULL); + self->close_dev_input_fds_thread = 0; + } + + pthread_mutex_destroy(&self->close_dev_input_mutex); } static void on_device_added_callback(const char *devname, void *userdata) { keyboard_event *keyboard_ev = userdata; - char dev_input_filepath[1024]; + char dev_input_filepath[256]; snprintf(dev_input_filepath, sizeof(dev_input_filepath), "/dev/%s", devname); keyboard_event_try_add_device_if_keyboard(keyboard_ev, dev_input_filepath); } -void keyboard_event_poll_events(keyboard_event *self, int timeout_milliseconds, key_callback callback, void *userdata) { +/* Returns -1 on error */ +static int parse_u8(const char *str, int size) { + if(size <= 0) + return -1; + + int result = 0; + for(int i = 0; i < size; ++i) { + char c = str[i]; + if(c >= '0' && c <= '9') { + result = result * 10 + (c - '0'); + if(result > 255) + return -1; + } else { + return -1; + } + } + return result; +} + +static bool is_key_alpha_numerical(uint8_t key) { + return (key >= KEY_1 && key <= KEY_0) + || (key >= KEY_Q && key <= KEY_P) + || (key >= KEY_A && key <= KEY_L) + || (key >= KEY_Z && key <= KEY_M); +} + +static bool keyboard_event_parse_bind_keys(const char *str, int size, uint8_t *key, uint32_t *modifiers) { + *key = 0; + *modifiers = 0; + + const char *number_start = str; + const char *end = str + size; + for(;;) { + const char *next = strchr(number_start, '+'); + if(!next) + next = end; + + const int number_len = next - number_start; + const int number = parse_u8(number_start, number_len); + if(number == -1) { + fprintf(stderr, "Error: bind command keys \"%s\" is in invalid format\n", str); + return false; + } + + const uint32_t modifier_bit = keycode_to_modifier_bit(number); + if(modifier_bit == 0) { + if(*key != 0) { + fprintf(stderr, "Error: can't bind hotkey with multiple non-modifier keys\n"); + return false; + } + *key = number; + } else { + *modifiers = set_bit(*modifiers, modifier_bit, true); + } + + number_start = next + 1; + if(next == end) + break; + } + + if(*key == 0) { + fprintf(stderr, "Error: can't bind hotkey without a non-modifier key\n"); + return false; + } + + if(*modifiers == 0 && is_key_alpha_numerical(*key)) { + fprintf(stderr, "Error: can't bind hotkey without a modifier unless the key is a non alpha-numerical key\n"); + return false; + } + + return true; +} + +/* |command| is null-terminated */ +static void keyboard_event_parse_stdin_command(keyboard_event *self, const char *command, int command_size) { + if(strncmp(command, "bind ", 5) == 0) { + /* Example: |bind show_hide 20+40| */ + if(self->num_global_hotkeys >= MAX_GLOBAL_HOTKEYS) { + fprintf(stderr, "Error: can't add another hotkey. The maximum number of hotkeys (%d) has been reached\n", MAX_GLOBAL_HOTKEYS); + return; + } + + const char *action_name_end = strchr(command + 5, ' '); + if(!action_name_end) { + fprintf(stderr, "Error: command \"%s\" is in invalid format\n", command); + return; + } + + const char *action_name = command + 5; + const int action_name_size = action_name_end - action_name; + + uint8_t key = 0; + uint32_t modifiers = 0; + const char *number_start = action_name_end + 1; + const char *end = command + command_size; + if(!keyboard_event_parse_bind_keys(number_start, end - number_start, &key, &modifiers)) + return; + + char *action = strndup(action_name, action_name_size); + if(!action) { + fprintf(stderr, "Error: failed to duplicate %.*s\n", action_name_size, action_name); + return; + } + + self->global_hotkeys[self->num_global_hotkeys] = (global_hotkey) { + .action = action, + .key = key, + .modifiers = modifiers + }; + ++self->num_global_hotkeys; + fprintf(stderr, "Info: binded hotkey: %s\n", action); + } else if(strncmp(command, "unbind_all", 10) == 0) { + for(int i = 0; i < self->num_global_hotkeys; ++i) { + free(self->global_hotkeys[i].action); + } + self->num_global_hotkeys = 0; + fprintf(stderr, "Info: unbinded all hotkeys\n"); + } else if(strncmp(command, "exit", 4) == 0) { + self->stdin_failed = true; + fprintf(stderr, "Info: received exit command\n"); + } else { + fprintf(stderr, "Warning: got invalid command: \"%s\", expected command to start with either \"bind\", \"unbind_all\" or \"exit\"\n", command); + } +} + +static void keyboard_event_process_stdin_command_data(keyboard_event *self, int fd) { + const int num_bytes_to_read = sizeof(self->stdin_command_data) - self->stdin_command_data_size; + if(num_bytes_to_read == 0) { + fprintf(stderr, "Error: failed to read data from stdin, buffer is full. Clearing buffer\n"); + self->stdin_command_data_size = 0; + return; + } + + const ssize_t bytes_read = read(fd, self->stdin_command_data + self->stdin_command_data_size, num_bytes_to_read); + if(bytes_read <= 0) + return; + + const char *command_start = self->stdin_command_data; + const char *search = self->stdin_command_data + self->stdin_command_data_size; + const char *end = search + bytes_read; + self->stdin_command_data_size += bytes_read; + + for(;;) { + char *next = memchr(search, '\n', end - search); + if(!next) + break; + + *next = '\0'; + keyboard_event_parse_stdin_command(self, command_start, next - command_start); + search = next + 1; + command_start = search; + if(next == end) + break; + } + + const int bytes_parsed = command_start - self->stdin_command_data; + if(bytes_parsed > 0) { + self->stdin_command_data_size -= bytes_parsed; + memmove(self->stdin_command_data, command_start, self->stdin_command_data_size); + } +} + +void keyboard_event_poll_events(keyboard_event *self, int timeout_milliseconds) { if(poll(self->event_polls, self->num_event_polls, timeout_milliseconds) <= 0) return; + if(self->stdin_failed) + return; + for(int i = 0; i < self->num_event_polls; ++i) { - if(i == self->stdout_event_index && (self->event_polls[i].revents & (POLLHUP|POLLERR))) - self->stdout_failed = true; + if(i == self->stdin_event_index && (self->event_polls[i].revents & (POLLHUP|POLLERR))) + self->stdin_failed = true; if(self->event_polls[i].revents & POLLHUP) { /* TODO: What if this is the hotplug fd? */ keyboard_event_remove_event(self, i); @@ -510,14 +832,14 @@ void keyboard_event_poll_events(keyboard_event *self, int timeout_milliseconds, if(i == self->hotplug_event_index) { /* Device is added to end of |event_polls| so it's ok to add while iterating it via index */ hotplug_event_process_event_data(&self->hotplug_ev, self->event_polls[i].fd, on_device_added_callback, self); - } else if(i == self->stdout_event_index) { - /* Do nothing, this shouldn't happen anyways since we dont poll for input */ + } else if(i == self->stdin_event_index) { + keyboard_event_process_stdin_command_data(self, self->event_polls[i].fd); } else { - keyboard_event_process_input_event_data(self, &self->event_extra_data[i], self->event_polls[i].fd, callback, userdata); + keyboard_event_process_input_event_data(self, &self->event_extra_data[i], self->event_polls[i].fd); } } } -bool keyboard_event_stdout_has_failed(const keyboard_event *self) { - return self->stdout_failed; +bool keyboard_event_stdin_has_failed(const keyboard_event *self) { + return self->stdin_failed; } diff --git a/tools/gsr-global-hotkeys/keyboard_event.h b/tools/gsr-global-hotkeys/keyboard_event.h index 0283afd..4a4c1fd 100644 --- a/tools/gsr-global-hotkeys/keyboard_event.h +++ b/tools/gsr-global-hotkeys/keyboard_event.h @@ -10,19 +10,25 @@ #include <stdint.h> /* POSIX */ -#include <sys/poll.h> +#include <poll.h> +#include <pthread.h> /* LINUX */ #include <linux/input-event-codes.h> #define MAX_EVENT_POLLS 32 +#define MAX_CLOSE_FDS 256 +#define MAX_GLOBAL_HOTKEYS 32 typedef enum { KEYBOARD_MODKEY_LALT = 1 << 0, - KEYBOARD_MODKEY_RALT = 1 << 2, - KEYBOARD_MODKEY_SUPER = 1 << 3, - KEYBOARD_MODKEY_CTRL = 1 << 4, - KEYBOARD_MODKEY_SHIFT = 1 << 5 + KEYBOARD_MODKEY_RALT = 1 << 1, + KEYBOARD_MODKEY_LSUPER = 1 << 2, + KEYBOARD_MODKEY_RSUPER = 1 << 3, + KEYBOARD_MODKEY_LCTRL = 1 << 4, + KEYBOARD_MODKEY_RCTRL = 1 << 5, + KEYBOARD_MODKEY_LSHIFT = 1 << 6, + KEYBOARD_MODKEY_RSHIFT = 1 << 7 } keyboard_modkeys; typedef enum { @@ -33,7 +39,10 @@ typedef enum { typedef struct { int dev_input_id; bool grabbed; + bool is_non_keyboard_device; + bool is_possibly_non_keyboard_device; unsigned char *key_states; + unsigned char *key_presses_grabbed; int num_keys_pressed; } event_extra_data; @@ -43,37 +52,44 @@ typedef enum { } keyboard_grab_type; typedef struct { + uint32_t key; + uint32_t modifiers; /* keyboard_modkeys bitmask */ + char *action; +} global_hotkey; + +typedef struct { struct pollfd event_polls[MAX_EVENT_POLLS]; /* Current size is |num_event_polls| */ event_extra_data event_extra_data[MAX_EVENT_POLLS]; /* Current size is |num_event_polls| */ int num_event_polls; - int stdout_event_index; + int stdin_event_index; int hotplug_event_index; int uinput_fd; - bool stdout_failed; + bool stdin_failed; keyboard_grab_type grab_type; + pthread_t close_dev_input_fds_thread; + pthread_mutex_t close_dev_input_mutex; + int close_fds[MAX_CLOSE_FDS]; + int num_close_fds; + bool running; + + char stdin_command_data[512]; + int stdin_command_data_size; + + global_hotkey global_hotkeys[MAX_GLOBAL_HOTKEYS]; + int num_global_hotkeys; + hotplug_event hotplug_ev; - keyboard_button_state lshift_button_state; - keyboard_button_state rshift_button_state; - keyboard_button_state lctrl_button_state; - keyboard_button_state rctrl_button_state; - keyboard_button_state lalt_button_state; - keyboard_button_state ralt_button_state; - keyboard_button_state lmeta_button_state; - keyboard_button_state rmeta_button_state; + uint32_t modifier_button_states; } keyboard_event; -/* |key| is a KEY_ from linux/input-event-codes.h. |modifiers| is a bitmask of keyboard_modkeys. |press_status| is 0 for released, 1 for pressed and 2 for repeat */ -/* Return true to allow other applications to receive the key input (when using exclusive grab) */ -typedef bool (*key_callback)(uint32_t key, uint32_t modifiers, int press_status, void *userdata); - -bool keyboard_event_init(keyboard_event *self, bool poll_stdout_error, bool exclusive_grab, keyboard_grab_type grab_type); +bool keyboard_event_init(keyboard_event *self, bool exclusive_grab, keyboard_grab_type grab_type); void keyboard_event_deinit(keyboard_event *self); /* If |timeout_milliseconds| is -1 then wait until an event is received */ -void keyboard_event_poll_events(keyboard_event *self, int timeout_milliseconds, key_callback callback, void *userdata); -bool keyboard_event_stdout_has_failed(const keyboard_event *self); +void keyboard_event_poll_events(keyboard_event *self, int timeout_milliseconds); +bool keyboard_event_stdin_has_failed(const keyboard_event *self); #endif /* KEYBOARD_EVENT_H */ 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 */ diff --git a/tools/gsr-global-hotkeys/main.c b/tools/gsr-global-hotkeys/main.c index 2524b45..c7e0403 100644 --- a/tools/gsr-global-hotkeys/main.c +++ b/tools/gsr-global-hotkeys/main.c @@ -2,42 +2,12 @@ /* C stdlib */ #include <stdio.h> -#include <stdint.h> #include <string.h> +#include <locale.h> /* POSIX */ #include <unistd.h> -typedef struct { - uint32_t key; - uint32_t modifiers; /* keyboard_modkeys bitmask */ - const char *action; -} global_hotkey; - -#define NUM_GLOBAL_HOTKEYS 6 -static global_hotkey global_hotkeys[NUM_GLOBAL_HOTKEYS] = { - { .key = KEY_Z, .modifiers = KEYBOARD_MODKEY_LALT, .action = "show_hide" }, - { .key = KEY_F9, .modifiers = KEYBOARD_MODKEY_LALT, .action = "record" }, - { .key = KEY_F7, .modifiers = KEYBOARD_MODKEY_LALT, .action = "pause" }, - { .key = KEY_F8, .modifiers = KEYBOARD_MODKEY_LALT, .action = "stream" }, - { .key = KEY_F10, .modifiers = KEYBOARD_MODKEY_LALT | KEYBOARD_MODKEY_SHIFT, .action = "replay_start" }, - { .key = KEY_F10, .modifiers = KEYBOARD_MODKEY_LALT, .action = "replay_save" } -}; - -static bool on_key_callback(uint32_t key, uint32_t modifiers, int press_status, void *userdata) { - (void)userdata; - for(int i = 0; i < NUM_GLOBAL_HOTKEYS; ++i) { - if(key == global_hotkeys[i].key && modifiers == global_hotkeys[i].modifiers) { - if(press_status == 1) { /* 1 == Pressed */ - puts(global_hotkeys[i].action); - fflush(stdout); - } - return false; - } - } - return true; -} - static void usage(void) { fprintf(stderr, "usage: gsr-global-hotkeys [--all|--virtual]\n"); fprintf(stderr, "OPTIONS:\n"); @@ -45,7 +15,27 @@ static void usage(void) { fprintf(stderr, " --virtual Grab all virtual devices only.\n"); } +static bool is_gsr_global_hotkeys_already_running(void) { + FILE *f = fopen("/proc/bus/input/devices", "rb"); + if(!f) + return false; + + bool virtual_keyboard_running = false; + char line[1024]; + while(fgets(line, sizeof(line), f)) { + if(strstr(line, "gsr-ui virtual keyboard")) { + virtual_keyboard_running = true; + break; + } + } + + fclose(f); + return virtual_keyboard_running; +} + int main(int argc, char **argv) { + setlocale(LC_ALL, "C"); /* Sigh... stupid C */ + keyboard_grab_type grab_type = KEYBOARD_GRAB_TYPE_ALL; if(argc == 2) { const char *grab_type_arg = argv[1]; @@ -54,37 +44,42 @@ int main(int argc, char **argv) { } else if(strcmp(grab_type_arg, "--virtual") == 0) { grab_type = KEYBOARD_GRAB_TYPE_VIRTUAL; } else { - fprintf(stderr, "Error: expected --all or --virtual, got %s\n", grab_type_arg); + fprintf(stderr, "gsr-global-hotkeys error: expected --all or --virtual, got %s\n", grab_type_arg); usage(); return 1; } } else if(argc != 1) { - fprintf(stderr, "Error: expected 0 or 1 arguments, got %d argument(s)\n", argc); + fprintf(stderr, "gsr-global-hotkeys error: expected 0 or 1 arguments, got %d argument(s)\n", argc); usage(); return 1; } + if(is_gsr_global_hotkeys_already_running()) { + fprintf(stderr, "gsr-global-hotkeys error: gsr-global-hotkeys is already running\n"); + return 1; + } + const uid_t user_id = getuid(); if(geteuid() != 0) { if(setuid(0) == -1) { - fprintf(stderr, "Error: failed to change user to root\n"); + fprintf(stderr, "gsr-global-hotkeys error: failed to change user to root, global hotkeys will not work. Make sure to set the correct capability on gsr-global-hotkeys\n"); return 1; } } keyboard_event keyboard_ev; - if(!keyboard_event_init(&keyboard_ev, true, true, grab_type)) { - fprintf(stderr, "Error: failed to setup hotplugging and no keyboard input devices were found\n"); + if(!keyboard_event_init(&keyboard_ev, true, grab_type)) { + fprintf(stderr, "gsr-global-hotkeys error: failed to setup hotplugging and no keyboard input devices were found\n"); setuid(user_id); return 1; } - fprintf(stderr, "Info: global hotkeys setup, waiting for hotkeys to be pressed\n"); + fprintf(stderr, "gsr-global-hotkeys info: global hotkeys setup, waiting for hotkeys to be pressed\n"); for(;;) { - keyboard_event_poll_events(&keyboard_ev, -1, on_key_callback, NULL); - if(keyboard_event_stdout_has_failed(&keyboard_ev)) { - fprintf(stderr, "Info: stdout closed (parent process likely closed this process), exiting...\n"); + keyboard_event_poll_events(&keyboard_ev, -1); + if(keyboard_event_stdin_has_failed(&keyboard_ev)) { + fprintf(stderr, "gsr-global-hotkeys info: stdin closed (parent process likely closed this process), exiting...\n"); break; } } |