From 50a735921800d857fc5c45bacb9fe728a1e2aac1 Mon Sep 17 00:00:00 2001 From: mateus-cezario-barreto Date: Sat, 8 Jun 2024 08:50:32 -0300 Subject: [PATCH] Add option to use a xkb file --- config.def.h | 8 ++++++++ dwl.c | 28 ++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/config.def.h b/config.def.h index a784eb4..414e58d 100644 --- a/config.def.h +++ b/config.def.h @@ -57,6 +57,14 @@ static const struct xkb_rule_names xkb_rules = { .options = NULL, }; +/* Wether to try to use an xkb file */ +static const int use_custom_xkb_file = 1; + +/* Complete path to a xkb file */ +/* NOTE: if dwl fails to compile the file, the xkb_names will be used */ +/* instead */ +static const char* path_to_xkb_file = "/etc/xkb/custom-layout.xkb"; + static const int repeat_rate = 25; static const int repeat_delay = 600; diff --git a/dwl.c b/dwl.c index 6f041a0..a8aa39e 100644 --- a/dwl.c +++ b/dwl.c @@ -814,17 +814,37 @@ KeyboardGroup * createkeyboardgroup(void) { KeyboardGroup *group = ecalloc(1, sizeof(*group)); - struct xkb_context *context; - struct xkb_keymap *keymap; + struct xkb_context *context = NULL; + struct xkb_keymap *keymap = NULL; group->wlr_group = wlr_keyboard_group_create(); group->wlr_group->data = group; /* Prepare an XKB keymap and assign it to the keyboard group. */ context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); - if (!(keymap = xkb_keymap_new_from_names(context, &xkb_rules, - XKB_KEYMAP_COMPILE_NO_FLAGS))) + + FILE* xkb_file = NULL; + + if (use_custom_xkb_file == 1) { + xkb_file = fopen(path_to_xkb_file, "r"); + } + + if (xkb_file != NULL) { + keymap = xkb_keymap_new_from_file(context, xkb_file, + XKB_KEYMAP_FORMAT_TEXT_V1, + XKB_KEYMAP_COMPILE_NO_FLAGS); + fclose(xkb_file); + } + + /* Compile keymap from names if it was not compiled from xkb_file */ + if (keymap == NULL) { + keymap = xkb_keymap_new_from_names(context, &xkb_rules, + XKB_KEYMAP_COMPILE_NO_FLAGS); + } + + if (keymap == NULL) { die("failed to compile keymap"); + } wlr_keyboard_set_keymap(&group->wlr_group->keyboard, keymap); xkb_keymap_unref(keymap);