]> git.proxmox.com Git - mirror_novnc.git/blame - utils/parse.js
Merge pull request #699 from CendioOssman/double
[mirror_novnc.git] / utils / parse.js
CommitLineData
ae52883b 1// Utility to parse keysymdef.h to produce mappings from Unicode codepoints to keysyms
2"use strict";
3
4var fs = require('fs');
5
6var show_help = process.argv.length === 2;
7var use_keynames = false;
8var filename;
9
10for (var i = 2; i < process.argv.length; ++i) {
11 switch (process.argv[i]) {
12 case "--help":
13 case "-h":
14 show_help = true;
15 break;
16 case "--debug-names":
17 case "-d":
18 use_keynames = true;
19 break;
20 case "--file":
21 case "-f":
22 default:
23 filename = process.argv[i];
24 }
25}
26
27if (!filename) {
28 show_help = true;
29 console.log("Error: No filename specified\n");
30}
31
32if (show_help) {
33 console.log("Parses a *nix keysymdef.h to generate Unicode code point mappings");
34 console.log("Usage: node parse.js [options] filename:");
35 console.log(" -h [ --help ] Produce this help message");
36 console.log(" -d [ --debug-names ] Preserve keysym names for debugging (Increases file size by ~40KB)");
37 console.log(" filename The keysymdef.h file to parse");
38 return;
39}
40
41// Set this to false to omit key names from the generated keysymdef.js
42// This reduces the file size by around 40kb, but may hinder debugging
43
44var buf = fs.readFileSync(filename);
45var str = buf.toString('utf8');
46
47var re = /^\#define XK_([a-zA-Z_0-9]+)\s+0x([0-9a-fA-F]+)\s*(\/\*\s*(.*)\s*\*\/)?\s*$/m;
48
49var arr = str.split('\n');
50
51var keysyms = {};
52var codepoints = {};
53
54for (var i = 0; i < arr.length; ++i) {
55 var result = re.exec(arr[i]);
56 if (result){
57 var keyname = result[1];
58 var keysym = parseInt(result[2], 16);
59 var remainder = result[3];
60
61 keysyms[keysym] = keyname;
62
63 var unicodeRes = /U\+([0-9a-fA-F]+)/.exec(remainder);
64 if (unicodeRes) {
65 var unicode = parseInt(unicodeRes[1], 16);
66 if (!codepoints[unicode]){
67 codepoints[unicode] = keysym;
68 }
69 }
70 else {
71 console.log("no unicode codepoint found:", arr[i]);
72 }
73 }
74 else {
75 console.log("line is not a keysym:", arr[i]);
76 }
77}
78
79var out = "// This file describes mappings from Unicode codepoints to the keysym values\n" +
80"// (and optionally, key names) expected by the RFB protocol\n" +
81"// How this file was generated:\n" +
82"// " + process.argv.join(" ") + "\n" +
83"var keysyms = (function(){\n" +
84" \"use strict\";\n" +
85" var keynames = {keysyms};\n" +
86" var codepoints = {codepoints};\n" +
87"\n" +
88" function lookup(k) { return k ? {keysym: k, keyname: keynames ? keynames[k] : k} : undefined; }\n" +
89" return {\n" +
115eedf6
PO
90" fromUnicode : function(u) {\n" +
91" var keysym = codepoints[u];\n" +
92" if (keysym === undefined) {\n" +
93" keysym = 0x01000000 | u;\n" +
94" }\n" +
95" return lookup(keysym);\n" +
96" },\n" +
ae52883b 97" lookup : lookup\n" +
98" };\n" +
99"})();\n";
100out = out.replace('{keysyms}', use_keynames ? JSON.stringify(keysyms) : "null");
101out = out.replace('{codepoints}', JSON.stringify(codepoints));
102
103fs.writeFileSync("keysymdef.js", out);