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