diff options
Diffstat (limited to 'tools/gsr-global-hotkeys/keyboard_event.c')
-rw-r--r-- | tools/gsr-global-hotkeys/keyboard_event.c | 119 |
1 files changed, 112 insertions, 7 deletions
diff --git a/tools/gsr-global-hotkeys/keyboard_event.c b/tools/gsr-global-hotkeys/keyboard_event.c index 63aeeef..bcfd616 100644 --- a/tools/gsr-global-hotkeys/keyboard_event.c +++ b/tools/gsr-global-hotkeys/keyboard_event.c @@ -25,6 +25,24 @@ #define KEY_STATES_SIZE (KEY_MAX/8 + 1) +#define KEYCODE_TO_XKB_KEYCODE(key) ((key) + 8) + +#define XK_Shift_L 0xffe1 /* Left shift */ +#define XK_Shift_R 0xffe2 /* Right shift */ +#define XK_Control_L 0xffe3 /* Left control */ +#define XK_Control_R 0xffe4 /* Right control */ +#define XK_Alt_L 0xffe9 /* Left alt */ +#define XK_Alt_R 0xffea /* Right alt */ +#define XK_Super_L 0xffeb /* Left super */ +#define XK_Super_R 0xffec /* Right super */ + +#define XK_z 0x007a +#define XK_F7 0xffc4 +#define XK_F8 0xffc5 +#define XK_F9 0xffc6 +#define XK_F10 0xffc7 +#define XK_F11 0xffc8 + static inline int count_num_bits_set(unsigned char c) { int n = 0; n += (c & 1); @@ -117,6 +135,35 @@ static void keyboard_event_process_key_state_change(keyboard_event *self, struct } } +static uint32_t keycode_to_keysym(keyboard_event *self, uint16_t keycode) { + const unsigned long xkb_keycode = KEYCODE_TO_XKB_KEYCODE(keycode); + if(self->x_context.display && self->x_context.XKeycodeToKeysym && xkb_keycode <= 255) + return self->x_context.XKeycodeToKeysym(self->x_context.display, xkb_keycode, 0); + else + return 0; +} + +/* TODO: Support more keys when needed */ +static uint32_t keysym_to_keycode(uint32_t keysym) { + switch(keysym) { + case XK_Control_L: return KEY_LEFTCTRL; + case XK_Shift_L: return KEY_LEFTSHIFT; + case XK_Alt_L: return KEY_LEFTALT; + case XK_Super_L: return KEY_LEFTMETA; + case XK_Control_R: return KEY_RIGHTCTRL; + case XK_Shift_R: return KEY_RIGHTSHIFT; + case XK_Alt_R: return KEY_RIGHTALT; + case XK_Super_R: return KEY_RIGHTMETA; + case XK_z: return KEY_Z; + case XK_F7: return KEY_F7; + case XK_F8: return KEY_F8; + case XK_F9: return KEY_F9; + case XK_F10: return KEY_F10; + case XK_F11: return KEY_F11; + default: return 0; + } +} + static void keyboard_event_process_input_event_data(keyboard_event *self, event_extra_data *extra_data, int fd, key_callback callback, void *userdata) { struct input_event event; if(read(fd, &event, sizeof(event)) != sizeof(event)) { @@ -137,7 +184,12 @@ static void keyboard_event_process_input_event_data(keyboard_event *self, event_ if(event.type == EV_KEY) { keyboard_event_process_key_state_change(self, event, extra_data, fd); - switch(event.code) { + uint32_t keycode = event.code; + const uint32_t keysym = keycode_to_keysym(self, event.code); + if(keysym) + keycode = keysym_to_keycode(keysym); + + switch(keycode) { case KEY_LEFTSHIFT: self->lshift_button_state = event.value >= 1 ? KEYBOARD_BUTTON_PRESSED : KEYBOARD_BUTTON_RELEASED; break; @@ -165,21 +217,24 @@ static void keyboard_event_process_input_event_data(keyboard_event *self, event_ 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 alt_pressed = self->lalt_button_state == KEYBOARD_BUTTON_PRESSED || self->ralt_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, + //fprintf(stderr, "pressed key: %d, state: %d, shift: %s, ctrl: %s, alt: %s, meta: %s\n", keycode, 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(alt_pressed) - modifiers |= KEYBOARD_MODKEY_ALT; + 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)) + if(!callback(keycode, modifiers, event.value, userdata)) return; break; @@ -213,11 +268,41 @@ static bool keyboard_event_has_event_with_dev_input_fd(keyboard_event *self, int return false; } +/* TODO: Is there a more efficient way to do this? */ +static bool dev_input_is_virtual(int dev_input_id) { + DIR *dir = opendir("/sys/devices/virtual/input"); + if(!dir) + return false; + + bool is_virtual = false; + char virtual_input_filepath[1024]; + for(;;) { + struct dirent *entry = readdir(dir); + if(!entry) + break; + + if(strncmp(entry->d_name, "input", 5) != 0) + continue; + + snprintf(virtual_input_filepath, sizeof(virtual_input_filepath), "/sys/devices/virtual/input/%s/event%d", entry->d_name, dev_input_id); + if(access(virtual_input_filepath, F_OK) == 0) { + is_virtual = true; + break; + } + } + + closedir(dir); + return is_virtual; +} + 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)) + return false; + if(keyboard_event_has_event_with_dev_input_fd(self, dev_input_id)) return false; @@ -373,10 +458,12 @@ 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) { +bool keyboard_event_init(keyboard_event *self, bool poll_stdout_error, bool exclusive_grab, keyboard_grab_type grab_type, x11_context x_context) { memset(self, 0, sizeof(*self)); self->stdout_event_index = -1; self->hotplug_event_index = -1; + self->grab_type = grab_type; + self->x_context = x_context; if(exclusive_grab) { self->uinput_fd = setup_virtual_keyboard_input(GSR_UI_VIRTUAL_KEYBOARD_NAME); @@ -456,7 +543,25 @@ static void on_device_added_callback(const char *devname, void *userdata) { keyboard_event_try_add_device_if_keyboard(keyboard_ev, dev_input_filepath); } +#define MappingNotify 34 + +static void keyboard_event_poll_x11_events(keyboard_event *self) { + if(!self->x_context.display || !self->x_context.XPending || !self->x_context.XNextEvent || !self->x_context.XRefreshKeyboardMapping) + return; + + XEvent xev; + while(self->x_context.XPending(self->x_context.display)) { + xev.type = 0; + self->x_context.XNextEvent(self->x_context.display, &xev); + if(xev.type == MappingNotify) + self->x_context.XRefreshKeyboardMapping(xev.data); + } +} + void keyboard_event_poll_events(keyboard_event *self, int timeout_milliseconds, key_callback callback, void *userdata) { + /* TODO: Add the x11 connection to the below poll? */ + keyboard_event_poll_x11_events(self); + if(poll(self->event_polls, self->num_event_polls, timeout_milliseconds) <= 0) return; |