]> git.proxmox.com Git - mirror_novnc.git/blame - utils/genkeysymdef.js
Use fat arrow functions `const foo = () => { ... };` for callbacks
[mirror_novnc.git] / utils / genkeysymdef.js
CommitLineData
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
2b5f94fa 11const fs = require('fs');
ae52883b 12
2b5f94fa
JD
13let show_help = process.argv.length === 2;
14let filename;
ae52883b 15
2b5f94fa 16for (let i = 2; i < process.argv.length; ++i) {
ae52883b 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
29if (!filename) {
30 show_help = true;
31 console.log("Error: No filename specified\n");
32}
33
34if (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");
8727f598 39 process.exit(0);
ae52883b 40}
41
2b5f94fa
JD
42const buf = fs.readFileSync(filename);
43const str = buf.toString('utf8');
ae52883b 44
2b5f94fa 45const re = /^#define XK_([a-zA-Z_0-9]+)\s+0x([0-9a-fA-F]+)\s*(\/\*\s*(.*)\s*\*\/)?\s*$/m;
ae52883b 46
2b5f94fa 47const arr = str.split('\n');
ae52883b 48
2b5f94fa 49const codepoints = {};
ae52883b 50
2b5f94fa
JD
51for (let i = 0; i < arr.length; ++i) {
52 const result = re.exec(arr[i]);
ae52883b 53 if (result){
2b5f94fa
JD
54 const keyname = result[1];
55 const keysym = parseInt(result[2], 16);
56 const remainder = result[3];
ae52883b 57
2b5f94fa 58 const unicodeRes = /U\+([0-9a-fA-F]+)/.exec(remainder);
ae52883b 59 if (unicodeRes) {
2b5f94fa 60 const 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
2b5f94fa 69let out =
041568bd
PO
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" +
2b5f94fa 79"const codepoints = {\n";
041568bd
PO
80
81function toHex(num) {
2b5f94fa 82 let s = num.toString(16);
041568bd
PO
83 if (s.length < 4) {
84 s = ("0000" + s).slice(-4);
85 }
86 return "0x" + s;
8727f598 87}
041568bd 88
2b5f94fa 89for (let 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
107out +=
108"};\n" +
ae52883b 109"\n" +
9076defa 110"export default {\n" +
651c23ec 111" lookup(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" +
2b5f94fa 118" const 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 128console.log(out);