From fd9ab9ee0659d5b4c2bd148e25b8bc6378f604d6 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 5 Mar 2024 08:44:04 +0100 Subject: [PATCH 1/3] config: error out on keysym translation XKB state failure If we can't create the XKB keymap used for keysym translation, gracefully error out instead of crashing. This can happen if the XKB_DEFAULT_LAYOUT is set to an invalid value, for instance. Closes: https://github.com/swaywm/sway/issues/7789 --- sway/config.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/sway/config.c b/sway/config.c index 568c8b534..d46b81ee5 100644 --- a/sway/config.c +++ b/sway/config.c @@ -43,13 +43,20 @@ static struct xkb_state *keysym_translation_state_create( context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS); - xkb_context_unref(context); + if (xkb_keymap == NULL) { + sway_log(SWAY_ERROR, "Failed to compile keysym translation XKB keymap"); + return NULL; + } + return xkb_state_new(xkb_keymap); } static void keysym_translation_state_destroy( struct xkb_state *state) { + if (state == NULL) { + return; + } xkb_keymap_unref(xkb_state_get_keymap(state)); xkb_state_unref(state); } @@ -339,6 +346,9 @@ static void config_defaults(struct sway_config *config) { struct xkb_rule_names rules = {0}; config->keysym_translation_state = keysym_translation_state_create(rules); + if (config->keysym_translation_state == NULL) { + goto cleanup; + } return; cleanup: @@ -987,6 +997,11 @@ void translate_keysyms(struct input_config *input_config) { input_config_fill_rule_names(input_config, &rules); config->keysym_translation_state = keysym_translation_state_create(rules); + if (config->keysym_translation_state == NULL) { + sway_log(SWAY_ERROR, "Failed to create keysym translation XKB state " + "for device '%s'", input_config->identifier); + return; + } for (int i = 0; i < config->modes->length; ++i) { struct sway_mode *mode = config->modes->items[i]; From 59f629238309e230b0e353e73d4f37a7de7fe820 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 5 Mar 2024 08:47:21 +0100 Subject: [PATCH 2/3] config: add fallback without env vars for keysym translation XKB keymap --- sway/config.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/sway/config.c b/sway/config.c index d46b81ee5..72fc41e78 100644 --- a/sway/config.c +++ b/sway/config.c @@ -37,8 +37,8 @@ struct sway_config *config = NULL; static struct xkb_state *keysym_translation_state_create( - struct xkb_rule_names rules) { - struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_SECURE_GETENV); + struct xkb_rule_names rules, uint32_t context_flags) { + struct xkb_context *context = xkb_context_new(context_flags | XKB_CONTEXT_NO_SECURE_GETENV); struct xkb_keymap *xkb_keymap = xkb_keymap_new_from_names( context, &rules, @@ -344,8 +344,11 @@ static void config_defaults(struct sway_config *config) { // The keysym to keycode translation struct xkb_rule_names rules = {0}; - config->keysym_translation_state = - keysym_translation_state_create(rules); + config->keysym_translation_state = keysym_translation_state_create(rules, 0); + if (config->keysym_translation_state == NULL) { + config->keysym_translation_state = keysym_translation_state_create(rules, + XKB_CONTEXT_NO_ENVIRONMENT_NAMES); + } if (config->keysym_translation_state == NULL) { goto cleanup; } @@ -995,8 +998,7 @@ void translate_keysyms(struct input_config *input_config) { struct xkb_rule_names rules = {0}; input_config_fill_rule_names(input_config, &rules); - config->keysym_translation_state = - keysym_translation_state_create(rules); + config->keysym_translation_state = keysym_translation_state_create(rules, 0); if (config->keysym_translation_state == NULL) { sway_log(SWAY_ERROR, "Failed to create keysym translation XKB state " "for device '%s'", input_config->identifier); From f2a0e81b2438853e12a2b8fe9bddde154852d85d Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Thu, 7 Mar 2024 12:16:11 +0100 Subject: [PATCH 3/3] Fetch input device vendor/product from libinput References: https://gitlab.freedesktop.org/wlroots/wlroots/-/merge_requests/4582 --- sway/input/input-manager.c | 11 +++++++++-- sway/ipc-json.c | 8 ++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index 056cc3ea8..089e1e718 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -65,8 +65,15 @@ struct sway_seat *input_manager_sway_seat_from_wlr_seat(struct wlr_seat *wlr_sea } char *input_device_get_identifier(struct wlr_input_device *device) { - int vendor = device->vendor; - int product = device->product; + int vendor = 0, product = 0; +#if WLR_HAS_LIBINPUT_BACKEND + if (wlr_input_device_is_libinput(device)) { + struct libinput_device *libinput_dev = wlr_libinput_get_device_handle(device); + vendor = libinput_device_get_id_vendor(libinput_dev); + product = libinput_device_get_id_product(libinput_dev); + } +#endif + char *name = strdup(device->name ? device->name : ""); strip_whitespace(name); diff --git a/sway/ipc-json.c b/sway/ipc-json.c index dfbb7a6ec..81ca34831 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -1097,10 +1097,6 @@ json_object *ipc_json_describe_input(struct sway_input_device *device) { json_object_new_string(device->identifier)); json_object_object_add(object, "name", json_object_new_string(device->wlr_device->name)); - json_object_object_add(object, "vendor", - json_object_new_int(device->wlr_device->vendor)); - json_object_object_add(object, "product", - json_object_new_int(device->wlr_device->product)); json_object_object_add(object, "type", json_object_new_string( input_device_get_type(device))); @@ -1154,6 +1150,10 @@ json_object *ipc_json_describe_input(struct sway_input_device *device) { libinput_dev = wlr_libinput_get_device_handle(device->wlr_device); json_object_object_add(object, "libinput", describe_libinput_device(libinput_dev)); + json_object_object_add(object, "vendor", + json_object_new_int(libinput_device_get_id_vendor(libinput_dev))); + json_object_object_add(object, "product", + json_object_new_int(libinput_device_get_id_product(libinput_dev))); } #endif