haven/ht/gen-unicode-map.sh
Přemysl Janouch cd6c9e4d8c
ht: add generated X11 keysym maps
Now we can make sense of keysyms and translate them to text.
2018-10-07 18:09:09 +02:00

50 lines
1.3 KiB
Bash
Executable File

#!/bin/sh
gofmt <<EOF
// Code generated by running "go generate" in janouch.name/haven. DO NOT EDIT.
package $GOPACKAGE
import "janouch.name/haven/nexgb/xproto"
// KeysymToRune tries to translate an X11 keysym to an appropriate rune.
// Returns -1 when no match is found.
func KeysymToRune(ks xproto.Keysym) rune {
// Visible Latin-1 is mapped 1:1
if (ks >= 0x20 && ks <= 0x7e) || (ks >= 0xa0 && ks <= 0xff) {
return rune(ks)
}
// Directly encoded 24-bit Unicode (going even above plane 16)
if (ks & 0xff000000) == 0x01000000 {
return rune(ks & 0x00ffffff)
}
min, max := 0, len(keysymToRuneTable)-1
for max >= min {
mid := (min + max) / 2
if keysymToRuneTable[mid].keysym < ks {
min = mid + 1
} else if keysymToRuneTable[mid].keysym > ks {
max = mid - 1
} else {
return keysymToRuneTable[mid].unicode
}
}
return -1
}
var keysymToRuneTable = []struct{
keysym xproto.Keysym
unicode rune
}{
$(curl --silent --show-error --location \
https://invisible-island.net/datafiles/release/xterm.tar.gz | \
tar --wildcards -xzOf - 'xterm-*/unicode/keysym.map' | \
sort | perl -lne '
next unless /^(0x([0-9a-f]{4}))\s+U([0-9a-f]{4})\s*(?:\#\s*(.*))?$/;
my ($keysym, $ks, $unicode, $comment) = ($1, hex($2), $3, ($4 // ""));
print "{$keysym, 0x$unicode}, // $comment"
if $ks >= 0x100 && $unicode ne "0000";
')
}
EOF