Debian Codegraph @

util/tool/gen-keysymdef/main.go

raw
1 // SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 package main
6
7 import (
8 "bufio"
9 "fmt"
10 "log"
11 "os"
12 "regexp"
13 "strconv"
14 "strings"
15 )
16
17 const (
18 goPkgName = "keysyms"
19 keysymdefFile = "/usr/include/X11/keysymdef.h"
20 xf86keysymFile = "/usr/include/X11/XF86keysym.h"
21 )
22
23 type Keysym struct {
24 Name string
25 Value string
26 Unicode string
27 Comment string
28 }
29
30 func (ks *Keysym) getVisibleChar() string {
31 if ks.Unicode == "" {
32 return ""
33 }
34
35 if len(ks.Unicode) <= 4 {
36 zeroCount := 4 - len(ks.Unicode)
37 return "\\u" + strings.Repeat("0", zeroCount) + strings.ToLower(ks.Unicode)
38
39 } else if len(ks.Unicode) <= 8 {
40 zeroCount := 8 - len(ks.Unicode)
41 return "\\U" + strings.Repeat("0", zeroCount) + strings.ToLower(ks.Unicode)
42
43 } else {
44 panic(fmt.Errorf("keysym %s invalid unicode %q", ks.Name, ks.Unicode))
45 }
46 }
47
48 var reIfDef = regexp.MustCompile(`^#ifdef\s+(\w+)`)
49 var reDef0 = regexp.MustCompile(`^#define (XK_[a-zA-Z_0-9]+)\s+0x([0-9a-f]+)\s*/\* U\+([0-9A-F]{4,6}) (.*)\s*\*/`)
50 var reDef1 = regexp.MustCompile(`^#define (XK_[a-zA-Z_0-9]+)\s+0x([0-9a-f]+)\s*/\*\(U\+([0-9A-F]{4,6}) (.*)\)\*/`)
51 var reDef2 = regexp.MustCompile(`^#define (XK_[a-zA-Z_0-9]+)\s+0x([0-9a-f]+)\s*(/\*\s*(.*)\s*\*/)?`)
52
53 // for xf86keys
54 var reDef3 = regexp.MustCompile(`^#define (XF86XK_[a-zA-Z_0-9]+)\s+0x([0-9a-fA-F]+)\s*(/\*\s*(.*)\s*\*/)?`)
55
56 var sections = []string{
57 "XK_MISCELLANY",
58 "XK_XKB_KEYS",
59 "XK_LATIN1",
60 "XK_LATIN2",
61 "XK_LATIN3",
62 "XK_LATIN4",
63 "XK_LATIN8",
64 "XK_LATIN9",
65 "XK_CAUCASUS",
66 "XK_GREEK",
67 "XK_KATAKANA",
68 "XK_ARABIC",
69 "XK_CYRILLIC",
70 "XK_HEBREW",
71 "XK_THAI",
72 "XK_KOREAN",
73 "XK_ARMENIAN",
74 "XK_GEORGIAN",
75 "XK_VIETNAMESE",
76 "XK_CURRENCY",
77 "XK_MATHEMATICAL",
78 "XK_BRAILLE",
79 "XK_SINHALA",
80 }
81
82 func definedSection(section string) bool {
83 for _, value := range sections {
84 if value == section {
85 return true
86 }
87 }
88 return false
89 }
90
91 func processKeysymdef() []Keysym {
92 fh, err := os.Open(keysymdefFile)
93 if err != nil {
94 log.Fatal(err)
95 }
96 defer fh.Close()
97
98 var currentSection string
99 var currentSectionOk bool = true
100 scanner := bufio.NewScanner(fh)
101
102 // stat vars
103 var (
104 def0Count int
105 def1Count int
106 def2Count int
107 defOtherCount int
108 )
109
110 var symbols []Keysym
111 for scanner.Scan() {
112 line := scanner.Text()
113 result := reIfDef.FindStringSubmatch(line)
114 if len(result) > 0 {
115 currentSection = result[1]
116 currentSectionOk = definedSection(currentSection)
117 continue
118 }
119
120 if strings.HasPrefix(line, "#endif") {
121 if currentSection != "" {
122 currentSection = ""
123 }
124 currentSectionOk = false
125 continue
126 }
127
128 // ignore some section
129 if !currentSectionOk {
130 log.Println("ignore ", line)
131 continue
132 }
133
134 result = reDef0.FindStringSubmatch(line)
135 if len(result) > 0 {
136 def0Count++
137 log.Printf("def0 result: %#v\n", result[1:])
138 symbols = append(symbols, Keysym{
139 Name: result[1],
140 Value: result[2],
141 Unicode: result[3],
142 Comment: getComment(result[3], result[4]),
143 })
144 continue
145 }
146
147 result = reDef1.FindStringSubmatch(line)
148 if len(result) > 0 {
149 def1Count++
150 log.Printf("def1 result: %#v\n", result[1:])
151 symbols = append(symbols, Keysym{
152 Name: result[1],
153 Value: result[2],
154 Unicode: result[3],
155 Comment: getComment(result[3], result[4]),
156 })
157 continue
158 }
159
160 result = reDef2.FindStringSubmatch(line)
161 if len(result) > 0 {
162 def2Count++
163 log.Printf("def2 result: %#v\n", result[1:])
164 // no unicode
165 symbols = append(symbols, Keysym{
166 Name: result[1],
167 Value: result[2],
168 Comment: result[4],
169 })
170 continue
171 }
172
173 if strings.HasPrefix(line, "#define") {
174 defOtherCount++
175 log.Println("warn line: ", line)
176 continue
177 }
178 }
179 if err := scanner.Err(); err != nil {
180 log.Fatal(err)
181 }
182
183 log.Printf("stat:\ndef0: %d\ndef1: %d\ndef2: %d\ndef other: %d\n",
184 def0Count, def1Count, def2Count, defOtherCount)
185
186 return symbols
187 }
188
189 func processXF86Keysymdef() []Keysym {
190 fh, err := os.Open(xf86keysymFile)
191 if err != nil {
192 log.Fatal(err)
193 }
194 defer fh.Close()
195 scanner := bufio.NewScanner(fh)
196 var defOtherCount int
197 var def3Count int
198 var symbols []Keysym
199 for scanner.Scan() {
200 line := scanner.Text()
201
202 result := reDef3.FindStringSubmatch(line)
203 if len(result) > 0 {
204 def3Count++
205 log.Printf("def3 result: %#v\n", result[1:])
206 // no unicode
207 symbols = append(symbols, Keysym{
208 Name: result[1],
209 Value: result[2],
210 Comment: result[4],
211 })
212 continue
213 }
214
215 if strings.HasPrefix(line, "#define") {
216 defOtherCount++
217 log.Println("warn line: ", line)
218 continue
219 }
220 }
221 log.Printf("stat:\ndef3: %d\ndef other: %d\n",
222 def3Count, defOtherCount)
223
224 if err := scanner.Err(); err != nil {
225 log.Fatal(err)
226 }
227 return symbols
228 }
229
230 func getComment(unicode_, comment string) string {
231 if unicode_ != "" {
232 return fmt.Sprintf("U+%s %s", unicode_, comment)
233 }
234 return comment
235 }
236
237 func main() {
238 log.SetFlags(log.Lshortfile)
239 fmt.Println("package", goPkgName)
240 fmt.Printf("import x %q\n", "github.com/linuxdeepin/go-x11-client")
241
242 symbols := processKeysymdef()
243 xf86Symbols := processXF86Keysymdef()
244 symbols = append(symbols, xf86Symbols...)
245
246 // define constants
247 fmt.Println("const (")
248 for _, sym := range symbols {
249 if sym.Comment != "" {
250 fmt.Println("// ", sym.Comment)
251 }
252 fmt.Printf("%s = 0x%s\n", sym.Name, sym.Value)
253 }
254 fmt.Println(")")
255
256 // map keysym => english str
257 var tempMap = make(map[string]string, len(symbols))
258 fmt.Println("var KeysymEngMap = map[x.Keysym]string{")
259 for _, sym := range symbols {
260 dupSym, ok := tempMap[sym.Value]
261 if ok {
262 fmt.Printf("// %s == %s # %s\n", sym.Name, dupSym, sym.Comment)
263 } else {
264 tempMap[sym.Value] = sym.Name
265 fmt.Printf("%s:%q,\n", sym.Name, getEnglish(sym.Name))
266 }
267 }
268 fmt.Println("}")
269
270 // map english str => keysym
271 fmt.Println("var EngKeysymMap = map[string]x.Keysym{")
272 for _, sym := range symbols {
273 fmt.Printf("%q:%s,\n", getEnglish(sym.Name), sym.Name)
274 }
275 fmt.Println("}")
276
277 // map keysym => visible character
278 tempMap = make(map[string]string, len(symbols))
279 fmt.Println("var KeysymVisibleCharMap = map[x.Keysym]rune{")
280 for _, sym := range symbols {
281 visibleChar := sym.getVisibleChar()
282 if visibleChar == "" {
283 continue
284 }
285 dupSym, ok := tempMap[sym.Value]
286 if ok {
287 fmt.Printf("// %s == %s # %s\n", sym.Name, dupSym, sym.Comment)
288 } else {
289 tempMap[sym.Value] = sym.Name
290 str, err := strconv.Unquote("'" + visibleChar + "'")
291 if err != nil {
292 panic(fmt.Errorf("failed to unquote %q", visibleChar))
293 }
294
295 fmt.Printf("%s:'%s', // %s\n", sym.Name, visibleChar, str)
296 }
297 }
298 fmt.Println("}")
299 }
300
301 func getEnglish(symName string) string {
302 if strings.HasPrefix(symName, "XK_") {
303 return symName[len("XK_"):]
304 } else if strings.HasPrefix(symName, "XF86XK_") {
305 return "XF86" + symName[len("XF86XK_"):]
306 } else {
307 panic("invalid symbol name:" + symName)
308 }
309 }