diff options
Diffstat (limited to 'tools/gsr-global-hotkeys')
-rw-r--r-- | tools/gsr-global-hotkeys/README.md | 6 | ||||
-rw-r--r-- | tools/gsr-global-hotkeys/keyboard_event.c | 100 | ||||
-rw-r--r-- | tools/gsr-global-hotkeys/keyboard_event.h | 2 | ||||
-rw-r--r-- | tools/gsr-global-hotkeys/main.c | 14 |
4 files changed, 95 insertions, 27 deletions
diff --git a/tools/gsr-global-hotkeys/README.md b/tools/gsr-global-hotkeys/README.md index 8744107..38585c1 100644 --- a/tools/gsr-global-hotkeys/README.md +++ b/tools/gsr-global-hotkeys/README.md @@ -18,4 +18,10 @@ 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/keyboard_event.c b/tools/gsr-global-hotkeys/keyboard_event.c index e5221dc..4ff7f11 100644 --- a/tools/gsr-global-hotkeys/keyboard_event.c +++ b/tools/gsr-global-hotkeys/keyboard_event.c @@ -69,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); @@ -106,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) { @@ -193,7 +193,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 && is_keyboard_key(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) { @@ -214,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 */ @@ -292,6 +307,46 @@ 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) @@ -320,15 +375,11 @@ static bool keyboard_event_try_add_device_if_keyboard(keyboard_event *self, cons unsigned char key_bits[KEY_MAX/8 + 1] = {0}; ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bits)), &key_bits); - const bool supports_key_a = key_bits[KEY_A/8] & (1 << (KEY_A % 8)); - const bool supports_key_esc = key_bits[KEY_ESC/8] & (1 << (KEY_ESC % 8)); - const bool supports_key_volume_up = key_bits[KEY_VOLUMEUP/8] & (1 << (KEY_VOLUMEUP % 8)); - const bool supports_key_events = supports_key_a || supports_key_esc || supports_key_volume_up; + 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); - 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 && (is_virtual_device || (!supports_joystick_events && !supports_wheel_events))) { unsigned char *key_states = calloc(1, KEY_STATES_SIZE); unsigned char *key_presses_grabbed = calloc(1, KEY_STATES_SIZE); @@ -349,6 +400,7 @@ static bool keyboard_event_try_add_device_if_keyboard(keyboard_event *self, cons }; 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) @@ -404,8 +456,10 @@ 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); @@ -435,19 +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) { + // 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); @@ -566,8 +621,10 @@ void keyboard_event_deinit(keyboard_event *self) { } 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); } @@ -707,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); } } diff --git a/tools/gsr-global-hotkeys/keyboard_event.h b/tools/gsr-global-hotkeys/keyboard_event.h index 720aaa1..4a4c1fd 100644 --- a/tools/gsr-global-hotkeys/keyboard_event.h +++ b/tools/gsr-global-hotkeys/keyboard_event.h @@ -39,6 +39,8 @@ 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; diff --git a/tools/gsr-global-hotkeys/main.c b/tools/gsr-global-hotkeys/main.c index 41e5ca5..c7e0403 100644 --- a/tools/gsr-global-hotkeys/main.c +++ b/tools/gsr-global-hotkeys/main.c @@ -44,42 +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, "Error: gsr-global-hotkeys is already running\n"); + 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, grab_type)) { - fprintf(stderr, "Error: failed to setup hotplugging and no keyboard input devices were found\n"); + 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); if(keyboard_event_stdin_has_failed(&keyboard_ev)) { - fprintf(stderr, "Info: stdin closed (parent process likely closed this process), exiting...\n"); + fprintf(stderr, "gsr-global-hotkeys info: stdin closed (parent process likely closed this process), exiting...\n"); break; } } |