diff options
Diffstat (limited to 'utils')
-rwxr-xr-x | utils/genkeysymdef.js | 128 | ||||
-rw-r--r-- | utils/parse.js | 103 |
2 files changed, 128 insertions, 103 deletions
diff --git a/utils/genkeysymdef.js b/utils/genkeysymdef.js new file mode 100755 index 0000000..8486da3 --- /dev/null +++ b/utils/genkeysymdef.js @@ -0,0 +1,128 @@ +#!/usr/bin/env node +/* + * genkeysymdef: X11 keysymdef.h to JavaScript converter + * Copyright 2013 jalf <git@jalf.dk> + * Copyright 2017 Pierre Ossman for Cendio AB + * Licensed under MPL 2.0 (see LICENSE.txt) + */ + +"use strict"; + +var fs = require('fs'); + +var show_help = process.argv.length === 2; +var filename; + +for (var 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"); + return; +} + +var buf = fs.readFileSync(filename); +var str = buf.toString('utf8'); + +var re = /^\#define XK_([a-zA-Z_0-9]+)\s+0x([0-9a-fA-F]+)\s*(\/\*\s*(.*)\s*\*\/)?\s*$/m; + +var arr = str.split('\n'); + +var codepoints = {}; + +for (var i = 0; i < arr.length; ++i) { + var result = re.exec(arr[i]); + if (result){ + var keyname = result[1]; + var keysym = parseInt(result[2], 16); + var remainder = result[3]; + + var unicodeRes = /U\+([0-9a-fA-F]+)/.exec(remainder); + if (unicodeRes) { + var unicode = parseInt(unicodeRes[1], 16); + // The first entry is the preferred one + if (!codepoints[unicode]){ + codepoints[unicode] = { keysym: keysym, name: keyname }; + } + } + } +} + +var 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" + +"var codepoints = {\n"; + +function toHex(num) { + var s = num.toString(16); + if (s.length < 4) { + s = ("0000" + s).slice(-4); + } + return "0x" + s; +}; + +for (var 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 : function(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" + +" var 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); diff --git a/utils/parse.js b/utils/parse.js deleted file mode 100644 index fd79b12..0000000 --- a/utils/parse.js +++ /dev/null @@ -1,103 +0,0 @@ -// Utility to parse keysymdef.h to produce mappings from Unicode codepoints to keysyms -"use strict"; - -var fs = require('fs'); - -var show_help = process.argv.length === 2; -var use_keynames = false; -var filename; - -for (var i = 2; i < process.argv.length; ++i) { - switch (process.argv[i]) { - case "--help": - case "-h": - show_help = true; - break; - case "--debug-names": - case "-d": - use_keynames = 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(" -d [ --debug-names ] Preserve keysym names for debugging (Increases file size by ~40KB)"); - console.log(" filename The keysymdef.h file to parse"); - return; -} - -// Set this to false to omit key names from the generated keysymdef.js -// This reduces the file size by around 40kb, but may hinder debugging - -var buf = fs.readFileSync(filename); -var str = buf.toString('utf8'); - -var re = /^\#define XK_([a-zA-Z_0-9]+)\s+0x([0-9a-fA-F]+)\s*(\/\*\s*(.*)\s*\*\/)?\s*$/m; - -var arr = str.split('\n'); - -var keysyms = {}; -var codepoints = {}; - -for (var i = 0; i < arr.length; ++i) { - var result = re.exec(arr[i]); - if (result){ - var keyname = result[1]; - var keysym = parseInt(result[2], 16); - var remainder = result[3]; - - keysyms[keysym] = keyname; - - var unicodeRes = /U\+([0-9a-fA-F]+)/.exec(remainder); - if (unicodeRes) { - var unicode = parseInt(unicodeRes[1], 16); - if (!codepoints[unicode]){ - codepoints[unicode] = keysym; - } - } - else { - console.log("no unicode codepoint found:", arr[i]); - } - } - else { - console.log("line is not a keysym:", arr[i]); - } -} - -var out = "// This file describes mappings from Unicode codepoints to the keysym values\n" + -"// (and optionally, key names) expected by the RFB protocol\n" + -"// How this file was generated:\n" + -"// " + process.argv.join(" ") + "\n" + -"var keysyms = (function(){\n" + -" \"use strict\";\n" + -" var keynames = {keysyms};\n" + -" var codepoints = {codepoints};\n" + -"\n" + -" function lookup(k) { return k ? {keysym: k, keyname: keynames ? keynames[k] : k} : undefined; }\n" + -" return {\n" + -" fromUnicode : function(u) {\n" + -" var keysym = codepoints[u];\n" + -" if (keysym === undefined) {\n" + -" keysym = 0x01000000 | u;\n" + -" }\n" + -" return lookup(keysym);\n" + -" },\n" + -" lookup : lookup\n" + -" };\n" + -"})();\n"; -out = out.replace('{keysyms}', use_keynames ? JSON.stringify(keysyms) : "null"); -out = out.replace('{codepoints}', JSON.stringify(codepoints)); - -fs.writeFileSync("keysymdef.js", out); |