aruu

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

commit 1b0d67c0f2aecdd404e9e66fa0a9c86ec9b178c2
parent 25a490711c5827ec903bdc304d49a6c6d7e15246
Author: xplshn <anto@xplshn.com.ar>
Date:   Thu, 26 Nov 2026 19:59:33 -0300

kmap is portable: the console through each system's backend, linux, wscons on netbsd and openbsd, syscons and vt on freebsd

Diffstat:
Acmd/extra/kmap.c | 197+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dcmd/linux/kmap.c | 456-------------------------------------------------------------------------------
Mninqu.rules | 11++++++++---
Mshared/kmap.h | 27+++++++++++++++++++++++++++
Mshared/libutil/kmap.c | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ashared/libutil/kmap_fbsd.c | 163+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ashared/libutil/kmap_linux.c | 209+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ashared/libutil/kmap_posix.c | 16++++++++++++++++
Ashared/libutil/kmap_wscons.c | 111+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9 files changed, 823 insertions(+), 459 deletions(-)

diff --git a/cmd/extra/kmap.c b/cmd/extra/kmap.c @@ -0,0 +1,197 @@ +/* see LICENSE file for copyright and license details */ +#include <dirent.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" + +static void +usage(void) +{ + eprintf("usage: %s [-d dir] [-l [layout]] [-n] [-x [-o options] [-r rules]]\n" + " [layout[,layout] [variant[,variant]]]\n", argv0); +} + +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 through the system's own interface (linux's console, wscons, syscons +// ?man and vt). +// ?man with no layout it loads what /etc/kmap names ("layout variant"). +// ?man with -x it writes the layouts as one xkb keymap instead, a group +// ?man each, for 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 = kmap_load_console(&maps[0]); +done: + for (i = 0; i < (int)LEN(maps); i++) + kmap_free(&maps[i]); + return rc != 0; +} diff --git a/cmd/linux/kmap.c b/cmd/linux/kmap.c @@ -1,456 +0,0 @@ -/* 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)); - // the kernel takes one fewer than its table holds - for (j = 0; j < k->ncompose && acc.cnt < LEN(acc.d) - 1; 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 with -x it writes the layouts as one xkb keymap instead, a group -// ?man each, for 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/ninqu.rules b/ninqu.rules @@ -53,7 +53,8 @@ "session_linux.c" "session_posix.c" "sysinfo_linux.c" "sysinfo_posix.c" "timer_linux.c" "timer_posix.c" - "tty_linux.c" "tty_posix.c") + "tty_linux.c" "tty_posix.c" + "kmap_linux.c" "kmap_wscons.c" "kmap_fbsd.c" "kmap_posix.c") (dep $(MATCH) FEATURES_H) (after NOFORK_LIST_H) (out "$(STEM).o") @@ -93,10 +94,14 @@ "shared/libutil/timer_linux.c" "shared/libutil/timer_posix.c" "shared/libutil/tty_linux.c" - "shared/libutil/tty_posix.c") + "shared/libutil/tty_posix.c" + "shared/libutil/kmap_linux.c" + "shared/libutil/kmap_wscons.c" + "shared/libutil/kmap_fbsd.c" + "shared/libutil/kmap_posix.c") (dep $(MATCH) "shared/diskutil.h" "shared/filesystem.h" "shared/net.h" "shared/passwd.h" "shared/proc.h" "shared/reboot.h" "shared/session.h" "shared/sysinfo.h" - "shared/timer.h" "shared/util.h" FEATURES_H) + "shared/timer.h" "shared/util.h" "shared/kmap.h" FEATURES_H) (after NOFORK_LIST_H) (out "shared/libutil/$(BASESTEM).o") (exec $(CC) $(CPPFLAGS_LIBUTIL) -Ishared -Ishared/libutil $(CFLAGS) -c -o $(OUT) $(MATCH))) diff --git a/shared/kmap.h b/shared/kmap.h @@ -80,6 +80,33 @@ 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 ways systems number keys: evdev codes (linux), pc scancodes of set 1 + (wscons for pc keyboards, syscons and vt), and usb hid usages (wscons for + usb keyboards) */ +enum kmap_codes { + KMAP_EVDEV, + KMAP_PC, + KMAP_HID, +}; + +/* where a key is in one of those numberings, -1 for a key kmap does not + place there */ +int kmap_code(const char *name, enum kmap_codes scheme); + +/* the unicode character a symbol types: a character, a named one (space, + nobreakspace, combining_acute), or Uxxxx. -1 for anything else */ +long kmap_unicode(const char *sym); + +/* the combining character a dead key puts on the next one (U+0301 for + dead_acute), -1 when sym is no dead key */ +long kmap_dead(const char *sym); + +/* the console's keyboard set to k, through the system's own interface: the + linux console (kmap_linux.c), wscons on netbsd and openbsd + (kmap_wscons.c), syscons and vt on freebsd (kmap_fbsd.c). 0, or -1 with + the reason said, as on a system kmap has no backend for (kmap_posix.c) */ +int kmap_load_console(const struct kmap *k); + /* 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 diff --git a/shared/libutil/kmap.c b/shared/libutil/kmap.c @@ -310,3 +310,95 @@ kmap_write_xkb(FILE *fp, const struct kmap *maps, int n, const char *options, co fputs(" };\n};\n", fp); return ferror(fp) ? -1 : 0; } + +// the keys a layout places and where they are: evdev codes, which are the +// pc scancodes of set 1 but for the two japanese keys, and usb hid usages +static const struct { + const char *name; + int evdev, pc, hid; +} codes[] = { + {"TLDE", 41, 41, 53}, {"AE01", 2, 2, 30}, {"AE02", 3, 3, 31}, {"AE03", 4, 4, 32}, + {"AE04", 5, 5, 33}, {"AE05", 6, 6, 34}, {"AE06", 7, 7, 35}, {"AE07", 8, 8, 36}, + {"AE08", 9, 9, 37}, {"AE09", 10, 10, 38}, {"AE10", 11, 11, 39}, {"AE11", 12, 12, 45}, + {"AE12", 13, 13, 46}, {"AE13", 124, 125, 137}, {"AD01", 16, 16, 20}, {"AD02", 17, 17, 26}, + {"AD03", 18, 18, 8}, {"AD04", 19, 19, 21}, {"AD05", 20, 20, 23}, {"AD06", 21, 21, 28}, + {"AD07", 22, 22, 24}, {"AD08", 23, 23, 12}, {"AD09", 24, 24, 18}, {"AD10", 25, 25, 19}, + {"AD11", 26, 26, 47}, {"AD12", 27, 27, 48}, {"BKSL", 43, 43, 49}, {"AC01", 30, 30, 4}, + {"AC02", 31, 31, 22}, {"AC03", 32, 32, 7}, {"AC04", 33, 33, 9}, {"AC05", 34, 34, 10}, + {"AC06", 35, 35, 11}, {"AC07", 36, 36, 13}, {"AC08", 37, 37, 14}, {"AC09", 38, 38, 15}, + {"AC10", 39, 39, 51}, {"AC11", 40, 40, 52}, {"LSGT", 86, 86, 100}, {"AB01", 44, 44, 29}, + {"AB02", 45, 45, 27}, {"AB03", 46, 46, 6}, {"AB04", 47, 47, 25}, {"AB05", 48, 48, 5}, + {"AB06", 49, 49, 17}, {"AB07", 50, 50, 16}, {"AB08", 51, 51, 54}, {"AB09", 52, 52, 55}, + {"AB10", 53, 53, 56}, {"AB11", 89, 115, 135}, {"SPCE", 57, 57, 44}, {"KPDL", 83, 83, 99}, +}; + +int +kmap_code(const char *name, enum kmap_codes scheme) +{ + size_t i; + + for (i = 0; i < sizeof(codes) / sizeof(codes[0]); i++) + if (!strcmp(codes[i].name, name)) + return scheme == KMAP_PC ? codes[i].pc : scheme == KMAP_HID ? codes[i].hid : codes[i].evdev; + return -1; +} + +// characters a layout writes by name +static const struct { + const char *name; + long c; +} named[] = { + {"space", ' '}, + {"nobreakspace", 0xa0}, + {"combining_grave", 0x300}, + {"combining_acute", 0x301}, + {"combining_tilde", 0x303}, + {"combining_hook", 0x309}, + {"combining_belowdot", 0x323}, +}; + +long +kmap_unicode(const char *s) +{ + long c = kmap_char(s); + size_t i; + char *end; + + if (c >= 0) + return c; + for (i = 0; i < sizeof(named) / sizeof(named[0]); 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 && c > 0) + return c; + } + return -1; +} + +// the dead keys and the combining characters they stand for +static const struct { + const char *name; + long c; +} deads[] = { + {"dead_grave", 0x300}, {"dead_acute", 0x301}, {"dead_circumflex", 0x302}, + {"dead_tilde", 0x303}, {"dead_macron", 0x304}, {"dead_breve", 0x306}, + {"dead_abovedot", 0x307}, {"dead_diaeresis", 0x308}, {"dead_hook", 0x309}, + {"dead_abovering", 0x30a}, {"dead_doubleacute", 0x30b}, {"dead_caron", 0x30c}, + {"dead_doublegrave", 0x30f}, {"dead_invertedbreve", 0x311}, {"dead_abovecomma", 0x313}, + {"dead_abovereversedcomma", 0x314}, {"dead_horn", 0x31b}, {"dead_belowdot", 0x323}, + {"dead_belowcomma", 0x326}, {"dead_cedilla", 0x327}, {"dead_ogonek", 0x328}, + {"dead_belowmacron", 0x331}, {"dead_stroke", 0x335}, {"dead_iota", 0x345}, +}; + +long +kmap_dead(const char *s) +{ + size_t i; + + for (i = 0; i < sizeof(deads) / sizeof(deads[0]); i++) + if (!strcmp(deads[i].name, s)) + return deads[i].c; + return -1; +} diff --git a/shared/libutil/kmap_fbsd.c b/shared/libutil/kmap_fbsd.c @@ -0,0 +1,163 @@ +/* see LICENSE file for copyright and license details */ +#include "../kmap.h" +#include "../util.h" + +/* syscons and vt, freebsd's consoles: the keyboard's map + * read (GIO_KEYMAP), the layout's keys put in it and written back, and an + * accent map made of the layout's dead keys (PIO_DEADKEYMAP). a key has + * eight states, plain, shift, ctrl, ctrl shift, then the same with alt, + * which is where these consoles keep level 3 and 4, right alt reaching them */ +#if defined(__FreeBSD__) && !defined(ARUU_FORCE_POSIX) +#include <sys/ioctl.h> +#include <sys/kbio.h> +#include <sys/types.h> + +#include <fcntl.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +// the states the four levels go in, and those control and alt get +#define S_PLAIN 0 +#define S_SHIFT 1 +#define S_CTRL 2 +#define S_CTRL_SHIFT 3 +#define S_ALT 4 +#define S_ALT_SHIFT 5 + +// right alt in these consoles' numbering +#define KC_RALT 93 + +static const char *ttys[] = {"/dev/ttyv0", "/dev/console"}; + +// the console's accent keys and the combining characters kmap names them by, +// with the spacing character the accent map gives each +static const struct { + long comb; + u_int key; + u_int spacing; +} accs[] = { + {0x300, DGRA, '`'}, {0x301, DACU, 0xb4}, {0x302, DCIR, '^'}, {0x303, DTIL, '~'}, + {0x304, DMAC, 0xaf}, {0x306, DBRE, 0x2d8}, {0x307, DDOT, 0x2d9}, {0x308, DDIA, 0xa8}, + {0x335, DSLA, '/'}, {0x30a, DRIN, 0xb0}, {0x327, DCED, 0xb8}, {0x30b, DDAC, 0x2dd}, + {0x328, DOGO, 0x2db}, {0x30c, DCAR, 0x2c7}, +}; + +static int +acc_of(const char *s) +{ + long c = kmap_dead(s); + size_t i; + + for (i = 0; c >= 0 && i < LEN(accs); i++) + if (accs[i].comb == c) + return (int)i; + return -1; +} + +// a state of a key: a character, or a special key (a dead key, alt gr) +static void +put(struct keyent_t *e, int state, const char *s) +{ + long c; + int a; + + e->spcl &= ~(0x80 >> state); + if ((a = acc_of(s)) >= 0) { + e->map[state] = accs[a].key; + e->spcl |= 0x80 >> state; + } else if (!strcmp(s, "ISO_Level3_Shift")) { + e->map[state] = RALT; + e->spcl |= 0x80 >> state; + } else if ((c = kmap_unicode(s)) >= 0) + e->map[state] = (u_int)c; + else { + e->map[state] = NOP; + e->spcl |= 0x80 >> state; + } +} + +int +kmap_load_console(const struct kmap *k) +{ + static const int states[4] = {S_PLAIN, S_SHIFT, S_ALT, S_ALT_SHIFT}; + keymap_t *km = ecalloc(1, sizeof(*km)); + accentmap_t *am = ecalloc(1, sizeof(*am)); + size_t i, j; + int fd = -1, code, l, rc = -1; + + if (ioctl(0, GIO_KEYMAP, km) == 0) + fd = 0; + for (i = 0; i < LEN(ttys) && fd < 0; i++) + if ((fd = open(ttys[i], O_RDWR)) >= 0 && ioctl(fd, GIO_KEYMAP, km) != 0) { + close(fd); + fd = -1; + } + if (fd < 0) { + weprintf("no console to set the keyboard of\n"); + goto done; + } + for (i = 0; i < k->nkeys; i++) { + const struct kmap_key *key = &k->keys[i]; + struct keyent_t *e; + long c0; + + if ((code = kmap_code(key->name, KMAP_PC)) < 0 || code >= km->n_keys) + continue; + e = &km->key[code]; + for (l = 0; l < 4; l++) + put(e, states[l], l < key->nsym ? key->sym[l] : (l & 1 ? key->sym[l - 1] : "none")); + e->flgs = key->caps ? FLAG_LOCK_C : 0; + // control follows the letter the key types, so ctrl+a is ^A on any latin + // layout. on another script it stays where the console had it + c0 = key->nsym ? kmap_unicode(key->sym[0]) : -1; + if (c0 >= 'a' && c0 <= 'z') { + e->map[S_CTRL] = e->map[S_CTRL_SHIFT] = (u_int)(c0 & 0x1f); + e->spcl &= ~((0x80 >> S_CTRL) | (0x80 >> S_CTRL_SHIFT)); + } + } + if (k->level3_ralt && KC_RALT < km->n_keys) { + for (l = 0; l < NUM_STATES; l++) + km->key[KC_RALT].map[l] = RALT; + km->key[KC_RALT].spcl = 0xff; + km->key[KC_RALT].flgs = 0; + } + + // the accent map: for each dead key of the layout, what it makes + memset(am, 0, sizeof(*am)); + am->n_accs = NUM_DEADKEYS; + for (j = 0; j < k->ncompose; j++) { + const struct kmap_compose *cp = &k->compose[j]; + long base = kmap_unicode(cp->base), res = kmap_unicode(cp->result); + int a = acc_of(cp->dead), n; + struct acc_t *acc; + + if (a < 0 || base < 0 || res < 0) + continue; + acc = &am->acc[accs[a].key - F_ACC]; + acc->accchar = accs[a].spacing; + for (n = 0; n < NUM_ACCENTCHARS && acc->map[n][0]; n++) + ; + if (n < NUM_ACCENTCHARS) { + acc->map[n][0] = (u_int)base; + acc->map[n][1] = (u_int)res; + } + } + + rc = 0; + if (ioctl(fd, PIO_KEYMAP, km) != 0) { + weprintf("PIO_KEYMAP:"); + rc = -1; + } + if (ioctl(fd, PIO_DEADKEYMAP, am) != 0) { + weprintf("PIO_DEADKEYMAP:"); + rc = -1; + } + if (fd > 0) + close(fd); +done: + free(km); + free(am); + return rc; +} +#endif diff --git a/shared/libutil/kmap_linux.c b/shared/libutil/kmap_linux.c @@ -0,0 +1,209 @@ +/* see LICENSE file for copyright and license details */ +#include "../kmap.h" +#include "../paths.h" +#include "../util.h" + +/* the linux console: a key's meaning in one table per held modifier set + * (KDSKBENT), and an accent table for the dead keys (KDSKBDIACRUC) */ +#if defined(__linux__) && !defined(ARUU_FORCE_POSIX) +#include <sys/ioctl.h> + +#include <fcntl.h> +#include <string.h> +#include <unistd.h> + +// linux/kd.h and linux/keyboard.h, which a libc need not carry +#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) + +#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 dead key's console value: the kernel's own six, then the rest as dead2, +// named in the accent table by a latin-1 character, as only those fit +static const struct { + long comb; // what kmap_dead gives + int type, value; + unsigned diacr; +} deads[] = { + {0x300, KT_DEAD, 0, '`'}, {0x301, KT_DEAD, 1, '\''}, {0x302, KT_DEAD, 2, '^'}, + {0x303, KT_DEAD, 3, '~'}, {0x308, KT_DEAD, 4, '"'}, {0x327, KT_DEAD, 5, ','}, + {0x304, KT_DEAD2, 0xaf, 0xaf}, {0x30a, KT_DEAD2, 0xb0, 0xb0}, {0x30c, KT_DEAD2, 0xa6, 0xa6}, + {0x306, KT_DEAD2, 0xa4, 0xa4}, {0x307, KT_DEAD2, 0xb7, 0xb7}, {0x30b, KT_DEAD2, 0xa8, 0xa8}, + {0x328, KT_DEAD2, 0xb8, 0xb8}, {0x323, KT_DEAD2, 0xa7, 0xa7}, {0x309, KT_DEAD2, 0xbf, 0xbf}, + {0x31b, KT_DEAD2, 0xb9, 0xb9}, {0x335, KT_DEAD2, 0xac, 0xac}, {0x326, KT_DEAD2, 0xb5, 0xb5}, + {0x331, KT_DEAD2, 0xb3, 0xb3}, {0x345, KT_DEAD2, 0xbd, 0xbd}, +}; + +static char *ttys[] = { + ARUU_LINUX_PATH_PROC "/self/fd/0", + ARUU_PATH_DEVCONSOLE, + ARUU_PATH_DEVTTY, + "/dev/tty0", +}; + +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); + } + weprintf("no console to set the keyboard of\n"); + return -1; +} + +static int +dead_of(const char *s) +{ + long c = kmap_dead(s); + size_t i; + + for (i = 0; c >= 0 && i < LEN(deads); i++) + if (deads[i].comb == c) + return (int)i; + return -1; +} + +// 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 means +// something by, 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 = kmap_unicode(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; +} + +int +kmap_load_console(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; + + if (fd < 0) + return -1; + for (i = 0; i < k->nkeys; i++) { + const struct kmap_key *key = &k->keys[i]; + long c0; + + if ((code = kmap_code(key->name, KMAP_EVDEV)) < 0) + continue; + // the keypad's decimal key keeps the console's own, which num lock turns + // between delete and its character: only which character 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 ? kmap_unicode(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 this layout's dead keys make. the kernel takes + // one fewer than its table holds + memset(&acc, 0, sizeof(acc)); + for (j = 0; j < k->ncompose && acc.cnt < LEN(acc.d) - 1; j++) { + const struct kmap_compose *cp = &k->compose[j]; + long base = kmap_unicode(cp->base), res = kmap_unicode(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; +} +#endif diff --git a/shared/libutil/kmap_posix.c b/shared/libutil/kmap_posix.c @@ -0,0 +1,16 @@ +/* see LICENSE file for copyright and license details */ +#include "../kmap.h" +#include "../util.h" + +/* a system kmap has no console backend for: the layouts are still listed + * and written for xkb, only the console is not set */ +#if !(defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__) || \ + defined(__FreeBSD__)) || defined(ARUU_FORCE_POSIX) +int +kmap_load_console(const struct kmap *k) +{ + (void)k; + weprintf("this system's console takes no layout from kmap\n"); + return -1; +} +#endif diff --git a/shared/libutil/kmap_wscons.c b/shared/libutil/kmap_wscons.c @@ -0,0 +1,111 @@ +/* see LICENSE file for copyright and license details */ +#include "../kmap.h" +#include "../util.h" + +/* wscons, netbsd's and openbsd's console: the keyboard's whole map read + * (WSKBDIO_GETMAP), the layout's keys put in it, and written back. a + * keysym is 16 bit unicode and a dead key the combining character it puts + * on the next, as kmap_dead gives it; wscons composes them itself. group1 + * holds levels 1 and 2, group2 levels 3 and 4, reached with Mode_switch */ +#if (defined(__NetBSD__) || defined(__OpenBSD__)) && !defined(ARUU_FORCE_POSIX) +#include <sys/ioctl.h> +#include <sys/time.h> +#include <sys/types.h> + +#include <dev/wscons/wsconsio.h> +#include <dev/wscons/wsksymdef.h> +#include <dev/wscons/wsksymvar.h> + +#include <fcntl.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +static const char *kbds[] = {"/dev/wskbd", "/dev/wskbd0"}; + +// right alt, where a layout's level 3 is reached from: its pc scancode +// (0x38 behind e0, wscons numbers it 0xb8) and its hid usage +#define PC_RALT 0xb8 +#define HID_RALT 0xe6 + +static keysym_t +keysym_of(const char *s) +{ + long c; + + if ((c = kmap_dead(s)) >= 0) + return (keysym_t)c; + if (!strcmp(s, "ISO_Level3_Shift")) + return KS_Mode_switch; + if (!strcmp(s, "Multi_key")) + return KS_Multi_key; + if (!strcmp(s, "Return")) + return KS_Return; + if ((c = kmap_unicode(s)) < 0 || c >= 0xf000) + return KS_voidSymbol; + return (keysym_t)c; +} + +int +kmap_load_console(const struct kmap *k) +{ + struct wskbd_map_data md; + struct wscons_keymap *map; + enum kmap_codes scheme; + u_int type; + size_t i; + int fd = -1, code, l, rc = 0; + + for (i = 0; i < LEN(kbds) && fd < 0; i++) + fd = open(kbds[i], O_RDWR); + if (fd < 0) { + weprintf("open %s:", kbds[LEN(kbds) - 1]); + return -1; + } + // a usb keyboard's keys are numbered by hid usage, a pc keyboard's by + // its scancodes + scheme = ioctl(fd, WSKBDIO_GTYPE, &type) == 0 && type == WSKBD_TYPE_USB ? KMAP_HID : KMAP_PC; + map = ecalloc(KS_NUMKEYCODES, sizeof(*map)); + md.maplen = KS_NUMKEYCODES; + md.map = map; + if (ioctl(fd, WSKBDIO_GETMAP, &md) != 0) { + weprintf("WSKBDIO_GETMAP:"); + goto fail; + } + for (i = 0; i < k->nkeys; i++) { + const struct kmap_key *key = &k->keys[i]; + keysym_t s[4]; + + if ((code = kmap_code(key->name, scheme)) < 0 || (u_int)code >= md.maplen) + continue; + for (l = 0; l < 4; l++) + s[l] = l < key->nsym ? keysym_of(key->sym[l]) : KS_voidSymbol; + // a key with one level types it shifted too, as wscons keymaps write it + if (s[1] == KS_voidSymbol) + s[1] = s[0]; + map[code].group1[0] = s[0]; + map[code].group1[1] = s[1]; + map[code].group2[0] = s[2]; + map[code].group2[1] = s[3] == KS_voidSymbol ? s[2] : s[3]; + } + if (k->level3_ralt) { + code = scheme == KMAP_HID ? HID_RALT : PC_RALT; + if ((u_int)code < md.maplen) { + map[code].command = KS_voidSymbol; + map[code].group1[0] = map[code].group1[1] = KS_Mode_switch; + map[code].group2[0] = map[code].group2[1] = KS_Mode_switch; + } + } + if (ioctl(fd, WSKBDIO_SETMAP, &md) != 0) { + weprintf("WSKBDIO_SETMAP:"); + goto fail; + } + free(map); + close(fd); + return rc; +fail: + free(map); + close(fd); + return -1; +} +#endif