]>
git.proxmox.com Git - mirror_novnc.git/blob - utils/parse.js
1 // Utility to parse keysymdef.h to produce mappings from Unicode codepoints to keysyms
4 var fs
= require('fs');
6 var show_help
= process
.argv
.length
=== 2;
9 for (var i
= 2; i
< process
.argv
.length
; ++i
) {
10 switch (process
.argv
[i
]) {
18 filename
= process
.argv
[i
];
24 console
.log("Error: No filename specified\n");
28 console
.log("Parses a *nix keysymdef.h to generate Unicode code point mappings");
29 console
.log("Usage: node parse.js [options] filename:");
30 console
.log(" -h [ --help ] Produce this help message");
31 console
.log(" filename The keysymdef.h file to parse");
35 var buf
= fs
.readFileSync(filename
);
36 var str
= buf
.toString('utf8');
38 var re
= /^\#define XK_([a-zA-Z_0-9]+)\s+0x([0-9a-fA-F]+)\s*(\/\*\s*(.*)\s*\*\/)?\s*$/m;
40 var arr
= str
.split('\n');
44 for (var i
= 0; i
< arr
.length
; ++i
) {
45 var result
= re
.exec(arr
[i
]);
47 var keyname
= result
[1];
48 var keysym
= parseInt(result
[2], 16);
49 var remainder
= result
[3];
51 var unicodeRes
= /U\+([0-9a-fA-F]+)/.exec(remainder
);
53 var unicode
= parseInt(unicodeRes
[1], 16);
54 if (!codepoints
[unicode
]){
55 codepoints
[unicode
] = keysym
;
59 console
.log("no unicode codepoint found:", arr
[i
]);
63 console
.log("line is not a keysym:", arr
[i
]);
67 var out
= "// This file describes mappings from Unicode codepoints to the keysym values\n" +
68 "// (and optionally, key names) expected by the RFB protocol\n" +
69 "// How this file was generated:\n" +
70 "// " + process
.argv
.join(" ") + "\n" +
72 "var codepoints = {codepoints};\n" +
74 "export default {\n" +
75 " lookup : function(u) {\n" +
76 " var keysym = codepoints[u];\n" +
77 " if (keysym === undefined) {\n" +
78 " keysym = 0x01000000 | u;\n" +
83 out
= out
.replace('{codepoints}', JSON
.stringify(codepoints
));
85 fs
.writeFileSync("keysymdef.js", out
);