Files
noVNC/utils/genkeysymdef.js
T

128 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-01-24 12:39:21 +01:00
#!/usr/bin/env node
/*
* genkeysymdef: X11 keysymdef.h to JavaScript converter
2024-11-27 13:58:26 +01:00
* Copyright (C) 2018 The noVNC authors
2017-01-24 12:39:21 +01:00
* Licensed under MPL 2.0 (see LICENSE.txt)
*/
"use strict";
2025-05-06 12:40:30 +02:00
import fs from 'fs';
2020-05-31 02:04:27 +02:00
let showHelp = process.argv.length === 2;
2018-05-24 00:27:09 +03:00
let filename;
2018-05-24 00:27:09 +03:00
for (let i = 2; i < process.argv.length; ++i) {
2018-09-06 16:17:43 +02:00
switch (process.argv[i]) {
case "--help":
case "-h":
2020-05-31 02:04:27 +02:00
showHelp = true;
2018-09-06 16:17:43 +02:00
break;
case "--file":
case "-f":
default:
filename = process.argv[i];
}
}
if (!filename) {
2020-05-31 02:04:27 +02:00
showHelp = true;
2018-09-06 16:17:43 +02:00
console.log("Error: No filename specified\n");
}
2020-05-31 02:04:27 +02:00
if (showHelp) {
2018-09-06 16:17:43 +02:00
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);
}
2018-05-24 00:27:09 +03:00
const buf = fs.readFileSync(filename);
const str = buf.toString('utf8');
2018-05-24 00:27:09 +03:00
const re = /^#define XK_([a-zA-Z_0-9]+)\s+0x([0-9a-fA-F]+)\s*(\/\*\s*(.*)\s*\*\/)?\s*$/m;
2018-05-24 00:27:09 +03:00
const arr = str.split('\n');
2018-05-24 00:27:09 +03:00
const codepoints = {};
2018-05-24 00:27:09 +03:00
for (let i = 0; i < arr.length; ++i) {
const result = re.exec(arr[i]);
2018-09-06 17:29:26 +02:00
if (result) {
2018-05-24 00:27:09 +03:00
const keyname = result[1];
const keysym = parseInt(result[2], 16);
const remainder = result[3];
2018-05-24 00:27:09 +03:00
const unicodeRes = /U\+([0-9a-fA-F]+)/.exec(remainder);
if (unicodeRes) {
2018-05-24 00:27:09 +03:00
const unicode = parseInt(unicodeRes[1], 16);
2017-01-24 12:39:21 +01:00
// The first entry is the preferred one
2018-09-06 17:29:26 +02:00
if (!codepoints[unicode]) {
2017-01-24 12:39:21 +01:00
codepoints[unicode] = { keysym: keysym, name: keyname };
}
}
}
}
2018-05-24 00:27:09 +03:00
let out =
2017-01-24 12:39:21 +01:00
"/*\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" +
2017-01-24 12:07:26 +01:00
"\n" +
2017-01-24 12:39:21 +01:00
"/* Functions at the bottom */\n" +
"\n" +
2018-05-24 00:27:09 +03:00
"const codepoints = {\n";
2017-01-24 12:39:21 +01:00
function toHex(num) {
2018-05-24 00:27:09 +03:00
let s = num.toString(16);
2017-01-24 12:39:21 +01:00
if (s.length < 4) {
s = ("0000" + s).slice(-4);
}
return "0x" + s;
2018-05-24 00:25:44 +03:00
}
2017-01-24 12:39:21 +01:00
2018-05-24 00:27:09 +03:00
for (let codepoint in codepoints) {
2017-01-24 12:49:29 +01:00
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) + ": " +
2017-01-24 12:39:21 +01:00
toHex(codepoints[codepoint].keysym) +
", // XK_" + codepoints[codepoint].name + "\n";
}
out +=
"};\n" +
"\n" +
2017-03-24 10:49:00 +01:00
"export default {\n" +
" lookup(u) {\n" +
2017-01-24 12:49:29 +01:00
" // Latin-1 is one-to-one mapping\n" +
" if ((u >= 0x20) && (u <= 0xff)) {\n" +
" return u;\n" +
" }\n" +
"\n" +
" // Lookup table (fairly random)\n" +
2018-05-24 00:27:09 +03:00
" const keysym = codepoints[u];\n" +
2017-01-24 12:49:29 +01:00
" if (keysym !== undefined) {\n" +
" return keysym;\n" +
2017-03-24 10:49:00 +01:00
" }\n" +
2017-01-24 12:49:29 +01:00
"\n" +
" // General mapping as final fallback\n" +
" return 0x01000000 | u;\n" +
2017-03-24 10:49:00 +01:00
" },\n" +
2017-01-24 12:39:21 +01:00
"};";
2017-01-24 12:39:21 +01:00
console.log(out);