Skip to content
Myr

key-binding.c

This file

Path

key-binding.c

376 lines · 11.0 KiB · C

Last changed by

key-binding: fix crash in debug builds, if exiting earlyDaniel Eklöfbefore adoption805769a

History of this file

Take it

The walk stopped after 200 commits: lines older than 6a4c416 point at it.

changelog: add missing refDaniel Eklöfbefore adoption6a4c416

lines 1–37why, one step back
1#include "key-binding.h"
23#include <string.h>4#include <assert.h>56#define LOG_MODULE "key-binding"7#define LOG_ENABLE_DBG 08#include "log.h"9#include "macros.h"10#include "xmalloc.h"1112struct key_set {13 struct key_binding_set public;14 const struct config *conf;15 const struct seat *seat;16};17typedef tll(struct key_set) bind_set_list_t;1819struct kb_manager {20 struct key_set *last_used_set;21 bind_set_list_t binding_sets;22};2324static void load_keymap(25 struct key_set *set, struct xkb_state *xkb, struct xkb_keymap *keymap);26static void unload_keymap(struct key_set *set);2728struct kb_manager *29kb_manager_new(void)30{31 struct kb_manager *mgr = xcalloc(1, sizeof(*mgr));32 return mgr;33}3435void36kb_manager_destroy(struct kb_manager *mgr)37{

key-binding: fix crash in debug builds, if exiting earlyDaniel Eklöfbefore adoption805769a

line 38why, one step back
38 assert(mgr == NULL || tll_length(mgr->binding_sets) == 0);

changelog: add missing refDaniel Eklöfbefore adoption6a4c416

lines 39–376why, one step back
39 free(mgr);
40}4142void43kb_new_for_seat(struct kb_manager *mgr, const struct config *conf,44 const struct seat *seat)45{46#if defined(_DEBUG)47 tll_foreach(mgr->binding_sets, it)48 assert(it->item.seat != seat);49#endif5051 struct key_set set = {52 .public = {53 .key = tll_init(),54 //.mouse = tll_init(),55 },56 .conf = conf,57 .seat = seat,58 };5960 tll_push_back(mgr->binding_sets, set);6162 LOG_DBG("new (seat): set=%p, seat=%p, conf=%p, ref-count=1",63 (void *)&tll_back(mgr->binding_sets),64 (void *)set.seat, (void *)set.conf);6566 //load_keymap(&tll_back(mgr->binding_sets));6768 LOG_DBG("new (seat): total number of sets: %zu",69 tll_length(mgr->binding_sets));70}7172static void73kb_set_destroy(struct kb_manager *mgr, struct key_set *set)74{75 unload_keymap(set);76 if (mgr->last_used_set == set)77 mgr->last_used_set = NULL;7879 /* Note: caller must remove from binding_sets */80}8182void83kb_remove_seat(struct kb_manager *mgr, const struct seat *seat)84{85 tll_foreach(mgr->binding_sets, it) {86 struct key_set *set = &it->item;8788 if (set->seat != seat)89 continue;9091 kb_set_destroy(mgr, set);92 tll_remove(mgr->binding_sets, it);9394 LOG_DBG("remove seat: set=%p, seat=%p, total number of sets: %zu",95 (void *)set, (void *)seat, tll_length(mgr->binding_sets));96 }9798 LOG_DBG("remove seat: total number of sets: %zu",99 tll_length(mgr->binding_sets));100}101102static xkb_keycode_list_t103key_codes_for_xkb_sym(struct xkb_keymap *keymap, xkb_keysym_t sym)104{105 xkb_keycode_list_t key_codes = tll_init();106107 /*108 * Find all key codes that map to this symbol.109 *110 * This allows us to match bindings in other layouts111 * too.112 */113 struct xkb_state *state = xkb_state_new(keymap);114115 for (xkb_keycode_t code = xkb_keymap_min_keycode(keymap);116 code <= xkb_keymap_max_keycode(keymap);117 code++)118 {119 if (xkb_state_key_get_one_sym(state, code) == sym)120 tll_push_back(key_codes, code);121 }122123 xkb_state_unref(state);124 return key_codes;125}126127static xkb_keysym_t128maybe_repair_key_combo(struct xkb_state *xkb, struct xkb_keymap *keymap,129 xkb_keysym_t sym, xkb_mod_mask_t mods)130{131 /*132 * Detect combos containing a shifted symbol and the corresponding133 * modifier, and replace the shifted symbol with its unshifted134 * variant.135 *136 * For example, the combo is “Control+Shift+U”. In this case,137 * Shift is the modifier used to “shift” ‘u’ to ‘U’, after which138 * ‘Shift’ will have been “consumed”. Since we filter out consumed139 * modifiers when matching key combos, this key combo will never140 * trigger (we will never be able to match the ‘Shift’ modifier).141 *142 * There are two correct variants of the above key combo:143 * - “Control+U” (upper case ‘U’)144 * - “Control+Shift+u” (lower case ‘u’)145 *146 * What we do here is, for each key *code*, check if there are any147 * (shifted) levels where it produces ‘sym’. If there are, check148 * *which* sets of modifiers are needed to produce it, and compare149 * with ‘mods’.150 *151 * If there is at least one common modifier, it means ‘sym’ is a152 * “shifted” symbol, with the corresponding shifting modifier153 * explicitly included in the key combo. I.e. the key combo will154 * never trigger.155 *156 * We then proceed and “repair” the key combo by replacing ‘sym’157 * with the corresponding unshifted symbol.158 *159 * To reduce the noise, we ignore all key codes where the shifted160 * symbol is the same as the unshifted symbol.161 */162163 for (xkb_keycode_t code = xkb_keymap_min_keycode(keymap);164 code <= xkb_keymap_max_keycode(keymap);165 code++)166 {167 xkb_layout_index_t layout_idx =168 xkb_state_key_get_layout(xkb, code);169170 /* Get all unshifted symbols for this key */171 const xkb_keysym_t *base_syms = NULL;172 size_t base_count = xkb_keymap_key_get_syms_by_level(173 keymap, code, layout_idx, 0, &base_syms);174175 if (base_count == 0 || sym == base_syms[0]) {176 /* No unshifted symbols, or unshifted symbol is same as ‘sym’ */177 continue;178 }179180 /* Name of the unshifted symbol, for logging */181 char base_name[100];182 xkb_keysym_get_name(base_syms[0], base_name, sizeof(base_name));183184 /* Iterate all shift levels */185 for (xkb_level_index_t level_idx = 1;186 level_idx < xkb_keymap_num_levels_for_key(187 keymap, code, layout_idx);188 level_idx++) {189190 /* Get all symbols for current shift level */191 const xkb_keysym_t *shifted_syms = NULL;192 size_t shifted_count = xkb_keymap_key_get_syms_by_level(193 keymap, code, layout_idx, level_idx, &shifted_syms);194195 for (size_t i = 0; i < shifted_count; i++) {196 if (shifted_syms[i] != sym)197 continue;198199 /* Get modifier sets that produces the current shift level */200 xkb_mod_mask_t mod_masks[16];201 size_t mod_mask_count = xkb_keymap_key_get_mods_for_level(202 keymap, code, layout_idx, level_idx,203 mod_masks, ALEN(mod_masks));204205 /* Check if key combo’s modifier set intersects */206 for (size_t j = 0; j < mod_mask_count; j++) {207 if ((mod_masks[j] & mods) != mod_masks[j])208 continue;209210 char combo[64] = {0};211212 for (int k = 0; k < sizeof(xkb_mod_mask_t) * 8; k++) {213 if (!(mods & (1u << k)))214 continue;215216 const char *mod_name = xkb_keymap_mod_get_name(217 keymap, k);218 strcat(combo, mod_name);219 strcat(combo, "+");220 }221222 size_t len = strlen(combo);223 xkb_keysym_get_name(224 sym, &combo[len], sizeof(combo) - len);225226 LOG_WARN(227 "%s: combo with both explicit modifier and shifted symbol "228 "(level=%d, mod-mask=0x%08x), "229 "replacing with %s",230 combo, level_idx, mod_masks[j], base_name);231232 /* Replace with unshifted symbol */233 return base_syms[0];234 }235 }236 }237 }238239 return sym;240}241242static xkb_mod_mask_t243conf_modifiers_to_mask(struct xkb_keymap *keymap,244 const struct config_key_modifiers *modifiers)245{246 xkb_mod_index_t shift = xkb_keymap_mod_get_index(247 keymap, XKB_MOD_NAME_SHIFT);248 xkb_mod_index_t alt = xkb_keymap_mod_get_index(249 keymap, XKB_MOD_NAME_ALT);250 xkb_mod_index_t ctrl = xkb_keymap_mod_get_index(251 keymap, XKB_MOD_NAME_CTRL);252 xkb_mod_index_t super = xkb_keymap_mod_get_index(253 keymap, XKB_MOD_NAME_LOGO);254255 xkb_mod_mask_t mods = 0;256257 if (shift != XKB_MOD_INVALID) mods |= modifiers->shift << shift;258 if (ctrl != XKB_MOD_INVALID) mods |= modifiers->ctrl << ctrl;259 if (alt != XKB_MOD_INVALID) mods |= modifiers->alt << alt;260 if (super != XKB_MOD_INVALID) mods |= modifiers->super << super;261262 return mods;263}264265static void266convert_key_binding(struct xkb_state *xkb, struct xkb_keymap *keymap,267 const struct config_key_binding *conf_binding,268 key_binding_list_t *bindings)269{270 xkb_mod_mask_t mods = conf_modifiers_to_mask(271 keymap, &conf_binding->modifiers);272 xkb_keysym_t sym = maybe_repair_key_combo(273 xkb, keymap, conf_binding->k.sym, mods);274275 struct key_binding binding = {276 .action = conf_binding->action,277 .mods = mods,278 .k = {279 .sym = sym,280 .key_codes = key_codes_for_xkb_sym(keymap, sym),281 },282 };283 tll_push_back(*bindings, binding);284}285286static void287convert_key_bindings(struct key_set *set, struct xkb_state *xkb,288 struct xkb_keymap *keymap)289{290 const struct config *conf = set->conf;291292 for (size_t i = 0; i < conf->key_bindings.count; i++) {293 const struct config_key_binding *binding = &conf->key_bindings.arr[i];294 convert_key_binding(xkb, keymap, binding, &set->public.key);295 }296}297298static void299load_keymap(struct key_set *set, struct xkb_state *xkb,300 struct xkb_keymap *keymap)301{302 LOG_DBG("load keymap: set=%p, seat=%p, conf=%p",303 (void *)set, (void *)set->seat, (void *)set->conf);304305 if (keymap == NULL) {306 LOG_DBG("no XKB keymap");307 return;308 }309310 convert_key_bindings(set, xkb, keymap);311 //convert_mouse_bindings(set);312}313314void315kb_load_keymap(struct kb_manager *mgr, const struct seat *seat,316 struct xkb_state *xkb, struct xkb_keymap *keymap)317{318 tll_foreach(mgr->binding_sets, it) {319 struct key_set *set = &it->item;320321 if (set->seat == seat)322 load_keymap(set, xkb, keymap);323 }324}325326static void327kb_destroy(key_binding_list_t *bindings)328{329 tll_foreach(*bindings, it) {330 struct key_binding *bind = &it->item;331 tll_free(bind->k.key_codes);332 tll_remove(*bindings, it);333 }334}335336static void337unload_keymap(struct key_set *set)338{339 kb_destroy(&set->public.key);340 //kb_destroy(&set->public.mouse);341}342343void344kb_unload_keymap(struct kb_manager *mgr, const struct seat *seat)345{346 tll_foreach(mgr->binding_sets, it) {347 struct key_set *set = &it->item;348 if (set->seat != seat)349 continue;350351 LOG_DBG("unload keymap: set=%p, seat=%p, conf=%p",352 (void *)set, (void *)seat, (void *)set->conf);353354 unload_keymap(set);355 }356}357358struct key_binding_set *359key_binding_for(struct kb_manager *mgr, const struct seat *seat)360{361 struct key_set *last_used = mgr->last_used_set;362 if (last_used != NULL && last_used->seat == seat)363 return &last_used->public;364365 tll_foreach(mgr->binding_sets, it) {366 struct key_set *set = &it->item;367368 if (set->seat != seat)369 continue;370371 mgr->last_used_set = set;372 return &set->public;373 }374375 return NULL;376}