]>
Commit | Line | Data |
---|---|---|
ae52883b | 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 use_keynames = false; | |
8 | var filename; | |
9 | ||
10 | for (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 | ||
27 | if (!filename) { | |
28 | show_help = true; | |
29 | console.log("Error: No filename specified\n"); | |
30 | } | |
31 | ||
32 | if (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 | ||
44 | var buf = fs.readFileSync(filename); | |
45 | var str = buf.toString('utf8'); | |
46 | ||
47 | var re = /^\#define XK_([a-zA-Z_0-9]+)\s+0x([0-9a-fA-F]+)\s*(\/\*\s*(.*)\s*\*\/)?\s*$/m; | |
48 | ||
49 | var arr = str.split('\n'); | |
50 | ||
51 | var keysyms = {}; | |
52 | var codepoints = {}; | |
53 | ||
54 | for (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 | ||
79 | var 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" + | |
90 | " fromUnicode : function(u) { return lookup(codepoints[u]); },\n" + | |
91 | " lookup : lookup\n" + | |
92 | " };\n" + | |
93 | "})();\n"; | |
94 | out = out.replace('{keysyms}', use_keynames ? JSON.stringify(keysyms) : "null"); | |
95 | out = out.replace('{codepoints}', JSON.stringify(codepoints)); | |
96 | ||
97 | fs.writeFileSync("keysymdef.js", out); |