diff options
author | Drew DeVault <sir@cmpwn.com> | 2017-10-03 10:36:16 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-03 10:36:16 -0400 |
commit | 5f3c56de2805f11e84e0bb17edc30f455f0a3aaf (patch) | |
tree | 598f2b661eb02e78f78d6cd3f56a9877c23fc2d9 /rootston | |
parent | ddba002d2ac8060863db622c5391dc4b10f40760 (diff) | |
parent | eadda29e841383c6649e8f85494516aff00b493f (diff) |
Merge pull request #184 from emersion/rootston-keys
Keyboard modifiers
Diffstat (limited to 'rootston')
-rw-r--r-- | rootston/config.c | 70 | ||||
-rw-r--r-- | rootston/keyboard.c | 109 | ||||
-rw-r--r-- | rootston/main.c | 1 | ||||
-rw-r--r-- | rootston/rootston.ini.example | 6 |
4 files changed, 166 insertions, 20 deletions
diff --git a/rootston/config.c b/rootston/config.c index 3280c6d1..3a21aff3 100644 --- a/rootston/config.c +++ b/rootston/config.c @@ -171,6 +171,59 @@ static int config_ini_handler(void *user, const char *section, const char *name, } else { wlr_log(L_ERROR, "got unknown device config: %s", name); } + } else if (strcmp(section, "bindings") == 0) { + struct binding_config *bc = calloc(1, sizeof(struct binding_config)); + wl_list_insert(&config->bindings, &bc->link); + + bc->command = strdup(value); + + size_t keysyms_len = 1; + char *symnames = strdup(name); + for (char *c = symnames; *c != '\0'; c++) { + if (*c == '+') { + *c = '\0'; + keysyms_len++; + } + } + + // TODO: bc->keysyms is larger than needed + bc->keysyms = calloc(1, keysyms_len * sizeof(xkb_keysym_t)); + char *symname = symnames; + for (size_t i = 0; i < keysyms_len; i++) { + if (strcmp(symname, "Shift") == 0) { + bc->modifiers |= WLR_MODIFIER_SHIFT; + } else if (strcmp(symname, "Caps") == 0) { + bc->modifiers |= WLR_MODIFIER_CAPS; + } else if (strcmp(symname, "Ctrl") == 0) { + bc->modifiers |= WLR_MODIFIER_CTRL; + } else if (strcmp(symname, "Alt") == 0) { + bc->modifiers |= WLR_MODIFIER_ALT; + } else if (strcmp(symname, "Mod2") == 0) { + bc->modifiers |= WLR_MODIFIER_MOD2; + } else if (strcmp(symname, "Mod3") == 0) { + bc->modifiers |= WLR_MODIFIER_MOD3; + } else if (strcmp(symname, "Logo") == 0) { + bc->modifiers |= WLR_MODIFIER_LOGO; + } else if (strcmp(symname, "Mod5") == 0) { + bc->modifiers |= WLR_MODIFIER_MOD5; + } else { + xkb_keysym_t sym = xkb_keysym_from_name(symname, + XKB_KEYSYM_NO_FLAGS); + if (sym == XKB_KEY_NoSymbol) { + wlr_log(L_ERROR, "got unknown key binding symbol: %s", + symname); + wl_list_remove(&bc->link); + free(bc->keysyms); + free(bc); + break; + } + bc->keysyms[bc->keysyms_len] = sym; + bc->keysyms_len++; + } + symname += strlen(symname) + 1; + } + + free(symnames); } else { wlr_log(L_ERROR, "got unknown config section: %s", section); } @@ -182,6 +235,7 @@ struct roots_config *parse_args(int argc, char *argv[]) { struct roots_config *config = calloc(1, sizeof(struct roots_config)); wl_list_init(&config->outputs); wl_list_init(&config->devices); + wl_list_init(&config->bindings); int c; while ((c = getopt(argc, argv, "C:h")) != -1) { @@ -212,6 +266,15 @@ struct roots_config *parse_args(int argc, char *argv[]) { if (result == -1) { wlr_log(L_DEBUG, "No config file found. Using empty config."); + + struct binding_config *bc = calloc(1, sizeof(struct binding_config)); + wl_list_insert(&config->bindings, &bc->link); + bc->command = strdup("exit"); + bc->modifiers = WLR_MODIFIER_LOGO; + bc->keysyms_len = 2; + bc->keysyms = calloc(1, bc->keysyms_len * sizeof(xkb_keysym_t)); + bc->keysyms[0] = XKB_KEY_Meta_L; + bc->keysyms[1] = XKB_KEY_q; } else if (result == -2) { wlr_log(L_ERROR, "Could not allocate memory to parse config file"); exit(1); @@ -238,6 +301,13 @@ void roots_config_destroy(struct roots_config *config) { free(dc); } + struct binding_config *bc, *btmp = NULL; + wl_list_for_each_safe(bc, btmp, &config->bindings, link) { + free(bc->keysyms); + free(bc->command); + free(bc); + } + free(config->config_path); free(config->cursor.mapped_output); free(config->cursor.mapped_box); diff --git a/rootston/keyboard.c b/rootston/keyboard.c index 003aab98..6f6fa0e8 100644 --- a/rootston/keyboard.c +++ b/rootston/keyboard.c @@ -1,6 +1,7 @@ #include <assert.h> #include <stdint.h> #include <stdlib.h> +#include <unistd.h> #include <wayland-server.h> #include <wlr/types/wlr_input_device.h> #include <wlr/types/wlr_pointer.h> @@ -10,32 +11,100 @@ #include <xkbcommon/xkbcommon.h> #include "rootston/input.h" +static ssize_t keyboard_pressed_keysym_index(struct roots_keyboard *keyboard, + xkb_keysym_t keysym) { + for (size_t i = 0; i < ROOTS_KEYBOARD_PRESSED_KEYSYMS_CAP; i++) { + if (keyboard->pressed_keysyms[i] == keysym) { + return i; + } + } + return -1; +} + +static void keyboard_binding_execute(struct roots_keyboard *keyboard, + char *command) { + struct roots_server *server = keyboard->input->server; + if (strcmp(command, "exit") == 0) { + wl_display_terminate(server->wl_display); + } else { + pid_t pid = fork(); + if (pid < 0) { + wlr_log(L_ERROR, "cannot execute binding command: fork() failed"); + return; + } else if (pid == 0) { + execl("/bin/sh", "/bin/sh", "-c", command, (void *)NULL); + } + } +} + +static void keyboard_keysym_press(struct roots_keyboard *keyboard, + xkb_keysym_t keysym) { + ssize_t i = keyboard_pressed_keysym_index(keyboard, keysym); + if (i < 0) { + i = keyboard_pressed_keysym_index(keyboard, XKB_KEY_NoSymbol); + if (i >= 0) { + keyboard->pressed_keysyms[i] = keysym; + } + } + + if (keysym >= XKB_KEY_XF86Switch_VT_1 && + keysym <= XKB_KEY_XF86Switch_VT_12) { + struct roots_server *server = keyboard->input->server; + if (wlr_backend_is_multi(server->backend)) { + struct wlr_session *session = + wlr_multi_get_session(server->backend); + if (session) { + unsigned vt = keysym - XKB_KEY_XF86Switch_VT_1 + 1; + wlr_session_change_vt(session, vt); + } + } + return; + } + + uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); + struct wl_list *bindings = &keyboard->input->server->config->bindings; + struct binding_config *bc; + wl_list_for_each(bc, bindings, link) { + if (modifiers ^ bc->modifiers) { + continue; + } + + bool ok = true; + for (size_t i = 0; i < bc->keysyms_len; i++) { + ssize_t j = keyboard_pressed_keysym_index(keyboard, bc->keysyms[i]); + if (j < 0) { + ok = false; + break; + } + } + + if (ok) { + keyboard_binding_execute(keyboard, bc->command); + } + } +} + +static void keyboard_keysym_release(struct roots_keyboard *keyboard, + xkb_keysym_t keysym) { + ssize_t i = keyboard_pressed_keysym_index(keyboard, keysym); + if (i >= 0) { + keyboard->pressed_keysyms[i] = XKB_KEY_NoSymbol; + } +} + static void keyboard_key_notify(struct wl_listener *listener, void *data) { struct wlr_event_keyboard_key *event = data; struct roots_keyboard *keyboard = wl_container_of(listener, keyboard, key); - struct roots_input *input = keyboard->input; - struct roots_server *server = input->server; - enum wlr_key_state key_state = event->state; uint32_t keycode = event->keycode + 8; const xkb_keysym_t *syms; - int nsyms = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, - keycode, &syms); - for (int i = 0; i < nsyms; ++i) { - xkb_keysym_t sym = syms[i]; - if (sym == XKB_KEY_Escape) { - // TEMPORARY, probably - wl_display_terminate(server->wl_display); - } else if (key_state == WLR_KEY_PRESSED && - sym >= XKB_KEY_XF86Switch_VT_1 && - sym <= XKB_KEY_XF86Switch_VT_12) { - if (wlr_backend_is_multi(server->backend)) { - struct wlr_session *session = - wlr_multi_get_session(server->backend); - if (session) { - wlr_session_change_vt(session, sym - XKB_KEY_XF86Switch_VT_1 + 1); - } - } + int syms_len = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, + keycode, &syms); + for (int i = 0; i < syms_len; i++) { + if (event->state == WLR_KEY_PRESSED) { + keyboard_keysym_press(keyboard, syms[i]); + } else { // WLR_KEY_RELEASED + keyboard_keysym_release(keyboard, syms[i]); } } } diff --git a/rootston/main.c b/rootston/main.c index 7b1ca298..65a06a2f 100644 --- a/rootston/main.c +++ b/rootston/main.c @@ -41,6 +41,7 @@ int main(int argc, char **argv) { return 1; } + setenv("WAYLAND_DISPLAY", socket, true); wl_display_run(server.wl_display); return 0; } diff --git a/rootston/rootston.ini.example b/rootston/rootston.ini.example index 58e5816b..8dd51ea2 100644 --- a/rootston/rootston.ini.example +++ b/rootston/rootston.ini.example @@ -24,3 +24,9 @@ geometry = 2500x800 map-to-output = VGA-1 # Restrict cursor movements for this mouse to concrete rectangle geometry = 2500x800 + +# Keybindings +# Maps key combinations with commands to execute +# The special command "exit" stops the compositor +[bindings] +Logo+q=exit |