summaryrefslogtreecommitdiff
path: root/utils/genkeysymdef.js
blob: d21773f9f65fbab03c0b76952cfc781af7aff79e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env node
/*
 * genkeysymdef: X11 keysymdef.h to JavaScript converter
 * Copyright (C) 2018 The noVNC Authors
 * Licensed under MPL 2.0 (see LICENSE.txt)
 */

"use strict";

const fs = require('fs');

let show_help = process.argv.length === 2;
let filename;

for (let i = 2; i < process.argv.length; ++i) {
    switch (process.argv[i]) {
        case "--help":
        case "-h":
            show_help = true;
            break;
        case "--file":
        case "-f":
        default:
            filename = process.argv[i];
    }
}

if (!filename) {
    show_help = true;
    console.log("Error: No filename specified\n");
}

if (show_help) {
    console.log("Parses a *nix keysymdef.h to generate Unicode code point mappings");
    console.log("Usage: node parse.js [options] filename:");
    console.log("  -h [ --help ]                 Produce this help message");
    console.log("  filename                      The keysymdef.h file to parse");
    process.exit(0);
}

const buf = fs.readFileSync(filename);
const str = buf.toString('utf8');

const re = /^#define XK_([a-zA-Z_0-9]+)\s+0x([0-9a-fA-F]+)\s*(\/\*\s*(.*)\s*\*\/)?\s*$/m;

const arr = str.split('\n');

const codepoints = {};

for (let i = 0; i < arr.length; ++i) {
    const result = re.exec(arr[i]);
    if (result) {
        const keyname = result[1];
        const keysym = parseInt(result[2], 16);
        const remainder = result[3];

        const unicodeRes = /U\+([0-9a-fA-F]+)/.exec(remainder);
        if (unicodeRes) {
            const unicode = parseInt(unicodeRes[1], 16);
            // The first entry is the preferred one
            if (!codepoints[unicode]) {
                codepoints[unicode] = { keysym: keysym, name: keyname };
            }
        }
    }
}

let out =
"/*\n" +
" * Mapping from Unicode codepoints to X11/RFB keysyms\n" +
" *\n" +
" * This file was automatically generated from keysymdef.h\n" +
" * DO NOT EDIT!\n" +
" */\n" +
"\n" +
"/* Functions at the bottom */\n" +
"\n" +
"const codepoints = {\n";

function toHex(num) {
    let s = num.toString(16);
    if (s.length < 4) {
        s = ("0000" + s).slice(-4);
    }
    return "0x" + s;
}

for (let codepoint in codepoints) {
    codepoint = parseInt(codepoint);

    // Latin-1?
    if ((codepoint >= 0x20) && (codepoint <= 0xff)) {
        continue;
    }

    // Handled by the general Unicode mapping?
    if ((codepoint | 0x01000000) === codepoints[codepoint].keysym) {
        continue;
    }

    out += "    " + toHex(codepoint) + ": " +
           toHex(codepoints[codepoint].keysym) +
           ", // XK_" + codepoints[codepoint].name + "\n";
}

out +=
"};\n" +
"\n" +
"export default {\n" +
"    lookup(u) {\n" +
"        // Latin-1 is one-to-one mapping\n" +
"        if ((u >= 0x20) && (u <= 0xff)) {\n" +
"            return u;\n" +
"        }\n" +
"\n" +
"        // Lookup table (fairly random)\n" +
"        const keysym = codepoints[u];\n" +
"        if (keysym !== undefined) {\n" +
"            return keysym;\n" +
"        }\n" +
"\n" +
"        // General mapping as final fallback\n" +
"        return 0x01000000 | u;\n" +
"    },\n" +
"};";

console.log(out);