aboutsummaryrefslogtreecommitdiffstats
path: root/sway/config.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/config.c')
-rw-r--r--sway/config.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/sway/config.c b/sway/config.c
index e14ea83a..45d16758 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -33,6 +33,31 @@
33 33
34struct sway_config *config = NULL; 34struct sway_config *config = NULL;
35 35
36static struct keysym_translation_data new_keysym_translation_data(
37 const char *layout) {
38 struct xkb_rule_names rules = {
39 .layout = layout,
40 };
41
42 struct xkb_keymap *xkb_keymap = xkb_keymap_new_from_names(
43 xkb_context_new(XKB_CONTEXT_NO_FLAGS),
44 &rules,
45 XKB_KEYMAP_COMPILE_NO_FLAGS);
46
47 struct keysym_translation_data result = {
48 .xkb_keymap = xkb_keymap,
49 .xkb_state = xkb_state_new(xkb_keymap),
50 };
51
52 return result;
53}
54
55static void free_keysym_translation_data(
56 struct keysym_translation_data config) {
57 xkb_state_unref(config.xkb_state);
58 xkb_keymap_unref(config.xkb_keymap);
59}
60
36static void free_mode(struct sway_mode *mode) { 61static void free_mode(struct sway_mode *mode) {
37 if (!mode) { 62 if (!mode) {
38 return; 63 return;
@@ -146,6 +171,7 @@ void free_config(struct sway_config *config) {
146 free(config->swaynag_command); 171 free(config->swaynag_command);
147 free((char *)config->current_config_path); 172 free((char *)config->current_config_path);
148 free((char *)config->current_config); 173 free((char *)config->current_config);
174 free_keysym_translation_data(config->keysym_translation);
149 free(config); 175 free(config);
150} 176}
151 177
@@ -317,6 +343,9 @@ static void config_defaults(struct sway_config *config) {
317 if (!(config->feature_policies = create_list())) goto cleanup; 343 if (!(config->feature_policies = create_list())) goto cleanup;
318 if (!(config->ipc_policies = create_list())) goto cleanup; 344 if (!(config->ipc_policies = create_list())) goto cleanup;
319 345
346 // The keysym to keycode translation
347 config->keysym_translation = new_keysym_translation_data(getenv("XKB_DEFAULT_LAYOUT"));
348
320 return; 349 return;
321cleanup: 350cleanup:
322 sway_abort("Unable to allocate config structures"); 351 sway_abort("Unable to allocate config structures");
@@ -937,3 +966,50 @@ void config_update_font_height(bool recalculate) {
937 arrange_root(); 966 arrange_root();
938 } 967 }
939} 968}
969
970static void translate_binding_list(list_t *bindings, list_t *bindsyms,
971 list_t *bindcodes) {
972 for (int i = 0; i < bindings->length; ++i) {
973 struct sway_binding *binding = bindings->items[i];
974 translate_binding(binding);
975
976 list_t *bindings;
977 switch (binding->type) {
978 case BINDING_KEYSYM:
979 bindings = bindsyms;
980 break;
981 case BINDING_KEYCODE:
982 bindings = bindcodes;
983 break;
984 default:
985 sway_assert(false, "unexpected translated binding type: %d",
986 binding->type);
987 break;
988 }
989
990 binding_add_translated(binding, bindings);
991 }
992}
993
994void translate_keysyms(const char *layout) {
995 free_keysym_translation_data(config->keysym_translation);
996 config->keysym_translation = new_keysym_translation_data(layout);
997
998 for (int i = 0; i < config->modes->length; ++i) {
999 struct sway_mode *mode = config->modes->items[i];
1000
1001 list_t *bindsyms = create_list();
1002 list_t *bindcodes = create_list();
1003
1004 translate_binding_list(mode->keysym_bindings, bindsyms, bindcodes);
1005 translate_binding_list(mode->keycode_bindings, bindsyms, bindcodes);
1006
1007 list_free(mode->keysym_bindings);
1008 list_free(mode->keycode_bindings);
1009
1010 mode->keysym_bindings = bindsyms;
1011 mode->keycode_bindings = bindcodes;
1012 }
1013
1014 sway_log(SWAY_DEBUG, "Translated keysyms for layout %s", layout);
1015}