]>
git.proxmox.com Git - mirror_novnc.git/blob - utils/genkeysymdef.js
3 * genkeysymdef: X11 keysymdef.h to JavaScript converter
4 * Copyright (C) 2018 The noVNC Authors
5 * Licensed under MPL 2.0 (see LICENSE.txt)
10 const fs
= require('fs');
12 let showHelp
= process
.argv
.length
=== 2;
15 for (let i
= 2; i
< process
.argv
.length
; ++i
) {
16 switch (process
.argv
[i
]) {
24 filename
= process
.argv
[i
];
30 console
.log("Error: No filename specified\n");
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");
41 const buf
= fs
.readFileSync(filename
);
42 const str
= buf
.toString('utf8');
44 const re
= /^#define XK_([a-zA-Z_0-9]+)\s+0x([0-9a-fA-F]+)\s*(\/\*\s*(.*)\s*\*\/)?\s*$/m;
46 const arr
= str
.split('\n');
48 const codepoints
= {};
50 for (let i
= 0; i
< arr
.length
; ++i
) {
51 const result
= re
.exec(arr
[i
]);
53 const keyname
= result
[1];
54 const keysym
= parseInt(result
[2], 16);
55 const remainder
= result
[3];
57 const unicodeRes
= /U\+([0-9a-fA-F]+)/.exec(remainder
);
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
};
70 " * Mapping from Unicode codepoints to X11/RFB keysyms\n" +
72 " * This file was automatically generated from keysymdef.h\n" +
76 "/* Functions at the bottom */\n" +
78 "const codepoints = {\n";
81 let s
= num
.toString(16);
83 s
= ("0000" + s
).slice(-4);
88 for (let codepoint
in codepoints
) {
89 codepoint
= parseInt(codepoint
);
92 if ((codepoint
>= 0x20) && (codepoint
<= 0xff)) {
96 // Handled by the general Unicode mapping?
97 if ((codepoint
| 0x01000000) === codepoints
[codepoint
].keysym
) {
101 out
+= " " + toHex(codepoint
) + ": " +
102 toHex(codepoints
[codepoint
].keysym
) +
103 ", // XK_" + codepoints
[codepoint
].name
+ "\n";
109 "export default {\n" +
111 " // Latin-1 is one-to-one mapping\n" +
112 " if ((u >= 0x20) && (u <= 0xff)) {\n" +
116 " // Lookup table (fairly random)\n" +
117 " const keysym = codepoints[u];\n" +
118 " if (keysym !== undefined) {\n" +
119 " return keysym;\n" +
122 " // General mapping as final fallback\n" +
123 " return 0x01000000 | u;\n" +