aboutsummaryrefslogtreecommitdiff
path: root/ht/gen-keysyms.sh
blob: 064dec4726536e8ef7f28946913b29fd4dccdfef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/sh
gofmt <<EOF | sed 's, *//$,,'
// Code generated by running "go generate" in janouch.name/haven. DO NOT EDIT.

package $GOPACKAGE

import "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}++;

	END {
		print "const (";
		print "$_->{ident} = 0x$_->{hex} // $_->{comment}" for @a;
		print ")";

		# Very large tables, should be an on-demand package :(

		print "// KeysymToName maps X11 keysym constants to their names";
		print "var KeysymToName = map[xproto.Keysym]string{";
		print "$_: \"$keysymtoname{$_}\"," for sort keys %keysymtoname;
		print "}";

		print "// NameToKeysym maps X11 keysym names to their constants";
		print "var NameToKeysym = map[string]xproto.Keysym{";
		print "\"$_\": $nametokeysym{$_}," for sort keys %nametokeysym;
		print "}";
	}
')
EOF