11 #include "modifiers.h"
14 lk_map_exists(struct lk_ctx *ctx, unsigned int k_table)
16 return (lk_array_get_ptr(ctx->keymap, k_table) != NULL);
20 lk_maps_total(struct lk_ctx *ctx)
22 return ctx->keymap->total;
26 lk_keys_total(struct lk_ctx *ctx, unsigned int k_table)
29 map = lk_array_get_ptr(ctx->keymap, k_table);
37 lk_key_exists(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index)
42 map = lk_array_get_ptr(ctx->keymap, k_table);
47 key = lk_array_get(map, k_index);
56 lk_add_map(struct lk_ctx *ctx, unsigned int k_table)
58 struct lk_array *keys;
60 if (lk_map_exists(ctx, k_table)) {
64 keys = malloc(sizeof(struct lk_array));
66 ERR(ctx, _("out of memory"));
70 lk_array_init(keys, sizeof(unsigned int), 0);
72 if (lk_array_set(ctx->keymap, k_table, &keys) < 0) {
74 ERR(ctx, _("out of memory"));
82 lk_get_key(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index)
87 map = lk_array_get_ptr(ctx->keymap, k_table);
89 ERR(ctx, _("unable to get keymap %d"), k_table);
93 key = lk_array_get(map, k_index);
94 if (!key || *key == 0) {
102 lk_del_key(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index)
104 struct lk_array *map;
106 map = lk_array_get_ptr(ctx->keymap, k_table);
108 ERR(ctx, _("unable to get keymap %d"), k_table);
112 if (!lk_array_exists(map, k_index))
115 if (lk_array_unset(map, k_index) < 0) {
116 ERR(ctx, _("unable to unset key %d for table %d"),
125 lk_add_key(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index, int keycode)
127 struct lk_array *map;
128 unsigned int code = keycode + 1;
130 if (keycode == CODE_FOR_UNKNOWN_KSYM) {
131 /* is safer not to be silent in this case,
132 * it can be caused by coding errors as well. */
133 ERR(ctx, _("lk_add_key called with bad keycode %d"), keycode);
137 if (!k_index && keycode == K_NOSUCHMAP)
140 map = lk_array_get_ptr(ctx->keymap, k_table);
142 if (ctx->keywords & LK_KEYWORD_KEYMAPS) {
143 ERR(ctx, _("adding map %d violates explicit keymaps line"),
148 if (lk_add_map(ctx, k_table) < 0)
152 if ((ctx->keywords & LK_KEYWORD_ALTISMETA) && keycode == K_HOLE &&
153 lk_key_exists(ctx, k_table, k_index))
156 map = lk_array_get_ptr(ctx->keymap, k_table);
158 if (lk_array_set(map, k_index, &code) < 0) {
159 ERR(ctx, _("unable to set key %d for table %d"),
164 if (ctx->keywords & LK_KEYWORD_ALTISMETA) {
165 unsigned int alttable = k_table | M_ALT;
166 int type = KTYP(keycode);
167 int val = KVAL(keycode);
169 if (alttable != k_table && lk_map_exists(ctx, alttable) &&
170 !lk_key_exists(ctx, alttable, k_index) &&
171 (type == KT_LATIN || type == KT_LETTER) &&
173 if (lk_add_key(ctx, alttable, k_index, K(KT_META, val)) < 0)
182 lk_get_func(struct lk_ctx *ctx, struct kbsentry *kbs)
186 s = lk_array_get_ptr(ctx->func_table, kbs->kb_func);
188 ERR(ctx, _("func %d not allocated"), kbs->kb_func);
192 strncpy((char *)kbs->kb_string, s, sizeof(kbs->kb_string));
193 kbs->kb_string[sizeof(kbs->kb_string) - 1] = 0;
200 lk_add_func(struct lk_ctx *ctx, struct kbsentry kbs)
204 s = lk_array_get_ptr(ctx->func_table, kbs.kb_func);
208 s = strdup((char *)kbs.kb_string);
210 if (lk_array_set(ctx->func_table, kbs.kb_func, &s) < 0) {
212 ERR(ctx, _("out of memory"));
220 lk_add_diacr(struct lk_ctx *ctx, unsigned int diacr, unsigned int base, unsigned int res)
222 struct kb_diacr *ptr;
224 ptr = malloc(sizeof(struct kb_diacr));
226 ERR(ctx, _("out of memory"));
234 lk_array_append(ctx->accent_table, &ptr);
240 lk_add_compose(struct lk_ctx *ctx,
245 int direction = TO_8BIT;
248 if (ctx->flags & LK_FLAG_PREFER_UNICODE)
249 direction = TO_UNICODE;
251 return lk_add_diacr(ctx,
252 convert_code(ctx, diacr, direction),
253 convert_code(ctx, base, direction),
254 convert_code(ctx, res, direction)
259 do_constant_key(struct lk_ctx *ctx, int i, u_short key)
267 if ((typ == KT_LATIN || typ == KT_LETTER) &&
268 ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'))) {
270 defs[0] = K(KT_LETTER, val);
271 defs[1] = K(KT_LETTER, val ^ 32);
275 for (j = 4; j < 8; j++)
276 defs[j] = K(KT_LATIN, val & ~96);
278 for (j = 8; j < 16; j++)
279 defs[j] = K(KT_META, KVAL(defs[j - 8]));
281 for (j = 0; j < ctx->keymap->total; j++) {
282 if (!lk_map_exists(ctx, j))
285 if (j > 0 && lk_key_exists(ctx, j, i))
288 if (lk_add_key(ctx, j, i, defs[j % 16]) < 0)
293 /* do this also for keys like Escape,
294 as promised in the man page */
295 for (j = 1; j < ctx->keymap->total; j++) {
296 if (!lk_map_exists(ctx, j))
299 if (lk_key_exists(ctx, j, i))
302 if (lk_add_key(ctx, j, i, key) < 0)
310 lk_add_constants(struct lk_ctx *ctx)
312 unsigned int i, r0 = 0;
314 if (ctx->keywords & LK_KEYWORD_KEYMAPS) {
315 while (r0 < ctx->keymap->total && !lk_map_exists(ctx, r0))
319 for (i = 0; i < ctx->key_constant->total; i++) {
323 constant = lk_array_get(ctx->key_constant, i);
324 if (!constant || !(*constant))
327 if (!lk_map_exists(ctx, r0)) {
328 ERR(ctx, _("impossible error in lk_add_constants"));
332 key = lk_get_key(ctx, r0, i);
334 if (do_constant_key(ctx, i, key) < 0)