@@ -12,8 +12,10 @@ import (
1212 "bytes"
1313 "fmt"
1414 "go/format"
15+ "io"
1516 "io/ioutil"
1617 "log"
18+ "net/http"
1719 "os"
1820 "regexp"
1921 "strings"
@@ -29,6 +31,8 @@ func main() {
2931
3032 seen := make (map [string ]struct {})
3133
34+ xkCodes := make (map [string ]string )
35+
3236 buf := & bytes.Buffer {}
3337
3438 fmt .Fprintf (buf , `// generated by go generate; DO NOT EDIT.
@@ -40,15 +44,21 @@ var keysymCodePoints = map[rune]rune{
4044` )
4145
4246 re := regexp .MustCompile (`^#define (XK_[^ ]*) *0x([[:xdigit:]]+) .*U\+([[:xdigit:]]+) (.+)(?: |\))\*/$` )
47+ reAll := regexp .MustCompile (`^#define (XK_[^ ]*) *0x([[:xdigit:]]+)` )
4348
4449 s := bufio .NewScanner (fh )
4550 for s .Scan () {
51+ mAll := reAll .FindStringSubmatch (strings .TrimSpace (s .Text ()))
52+ if mAll != nil {
53+ xkCodes [mAll [1 ]] = mAll [2 ]
54+ }
4655 m := re .FindStringSubmatch (strings .TrimSpace (s .Text ()))
4756 if m == nil {
4857 continue
4958 }
5059
5160 if _ , isSeen := seen [m [2 ]]; isSeen {
61+ log .Printf ("Duplicated mapped XK %s" , m [2 ])
5262 continue
5363 }
5464 seen [m [2 ]] = struct {}{}
@@ -60,6 +70,45 @@ var keysymCodePoints = map[rune]rune{
6070 log .Fatalf ("reading keysymdef.h: %v" , err )
6171 }
6272
73+ tmpKeysymFile := os .TempDir () + "/keysym_to_unicode.cc"
74+ keysymUrl := "https://raw.githubusercontent.com/microsoft/node-native-keymap/master/deps/chromium/x/keysym_to_unicode.cc"
75+ err = DownloadFile (tmpKeysymFile , keysymUrl )
76+ if err != nil {
77+ log .Fatalf ("downloading keysym_to_unicode.cc: %v" , err )
78+ }
79+
80+ fh2 , err := os .Open (tmpKeysymFile )
81+ if err != nil {
82+ log .Fatalf ("opening keysym_to_unicode.cc: %v" , err )
83+ }
84+
85+ defer fh2 .Close ()
86+
87+ re2 := regexp .MustCompile (`^\{(XK_[^,]*), *0x([[:xdigit:]]+)\}, *\/\/ *(.+)$` )
88+
89+ s2 := bufio .NewScanner (fh2 )
90+ for s2 .Scan () {
91+ m := re2 .FindStringSubmatch (strings .TrimSpace (s2 .Text ()))
92+ if m == nil {
93+ continue
94+ }
95+
96+ var xkCode string
97+ var isXKCode bool
98+ if xkCode , isXKCode = xkCodes [m [1 ]]; ! isXKCode {
99+ log .Printf ("Unknown XK name %s" , m [1 ])
100+ continue
101+ }
102+
103+ if _ , isSeen := seen [xkCode ]; isSeen {
104+ log .Printf ("Already mapped XK %s" , xkCode )
105+ continue
106+ }
107+
108+ fmt .Fprintf (buf , "0x%s: 0x%s, // %s:\t %s\n " , xkCode , m [2 ], m [1 ], m [3 ])
109+
110+ }
111+
63112 fmt .Fprintf (buf , "}\n " )
64113
65114 fmted , err := format .Source (buf .Bytes ())
@@ -72,3 +121,26 @@ var keysymCodePoints = map[rune]rune{
72121 log .Fatalf ("writing table.go: %v" , err )
73122 }
74123}
124+
125+ // DownloadFile will download a url to a local file. It's efficient because it will
126+ // write as it downloads and not load the whole file into memory.
127+ func DownloadFile (filepath string , url string ) error {
128+
129+ // Get the data
130+ resp , err := http .Get (url )
131+ if err != nil {
132+ return err
133+ }
134+ defer resp .Body .Close ()
135+
136+ // Create the file
137+ out , err := os .Create (filepath )
138+ if err != nil {
139+ return err
140+ }
141+ defer out .Close ()
142+
143+ // Write the body to file
144+ _ , err = io .Copy (out , resp .Body )
145+ return err
146+ }
0 commit comments