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