]> git.proxmox.com Git - mirror_novnc.git/blob - utils/genkeysymdef.js
Use fat arrow functions `const foo = () => { ... };` for callbacks
[mirror_novnc.git] / utils / genkeysymdef.js
1 #!/usr/bin/env node
2 /*
3 * genkeysymdef: X11 keysymdef.h to JavaScript converter
4 * Copyright 2013 jalf <git@jalf.dk>
5 * Copyright 2017 Pierre Ossman for Cendio AB
6 * Licensed under MPL 2.0 (see LICENSE.txt)
7 */
8
9 "use strict";
10
11 const fs = require('fs');
12
13 let show_help = process.argv.length === 2;
14 let filename;
15
16 for (let i = 2; i < process.argv.length; ++i) {
17 switch (process.argv[i]) {
18 case "--help":
19 case "-h":
20 show_help = true;
21 break;
22 case "--file":
23 case "-f":
24 default:
25 filename = process.argv[i];
26 }
27 }
28
29 if (!filename) {
30 show_help = true;
31 console.log("Error: No filename specified\n");
32 }
33
34 if (show_help) {
35 console.log("Parses a *nix keysymdef.h to generate Unicode code point mappings");
36 console.log("Usage: node parse.js [options] filename:");
37 console.log(" -h [ --help ] Produce this help message");
38 console.log(" filename The keysymdef.h file to parse");
39 process.exit(0);
40 }
41
42 const buf = fs.readFileSync(filename);
43 const str = buf.toString('utf8');
44
45 const re = /^#define XK_([a-zA-Z_0-9]+)\s+0x([0-9a-fA-F]+)\s*(\/\*\s*(.*)\s*\*\/)?\s*$/m;
46
47 const arr = str.split('\n');
48
49 const codepoints = {};
50
51 for (let i = 0; i < arr.length; ++i) {
52 const result = re.exec(arr[i]);
53 if (result){
54 const keyname = result[1];
55 const keysym = parseInt(result[2], 16);
56 const remainder = result[3];
57
58 const unicodeRes = /U\+([0-9a-fA-F]+)/.exec(remainder);
59 if (unicodeRes) {
60 const unicode = parseInt(unicodeRes[1], 16);
61 // The first entry is the preferred one
62 if (!codepoints[unicode]){
63 codepoints[unicode] = { keysym: keysym, name: keyname };
64 }
65 }
66 }
67 }
68
69 let out =
70 "/*\n" +
71 " * Mapping from Unicode codepoints to X11/RFB keysyms\n" +
72 " *\n" +
73 " * This file was automatically generated from keysymdef.h\n" +
74 " * DO NOT EDIT!\n" +
75 " */\n" +
76 "\n" +
77 "/* Functions at the bottom */\n" +
78 "\n" +
79 "const codepoints = {\n";
80
81 function toHex(num) {
82 let s = num.toString(16);
83 if (s.length < 4) {
84 s = ("0000" + s).slice(-4);
85 }
86 return "0x" + s;
87 }
88
89 for (let codepoint in codepoints) {
90 codepoint = parseInt(codepoint);
91
92 // Latin-1?
93 if ((codepoint >= 0x20) && (codepoint <= 0xff)) {
94 continue;
95 }
96
97 // Handled by the general Unicode mapping?
98 if ((codepoint | 0x01000000) === codepoints[codepoint].keysym) {
99 continue;
100 }
101
102 out += " " + toHex(codepoint) + ": " +
103 toHex(codepoints[codepoint].keysym) +
104 ", // XK_" + codepoints[codepoint].name + "\n";
105 }
106
107 out +=
108 "};\n" +
109 "\n" +
110 "export default {\n" +
111 " lookup(u) {\n" +
112 " // Latin-1 is one-to-one mapping\n" +
113 " if ((u >= 0x20) && (u <= 0xff)) {\n" +
114 " return u;\n" +
115 " }\n" +
116 "\n" +
117 " // Lookup table (fairly random)\n" +
118 " const keysym = codepoints[u];\n" +
119 " if (keysym !== undefined) {\n" +
120 " return keysym;\n" +
121 " }\n" +
122 "\n" +
123 " // General mapping as final fallback\n" +
124 " return 0x01000000 | u;\n" +
125 " },\n" +
126 "};";
127
128 console.log(out);