diff options
author | Přemysl Eric Janouch <p@janouch.name> | 2021-12-07 21:05:07 +0100 |
---|---|---|
committer | Přemysl Eric Janouch <p@janouch.name> | 2024-11-03 06:12:32 +0100 |
commit | 25bc6acdfd132a4d67fbba934dc05e5539072a30 (patch) | |
tree | 1c5e3183cd6b7d31de99879032fcc157b09e5cda /ht/gen-keysyms-new.sh | |
parent | e8453669ac7360631e07fbf128586ce9fbc8a06e (diff) | |
download | haven-25bc6acdfd132a4d67fbba934dc05e5539072a30.tar.gz haven-25bc6acdfd132a4d67fbba934dc05e5539072a30.tar.xz haven-25bc6acdfd132a4d67fbba934dc05e5539072a30.zip |
WIP: ht: commit whatever I was trying out
Diffstat (limited to 'ht/gen-keysyms-new.sh')
-rwxr-xr-x | ht/gen-keysyms-new.sh | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/ht/gen-keysyms-new.sh b/ht/gen-keysyms-new.sh new file mode 100755 index 0000000..388d946 --- /dev/null +++ b/ht/gen-keysyms-new.sh @@ -0,0 +1,94 @@ +#!/bin/sh +gofmt <<EOF | sed 's, *//$,,' +// Code generated by running "go generate" in janouch.name/haven. DO NOT EDIT. + +package $GOPACKAGE + +import ( + "strings" + "janouch.name/haven/nexgb/xproto" +) + +$(curl --silent --show-error \ + https://cgit.freedesktop.org/xorg/proto/x11proto/plain/keysymdef.h \ + https://cgit.freedesktop.org/xorg/proto/x11proto/plain/XF86keysym.h \ +| perl -lne ' + next unless /^\#define\s+ + (XF86)?(XK_)([a-zA-Z_0-9]+)\s+ + 0x([0-9a-fA-F]+)\s* + (?:\/\*\s*(.*?)\s*\*\/)?\s* + $/x; + + my ($name, $ident, $hex, $comment) = + (($1 // "") . $3, ($1 // "") . $2 . $3, lc $4, ($5 // "")); + + # They are already somewhat sorted in the source file. + push @a, { hex => $hex, ident => $ident, comment => $comment }; + + $nametokeysym{$name} = $ident; + + # All but the first name listed should be considered deprecated. + $keysymtoname{$ident} = $name unless exists $seen{$hex}; + $seen{$hex}++; + + # TODO: Try stringer, see + # https://eli.thegreenplace.net/2021/a-comprehensive-guide-to-go-generate/ + END { + print "const ("; + print "$_->{ident} = 0x$_->{hex} // $_->{comment}" for @a; + print ")"; + + # Very large tables, should be an on-demand package :( + # + # KeysymToName/NameToKeysym can also be compress/lzw (small code), + # that may reduce string data to about 1/3. It's still rather small + # (27K -> 8K), I'm not sure where all those bytes come from. + # + # KeysymToName might be generated from NameToKeysym. + # KeysymToName may just be an index into the ntk constant. + # + # Actually we may just include a $name => $hex table and screw + # all else. But the text still begs for compression and the numbers + # for filtering and compression. + # + # Or actually []{$hex, $name}, stable sorted by $hex, and having + # the $name be an index into a constant. On construction we keep + # a map so that only the first $hex occurrence receives a name. + # + # My biggest gripe is probably source code size that won't go down + # because the constants are just so large (158K). + + my (@ktnK, @ktnV); + for (sort keys %keysymtoname) { + push @ktnK, $_; + push @ktnV, $keysymtoname{$_}; + } + print "var ktnKeys = []xproto.Keysym{${\join \", \", @ktnK}}"; + print "var ktnValues = \"${\join \" \", @ktnV}\""; + + my (@ntkK, @ntkV); + for (sort keys %nametokeysym) { + push @ntkK, $_; + push @ntkV, $nametokeysym{$_}; + } + print "var ntkKeys = \"${\join \" \", @ntkK}\""; + print "var ntkValues = []xproto.Keysym{${\join \", \", @ntkV}}"; + } +') + +// KeysymToName maps X11 keysym constants to their names. +var KeysymToName = map[xproto.Keysym]string{} + +// NameToKeysym maps X11 keysym names to their constants. +var NameToKeysym = map[string]xproto.Keysym{} + +func init() { + x := strings.Split(ktnValues, " ") + for i, keysym := range ktnKeys { + KeysymToName[keysym] = x[i] + } + for i, name := range strings.Split(ntkKeys, " ") { + NameToKeysym[name] = ntkValues[i] + } +} +EOF |