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_key_exists(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index)
25 map = lk_array_get_ptr(ctx->keymap, k_table);
30 key = lk_array_get(map, k_index);
39 lk_add_map(struct lk_ctx *ctx, unsigned int k_table)
41 struct lk_array *keys;
43 if (lk_map_exists(ctx, k_table)) {
47 keys = malloc(sizeof(struct lk_array));
49 ERR(ctx, _("out of memory"));
53 lk_array_init(keys, sizeof(unsigned int), 0);
55 if (lk_array_set(ctx->keymap, k_table, &keys) < 0) {
57 ERR(ctx, _("out of memory"));
65 lk_get_key(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index)
70 map = lk_array_get_ptr(ctx->keymap, k_table);
72 ERR(ctx, _("unable to keymap %d"), k_table);
76 key = lk_array_get(map, k_index);
77 if (!key || *key == 0) {
85 lk_del_key(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index)
89 map = lk_array_get_ptr(ctx->keymap, k_table);
91 ERR(ctx, _("unable to get keymap %d"), k_table);
95 if (!lk_array_exists(map, k_index))
98 if (lk_array_unset(map, k_index) < 0) {
99 ERR(ctx, _("unable to unset key %d for table %d"),
108 lk_add_key(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index, int keycode)
110 struct lk_array *map;
111 unsigned int code = keycode + 1;
113 if (keycode == CODE_FOR_UNKNOWN_KSYM) {
114 /* is safer not to be silent in this case,
115 * it can be caused by coding errors as well. */
116 ERR(ctx, _("lk_add_key called with bad keycode %d"), keycode);
120 if (!k_index && keycode == K_NOSUCHMAP)
123 map = lk_array_get_ptr(ctx->keymap, k_table);
125 if (ctx->keywords & LK_KEYWORD_KEYMAPS) {
126 ERR(ctx, _("adding map %d violates explicit keymaps line"),
131 if (lk_add_map(ctx, k_table) < 0)
135 if ((ctx->keywords & LK_KEYWORD_ALTISMETA) && keycode == K_HOLE &&
136 lk_key_exists(ctx, k_table, k_index))
139 map = lk_array_get_ptr(ctx->keymap, k_table);
141 if (lk_array_set(map, k_index, &code) < 0) {
142 ERR(ctx, _("unable to set key %d for table %d"),
147 if (ctx->keywords & LK_KEYWORD_ALTISMETA) {
148 unsigned int alttable = k_table | M_ALT;
149 int type = KTYP(keycode);
150 int val = KVAL(keycode);
152 if (alttable != k_table && !lk_key_exists(ctx, alttable, k_index) &&
153 (type == KT_LATIN || type == KT_LETTER) && val < 128) {
154 if (lk_add_map(ctx, alttable) < 0)
156 if (lk_add_key(ctx, alttable, k_index, K(KT_META, val)) < 0)
165 lk_get_func(struct lk_ctx *ctx, struct kbsentry *kbs)
169 s = lk_array_get_ptr(ctx->func_table, kbs->kb_func);
171 ERR(ctx, _("func %d not allocated"), kbs->kb_func);
175 strncpy((char *)kbs->kb_string, s, sizeof(kbs->kb_string));
176 kbs->kb_string[sizeof(kbs->kb_string) - 1] = 0;
183 lk_add_func(struct lk_ctx *ctx, struct kbsentry kbs)
187 s = lk_array_get_ptr(ctx->func_table, kbs.kb_func);
191 s = strdup((char *)kbs.kb_string);
193 if (lk_array_set(ctx->func_table, kbs.kb_func, &s) < 0) {
195 ERR(ctx, _("out of memory"));
203 lk_add_diacr(struct lk_ctx *ctx, unsigned int diacr, unsigned int base, unsigned int res)
205 struct kb_diacr *ptr;
207 ptr = malloc(sizeof(struct kb_diacr));
209 ERR(ctx, _("out of memory"));
217 lk_array_append(ctx->accent_table, &ptr);
223 lk_add_compose(struct lk_ctx *ctx,
228 int direction = TO_8BIT;
231 if (ctx->flags & LK_FLAG_PREFER_UNICODE)
232 direction = TO_UNICODE;
234 return lk_add_diacr(ctx,
235 convert_code(ctx, diacr, direction),
236 convert_code(ctx, base, direction),
237 convert_code(ctx, res, direction)
242 do_constant_key(struct lk_ctx *ctx, int i, u_short key)
250 if ((typ == KT_LATIN || typ == KT_LETTER) &&
251 ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'))) {
253 defs[0] = K(KT_LETTER, val);
254 defs[1] = K(KT_LETTER, val ^ 32);
258 for (j = 4; j < 8; j++)
259 defs[j] = K(KT_LATIN, val & ~96);
261 for (j = 8; j < 16; j++)
262 defs[j] = K(KT_META, KVAL(defs[j - 8]));
264 for (j = 0; j < ctx->keymap->total; j++) {
265 if (!lk_map_exists(ctx, j))
268 if (j > 0 && lk_key_exists(ctx, j, i))
271 if (lk_add_key(ctx, j, i, defs[j % 16]) < 0)
276 /* do this also for keys like Escape,
277 as promised in the man page */
278 for (j = 1; j < ctx->keymap->total; j++) {
279 if (!lk_map_exists(ctx, j))
282 if (lk_key_exists(ctx, j, i))
285 if (lk_add_key(ctx, j, i, key) < 0)
293 lk_add_constants(struct lk_ctx *ctx)
295 unsigned int i, r0 = 0;
297 if (ctx->keywords & LK_KEYWORD_KEYMAPS) {
298 while (r0 < ctx->keymap->total && !lk_map_exists(ctx, r0))
302 for (i = 0; i < ctx->key_constant->total; i++) {
306 constant = lk_array_get(ctx->key_constant, i);
307 if (!constant || !(*constant))
310 if (!lk_map_exists(ctx, r0)) {
311 ERR(ctx, _("impossible error in lk_add_constants"));
315 key = lk_get_key(ctx, r0, i);
317 if (do_constant_key(ctx, i, key) < 0)