]> git.proxmox.com Git - mirror_novnc.git/blob - utils/genkeysymdef.js
Add eslint and fix reported issues
[mirror_novnc.git] / utils / genkeysymdef.js
1 #!/usr/bin/env node
2 /*
3 * genkeysymdef: X11 keysymdef.h to JavaScript converter
4 * Copyright 2013 jalf <git@jalf.dk>
5 * Copyright 2017 Pierre Ossman for Cendio AB
6 * Licensed under MPL 2.0 (see LICENSE.txt)
7 */
8
9 "use strict";
10
11 var fs = require('fs');
12
13 var show_help = process.argv.length === 2;
14 var filename;
15
16 var i;
17 for (i = 2; i < process.argv.length; ++i) {
18 switch (process.argv[i]) {
19 case "--help":
20 case "-h":
21 show_help = true;
22 break;
23 case "--file":
24 case "-f":
25 default:
26 filename = process.argv[i];
27 }
28 }
29
30 if (!filename) {
31 show_help = true;
32 console.log("Error: No filename specified\n");
33 }
34
35 if (show_help) {
36 console.log("Parses a *nix keysymdef.h to generate Unicode code point mappings");
37 console.log("Usage: node parse.js [options] filename:");
38 console.log(" -h [ --help ] Produce this help message");
39 console.log(" filename The keysymdef.h file to parse");
40 process.exit(0);
41 }
42
43 var buf = fs.readFileSync(filename);
44 var str = buf.toString('utf8');
45
46 var re = /^#define XK_([a-zA-Z_0-9]+)\s+0x([0-9a-fA-F]+)\s*(\/\*\s*(.*)\s*\*\/)?\s*$/m;
47
48 var arr = str.split('\n');
49
50 var codepoints = {};
51
52 for (i = 0; i < arr.length; ++i) {
53 var result = re.exec(arr[i]);
54 if (result){
55 var keyname = result[1];
56 var keysym = parseInt(result[2], 16);
57 var remainder = result[3];
58
59 var unicodeRes = /U\+([0-9a-fA-F]+)/.exec(remainder);
60 if (unicodeRes) {
61 var unicode = parseInt(unicodeRes[1], 16);
62 // The first entry is the preferred one
63 if (!codepoints[unicode]){
64 codepoints[unicode] = { keysym: keysym, name: keyname };
65 }
66 }
67 }
68 }
69
70 var out =
71 "/*\n" +
72 " * Mapping from Unicode codepoints to X11/RFB keysyms\n" +
73 " *\n" +
74 " * This file was automatically generated from keysymdef.h\n" +
75 " * DO NOT EDIT!\n" +
76 " */\n" +
77 "\n" +
78 "/* Functions at the bottom */\n" +
79 "\n" +
80 "var codepoints = {\n";
81
82 function toHex(num) {
83 var s = num.toString(16);
84 if (s.length < 4) {
85 s = ("0000" + s).slice(-4);
86 }
87 return "0x" + s;
88 }
89
90 for (var codepoint in codepoints) {
91 codepoint = parseInt(codepoint);
92
93 // Latin-1?
94 if ((codepoint >= 0x20) && (codepoint <= 0xff)) {
95 continue;
96 }
97
98 // Handled by the general Unicode mapping?
99 if ((codepoint | 0x01000000) === codepoints[codepoint].keysym) {
100 continue;
101 }
102
103 out += " " + toHex(codepoint) + ": " +
104 toHex(codepoints[codepoint].keysym) +
105 ", // XK_" + codepoints[codepoint].name + "\n";
106 }
107
108 out +=
109 "};\n" +
110 "\n" +
111 "export default {\n" +
112 " lookup : function(u) {\n" +
113 " // Latin-1 is one-to-one mapping\n" +
114 " if ((u >= 0x20) && (u <= 0xff)) {\n" +
115 " return u;\n" +
116 " }\n" +
117 "\n" +
118 " // Lookup table (fairly random)\n" +
119 " var keysym = codepoints[u];\n" +
120 " if (keysym !== undefined) {\n" +
121 " return keysym;\n" +
122 " }\n" +
123 "\n" +
124 " // General mapping as final fallback\n" +
125 " return 0x01000000 | u;\n" +
126 " },\n" +
127 "};";
128
129 console.log(out);