keyboard: use 'us' as fallback for XKB_DEFAULT_LAYOUT

...if keymap cannot be created for the provided XKB_DEFAULT_LAYOUT.

If keymap still cannot be created, exit with a helpful message to avoid
crash that is hard to understand.

Fixes: https://github.com/stefonarch/lxqt-labwc-session/issues/7
This commit is contained in:
Johan Malm 2024-03-11 22:04:52 +00:00 committed by Johan Malm
parent 1e1e90d0bb
commit 5cb3583108
2 changed files with 17 additions and 1 deletions

View file

@ -1,5 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-only // SPDX-License-Identifier: GPL-2.0-only
#define _POSIX_C_SOURCE 200809L
#include <assert.h> #include <assert.h>
#include <stdlib.h>
#include <wlr/backend/multi.h> #include <wlr/backend/multi.h>
#include <wlr/backend/session.h> #include <wlr/backend/session.h>
#include <wlr/interfaces/wlr_keyboard.h> #include <wlr/interfaces/wlr_keyboard.h>
@ -628,6 +630,8 @@ reset_window_keyboard_layout_groups(struct server *server)
static void static void
set_layout(struct server *server, struct wlr_keyboard *kb) set_layout(struct server *server, struct wlr_keyboard *kb)
{ {
static bool fallback_mode;
struct xkb_rule_names rules = { 0 }; struct xkb_rule_names rules = { 0 };
struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
struct xkb_keymap *keymap = xkb_map_new_from_names(context, &rules, struct xkb_keymap *keymap = xkb_map_new_from_names(context, &rules,
@ -639,7 +643,14 @@ set_layout(struct server *server, struct wlr_keyboard *kb)
} }
xkb_keymap_unref(keymap); xkb_keymap_unref(keymap);
} else { } else {
wlr_log(WLR_ERROR, "Failed to create xkb keymap"); wlr_log(WLR_ERROR, "failed to create xkb keymap for layout '%s'",
getenv("XKB_DEFAULT_LAYOUT"));
if (!fallback_mode) {
wlr_log(WLR_ERROR, "entering fallback mode with layout 'us'");
fallback_mode = true;
setenv("XKB_DEFAULT_LAYOUT", "us", 1);
set_layout(server, kb);
}
} }
xkb_context_unref(context); xkb_context_unref(context);
} }

View file

@ -296,6 +296,11 @@ new_keyboard(struct seat *seat, struct wlr_input_device *device, bool virtual)
keyboard->wlr_keyboard = kb; keyboard->wlr_keyboard = kb;
keyboard->is_virtual = virtual; keyboard->is_virtual = virtual;
if (!seat->keyboard_group->keyboard.keymap) {
wlr_log(WLR_ERROR, "cannot set keymap");
exit(EXIT_FAILURE);
}
wlr_keyboard_set_keymap(kb, seat->keyboard_group->keyboard.keymap); wlr_keyboard_set_keymap(kb, seat->keyboard_group->keyboard.keymap);
/* /*