aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

commit ef38d85c356bf647205fc3167ae8d7b62c081e25
parent ee7601cd438c7ee7eab356d497f7e487c2b56f11
Author: xplshn <anto@xplshn.com.ar>
Date:   Thu, 26 Nov 2026 19:04:36 -0300

kmap: keyboard layouts as portable text, loaded into the console or written as an xkb keymap

Diffstat:
Acmd/linux/kmap.c | 455+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ashared/kmap.h | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ashared/libutil/kmap.c | 312+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 857 insertions(+), 0 deletions(-)

diff --git a/cmd/linux/kmap.c b/cmd/linux/kmap.c @@ -0,0 +1,455 @@ +/* see LICENSE file for copyright and license details */ +#include <sys/ioctl.h> +#include <sys/types.h> + +#include <dirent.h> +#include <fcntl.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "arg.h" +#include "kmap.h" +#include "paths.h" +#include "util.h" + +// the kernel's console keyboard interface (linux/kd.h, linux/keyboard.h) +#define KDSKBENT 0x4B47 +#define KDSKBDIACRUC 0x4BFB +#define KDGKBTYPE 0x4B33 + +#define K(t, v) (((t) << 8) | (v)) +#define KT_LATIN 0 +#define KT_SPEC 2 +#define KT_PAD 3 +#define KT_DEAD 4 +#define KT_SHIFT 7 +#define KT_META 8 +#define KT_LETTER 11 +#define KT_DEAD2 13 +#define KG_ALTGR 1 +#define K_HOLE K(KT_SPEC, 0) +#define K_ENTER K(KT_SPEC, 1) +#define K_COMPOSE K(KT_SPEC, 14) +#define K_PCOMMA K(KT_PAD, 15) + +// the tables the kernel keeps a key's meaning in, one per held modifier set +#define T_SHIFT 1 +#define T_ALTGR 2 +#define T_CTRL 4 +#define T_ALT 8 + +struct kbentry { + unsigned char table; + unsigned char index; + unsigned short value; +}; + +struct kbdiacruc { + unsigned int diacr, base, result; +}; + +struct kbdiacrsuc { + unsigned int cnt; + struct kbdiacruc d[256]; +}; + +// a key's xkb name and its evdev code, which the console's keycodes are +static const struct { + const char *name; + int code; +} codes[] = { + {"TLDE", 41}, {"AE01", 2}, {"AE02", 3}, {"AE03", 4}, {"AE04", 5}, {"AE05", 6}, + {"AE06", 7}, {"AE07", 8}, {"AE08", 9}, {"AE09", 10}, {"AE10", 11}, {"AE11", 12}, + {"AE12", 13}, {"AE13", 124}, {"AD01", 16}, {"AD02", 17}, {"AD03", 18}, {"AD04", 19}, + {"AD05", 20}, {"AD06", 21}, {"AD07", 22}, {"AD08", 23}, {"AD09", 24}, {"AD10", 25}, + {"AD11", 26}, {"AD12", 27}, {"BKSL", 43}, {"AC01", 30}, {"AC02", 31}, {"AC03", 32}, + {"AC04", 33}, {"AC05", 34}, {"AC06", 35}, {"AC07", 36}, {"AC08", 37}, {"AC09", 38}, + {"AC10", 39}, {"AC11", 40}, {"LSGT", 86}, {"AB01", 44}, {"AB02", 45}, {"AB03", 46}, + {"AB04", 47}, {"AB05", 48}, {"AB06", 49}, {"AB07", 50}, {"AB08", 51}, {"AB09", 52}, + {"AB10", 53}, {"AB11", 89}, {"SPCE", 57}, {"KPDL", 83}, +}; + +// dead keys: the kernel's own six, then the rest as dead2 with a latin-1 +// character naming them in the accent table, as only those fit its byte +static const struct { + const char *name; + int type, value; + unsigned diacr; // what the accent table calls it +} deads[] = { + {"dead_grave", KT_DEAD, 0, '`'}, {"dead_acute", KT_DEAD, 1, '\''}, + {"dead_circumflex", KT_DEAD, 2, '^'}, {"dead_tilde", KT_DEAD, 3, '~'}, + {"dead_diaeresis", KT_DEAD, 4, '"'}, {"dead_cedilla", KT_DEAD, 5, ','}, + {"dead_macron", KT_DEAD2, 0xaf, 0xaf}, {"dead_abovering", KT_DEAD2, 0xb0, 0xb0}, + {"dead_caron", KT_DEAD2, 0xa6, 0xa6}, {"dead_breve", KT_DEAD2, 0xa4, 0xa4}, + {"dead_abovedot", KT_DEAD2, 0xb7, 0xb7}, {"dead_doubleacute", KT_DEAD2, 0xa8, 0xa8}, + {"dead_ogonek", KT_DEAD2, 0xb8, 0xb8}, {"dead_belowdot", KT_DEAD2, 0xa7, 0xa7}, + {"dead_hook", KT_DEAD2, 0xbf, 0xbf}, {"dead_horn", KT_DEAD2, 0xb9, 0xb9}, + {"dead_stroke", KT_DEAD2, 0xac, 0xac}, {"dead_belowcomma", KT_DEAD2, 0xb5, 0xb5}, + {"dead_belowmacron", KT_DEAD2, 0xb3, 0xb3}, {"dead_iota", KT_DEAD2, 0xbd, 0xbd}, +}; + +// names of characters, the rest of what a layout may write as a name +static const struct { + const char *name; + unsigned c; +} named[] = { + {"space", ' '}, {"nobreakspace", 0xa0}, {"combining_grave", 0x300}, + {"combining_acute", 0x301}, {"combining_tilde", 0x303}, {"combining_hook", 0x309}, + {"combining_belowdot", 0x323}, +}; + +static char *ttys[] = { + ARUU_LINUX_PATH_PROC "/self/fd/0", + ARUU_PATH_DEVCONSOLE, + ARUU_PATH_DEVTTY, + "/dev/tty0", +}; + +static void +usage(void) +{ + eprintf("usage: %s [-d dir] [-l [layout]] [-n] [-x [-o options] [-r rules]]\n" + " [layout[,layout] [variant[,variant]]]\n", argv0); +} + +// a descriptor of a console that takes keyboard ioctls +static int +console(void) +{ + char type; + size_t i; + int fd; + + for (i = 0; i < LEN(ttys); i++) { + if ((fd = open(ttys[i], O_RDWR)) < 0 && (fd = open(ttys[i], O_RDONLY)) < 0) + continue; + if (ioctl(fd, KDGKBTYPE, &type) == 0) + return fd; + close(fd); + } + eprintf("no console to set the keyboard of\n"); + return -1; +} + +static int +dead_of(const char *s) +{ + size_t i; + + for (i = 0; i < LEN(deads); i++) + if (!strcmp(deads[i].name, s)) + return (int)i; + return -1; +} + +// the unicode character a symbol stands for, -1 for none +static long +char_of(const char *s) +{ + long c = kmap_char(s); + size_t i; + char *end; + + if (c >= 0) + return c; + for (i = 0; i < LEN(named); i++) + if (!strcmp(named[i].name, s)) + return named[i].c; + if (s[0] == 'U' && s[1]) { + c = strtol(s + 1, &end, 16); + if (!*end) + return c; + } + return -1; +} + +// the console value of a symbol: latin-1 as itself (a letter when caps +// shifts it), the rest of unicode as the kernel takes it, dead keys and the +// few names the console has a meaning for, else a hole +static unsigned short +value_of(const char *s, int caps) +{ + long c; + int d; + + if ((d = dead_of(s)) >= 0) + return K(deads[d].type, deads[d].value); + if (!strcmp(s, "ISO_Level3_Shift")) + return K(KT_SHIFT, KG_ALTGR); + if (!strcmp(s, "Multi_key")) + return K_COMPOSE; + if (!strcmp(s, "Return")) + return K_ENTER; + if ((c = char_of(s)) < 0 || c >= 0xf000) + return K_HOLE; + if (c < 0x100) + return K(caps ? KT_LETTER : KT_LATIN, c); + return (unsigned short)(c ^ 0xf000); +} + +static int +set(int fd, int table, int code, unsigned short v) +{ + struct kbentry e = {table, code, v}; + + if (ioctl(fd, KDSKBENT, &e) != 0) { + weprintf("KDSKBENT table %d key %d:", table, code); + return -1; + } + return 0; +} + +static int +code_of(const char *name) +{ + size_t i; + + for (i = 0; i < LEN(codes); i++) + if (!strcmp(codes[i].name, name)) + return codes[i].code; + return -1; +} + +static int +load(const struct kmap *k) +{ + // the console has four of a key's levels + static const int tables[4] = {0, T_SHIFT, T_ALTGR, T_SHIFT | T_ALTGR}; + struct kbdiacrsuc acc; + int fd = console(), rc = 0, l, code; + size_t i, j; + + for (i = 0; i < k->nkeys; i++) { + const struct kmap_key *key = &k->keys[i]; + long c0; + + if ((code = code_of(key->name)) < 0) + continue; + // the keypad's decimal key keeps the console's own, which numlock + // turns between delete and its character: only which one it types differs + if (!strcmp(key->name, "KPDL")) { + for (l = 0; l < key->nsym; l++) + if (!strcmp(key->sym[l], ",")) + rc |= set(fd, 0, code, K_PCOMMA); + continue; + } + for (l = 0; l < 4; l++) + rc |= set(fd, tables[l], code, + l < key->nsym ? value_of(key->sym[l], key->caps) : K_HOLE); + // control and alt follow the letter the key types, so ctrl+a is ^A on any + // latin layout. on another script they stay where the console had them + c0 = key->nsym ? char_of(key->sym[0]) : -1; + if (c0 >= 'a' && c0 <= 'z') { + rc |= set(fd, T_CTRL, code, K(KT_LATIN, c0 & 0x1f)); + rc |= set(fd, T_CTRL | T_SHIFT, code, K(KT_LATIN, c0 & 0x1f)); + } + if (c0 > 0x20 && c0 < 0x7f) + rc |= set(fd, T_ALT, code, K(KT_META, c0)); + } + + // the accent table: what each dead key of this layout makes, as many as + // the kernel holds + memset(&acc, 0, sizeof(acc)); + for (j = 0; j < k->ncompose && acc.cnt < LEN(acc.d); j++) { + const struct kmap_compose *cp = &k->compose[j]; + long base = char_of(cp->base), res = char_of(cp->result); + int d = dead_of(cp->dead), used = 0; + + if (d < 0 || base < 0 || res < 0) + continue; + for (i = 0; i < k->nkeys && !used; i++) + for (l = 0; l < k->keys[i].nsym && !used; l++) + used = !strcmp(k->keys[i].sym[l], cp->dead); + if (!used) + continue; + acc.d[acc.cnt].diacr = deads[d].diacr; + acc.d[acc.cnt].base = base; + acc.d[acc.cnt].result = res; + acc.cnt++; + } + if (ioctl(fd, KDSKBDIACRUC, &acc) != 0) { + weprintf("KDSKBDIACRUC:"); + rc = -1; + } + close(fd); + return rc; +} + +static int +by_name(const void *a, const void *b) +{ + return strcmp(*(char *const *)a, *(char *const *)b); +} + +// the layouts under dir, or one layout's variants, a line each with its name +static int +list(const char *dir, const char *layout) +{ + char path[PATH_MAX], **names = NULL; + size_t n = 0, i; + DIR *d; + struct dirent *e; + + snprintf(path, sizeof(path), "%s%s%s", dir, layout ? "/" : "", layout ? layout : ""); + if (!(d = opendir(path))) + eprintf("open %s:", path); + while ((e = readdir(d))) { + size_t l = strlen(e->d_name); + + if (e->d_name[0] == '.' || !strcmp(e->d_name, "compose.kmap")) + continue; + if (layout && (l < 6 || strcmp(e->d_name + l - 5, ".kmap"))) + continue; + names = ereallocarray(names, n + 1, sizeof(*names)); + names[n++] = layout ? estrndup(e->d_name, l - 5) : estrdup(e->d_name); + } + closedir(d); + qsort(names, n, sizeof(*names), by_name); + for (i = 0; i < n; i++) { + struct kmap k; + + memset(&k, 0, sizeof(k)); + kmap_path(path, sizeof(path), dir, layout ? layout : names[i], layout ? names[i] : NULL); + if (kmap_read(path, &k) == 0) + printf("%s\t%s\n", names[i], k.name); + kmap_free(&k); + free(names[i]); + } + free(names); + return 0; +} + +// "layout [variant]" from the file the system keeps its keyboard in +static int +configured(char *layout, size_t lsz, char *variant, size_t vsz) +{ + char line[256], *w[3]; + FILE *fp = fopen(ARUU_PATH_ETC "/kmap", "r"); + int n = 0; + + if (!fp) + return -1; + while (fgets(line, sizeof(line), fp) && !(n = kmap_words(line, w, 3))) + ; + fclose(fp); + if (!n) + return -1; + snprintf(layout, lsz, "%s", w[0]); + snprintf(variant, vsz, "%s", n > 1 ? w[1] : ""); + return 0; +} + +// the n-th comma separated word of list into buf, "" past its end +static void +nth(char *buf, size_t sz, const char *list, int n) +{ + const char *e; + + while (n-- > 0 && list && (list = strchr(list, ','))) + list++; + if (!list) { + buf[0] = '\0'; + return; + } + e = strchr(list, ','); + snprintf(buf, sz, "%.*s", e ? (int)(e - list) : (int)strlen(list), list); +} + +// ?man kmap: set the keyboard layout of the console, or give it to xkb +// ?man arguments: [-d dir] [-l [layout]] [-n] [-x [-o options] [-r rules]] [layout[,layout] [variant[,variant]]] +// ?man kmap loads a kmap layout (see kmap.h: portable text, every layout +// ?man alpine ships) into the console's keyboard: every console takes it. +// ?man with no layout it loads what /etc/kmap names ("layout variant"). +// ?man -x writes the layouts as one xkb keymap instead, a group each, for +// ?man wayland compositors and x: what wlde's keyboard is made from +int +main(int argc, char *argv[]) +{ + const char *dir = KMAP_DIR, *options = NULL, *rules = NULL; + char path[PATH_MAX], layouts[256], variants[256], rulesbuf[PATH_MAX]; + int lflag = 0, xflag = 0, nflag = 0, rc = 0, n, i; + struct kmap maps[4]; + + ARGBEGIN + { + // ?man -d:str: -d dir: where the layouts are, /share/kmap when not given + case 'd': + dir = EARGF(usage()); + break; + // ?man -l: list the layouts, or with a layout its variants + case 'l': + lflag = 1; + break; + // ?man -n: read the layout and say whether it is good, set nothing + case 'n': + nflag = 1; + break; + // ?man -o:str: -o options: xkb options for -x, comma separated + case 'o': + options = EARGF(usage()); + break; + // ?man -r:str: -r rules: the xkb rules file options are looked up in + case 'r': + rules = EARGF(usage()); + break; + // ?man -x: write the layouts as an xkb keymap + case 'x': + xflag = 1; + break; + default: + usage(); + } + ARGEND + + if (lflag) + return argc > 1 ? (usage(), 1) : list(dir, argc ? argv[0] : NULL); + if (argc > 2) + usage(); + if (argc) { + snprintf(layouts, sizeof(layouts), "%s", argv[0]); + snprintf(variants, sizeof(variants), "%s", argc > 1 ? argv[1] : ""); + } else if (configured(layouts, sizeof(layouts), variants, sizeof(variants)) != 0) { + // no layout chosen: the console keeps its own + return 0; + } + if (!rules) { + const char *root = getenv("XKB_CONFIG_ROOT"); + + snprintf(rulesbuf, sizeof(rulesbuf), "%s/rules/evdev", root && *root ? root : "/share/X11/xkb"); + rules = rulesbuf; + } + + // the console has one layout: the first of them + memset(maps, 0, sizeof(maps)); + for (n = 0; n < (xflag ? (int)LEN(maps) : 1); n++) { + char layout[64], variant[128]; + + nth(layout, sizeof(layout), layouts, n); + if (!layout[0]) + break; + nth(variant, sizeof(variant), variants, n); + kmap_path(path, sizeof(path), dir, layout, variant); + if (kmap_read(path, &maps[n]) != 0) { + rc = 1; + goto done; + } + } + if (!xflag) { + snprintf(path, sizeof(path), "%s/compose.kmap", dir); + if (access(path, F_OK) == 0 && kmap_read(path, &maps[0]) != 0) { + rc = 1; + goto done; + } + } + if (xflag) + rc = kmap_write_xkb(stdout, maps, n, options, rules); + else if (!nflag) + rc = load(&maps[0]); +done: + for (i = 0; i < (int)LEN(maps); i++) + kmap_free(&maps[i]); + return rc != 0; +} diff --git a/shared/kmap.h b/shared/kmap.h @@ -0,0 +1,90 @@ +/* see LICENSE file for copyright and license details */ +#ifndef KMAP_H +#define KMAP_H + +#include <stddef.h> +#include <stdio.h> + +/* + * kmap: a keyboard layout as portable text, one file a layout or variant. + * lines are a key and words, rc quoted ('...' one word, '' a quote inside it), + * # starts a comment: + * + * kmap 1 + * id de-nodeadkeys + * layout de + * variant nodeadkeys + * name German (no dead keys) + * level3 ralt + * key AD01 q Q @ Ω caps + * key SPCE space + * + * a key is named by its xkb position (AE01 the key under 1, AD01 under q on a + * us board, TLDE, BKSL, LSGT, SPCE, KP7), which no system owns. its symbols + * are its levels: base, shift, level 3 (altgr), level 3 with shift, and to + * eight for the layouts that reach level 5. type=NAME, on the keys that + * need it, is the xkb type that says which modifiers reach which level (a + * keypad's second level is num lock's: type=KEYPAD); without it the levels + * are xkb's own choice for them. a symbol + * is the one character it types, or a keysym name for what is not one + * (space, dead_acute, KP_Delete), none for nothing. caps marks a key caps + * lock shifts. level3 ralt says right alt reaches level 3. + * + * compose.kmap, beside the layouts, says what a dead key makes: + * + * compose dead_acute e é + * + * the layouts live in KMAP_DIR/<layout>/<variant>.kmap, the base layout in + * KMAP_DIR/<layout>/<layout>.kmap + */ + +#define KMAP_DIR "/share/kmap" +#define KMAP_LEVELS 8 +#define KMAP_SYM 32 + +struct kmap_key { + char name[8]; + char sym[KMAP_LEVELS][KMAP_SYM]; + int nsym; + int caps; + char type[48]; // the xkb type, "" when xkb's own choice is the layout's +}; + +struct kmap_compose { + char dead[KMAP_SYM]; + char base[KMAP_SYM]; + char result[KMAP_SYM]; +}; + +struct kmap { + char id[64], layout[32], variant[64], name[256]; + int level3_ralt; + struct kmap_key *keys; + size_t nkeys; + struct kmap_compose *compose; + size_t ncompose; +}; + +/* path of layout's variant (NULL or "" for the base layout) under dir */ +void kmap_path(char *buf, size_t sz, const char *dir, const char *layout, const char *variant); + +/* reads path into k, adding to what k has: a layout, then compose.kmap, whose + own id and name do not replace the layout's. k starts zeroed. 0, or -1 + with the reason said */ +int kmap_read(const char *path, struct kmap *k); +void kmap_free(struct kmap *k); + +/* the unicode character sym is, -1 when it is a name */ +long kmap_char(const char *sym); + +/* the words of line, rc unquoted in place, into w (max of them). their number */ +int kmap_words(char *line, char **w, int max); + +/* the n layouts of maps as one xkb keymap, a group each, the keys they do + not name from the pc layout. options are xkb's (caps:escape, + grp:alt_shift_toggle), looked up in rules (xkeyboard-config's + rules/evdev); either may be NULL */ +int kmap_write_xkb(FILE *fp, const struct kmap *maps, int n, const char *options, + const char *rules); + +#endif diff --git a/shared/libutil/kmap.c b/shared/libutil/kmap.c @@ -0,0 +1,312 @@ +/* see LICENSE file for copyright and license details */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "../kmap.h" +#include "../util.h" + +void +kmap_path(char *buf, size_t sz, const char *dir, const char *layout, const char *variant) +{ + snprintf(buf, sz, "%s/%s/%s.kmap", dir ? dir : KMAP_DIR, layout, + variant && *variant ? variant : layout); +} + +int +kmap_words(char *line, char **w, int max) +{ + char *r = line, *o = line; + int n = 0; + + while (*r) { + while (*r == ' ' || *r == '\t' || *r == '\r' || *r == '\n') + r++; + if (!*r || *r == '#') + break; + if (n == max) + break; + w[n++] = o; + while (*r && *r != ' ' && *r != '\t' && *r != '\r' && *r != '\n') { + if (*r != '\'') { + *o++ = *r++; + continue; + } + // a quoted run: '' inside it is one quote + for (r++; *r; r++) { + if (*r == '\'' && r[1] == '\'') { + *o++ = '\''; + r++; + } else if (*r == '\'') { + r++; + break; + } else + *o++ = *r; + } + } + if (*r) + r++; + *o++ = '\0'; + } + return n; +} + +long +kmap_char(const char *s) +{ + const unsigned char *u = (const unsigned char *)s; + long c; + int n, i; + + if (!u[0]) + return -1; + if (u[0] < 0x80) + return u[1] ? -1 : u[0]; + if ((u[0] & 0xe0) == 0xc0) + n = 1, c = u[0] & 0x1f; + else if ((u[0] & 0xf0) == 0xe0) + n = 2, c = u[0] & 0x0f; + else if ((u[0] & 0xf8) == 0xf0) + n = 3, c = u[0] & 0x07; + else + return -1; + for (i = 1; i <= n; i++) { + if ((u[i] & 0xc0) != 0x80) + return -1; + c = c << 6 | (u[i] & 0x3f); + } + return u[n + 1] ? -1 : c; +} + +static void +copy(char *dst, size_t sz, const char *src) +{ + snprintf(dst, sz, "%s", src); +} + +// the rest of the words joined by a space, a text such as a name +static void +join(char *dst, size_t sz, char **w, int n) +{ + int i; + + dst[0] = '\0'; + for (i = 0; i < n; i++) { + if (i) + strlcat(dst, " ", sz); + strlcat(dst, w[i], sz); + } +} + +int +kmap_read(const char *path, struct kmap *k) +{ + char line[1024], *w[16]; + FILE *fp; + int n, lineno = 0; + + if (!(fp = fopen(path, "r"))) { + weprintf("open %s:", path); + return -1; + } + while (fgets(line, sizeof(line), fp)) { + lineno++; + if (!(n = kmap_words(line, w, 16))) + continue; + // a header field keeps its first value: compose.kmap read after a + // layout does not rename it + if (!strcmp(w[0], "id")) { + if (n > 1 && !k->id[0]) + copy(k->id, sizeof(k->id), w[1]); + } else if (!strcmp(w[0], "layout")) { + if (n > 1 && !k->layout[0]) + copy(k->layout, sizeof(k->layout), w[1]); + } else if (!strcmp(w[0], "variant")) { + if (n > 1 && !k->variant[0]) + copy(k->variant, sizeof(k->variant), w[1]); + } else if (!strcmp(w[0], "name")) { + if (!k->name[0]) + join(k->name, sizeof(k->name), w + 1, n - 1); + } + else if (!strcmp(w[0], "level3") && n > 1) + k->level3_ralt = !strcmp(w[1], "ralt"); + else if (!strcmp(w[0], "key") && n > 2) { + struct kmap_key *key; + int i; + + k->keys = ereallocarray(k->keys, k->nkeys + 1, sizeof(*k->keys)); + key = &k->keys[k->nkeys++]; + memset(key, 0, sizeof(*key)); + copy(key->name, sizeof(key->name), w[1]); + for (i = 2; i < n; i++) { + if (!strcmp(w[i], "caps")) + key->caps = 1; + else if (!strncmp(w[i], "type=", 5)) + copy(key->type, sizeof(key->type), w[i] + 5); + else if (key->nsym < KMAP_LEVELS) + copy(key->sym[key->nsym++], KMAP_SYM, w[i]); + } + } else if (!strcmp(w[0], "compose") && n == 4) { + struct kmap_compose *c; + + k->compose = ereallocarray(k->compose, k->ncompose + 1, sizeof(*k->compose)); + c = &k->compose[k->ncompose++]; + copy(c->dead, sizeof(c->dead), w[1]); + copy(c->base, sizeof(c->base), w[2]); + copy(c->result, sizeof(c->result), w[3]); + } else if (strcmp(w[0], "kmap")) + weprintf("%s:%d: %s is not a kmap line, skipped\n", path, lineno, w[0]); + } + fclose(fp); + return 0; +} + +void +kmap_free(struct kmap *k) +{ + free(k->keys); + free(k->compose); + memset(k, 0, sizeof(*k)); +} + +// a symbol as xkb writes it: latin-1 by its code, which is its keysym, other +// characters as Uxxxx, names as they are +static void +xkb_sym(FILE *fp, const char *s) +{ + long c = kmap_char(s); + + if (!strcmp(s, "none")) + fputs("NoSymbol", fp); + else if (c < 0) + fputs(s, fp); + else if (c < 0x100) + fprintf(fp, "0x%02lx", c); + else + fprintf(fp, "U%04lX", c); +} + +// the includes an xkb option stands for in section (symbols, compat or +// types) of the rules file, written as include lines. an option the rules +// do not know is said +static int +option_includes(FILE *fp, const char *rules, const char *option, const char *section) +{ + char line[512], *w[8]; + FILE *rf; + int in = 0, found = 0, n; + + if (!(rf = fopen(rules, "r"))) + return 0; + while (fgets(line, sizeof(line), rf)) { + if (line[0] == '!') { + n = kmap_words(line + 1, w, 8); + in = n == 3 && !strcmp(w[0], "option") && !strcmp(w[1], "=") && !strcmp(w[2], section); + continue; + } + if (!in || kmap_words(line, w, 8) != 3 || strcmp(w[0], option) || strcmp(w[1], "=")) + continue; + // +a(b)+c(d): each part is a file and map of its own + for (char *p = strtok(w[2], "+|"); p; p = strtok(NULL, "+|")) + fprintf(fp, " include \"%s\"\n", p); + found = 1; + } + fclose(rf); + return found; +} + +static void +put_options(FILE *fp, const char *rules, const char *options, const char *section) +{ + const char *o, *end; + char one[128]; + + if (!options || !rules) + return; + // split by hand: option_includes tokenizes the rules lines with strtok + for (o = options; *o; o = *end ? end + 1 : end) { + end = strchr(o, ','); + if (!end) + end = o + strlen(o); + if (end == o || (size_t)(end - o) >= sizeof(one)) + continue; + snprintf(one, sizeof(one), "%.*s", (int)(end - o), o); + if (!option_includes(fp, rules, one, section) && !strcmp(section, "symbols")) + weprintf("xkb option %s is not in %s\n", one, rules); + } +} + +int +kmap_write_xkb(FILE *fp, const struct kmap *maps, int n, const char *options, const char *rules) +{ + const char **names = NULL; + size_t i, j, nnames = 0; + int g, l, ralt = 0; + + for (g = 0; g < n; g++) + ralt |= maps[g].level3_ralt; + fprintf(fp, "xkb_keymap {\n" + " xkb_keycodes { include \"evdev+aliases(qwerty)\" };\n" + " xkb_types {\n" + " include \"complete\"\n"); + put_options(fp, rules, options, "types"); + fprintf(fp, " };\n" + " xkb_compat {\n" + " include \"complete\"\n"); + put_options(fp, rules, options, "compat"); + fprintf(fp, " };\n" + " xkb_symbols {\n" + " include \"pc+inet(evdev)\"\n"); + if (ralt) + fputs(" include \"level3(ralt_switch)\"\n", fp); + for (g = 0; g < n; g++) { + fprintf(fp, " name[Group%d] = \"", g + 1); + for (i = 0; maps[g].name[i]; i++) + if (maps[g].name[i] != '"' && maps[g].name[i] != '\\') + fputc(maps[g].name[i], fp); + fputs("\";\n", fp); + } + // every key any of the layouts names, in the order they first name it + for (g = 0; g < n; g++) + for (i = 0; i < maps[g].nkeys; i++) { + for (j = 0; j < nnames && strcmp(names[j], maps[g].keys[i].name); j++) + ; + if (j == nnames) { + names = ereallocarray(names, nnames + 1, sizeof(*names)); + names[nnames++] = maps[g].keys[i].name; + } + } + for (j = 0; j < nnames; j++) { + int first = 1; + + // replace, so a level the layout leaves empty is not filled from pc + fprintf(fp, " replace key <%s> { ", names[j]); + for (g = 0; g < n; g++) { + const struct kmap_key *key = NULL; + + for (i = 0; i < maps[g].nkeys && !key; i++) + if (!strcmp(maps[g].keys[i].name, names[j])) + key = &maps[g].keys[i]; + if (!key) + continue; + if (key->type[0]) { + fprintf(fp, "%stype[Group%d] = \"%s\"", first ? "" : ", ", g + 1, key->type); + first = 0; + } + fprintf(fp, "%ssymbols[Group%d] = [ ", first ? "" : ", ", g + 1); + for (l = 0; l < key->nsym; l++) { + if (l) + fputs(", ", fp); + xkb_sym(fp, key->sym[l]); + } + fputs(" ]", fp); + first = 0; + } + fputs(" };\n", fp); + } + free(names); + // the options last, over the layouts' keys, as xkb's rules put them + put_options(fp, rules, options, "symbols"); + fputs(" };\n};\n", fp); + return ferror(fp) ? -1 : 0; +}