]> git.proxmox.com Git - mirror_novnc.git/blob - utils/parse.js
Remove keysym names from keysymdef.js
[mirror_novnc.git] / utils / parse.js
1 // Utility to parse keysymdef.h to produce mappings from Unicode codepoints to keysyms
2 "use strict";
3
4 var fs = require('fs');
5
6 var show_help = process.argv.length === 2;
7 var filename;
8
9 for (var i = 2; i < process.argv.length; ++i) {
10 switch (process.argv[i]) {
11 case "--help":
12 case "-h":
13 show_help = true;
14 break;
15 case "--file":
16 case "-f":
17 default:
18 filename = process.argv[i];
19 }
20 }
21
22 if (!filename) {
23 show_help = true;
24 console.log("Error: No filename specified\n");
25 }
26
27 if (show_help) {
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");
32 return;
33 }
34
35 var buf = fs.readFileSync(filename);
36 var str = buf.toString('utf8');
37
38 var re = /^\#define XK_([a-zA-Z_0-9]+)\s+0x([0-9a-fA-F]+)\s*(\/\*\s*(.*)\s*\*\/)?\s*$/m;
39
40 var arr = str.split('\n');
41
42 var codepoints = {};
43
44 for (var i = 0; i < arr.length; ++i) {
45 var result = re.exec(arr[i]);
46 if (result){
47 var keyname = result[1];
48 var keysym = parseInt(result[2], 16);
49 var remainder = result[3];
50
51 var unicodeRes = /U\+([0-9a-fA-F]+)/.exec(remainder);
52 if (unicodeRes) {
53 var unicode = parseInt(unicodeRes[1], 16);
54 if (!codepoints[unicode]){
55 codepoints[unicode] = keysym;
56 }
57 }
58 else {
59 console.log("no unicode codepoint found:", arr[i]);
60 }
61 }
62 else {
63 console.log("line is not a keysym:", arr[i]);
64 }
65 }
66
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" +
71 "\n" +
72 "var codepoints = {codepoints};\n" +
73 "\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" +
79 " }\n" +
80 " return keysym;\n" +
81 " },\n" +
82 "};\n";
83 out = out.replace('{codepoints}', JSON.stringify(codepoints));
84
85 fs.writeFileSync("keysymdef.js", out);