aboutsummaryrefslogtreecommitdiff
path: root/ht/gen-unicode-map.sh
diff options
context:
space:
mode:
authorPřemysl Janouch <p@janouch.name>2018-09-12 13:38:33 +0200
committerPřemysl Janouch <p@janouch.name>2018-10-07 18:09:09 +0200
commitcd6c9e4d8c6a49eb91091c00b55e0f879c9220bf (patch)
tree17dbca0adbac6ce78ae65be17be70c59f9b60755 /ht/gen-unicode-map.sh
parentbf14fd5e04ab96e755cec4fd46349339e1eb3e2b (diff)
downloadhaven-cd6c9e4d8c6a49eb91091c00b55e0f879c9220bf.tar.gz
haven-cd6c9e4d8c6a49eb91091c00b55e0f879c9220bf.tar.xz
haven-cd6c9e4d8c6a49eb91091c00b55e0f879c9220bf.zip
ht: add generated X11 keysym maps
Now we can make sense of keysyms and translate them to text.
Diffstat (limited to 'ht/gen-unicode-map.sh')
-rwxr-xr-xht/gen-unicode-map.sh49
1 files changed, 49 insertions, 0 deletions
diff --git a/ht/gen-unicode-map.sh b/ht/gen-unicode-map.sh
new file mode 100755
index 0000000..7e94880
--- /dev/null
+++ b/ht/gen-unicode-map.sh
@@ -0,0 +1,49 @@
+#!/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