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_get_keys_total(struct lk_ctx *ctx, unsigned int k_table)
23 map = lk_array_get_ptr(ctx->keymap, k_table);
31 lk_key_exists(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index)
36 map = lk_array_get_ptr(ctx->keymap, k_table);
41 key = lk_array_get(map, k_index);
50 lk_add_map(struct lk_ctx *ctx, unsigned int k_table)
52 struct lk_array *keys;
54 if (lk_map_exists(ctx, k_table)) {
58 keys = malloc(sizeof(struct lk_array));
60 ERR(ctx, _("out of memory"));
64 lk_array_init(keys, sizeof(unsigned int), 0);
66 if (lk_array_set(ctx->keymap, k_table, &keys) < 0) {
68 ERR(ctx, _("out of memory"));
76 lk_get_key(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index)
81 map = lk_array_get_ptr(ctx->keymap, k_table);
83 ERR(ctx, _("unable to get keymap %d"), k_table);
87 key = lk_array_get(map, k_index);
88 if (!key || *key == 0) {
96 lk_del_key(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index)
100 map = lk_array_get_ptr(ctx->keymap, k_table);
102 ERR(ctx, _("unable to get keymap %d"), k_table);
106 if (!lk_array_exists(map, k_index))
109 if (lk_array_unset(map, k_index) < 0) {
110 ERR(ctx, _("unable to unset key %d for table %d"),
119 lk_add_key(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index, int keycode)
121 struct lk_array *map;
122 unsigned int code = keycode + 1;
124 if (keycode == CODE_FOR_UNKNOWN_KSYM) {
125 /* is safer not to be silent in this case,
126 * it can be caused by coding errors as well. */
127 ERR(ctx, _("lk_add_key called with bad keycode %d"), keycode);
131 if (!k_index && keycode == K_NOSUCHMAP)
134 map = lk_array_get_ptr(ctx->keymap, k_table);
136 if (ctx->keywords & LK_KEYWORD_KEYMAPS) {
137 ERR(ctx, _("adding map %d violates explicit keymaps line"),
142 if (lk_add_map(ctx, k_table) < 0)
146 if ((ctx->keywords & LK_KEYWORD_ALTISMETA) && keycode == K_HOLE &&
147 lk_key_exists(ctx, k_table, k_index))
150 map = lk_array_get_ptr(ctx->keymap, k_table);
152 if (lk_array_set(map, k_index, &code) < 0) {
153 ERR(ctx, _("unable to set key %d for table %d"),
158 if (ctx->keywords & LK_KEYWORD_ALTISMETA) {
159 unsigned int alttable = k_table | M_ALT;
160 int type = KTYP(keycode);
161 int val = KVAL(keycode);
163 if (alttable != k_table && lk_map_exists(ctx, alttable) &&
164 !lk_key_exists(ctx, alttable, k_index) &&
165 (type == KT_LATIN || type == KT_LETTER) &&
167 if (lk_add_key(ctx, alttable, k_index, K(KT_META, val)) < 0)
176 lk_get_func(struct lk_ctx *ctx, struct kbsentry *kbs)
180 s = lk_array_get_ptr(ctx->func_table, kbs->kb_func);
182 ERR(ctx, _("func %d not allocated"), kbs->kb_func);
186 strncpy((char *)kbs->kb_string, s, sizeof(kbs->kb_string));
187 kbs->kb_string[sizeof(kbs->kb_string) - 1] = 0;
194 lk_add_func(struct lk_ctx *ctx, struct kbsentry kbs)
198 s = lk_array_get_ptr(ctx->func_table, kbs.kb_func);
202 s = strdup((char *)kbs.kb_string);
204 if (lk_array_set(ctx->func_table, kbs.kb_func, &s) < 0) {
206 ERR(ctx, _("out of memory"));
214 lk_add_diacr(struct lk_ctx *ctx, unsigned int diacr, unsigned int base, unsigned int res)
216 struct kb_diacr *ptr;
218 ptr = malloc(sizeof(struct kb_diacr));
220 ERR(ctx, _("out of memory"));
228 lk_array_append(ctx->accent_table, &ptr);
234 lk_add_compose(struct lk_ctx *ctx,
239 int direction = TO_8BIT;
242 if (ctx->flags & LK_FLAG_PREFER_UNICODE)
243 direction = TO_UNICODE;
245 return lk_add_diacr(ctx,
246 convert_code(ctx, diacr, direction),
247 convert_code(ctx, base, direction),
248 convert_code(ctx, res, direction)
253 do_constant_key(struct lk_ctx *ctx, int i, u_short key)
261 if ((typ == KT_LATIN || typ == KT_LETTER) &&
262 ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'))) {
264 defs[0] = K(KT_LETTER, val);
265 defs[1] = K(KT_LETTER, val ^ 32);
269 for (j = 4; j < 8; j++)
270 defs[j] = K(KT_LATIN, val & ~96);
272 for (j = 8; j < 16; j++)
273 defs[j] = K(KT_META, KVAL(defs[j - 8]));
275 for (j = 0; j < ctx->keymap->total; j++) {
276 if (!lk_map_exists(ctx, j))
279 if (j > 0 && lk_key_exists(ctx, j, i))
282 if (lk_add_key(ctx, j, i, defs[j % 16]) < 0)
287 /* do this also for keys like Escape,
288 as promised in the man page */
289 for (j = 1; j < ctx->keymap->total; j++) {
290 if (!lk_map_exists(ctx, j))
293 if (lk_key_exists(ctx, j, i))
296 if (lk_add_key(ctx, j, i, key) < 0)
304 lk_add_constants(struct lk_ctx *ctx)
306 unsigned int i, r0 = 0;
308 if (ctx->keywords & LK_KEYWORD_KEYMAPS) {
309 while (r0 < ctx->keymap->total && !lk_map_exists(ctx, r0))
313 for (i = 0; i < ctx->key_constant->total; i++) {
317 constant = lk_array_get(ctx->key_constant, i);
318 if (!constant || !(*constant))
321 if (!lk_map_exists(ctx, r0)) {
322 ERR(ctx, _("impossible error in lk_add_constants"));
326 key = lk_get_key(ctx, r0, i);
328 if (do_constant_key(ctx, i, key) < 0)