diff options
Diffstat (limited to 'tools/gsr-global-hotkeys/keyboard_event.c')
-rw-r--r-- | tools/gsr-global-hotkeys/keyboard_event.c | 177 |
1 files changed, 140 insertions, 37 deletions
diff --git a/tools/gsr-global-hotkeys/keyboard_event.c b/tools/gsr-global-hotkeys/keyboard_event.c index 99f33ee..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> @@ -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); @@ -105,7 +106,7 @@ static void keyboard_event_process_key_state_change(keyboard_event *self, const 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) { @@ -119,14 +120,13 @@ static void keyboard_event_process_key_state_change(keyboard_event *self, const /* 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) { - if(event->value != KEYBOARD_BUTTON_PRESSED) - return false; - 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) { - puts(self->global_hotkeys[i].action); - fflush(stdout); + if(event->value == KEYBOARD_BUTTON_PRESSED) { + puts(self->global_hotkeys[i].action); + fflush(stdout); + } global_hotkey_match = true; } } @@ -154,10 +154,26 @@ static uint32_t keycode_to_modifier_bit(uint32_t keycode) { return 0; } -static bool key_is_mouse_button(uint32_t keycode) { - return (keycode >= BTN_MISC && keycode <= BTN_GEAR_UP) - || (keycode >= BTN_TRIGGER_HAPPY && keycode <= BTN_TRIGGER_HAPPY40) - || (keycode >= BTN_DPAD_UP && keycode <= BTN_DPAD_RIGHT); +/* 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) { @@ -177,12 +193,18 @@ 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 && !key_is_mouse_button(event.code)) { + 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)) - return; + 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; + } } else { self->modifier_button_states = set_bit(self->modifier_button_states, modifier_bit, event.value >= 1); } @@ -193,6 +215,20 @@ 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 */ @@ -271,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)) @@ -292,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_SYN)) && (evbit & (1 << EV_KEY)) && (evbit & (1 << EV_MSC)) && (evbit & (1 << EV_REP)); + 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_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, @@ -317,10 +395,12 @@ 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 }; - if(supports_mouse_events) { + 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) @@ -335,6 +415,8 @@ static bool keyboard_event_try_add_device_if_keyboard(keyboard_event *self, cons 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); } } } @@ -374,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]; @@ -404,18 +489,20 @@ static int setup_virtual_keyboard_input(const char *name) { 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_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); } 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); - } + // 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); @@ -480,6 +567,7 @@ bool keyboard_event_init(keyboard_event *self, bool exclusive_grab, keyboard_gra .dev_input_id = -1, .grabbed = false, .key_states = NULL, + .key_presses_grabbed = NULL, .num_keys_pressed = 0 }; @@ -497,6 +585,7 @@ bool keyboard_event_init(keyboard_event *self, bool exclusive_grab, keyboard_gra .dev_input_id = -1, .grabbed = false, .key_states = NULL, + .key_presses_grabbed = NULL, .num_keys_pressed = 0 }; @@ -526,14 +615,18 @@ 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; } 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; @@ -573,6 +666,13 @@ static int parse_u8(const char *str, int size) { 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; @@ -607,13 +707,13 @@ static bool keyboard_event_parse_bind_keys(const char *str, int size, uint8_t *k break; } - if(key == 0) { + if(*key == 0) { fprintf(stderr, "Error: can't bind hotkey without a non-modifier key\n"); return false; } - if(modifiers == 0) { - fprintf(stderr, "Error: can't bind hotkey without a modifier\n"); + 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; } @@ -664,8 +764,11 @@ static void keyboard_event_parse_stdin_command(keyboard_event *self, const char } 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\" or \"unbind_all\"\n", command); + fprintf(stderr, "Warning: got invalid command: \"%s\", expected command to start with either \"bind\", \"unbind_all\" or \"exit\"\n", command); } } |