aboutsummaryrefslogtreecommitdiff
path: root/sway/input
diff options
context:
space:
mode:
authorBrian Ashworth <bosrsf04@gmail.com>2019-05-23 03:06:28 -0400
committerSimon Ser <contact@emersion.fr>2019-06-09 20:13:22 +0300
commitbe2d2a299a6f854f0494f84169ef82ad5b31a917 (patch)
tree1afefdc25efda19c13f137a49a88346b763ac8d9 /sway/input
parent4d93b96b6ec724e31363ebff725486167b99f660 (diff)
downloadsway-be2d2a299a6f854f0494f84169ef82ad5b31a917.tar.xz
commands/input: perform basic keymap validation
Before the delta input config is stored, this attempts to compile a keymap with it. If the keymap fails to compile, then the first line of the xkbcommon log entry will be included with a `CMD_FAILURE`, the entire xkbcommon log entry will be included in the sway error log, and the delta will not be stored. This only handles basic issues such as a layouts not existing. This will NOT catch more complex issues such as when a variant does exist, but not for the given layout (ex: `azerty` is a valid variant, but the `us` layout does not have a `azerty` variant).
Diffstat (limited to 'sway/input')
-rw-r--r--sway/input/keyboard.c47
1 files changed, 44 insertions, 3 deletions
diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c
index dcfaa4fa..ef20a3cf 100644
--- a/sway/input/keyboard.c
+++ b/sway/input/keyboard.c
@@ -495,7 +495,45 @@ struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat,
return keyboard;
}
-struct xkb_keymap *sway_keyboard_compile_keymap(struct input_config *ic) {
+static void handle_xkb_context_log(struct xkb_context *context,
+ enum xkb_log_level level, const char *format, va_list args) {
+ va_list args_copy;
+ va_copy(args_copy, args);
+ size_t length = vsnprintf(NULL, 0, format, args_copy) + 1;
+ va_end(args_copy);
+
+ char *error = malloc(length);
+ if (!error) {
+ sway_log(SWAY_ERROR, "Failed to allocate libxkbcommon log message");
+ return;
+ }
+
+ va_copy(args_copy, args);
+ vsnprintf(error, length, format, args_copy);
+ va_end(args_copy);
+
+ if (error[length - 2] == '\n') {
+ error[length - 2] = '\0';
+ }
+
+ sway_log_importance_t importance = SWAY_DEBUG;
+ if (level <= XKB_LOG_LEVEL_ERROR) { // Critical and Error
+ importance = SWAY_ERROR;
+ } else if (level <= XKB_LOG_LEVEL_INFO) { // Warning and Info
+ importance = SWAY_INFO;
+ }
+ sway_log(importance, "[xkbcommon] %s", error);
+
+ char **data = xkb_context_get_user_data(context);
+ if (importance == SWAY_ERROR && data && !*data) {
+ *data = error;
+ } else {
+ free(error);
+ }
+}
+
+struct xkb_keymap *sway_keyboard_compile_keymap(struct input_config *ic,
+ char **error) {
struct xkb_rule_names rules = {0};
if (ic) {
input_config_fill_rule_names(ic, &rules);
@@ -505,9 +543,12 @@ struct xkb_keymap *sway_keyboard_compile_keymap(struct input_config *ic) {
if (!sway_assert(context, "cannot create XKB context")) {
return NULL;
}
+ xkb_context_set_user_data(context, error);
+ xkb_context_set_log_fn(context, handle_xkb_context_log);
struct xkb_keymap *keymap =
xkb_keymap_new_from_names(context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS);
+ xkb_context_set_user_data(context, NULL);
xkb_context_unref(context);
return keymap;
}
@@ -518,10 +559,10 @@ void sway_keyboard_configure(struct sway_keyboard *keyboard) {
struct wlr_input_device *wlr_device =
keyboard->seat_device->input_device->wlr_device;
- struct xkb_keymap *keymap = sway_keyboard_compile_keymap(input_config);
+ struct xkb_keymap *keymap = sway_keyboard_compile_keymap(input_config, NULL);
if (!keymap) {
sway_log(SWAY_ERROR, "Failed to compile keymap. Attempting defaults");
- keymap = sway_keyboard_compile_keymap(NULL);
+ keymap = sway_keyboard_compile_keymap(NULL, NULL);
if (!keymap) {
sway_log(SWAY_ERROR,
"Failed to compile default keymap. Aborting configure");