From ab6c1f88e6314ae0d56d5dfffc121a824175e7fd Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 2 Oct 2017 12:12:06 +0200 Subject: [PATCH 01/13] Add keyboard state to rootston --- include/rootston/input.h | 11 ++++++ rootston/keyboard.c | 81 ++++++++++++++++++++++++++++++++-------- 2 files changed, 77 insertions(+), 15 deletions(-) diff --git a/include/rootston/input.h b/include/rootston/input.h index ecd53f3b..a9cc2014 100644 --- a/include/rootston/input.h +++ b/include/rootston/input.h @@ -10,11 +10,22 @@ #include "rootston/view.h" #include "rootston/server.h" +#define ROOTS_KEYBOARD_PRESSED_KEYS_CAP 32 + +struct roots_keyboard_key { + uint32_t keycode; + + const xkb_keysym_t *syms; + size_t syms_len; +}; + struct roots_keyboard { struct roots_input *input; struct wlr_input_device *device; struct wl_listener key; struct wl_list link; + + struct roots_keyboard_key pressed_keys[ROOTS_KEYBOARD_PRESSED_KEYS_CAP]; }; struct roots_pointer { diff --git a/rootston/keyboard.c b/rootston/keyboard.c index 003aab98..84e5213c 100644 --- a/rootston/keyboard.c +++ b/rootston/keyboard.c @@ -10,36 +10,87 @@ #include #include "rootston/input.h" -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; +static ssize_t keyboard_pressed_key_index(struct roots_keyboard *keyboard, + uint32_t keycode) { + for (size_t i = 0; i < ROOTS_KEYBOARD_PRESSED_KEYS_CAP; i++) { + if (keyboard->pressed_keys[i].keycode == keycode) { + return i; + } + } + return -1; +} - 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]; +static void keyboard_key_press(struct roots_keyboard *keyboard, + struct roots_keyboard_key *key) { + struct roots_server *server = keyboard->input->server; + + ssize_t i = keyboard_pressed_key_index(keyboard, key->keycode); + if (i < 0) { + i = keyboard_pressed_key_index(keyboard, 0); + if (i >= 0) { + memcpy(&keyboard->pressed_keys[i], key, + sizeof(struct roots_keyboard_key)); + } + } + + for (size_t i = 0; i < key->syms_len; i++) { + xkb_keysym_t sym = key->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 && + } else if (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); + unsigned vt = sym - XKB_KEY_XF86Switch_VT_1 + 1; + wlr_session_change_vt(session, vt); } } } } } +static void keyboard_key_release(struct roots_keyboard *keyboard, + struct roots_keyboard_key *key) { + ssize_t i = keyboard_pressed_key_index(keyboard, key->keycode); + if (i >= 0) { + struct roots_keyboard_key *pressed_key = &keyboard->pressed_keys[i]; + memset(pressed_key, 0, sizeof(struct roots_keyboard_key)); + } +} + +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); + + uint32_t keycode = event->keycode + 8; + const xkb_keysym_t *syms; + int syms_len = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, + keycode, &syms); + + struct roots_keyboard_key *key = calloc(1, + sizeof(struct roots_keyboard_key)); + if (key == NULL) { + return; + } + key->keycode = keycode; + key->syms = syms; + key->syms_len = syms_len; + + switch (event->state) { + case WLR_KEY_PRESSED: + keyboard_key_press(keyboard, key); + break; + case WLR_KEY_RELEASED: + keyboard_key_release(keyboard, key); + break; + } + + free(key); +} + void keyboard_add(struct wlr_input_device *device, struct roots_input *input) { struct roots_keyboard *keyboard = calloc(sizeof(struct roots_keyboard), 1); device->data = keyboard; From d32018f1ac5f3b309a01627b4338e42f668b1887 Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 2 Oct 2017 15:24:01 +0200 Subject: [PATCH 02/13] Use keysyms instead of keycodes --- include/rootston/input.h | 11 ++---- rootston/keyboard.c | 78 ++++++++++++++++------------------------ 2 files changed, 32 insertions(+), 57 deletions(-) diff --git a/include/rootston/input.h b/include/rootston/input.h index a9cc2014..6161eb7a 100644 --- a/include/rootston/input.h +++ b/include/rootston/input.h @@ -10,14 +10,7 @@ #include "rootston/view.h" #include "rootston/server.h" -#define ROOTS_KEYBOARD_PRESSED_KEYS_CAP 32 - -struct roots_keyboard_key { - uint32_t keycode; - - const xkb_keysym_t *syms; - size_t syms_len; -}; +#define ROOTS_KEYBOARD_PRESSED_KEYSYMS_CAP 32 struct roots_keyboard { struct roots_input *input; @@ -25,7 +18,7 @@ struct roots_keyboard { struct wl_listener key; struct wl_list link; - struct roots_keyboard_key pressed_keys[ROOTS_KEYBOARD_PRESSED_KEYS_CAP]; + xkb_keysym_t pressed_keysyms[ROOTS_KEYBOARD_PRESSED_KEYSYMS_CAP]; }; struct roots_pointer { diff --git a/rootston/keyboard.c b/rootston/keyboard.c index 84e5213c..81d564d5 100644 --- a/rootston/keyboard.c +++ b/rootston/keyboard.c @@ -10,54 +10,49 @@ #include #include "rootston/input.h" -static ssize_t keyboard_pressed_key_index(struct roots_keyboard *keyboard, - uint32_t keycode) { - for (size_t i = 0; i < ROOTS_KEYBOARD_PRESSED_KEYS_CAP; i++) { - if (keyboard->pressed_keys[i].keycode == keycode) { +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_key_press(struct roots_keyboard *keyboard, - struct roots_keyboard_key *key) { +static void keyboard_keysym_press(struct roots_keyboard *keyboard, + xkb_keysym_t keysym) { struct roots_server *server = keyboard->input->server; - ssize_t i = keyboard_pressed_key_index(keyboard, key->keycode); + ssize_t i = keyboard_pressed_keysym_index(keyboard, keysym); if (i < 0) { - i = keyboard_pressed_key_index(keyboard, 0); + i = keyboard_pressed_keysym_index(keyboard, 0); if (i >= 0) { - memcpy(&keyboard->pressed_keys[i], key, - sizeof(struct roots_keyboard_key)); + keyboard->pressed_keysyms[i] = keysym; } } - for (size_t i = 0; i < key->syms_len; i++) { - xkb_keysym_t sym = key->syms[i]; - if (sym == XKB_KEY_Escape) { - // TEMPORARY, probably - wl_display_terminate(server->wl_display); - } else if (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) { - unsigned vt = sym - XKB_KEY_XF86Switch_VT_1 + 1; - wlr_session_change_vt(session, vt); - } + if (keysym == XKB_KEY_Escape) { + // TEMPORARY, probably + wl_display_terminate(server->wl_display); + } else if (keysym >= XKB_KEY_XF86Switch_VT_1 && + keysym <= XKB_KEY_XF86Switch_VT_12) { + 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); } } } } -static void keyboard_key_release(struct roots_keyboard *keyboard, - struct roots_keyboard_key *key) { - ssize_t i = keyboard_pressed_key_index(keyboard, key->keycode); +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) { - struct roots_keyboard_key *pressed_key = &keyboard->pressed_keys[i]; - memset(pressed_key, 0, sizeof(struct roots_keyboard_key)); + keyboard->pressed_keysyms[i] = 0; } } @@ -69,26 +64,13 @@ static void keyboard_key_notify(struct wl_listener *listener, void *data) { const xkb_keysym_t *syms; int syms_len = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, keycode, &syms); - - struct roots_keyboard_key *key = calloc(1, - sizeof(struct roots_keyboard_key)); - if (key == NULL) { - return; + 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]); + } } - key->keycode = keycode; - key->syms = syms; - key->syms_len = syms_len; - - switch (event->state) { - case WLR_KEY_PRESSED: - keyboard_key_press(keyboard, key); - break; - case WLR_KEY_RELEASED: - keyboard_key_release(keyboard, key); - break; - } - - free(key); } void keyboard_add(struct wlr_input_device *device, struct roots_input *input) { From 97d0fe89768697ff2f6f19f0c9a900591c1d1a13 Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 2 Oct 2017 16:25:48 +0200 Subject: [PATCH 03/13] Parse bindings in config, execute bindings --- include/rootston/config.h | 12 ++++++++++ rootston/config.c | 50 +++++++++++++++++++++++++++++++++++++++ rootston/keyboard.c | 40 ++++++++++++++++++++++++------- 3 files changed, 94 insertions(+), 8 deletions(-) diff --git a/include/rootston/config.h b/include/rootston/config.h index 0832d88d..deff7578 100644 --- a/include/rootston/config.h +++ b/include/rootston/config.h @@ -18,6 +18,17 @@ struct device_config { struct wl_list link; }; +enum binding_config_action { + BINDING_CONFIG_ACTION_QUIT, +}; + +struct binding_config { + xkb_keysym_t *keysyms; + size_t keysyms_len; + enum binding_config_action action; + struct wl_list link; +}; + struct roots_config { // TODO: Multiple cursors, multiseat struct { @@ -27,6 +38,7 @@ struct roots_config { struct wl_list outputs; struct wl_list devices; + struct wl_list bindings; char *config_path; }; diff --git a/rootston/config.c b/rootston/config.c index 3280c6d1..5c889975 100644 --- a/rootston/config.c +++ b/rootston/config.c @@ -171,6 +171,45 @@ 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); + + if (strcmp(value, "quit") == 0) { + bc->action = BINDING_CONFIG_ACTION_QUIT; + } else { + wlr_log(L_ERROR, "got unknown key binding action: %s", value); + wl_list_remove(&bc->link); + free(bc); + return 1; + } + + bc->keysyms_len = 1; + char *symnames = strdup(name); + for (char *c = symnames; *c != '\0'; c++) { + if (*c == '+') { + *c = '\0'; + bc->keysyms_len++; + } + } + + bc->keysyms = calloc(1, bc->keysyms_len * sizeof(xkb_keysym_t)); + char *symname = symnames; + for (size_t i = 0; i < bc->keysyms_len; i++) { + 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[i] = sym; + symname += strlen(symname) + 1; + } + + free(symnames); } else { wlr_log(L_ERROR, "got unknown config section: %s", section); } @@ -182,6 +221,15 @@ 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); + + // TEMPORARY, probably + struct binding_config *bc = calloc(1, sizeof(struct binding_config)); + wl_list_insert(&config->bindings, &bc->link); + bc->action = BINDING_CONFIG_ACTION_QUIT; + bc->keysyms_len = 1; + bc->keysyms = calloc(1, sizeof(xkb_keysym_t)); + bc->keysyms[0] = XKB_KEY_Escape; int c; while ((c = getopt(argc, argv, "C:h")) != -1) { @@ -238,6 +286,8 @@ void roots_config_destroy(struct roots_config *config) { free(dc); } + // TODO: free bindings + 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 81d564d5..79295ab4 100644 --- a/rootston/keyboard.c +++ b/rootston/keyboard.c @@ -20,23 +20,29 @@ static ssize_t keyboard_pressed_keysym_index(struct roots_keyboard *keyboard, return -1; } +static void keyboard_binding_execute(struct roots_keyboard *keyboard, + struct binding_config *bc) { + struct roots_server *server = keyboard->input->server; + switch (bc->action) { + case BINDING_CONFIG_ACTION_QUIT: + wl_display_terminate(server->wl_display); + break; + } +} + static void keyboard_keysym_press(struct roots_keyboard *keyboard, xkb_keysym_t keysym) { - struct roots_server *server = keyboard->input->server; - ssize_t i = keyboard_pressed_keysym_index(keyboard, keysym); if (i < 0) { - i = keyboard_pressed_keysym_index(keyboard, 0); + i = keyboard_pressed_keysym_index(keyboard, XKB_KEY_NoSymbol); if (i >= 0) { keyboard->pressed_keysyms[i] = keysym; } } - if (keysym == XKB_KEY_Escape) { - // TEMPORARY, probably - wl_display_terminate(server->wl_display); - } else if (keysym >= XKB_KEY_XF86Switch_VT_1 && + 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); @@ -45,6 +51,24 @@ static void keyboard_keysym_press(struct roots_keyboard *keyboard, wlr_session_change_vt(session, vt); } } + return; + } + + struct wl_list *bindings = &keyboard->input->server->config->bindings; + struct binding_config *bc; + wl_list_for_each(bc, bindings, link) { + 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); + } } } @@ -52,7 +76,7 @@ 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] = 0; + keyboard->pressed_keysyms[i] = XKB_KEY_NoSymbol; } } From 238ded1578925a1c80cba1d838ff428c25249e6a Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 2 Oct 2017 17:53:30 +0200 Subject: [PATCH 04/13] Use a command string rather than an action enum --- include/rootston/config.h | 6 +----- rootston/config.c | 11 ++--------- rootston/keyboard.c | 8 +++----- 3 files changed, 6 insertions(+), 19 deletions(-) diff --git a/include/rootston/config.h b/include/rootston/config.h index deff7578..ef101f2d 100644 --- a/include/rootston/config.h +++ b/include/rootston/config.h @@ -18,14 +18,10 @@ struct device_config { struct wl_list link; }; -enum binding_config_action { - BINDING_CONFIG_ACTION_QUIT, -}; - struct binding_config { xkb_keysym_t *keysyms; size_t keysyms_len; - enum binding_config_action action; + char *command; struct wl_list link; }; diff --git a/rootston/config.c b/rootston/config.c index 5c889975..1f524ab2 100644 --- a/rootston/config.c +++ b/rootston/config.c @@ -175,14 +175,7 @@ static int config_ini_handler(void *user, const char *section, const char *name, struct binding_config *bc = calloc(1, sizeof(struct binding_config)); wl_list_insert(&config->bindings, &bc->link); - if (strcmp(value, "quit") == 0) { - bc->action = BINDING_CONFIG_ACTION_QUIT; - } else { - wlr_log(L_ERROR, "got unknown key binding action: %s", value); - wl_list_remove(&bc->link); - free(bc); - return 1; - } + bc->command = strdup(value); bc->keysyms_len = 1; char *symnames = strdup(name); @@ -226,7 +219,7 @@ struct roots_config *parse_args(int argc, char *argv[]) { // TEMPORARY, probably struct binding_config *bc = calloc(1, sizeof(struct binding_config)); wl_list_insert(&config->bindings, &bc->link); - bc->action = BINDING_CONFIG_ACTION_QUIT; + bc->command = strdup("exit"); bc->keysyms_len = 1; bc->keysyms = calloc(1, sizeof(xkb_keysym_t)); bc->keysyms[0] = XKB_KEY_Escape; diff --git a/rootston/keyboard.c b/rootston/keyboard.c index 79295ab4..d3f0bc67 100644 --- a/rootston/keyboard.c +++ b/rootston/keyboard.c @@ -21,12 +21,10 @@ static ssize_t keyboard_pressed_keysym_index(struct roots_keyboard *keyboard, } static void keyboard_binding_execute(struct roots_keyboard *keyboard, - struct binding_config *bc) { + char *command) { struct roots_server *server = keyboard->input->server; - switch (bc->action) { - case BINDING_CONFIG_ACTION_QUIT: + if (strcmp(command, "exit") == 0) { wl_display_terminate(server->wl_display); - break; } } @@ -67,7 +65,7 @@ static void keyboard_keysym_press(struct roots_keyboard *keyboard, } if (ok) { - keyboard_binding_execute(keyboard, bc); + keyboard_binding_execute(keyboard, bc->command); } } } From 517a709cddcdc5b5101cfefb7bec9d3c46ba10b0 Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 2 Oct 2017 18:03:28 +0200 Subject: [PATCH 05/13] Bind logo+q when no config file is specified --- rootston/config.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/rootston/config.c b/rootston/config.c index 1f524ab2..49bb79fb 100644 --- a/rootston/config.c +++ b/rootston/config.c @@ -216,14 +216,6 @@ struct roots_config *parse_args(int argc, char *argv[]) { wl_list_init(&config->devices); wl_list_init(&config->bindings); - // TEMPORARY, probably - struct binding_config *bc = calloc(1, sizeof(struct binding_config)); - wl_list_insert(&config->bindings, &bc->link); - bc->command = strdup("exit"); - bc->keysyms_len = 1; - bc->keysyms = calloc(1, sizeof(xkb_keysym_t)); - bc->keysyms[0] = XKB_KEY_Escape; - int c; while ((c = getopt(argc, argv, "C:h")) != -1) { switch (c) { @@ -253,6 +245,14 @@ 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->keysyms_len = 2; + bc->keysyms = calloc(1, 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); From c6866998173138d582a39a5ba59705fe1cb50b1a Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 2 Oct 2017 18:05:52 +0200 Subject: [PATCH 06/13] Free config->bindings --- rootston/config.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/rootston/config.c b/rootston/config.c index 49bb79fb..a80a8304 100644 --- a/rootston/config.c +++ b/rootston/config.c @@ -250,7 +250,7 @@ struct roots_config *parse_args(int argc, char *argv[]) { wl_list_insert(&config->bindings, &bc->link); bc->command = strdup("exit"); bc->keysyms_len = 2; - bc->keysyms = calloc(1, sizeof(xkb_keysym_t)); + 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) { @@ -279,7 +279,12 @@ void roots_config_destroy(struct roots_config *config) { free(dc); } - // TODO: free bindings + 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); From eb0b315c6ca190cdc51d83b80405272ccbba45c0 Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 2 Oct 2017 18:21:39 +0200 Subject: [PATCH 07/13] exec command if it's not exit --- rootston/keyboard.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/rootston/keyboard.c b/rootston/keyboard.c index d3f0bc67..5ac6e59b 100644 --- a/rootston/keyboard.c +++ b/rootston/keyboard.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -25,6 +26,14 @@ static void keyboard_binding_execute(struct roots_keyboard *keyboard, 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); + } } } From 2fccff16eb48fbf57f9ad237e3dc748676d9fb03 Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 2 Oct 2017 19:23:30 +0200 Subject: [PATCH 08/13] Begin modifiers support --- include/wlr/types/wlr_keyboard.h | 26 +++++++++++++-- types/wlr_keyboard.c | 56 ++++++++++++++++++++++++++++---- 2 files changed, 73 insertions(+), 9 deletions(-) diff --git a/include/wlr/types/wlr_keyboard.h b/include/wlr/types/wlr_keyboard.h index af205c63..97517cbe 100644 --- a/include/wlr/types/wlr_keyboard.h +++ b/include/wlr/types/wlr_keyboard.h @@ -6,13 +6,25 @@ #include #include -enum WLR_KEYBOARD_LED { +enum wlr_keyboard_led { WLR_LED_NUM_LOCK = 1, WLR_LED_CAPS_LOCK = 2, WLR_LED_SCROLL_LOCK = 4, WLR_LED_LAST }; +enum wlr_keyboard_modifier { + WLR_MODIFIER_SHIFT = 1, + WLR_MODIFIER_CAPS = 2, + WLR_MODIFIER_CTRL = 4, + WLR_MODIFIER_ALT = 8, + WLR_MODIFIER_MOD2 = 16, + WLR_MODIFIER_MOD3 = 32, + WLR_MODIFIER_LOGO = 64, + WLR_MODIFIER_MOD5 = 128, + WLR_MODIFIER_LAST +}; + struct wlr_keyboard_impl; struct wlr_keyboard { @@ -23,7 +35,15 @@ struct wlr_keyboard { size_t keymap_size; struct xkb_keymap *keymap; struct xkb_state *xkb_state; - xkb_led_index_t leds[WLR_LED_LAST]; + xkb_led_index_t led_indexes[WLR_LED_LAST]; + xkb_mod_index_t mod_indexes[WLR_MODIFIER_LAST]; + + struct { + xkb_mod_mask_t depressed; + xkb_mod_mask_t latched; + xkb_mod_mask_t locked; + xkb_mod_mask_t group; + } modifiers; struct { struct wl_signal key; @@ -48,6 +68,6 @@ struct wlr_event_keyboard_key { }; void wlr_keyboard_set_keymap(struct wlr_keyboard *kb, - struct xkb_keymap *keymap); + struct xkb_keymap *keymap); #endif diff --git a/types/wlr_keyboard.c b/types/wlr_keyboard.c index b356f63a..dc3b8f8e 100644 --- a/types/wlr_keyboard.c +++ b/types/wlr_keyboard.c @@ -13,19 +13,45 @@ int os_create_anonymous_file(off_t size); static void keyboard_led_update(struct wlr_keyboard *keyboard) { uint32_t leds = 0; for (uint32_t i = 0; i < WLR_LED_LAST; ++i) { - if (xkb_state_led_index_is_active(keyboard->xkb_state, keyboard->leds[i])) { + if (xkb_state_led_index_is_active(keyboard->xkb_state, + keyboard->led_indexes[i])) { leds |= (1 << i); } } wlr_keyboard_led_update(keyboard, leds); } +static void keyboard_modifier_update(struct wlr_keyboard *keyboard) { + xkb_mod_mask_t depressed = xkb_state_serialize_mods(keyboard->xkb_state, + XKB_STATE_DEPRESSED); + xkb_mod_mask_t latched = xkb_state_serialize_mods(keyboard->xkb_state, + XKB_STATE_LATCHED); + xkb_mod_mask_t locked = xkb_state_serialize_mods(keyboard->xkb_state, + XKB_STATE_LOCKED); + xkb_mod_mask_t group = xkb_state_serialize_layout(keyboard->xkb_state, + XKB_STATE_LAYOUT_EFFECTIVE); + if (depressed == keyboard->modifiers.depressed && + latched == keyboard->modifiers.latched && + locked == keyboard->modifiers.locked && + group == keyboard->modifiers.group) { + return; + } + + keyboard->modifiers.depressed = depressed; + keyboard->modifiers.latched = latched; + keyboard->modifiers.locked = locked; + keyboard->modifiers.group = group; + + // TODO: wl_keyboard_send_modifiers +} + void wlr_keyboard_update_state(struct wlr_keyboard *keyboard, struct wlr_event_keyboard_key *event) { uint32_t keycode = event->keycode + 8; xkb_state_update_key(keyboard->xkb_state, keycode, - event->state == WLR_KEY_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP); + event->state == WLR_KEY_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP); keyboard_led_update(keyboard); + keyboard_modifier_update(keyboard); wl_signal_emit(&keyboard->events.key, event); } @@ -57,21 +83,39 @@ void wlr_keyboard_set_keymap(struct wlr_keyboard *kb, wlr_log(L_DEBUG, "Keymap set"); kb->keymap = keymap; assert(kb->xkb_state = xkb_state_new(kb->keymap)); - const char *led_names[3] = { + + const char *led_names[WLR_LED_LAST] = { XKB_LED_NAME_NUM, XKB_LED_NAME_CAPS, XKB_LED_NAME_SCROLL }; - for (uint32_t i = 0; i < 3; ++i) { - kb->leds[i] = xkb_map_led_get_index(kb->keymap, led_names[i]); + for (size_t i = 0; i < WLR_LED_LAST; ++i) { + kb->led_indexes[i] = xkb_map_led_get_index(kb->keymap, led_names[i]); } + + const char *mod_names[WLR_MODIFIER_LAST] = { + XKB_MOD_NAME_SHIFT, + XKB_MOD_NAME_CAPS, + XKB_MOD_NAME_CTRL, // "Control" + XKB_MOD_NAME_ALT, // "Mod1" + XKB_MOD_NAME_NUM, // "Mod2" + "Mod3", + XKB_MOD_NAME_LOGO, // "Mod4" + "Mod5", + }; + // TODO: there's also "Ctrl", "Alt"? + for (size_t i = 0; i < WLR_LED_LAST; ++i) { + kb->mod_indexes[i] = xkb_map_mod_get_index(kb->keymap, mod_names[i]); + } + char *keymap_str = xkb_keymap_get_as_string(kb->keymap, XKB_KEYMAP_FORMAT_TEXT_V1); kb->keymap_size = strlen(keymap_str) + 1; kb->keymap_fd = os_create_anonymous_file(kb->keymap_size); void *ptr = mmap(NULL, kb->keymap_size, - PROT_READ | PROT_WRITE, MAP_SHARED, kb->keymap_fd, 0); + PROT_READ | PROT_WRITE, MAP_SHARED, kb->keymap_fd, 0); strcpy(ptr, keymap_str); free(keymap_str); + wl_signal_emit(&kb->events.keymap, kb); } From 3a8fdebaaa819f7b27b51e507cccff638b4939ae Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 2 Oct 2017 20:21:47 +0200 Subject: [PATCH 09/13] Fix modifiers handling, add wlr_keyboard_get_modifiers --- include/wlr/types/wlr_keyboard.h | 14 +++++---- types/wlr_keyboard.c | 49 ++++++++++++-------------------- types/wlr_seat.c | 32 ++++++++++++++------- 3 files changed, 47 insertions(+), 48 deletions(-) diff --git a/include/wlr/types/wlr_keyboard.h b/include/wlr/types/wlr_keyboard.h index 97517cbe..2d9f1490 100644 --- a/include/wlr/types/wlr_keyboard.h +++ b/include/wlr/types/wlr_keyboard.h @@ -6,13 +6,16 @@ #include #include +#define WLR_LED_COUNT 3 + enum wlr_keyboard_led { WLR_LED_NUM_LOCK = 1, WLR_LED_CAPS_LOCK = 2, WLR_LED_SCROLL_LOCK = 4, - WLR_LED_LAST }; +#define WLR_MODIFIER_COUNT 8 + enum wlr_keyboard_modifier { WLR_MODIFIER_SHIFT = 1, WLR_MODIFIER_CAPS = 2, @@ -22,7 +25,6 @@ enum wlr_keyboard_modifier { WLR_MODIFIER_MOD3 = 32, WLR_MODIFIER_LOGO = 64, WLR_MODIFIER_MOD5 = 128, - WLR_MODIFIER_LAST }; struct wlr_keyboard_impl; @@ -35,8 +37,8 @@ struct wlr_keyboard { size_t keymap_size; struct xkb_keymap *keymap; struct xkb_state *xkb_state; - xkb_led_index_t led_indexes[WLR_LED_LAST]; - xkb_mod_index_t mod_indexes[WLR_MODIFIER_LAST]; + xkb_led_index_t led_indexes[WLR_LED_COUNT]; + xkb_mod_index_t mod_indexes[WLR_MODIFIER_COUNT]; struct { xkb_mod_mask_t depressed; @@ -53,8 +55,6 @@ struct wlr_keyboard { void *data; }; -void wlr_keyboard_led_update(struct wlr_keyboard *keyboard, uint32_t leds); - enum wlr_key_state { WLR_KEY_RELEASED, WLR_KEY_PRESSED, @@ -69,5 +69,7 @@ struct wlr_event_keyboard_key { void wlr_keyboard_set_keymap(struct wlr_keyboard *kb, struct xkb_keymap *keymap); +void wlr_keyboard_led_update(struct wlr_keyboard *keyboard, uint32_t leds); +uint32_t wlr_keyboard_get_modifiers(struct wlr_keyboard *keyboard); #endif diff --git a/types/wlr_keyboard.c b/types/wlr_keyboard.c index dc3b8f8e..4d7f215c 100644 --- a/types/wlr_keyboard.c +++ b/types/wlr_keyboard.c @@ -12,7 +12,7 @@ int os_create_anonymous_file(off_t size); static void keyboard_led_update(struct wlr_keyboard *keyboard) { uint32_t leds = 0; - for (uint32_t i = 0; i < WLR_LED_LAST; ++i) { + for (uint32_t i = 0; i < WLR_LED_COUNT; ++i) { if (xkb_state_led_index_is_active(keyboard->xkb_state, keyboard->led_indexes[i])) { leds |= (1 << i); @@ -21,37 +21,12 @@ static void keyboard_led_update(struct wlr_keyboard *keyboard) { wlr_keyboard_led_update(keyboard, leds); } -static void keyboard_modifier_update(struct wlr_keyboard *keyboard) { - xkb_mod_mask_t depressed = xkb_state_serialize_mods(keyboard->xkb_state, - XKB_STATE_DEPRESSED); - xkb_mod_mask_t latched = xkb_state_serialize_mods(keyboard->xkb_state, - XKB_STATE_LATCHED); - xkb_mod_mask_t locked = xkb_state_serialize_mods(keyboard->xkb_state, - XKB_STATE_LOCKED); - xkb_mod_mask_t group = xkb_state_serialize_layout(keyboard->xkb_state, - XKB_STATE_LAYOUT_EFFECTIVE); - if (depressed == keyboard->modifiers.depressed && - latched == keyboard->modifiers.latched && - locked == keyboard->modifiers.locked && - group == keyboard->modifiers.group) { - return; - } - - keyboard->modifiers.depressed = depressed; - keyboard->modifiers.latched = latched; - keyboard->modifiers.locked = locked; - keyboard->modifiers.group = group; - - // TODO: wl_keyboard_send_modifiers -} - void wlr_keyboard_update_state(struct wlr_keyboard *keyboard, struct wlr_event_keyboard_key *event) { uint32_t keycode = event->keycode + 8; xkb_state_update_key(keyboard->xkb_state, keycode, event->state == WLR_KEY_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP); keyboard_led_update(keyboard); - keyboard_modifier_update(keyboard); wl_signal_emit(&keyboard->events.key, event); } @@ -84,16 +59,16 @@ void wlr_keyboard_set_keymap(struct wlr_keyboard *kb, kb->keymap = keymap; assert(kb->xkb_state = xkb_state_new(kb->keymap)); - const char *led_names[WLR_LED_LAST] = { + const char *led_names[WLR_LED_COUNT] = { XKB_LED_NAME_NUM, XKB_LED_NAME_CAPS, - XKB_LED_NAME_SCROLL + XKB_LED_NAME_SCROLL, }; - for (size_t i = 0; i < WLR_LED_LAST; ++i) { + for (size_t i = 0; i < WLR_LED_COUNT; ++i) { kb->led_indexes[i] = xkb_map_led_get_index(kb->keymap, led_names[i]); } - const char *mod_names[WLR_MODIFIER_LAST] = { + const char *mod_names[WLR_MODIFIER_COUNT] = { XKB_MOD_NAME_SHIFT, XKB_MOD_NAME_CAPS, XKB_MOD_NAME_CTRL, // "Control" @@ -104,7 +79,7 @@ void wlr_keyboard_set_keymap(struct wlr_keyboard *kb, "Mod5", }; // TODO: there's also "Ctrl", "Alt"? - for (size_t i = 0; i < WLR_LED_LAST; ++i) { + for (size_t i = 0; i < WLR_MODIFIER_COUNT; ++i) { kb->mod_indexes[i] = xkb_map_mod_get_index(kb->keymap, mod_names[i]); } @@ -119,3 +94,15 @@ void wlr_keyboard_set_keymap(struct wlr_keyboard *kb, wl_signal_emit(&kb->events.keymap, kb); } + +uint32_t wlr_keyboard_get_modifiers(struct wlr_keyboard *kb) { + xkb_mod_mask_t mask = kb->modifiers.depressed | kb->modifiers.latched; + uint32_t modifiers = 0; + for (size_t i = 0; i < WLR_MODIFIER_COUNT; ++i) { + if (kb->mod_indexes[i] != XKB_MOD_INVALID && + (mask & (1 << kb->mod_indexes[i]))) { + modifiers |= (1 << i); + } + } + return modifiers; +} diff --git a/types/wlr_seat.c b/types/wlr_seat.c index aaff6005..ff050223 100644 --- a/types/wlr_seat.c +++ b/types/wlr_seat.c @@ -391,9 +391,9 @@ static void keyboard_key_notify(struct wl_listener *listener, void *data) { // TODO: We should probably lift all of the keys set by the other // keyboard wl_keyboard_send_keymap(handle->keyboard, - WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, - seat_kb->keyboard->keymap_fd, - seat_kb->keyboard->keymap_size); + WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, + seat_kb->keyboard->keymap_fd, + seat_kb->keyboard->keymap_size); if (wl_resource_get_version(handle->keyboard) >= 2) { // TODO: Make this better @@ -402,21 +402,31 @@ static void keyboard_key_notify(struct wl_listener *listener, void *data) { handle->seat_keyboard = seat_kb; } - uint32_t depressed = xkb_state_serialize_mods(keyboard->xkb_state, + xkb_mod_mask_t depressed = xkb_state_serialize_mods(keyboard->xkb_state, XKB_STATE_MODS_DEPRESSED); - uint32_t latched = xkb_state_serialize_mods(keyboard->xkb_state, + xkb_mod_mask_t latched = xkb_state_serialize_mods(keyboard->xkb_state, XKB_STATE_MODS_LATCHED); - uint32_t locked = xkb_state_serialize_mods(keyboard->xkb_state, + xkb_mod_mask_t locked = xkb_state_serialize_mods(keyboard->xkb_state, XKB_STATE_MODS_LOCKED); - uint32_t group = xkb_state_serialize_layout(keyboard->xkb_state, + xkb_mod_mask_t group = xkb_state_serialize_layout(keyboard->xkb_state, XKB_STATE_LAYOUT_EFFECTIVE); + if (depressed != keyboard->modifiers.depressed || + latched != keyboard->modifiers.latched || + locked != keyboard->modifiers.locked || + group != keyboard->modifiers.group) { + keyboard->modifiers.depressed = depressed; + keyboard->modifiers.latched = latched; + keyboard->modifiers.locked = locked; + keyboard->modifiers.group = group; + + uint32_t modifiers_serial = wl_display_next_serial(seat->display); + wl_keyboard_send_modifiers(handle->keyboard, modifiers_serial, depressed, + latched, locked, group); + } - uint32_t modifiers_serial = wl_display_next_serial(seat->display); uint32_t key_serial = wl_display_next_serial(seat->display); - wl_keyboard_send_modifiers(handle->keyboard, modifiers_serial, - depressed, latched, locked, group); wl_keyboard_send_key(handle->keyboard, key_serial, - (uint32_t)event->time_usec, event->keycode, key_state); + (uint32_t)event->time_usec, event->keycode, key_state); } static void keyboard_keymap_notify(struct wl_listener *listener, void *data) { From d3d9aca40cfccf7f3d03a3111f732c884aab8766 Mon Sep 17 00:00:00 2001 From: emersion Date: Tue, 3 Oct 2017 08:40:10 +0200 Subject: [PATCH 10/13] Add modifiers support in wayland backend, parse modifiers in rootston config --- backend/wayland/wl_seat.c | 4 +++ include/rootston/config.h | 1 + include/wlr/interfaces/wlr_keyboard.h | 3 ++ rootston/config.c | 48 +++++++++++++++++++-------- rootston/keyboard.c | 5 +++ types/wlr_keyboard.c | 9 +++++ 6 files changed, 57 insertions(+), 13 deletions(-) diff --git a/backend/wayland/wl_seat.c b/backend/wayland/wl_seat.c index ba3feb8d..44ce6028 100644 --- a/backend/wayland/wl_seat.c +++ b/backend/wayland/wl_seat.c @@ -155,7 +155,11 @@ static void keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard, static void keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) { + struct wlr_input_device *dev = data; + assert(dev && dev->keyboard); + wlr_keyboard_update_modifiers(dev->keyboard, mods_depressed, mods_latched, + mods_locked, group); } static void keyboard_handle_repeat_info(void *data, struct wl_keyboard *wl_keyboard, diff --git a/include/rootston/config.h b/include/rootston/config.h index ef101f2d..ece11829 100644 --- a/include/rootston/config.h +++ b/include/rootston/config.h @@ -19,6 +19,7 @@ struct device_config { }; struct binding_config { + uint32_t modifiers; xkb_keysym_t *keysyms; size_t keysyms_len; char *command; diff --git a/include/wlr/interfaces/wlr_keyboard.h b/include/wlr/interfaces/wlr_keyboard.h index 78c1f753..9755267a 100644 --- a/include/wlr/interfaces/wlr_keyboard.h +++ b/include/wlr/interfaces/wlr_keyboard.h @@ -13,5 +13,8 @@ void wlr_keyboard_init(struct wlr_keyboard *keyboard, struct wlr_keyboard_impl * void wlr_keyboard_destroy(struct wlr_keyboard *keyboard); void wlr_keyboard_update_state(struct wlr_keyboard *keyboard, struct wlr_event_keyboard_key *event); +void wlr_keyboard_update_modifiers(struct wlr_keyboard *keyboard, + uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, + uint32_t group); #endif diff --git a/rootston/config.c b/rootston/config.c index a80a8304..3a21aff3 100644 --- a/rootston/config.c +++ b/rootston/config.c @@ -177,28 +177,49 @@ static int config_ini_handler(void *user, const char *section, const char *name, bc->command = strdup(value); - bc->keysyms_len = 1; + size_t keysyms_len = 1; char *symnames = strdup(name); for (char *c = symnames; *c != '\0'; c++) { if (*c == '+') { *c = '\0'; - bc->keysyms_len++; + keysyms_len++; } } - bc->keysyms = calloc(1, bc->keysyms_len * sizeof(xkb_keysym_t)); + // 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 < bc->keysyms_len; i++) { - 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; + 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++; } - bc->keysyms[i] = sym; symname += strlen(symname) + 1; } @@ -249,6 +270,7 @@ struct roots_config *parse_args(int argc, char *argv[]) { 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; diff --git a/rootston/keyboard.c b/rootston/keyboard.c index 5ac6e59b..6f6fa0e8 100644 --- a/rootston/keyboard.c +++ b/rootston/keyboard.c @@ -61,9 +61,14 @@ static void keyboard_keysym_press(struct roots_keyboard *keyboard, 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]); diff --git a/types/wlr_keyboard.c b/types/wlr_keyboard.c index 4d7f215c..9c111af4 100644 --- a/types/wlr_keyboard.c +++ b/types/wlr_keyboard.c @@ -30,6 +30,15 @@ void wlr_keyboard_update_state(struct wlr_keyboard *keyboard, wl_signal_emit(&keyboard->events.key, event); } +void wlr_keyboard_update_modifiers(struct wlr_keyboard *keyboard, + uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, + uint32_t group) { + keyboard->modifiers.depressed = mods_depressed; + keyboard->modifiers.latched = mods_latched; + keyboard->modifiers.locked = mods_locked; + keyboard->modifiers.group = group; +} + void wlr_keyboard_init(struct wlr_keyboard *kb, struct wlr_keyboard_impl *impl) { kb->impl = impl; From d71344bcddd2fe7447224f29a31695f5844b4474 Mon Sep 17 00:00:00 2001 From: emersion Date: Tue, 3 Oct 2017 09:15:48 +0200 Subject: [PATCH 11/13] Fix modifiers support with backend != wayland --- rootston/rootston.ini.example | 6 ++++++ types/wlr_keyboard.c | 35 ++++++++++++++++++++++++++--------- types/wlr_seat.c | 26 ++++---------------------- 3 files changed, 36 insertions(+), 31 deletions(-) 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 diff --git a/types/wlr_keyboard.c b/types/wlr_keyboard.c index 9c111af4..168475bd 100644 --- a/types/wlr_keyboard.c +++ b/types/wlr_keyboard.c @@ -21,22 +21,39 @@ static void keyboard_led_update(struct wlr_keyboard *keyboard) { wlr_keyboard_led_update(keyboard, leds); } +void wlr_keyboard_update_modifiers(struct wlr_keyboard *keyboard, + uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, + uint32_t group) { + if (mods_depressed == keyboard->modifiers.depressed && + mods_latched == keyboard->modifiers.latched && + mods_locked == keyboard->modifiers.locked && + group == keyboard->modifiers.group) { + return; + } + keyboard->modifiers.depressed = mods_depressed; + keyboard->modifiers.latched = mods_latched; + keyboard->modifiers.locked = mods_locked; + keyboard->modifiers.group = group; +} + void wlr_keyboard_update_state(struct wlr_keyboard *keyboard, struct wlr_event_keyboard_key *event) { uint32_t keycode = event->keycode + 8; xkb_state_update_key(keyboard->xkb_state, keycode, event->state == WLR_KEY_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP); keyboard_led_update(keyboard); - wl_signal_emit(&keyboard->events.key, event); -} -void wlr_keyboard_update_modifiers(struct wlr_keyboard *keyboard, - uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, - uint32_t group) { - keyboard->modifiers.depressed = mods_depressed; - keyboard->modifiers.latched = mods_latched; - keyboard->modifiers.locked = mods_locked; - keyboard->modifiers.group = group; + xkb_mod_mask_t depressed = xkb_state_serialize_mods(keyboard->xkb_state, + XKB_STATE_MODS_DEPRESSED); + xkb_mod_mask_t latched = xkb_state_serialize_mods(keyboard->xkb_state, + XKB_STATE_MODS_LATCHED); + xkb_mod_mask_t locked = xkb_state_serialize_mods(keyboard->xkb_state, + XKB_STATE_MODS_LOCKED); + xkb_mod_mask_t group = xkb_state_serialize_layout(keyboard->xkb_state, + XKB_STATE_LAYOUT_EFFECTIVE); + wlr_keyboard_update_modifiers(keyboard, depressed, latched, locked, group); + + wl_signal_emit(&keyboard->events.key, event); } void wlr_keyboard_init(struct wlr_keyboard *kb, diff --git a/types/wlr_seat.c b/types/wlr_seat.c index ff050223..66b2f18e 100644 --- a/types/wlr_seat.c +++ b/types/wlr_seat.c @@ -402,28 +402,10 @@ static void keyboard_key_notify(struct wl_listener *listener, void *data) { handle->seat_keyboard = seat_kb; } - xkb_mod_mask_t depressed = xkb_state_serialize_mods(keyboard->xkb_state, - XKB_STATE_MODS_DEPRESSED); - xkb_mod_mask_t latched = xkb_state_serialize_mods(keyboard->xkb_state, - XKB_STATE_MODS_LATCHED); - xkb_mod_mask_t locked = xkb_state_serialize_mods(keyboard->xkb_state, - XKB_STATE_MODS_LOCKED); - xkb_mod_mask_t group = xkb_state_serialize_layout(keyboard->xkb_state, - XKB_STATE_LAYOUT_EFFECTIVE); - if (depressed != keyboard->modifiers.depressed || - latched != keyboard->modifiers.latched || - locked != keyboard->modifiers.locked || - group != keyboard->modifiers.group) { - keyboard->modifiers.depressed = depressed; - keyboard->modifiers.latched = latched; - keyboard->modifiers.locked = locked; - keyboard->modifiers.group = group; - - uint32_t modifiers_serial = wl_display_next_serial(seat->display); - wl_keyboard_send_modifiers(handle->keyboard, modifiers_serial, depressed, - latched, locked, group); - } - + uint32_t modifiers_serial = wl_display_next_serial(seat->display); + wl_keyboard_send_modifiers(handle->keyboard, modifiers_serial, + keyboard->modifiers.depressed, keyboard->modifiers.latched, + keyboard->modifiers.locked, keyboard->modifiers.group); uint32_t key_serial = wl_display_next_serial(seat->display); wl_keyboard_send_key(handle->keyboard, key_serial, (uint32_t)event->time_usec, event->keycode, key_state); From 521e893528eb68691fad4944257fd073619d6338 Mon Sep 17 00:00:00 2001 From: emersion Date: Tue, 3 Oct 2017 14:03:26 +0200 Subject: [PATCH 12/13] Add modifiers signal, remove wlr_keyboard_update_modifiers --- backend/wayland/wl_seat.c | 4 -- include/wlr/interfaces/wlr_keyboard.h | 3 -- include/wlr/types/wlr_keyboard.h | 1 + include/wlr/types/wlr_seat.h | 1 + types/wlr_keyboard.c | 46 ++++++++++---------- types/wlr_seat.c | 62 +++++++++++++++++++-------- 6 files changed, 69 insertions(+), 48 deletions(-) diff --git a/backend/wayland/wl_seat.c b/backend/wayland/wl_seat.c index 44ce6028..ba3feb8d 100644 --- a/backend/wayland/wl_seat.c +++ b/backend/wayland/wl_seat.c @@ -155,11 +155,7 @@ static void keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard, static void keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) { - struct wlr_input_device *dev = data; - assert(dev && dev->keyboard); - wlr_keyboard_update_modifiers(dev->keyboard, mods_depressed, mods_latched, - mods_locked, group); } static void keyboard_handle_repeat_info(void *data, struct wl_keyboard *wl_keyboard, diff --git a/include/wlr/interfaces/wlr_keyboard.h b/include/wlr/interfaces/wlr_keyboard.h index 9755267a..78c1f753 100644 --- a/include/wlr/interfaces/wlr_keyboard.h +++ b/include/wlr/interfaces/wlr_keyboard.h @@ -13,8 +13,5 @@ void wlr_keyboard_init(struct wlr_keyboard *keyboard, struct wlr_keyboard_impl * void wlr_keyboard_destroy(struct wlr_keyboard *keyboard); void wlr_keyboard_update_state(struct wlr_keyboard *keyboard, struct wlr_event_keyboard_key *event); -void wlr_keyboard_update_modifiers(struct wlr_keyboard *keyboard, - uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, - uint32_t group); #endif diff --git a/include/wlr/types/wlr_keyboard.h b/include/wlr/types/wlr_keyboard.h index 2d9f1490..9ec8ddd4 100644 --- a/include/wlr/types/wlr_keyboard.h +++ b/include/wlr/types/wlr_keyboard.h @@ -49,6 +49,7 @@ struct wlr_keyboard { struct { struct wl_signal key; + struct wl_signal modifiers; struct wl_signal keymap; } events; diff --git a/include/wlr/types/wlr_seat.h b/include/wlr/types/wlr_seat.h index 6927cd16..489bd529 100644 --- a/include/wlr/types/wlr_seat.h +++ b/include/wlr/types/wlr_seat.h @@ -37,6 +37,7 @@ struct wlr_seat_keyboard { struct wlr_seat *seat; struct wlr_keyboard *keyboard; struct wl_listener key; + struct wl_listener modifiers; struct wl_listener keymap; struct wl_listener destroy; struct wl_list link; diff --git a/types/wlr_keyboard.c b/types/wlr_keyboard.c index 168475bd..f51ea8c4 100644 --- a/types/wlr_keyboard.c +++ b/types/wlr_keyboard.c @@ -21,28 +21,7 @@ static void keyboard_led_update(struct wlr_keyboard *keyboard) { wlr_keyboard_led_update(keyboard, leds); } -void wlr_keyboard_update_modifiers(struct wlr_keyboard *keyboard, - uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, - uint32_t group) { - if (mods_depressed == keyboard->modifiers.depressed && - mods_latched == keyboard->modifiers.latched && - mods_locked == keyboard->modifiers.locked && - group == keyboard->modifiers.group) { - return; - } - keyboard->modifiers.depressed = mods_depressed; - keyboard->modifiers.latched = mods_latched; - keyboard->modifiers.locked = mods_locked; - keyboard->modifiers.group = group; -} - -void wlr_keyboard_update_state(struct wlr_keyboard *keyboard, - struct wlr_event_keyboard_key *event) { - uint32_t keycode = event->keycode + 8; - xkb_state_update_key(keyboard->xkb_state, keycode, - event->state == WLR_KEY_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP); - keyboard_led_update(keyboard); - +static void keyboard_modifier_update(struct wlr_keyboard *keyboard) { xkb_mod_mask_t depressed = xkb_state_serialize_mods(keyboard->xkb_state, XKB_STATE_MODS_DEPRESSED); xkb_mod_mask_t latched = xkb_state_serialize_mods(keyboard->xkb_state, @@ -51,8 +30,28 @@ void wlr_keyboard_update_state(struct wlr_keyboard *keyboard, XKB_STATE_MODS_LOCKED); xkb_mod_mask_t group = xkb_state_serialize_layout(keyboard->xkb_state, XKB_STATE_LAYOUT_EFFECTIVE); - wlr_keyboard_update_modifiers(keyboard, depressed, latched, locked, group); + if (depressed == keyboard->modifiers.depressed && + latched == keyboard->modifiers.latched && + locked == keyboard->modifiers.locked && + group == keyboard->modifiers.group) { + return; + } + keyboard->modifiers.depressed = depressed; + keyboard->modifiers.latched = latched; + keyboard->modifiers.locked = locked; + keyboard->modifiers.group = group; + + wl_signal_emit(&keyboard->events.modifiers, keyboard); +} + +void wlr_keyboard_update_state(struct wlr_keyboard *keyboard, + struct wlr_event_keyboard_key *event) { + uint32_t keycode = event->keycode + 8; + xkb_state_update_key(keyboard->xkb_state, keycode, + event->state == WLR_KEY_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP); + keyboard_led_update(keyboard); + keyboard_modifier_update(keyboard); wl_signal_emit(&keyboard->events.key, event); } @@ -60,6 +59,7 @@ void wlr_keyboard_init(struct wlr_keyboard *kb, struct wlr_keyboard_impl *impl) { kb->impl = impl; wl_signal_init(&kb->events.key); + wl_signal_init(&kb->events.modifiers); wl_signal_init(&kb->events.keymap); } diff --git a/types/wlr_seat.c b/types/wlr_seat.c index 66b2f18e..2702f1b5 100644 --- a/types/wlr_seat.c +++ b/types/wlr_seat.c @@ -376,39 +376,61 @@ void wlr_seat_pointer_send_axis(struct wlr_seat *wlr_seat, uint32_t time, wl_pointer_send_frame(pointer); } +static void keyboard_switch_seat_keyboard(struct wlr_seat_handle *handle, + struct wlr_seat_keyboard *seat_kb) { + if (handle->seat_keyboard == seat_kb) { + return; + } + + // TODO: We should probably lift all of the keys set by the other + // keyboard + wl_keyboard_send_keymap(handle->keyboard, + WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, seat_kb->keyboard->keymap_fd, + seat_kb->keyboard->keymap_size); + + if (wl_resource_get_version(handle->keyboard) >= 2) { + // TODO: Make this better + wl_keyboard_send_repeat_info(handle->keyboard, 25, 600); + } + handle->seat_keyboard = seat_kb; +} + static void keyboard_key_notify(struct wl_listener *listener, void *data) { - struct wlr_seat_keyboard *seat_kb = wl_container_of( - listener, seat_kb, key); + struct wlr_seat_keyboard *seat_kb = wl_container_of(listener, seat_kb, key); struct wlr_seat *seat = seat_kb->seat; struct wlr_seat_handle *handle = seat->keyboard_state.focused_handle; if (!handle || !handle->keyboard) { return; } - struct wlr_keyboard *keyboard = seat_kb->keyboard; + + keyboard_switch_seat_keyboard(handle, seat_kb); + struct wlr_event_keyboard_key *event = data; enum wlr_key_state key_state = event->state; - if (handle->seat_keyboard != seat_kb) { - // TODO: We should probably lift all of the keys set by the other - // keyboard - wl_keyboard_send_keymap(handle->keyboard, - WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, - seat_kb->keyboard->keymap_fd, - seat_kb->keyboard->keymap_size); - if (wl_resource_get_version(handle->keyboard) >= 2) { - // TODO: Make this better - wl_keyboard_send_repeat_info(handle->keyboard, 25, 600); - } - handle->seat_keyboard = seat_kb; + uint32_t key_serial = wl_display_next_serial(seat->display); + wl_keyboard_send_key(handle->keyboard, key_serial, + (uint32_t)event->time_usec, event->keycode, key_state); +} + +static void keyboard_modifiers_notify(struct wl_listener *listener, + void *data) { + struct wlr_seat_keyboard *seat_kb = wl_container_of(listener, seat_kb, + modifiers); + struct wlr_seat *seat = seat_kb->seat; + struct wlr_seat_handle *handle = seat->keyboard_state.focused_handle; + if (!handle || !handle->keyboard) { + return; } + keyboard_switch_seat_keyboard(handle, seat_kb); + + struct wlr_keyboard *keyboard = seat_kb->keyboard; + uint32_t modifiers_serial = wl_display_next_serial(seat->display); wl_keyboard_send_modifiers(handle->keyboard, modifiers_serial, keyboard->modifiers.depressed, keyboard->modifiers.latched, keyboard->modifiers.locked, keyboard->modifiers.group); - uint32_t key_serial = wl_display_next_serial(seat->display); - wl_keyboard_send_key(handle->keyboard, key_serial, - (uint32_t)event->time_usec, event->keycode, key_state); } static void keyboard_keymap_notify(struct wl_listener *listener, void *data) { @@ -434,6 +456,9 @@ void wlr_seat_attach_keyboard(struct wlr_seat *seat, wl_list_init(&seat_kb->key.link); seat_kb->key.notify = keyboard_key_notify; wl_signal_add(&kb->events.key, &seat_kb->key); + wl_list_init(&seat_kb->modifiers.link); + seat_kb->modifiers.notify = keyboard_modifiers_notify; + wl_signal_add(&kb->events.modifiers, &seat_kb->modifiers); wl_list_init(&seat_kb->keymap.link); seat_kb->keymap.notify = keyboard_keymap_notify; wl_signal_add(&kb->events.keymap, &seat_kb->keymap); @@ -450,6 +475,7 @@ void wlr_seat_detach_keyboard(struct wlr_seat *seat, struct wlr_keyboard *kb) { if (seat_kb->keyboard == kb) { wl_list_remove(&seat_kb->link); wl_list_remove(&seat_kb->key.link); + wl_list_remove(&seat_kb->modifiers.link); wl_list_remove(&seat_kb->keymap.link); wl_list_remove(&seat_kb->destroy.link); free(seat_kb); From eadda29e841383c6649e8f85494516aff00b493f Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 3 Oct 2017 10:35:54 -0400 Subject: [PATCH 13/13] Set WAYLAND_DISPLAY in rootston environment Duh --- rootston/main.c | 1 + 1 file changed, 1 insertion(+) 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; }