This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathkbadjust
145 lines (131 loc) · 5.14 KB
/
kbadjust
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env node
/**
* @fileoverview Tool for adjusting keyboard layouts
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @copyright © 2012-2018 Jeff Parsons
* @suppress {missingProperties}
*
* This file is part of PCjs, a computer emulation software project at <https://www.pcjs.org>.
*
* PCjs is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* PCjs is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCjs. If not,
* see <http://www.gnu.org/licenses/gpl.html>.
*
* You are required to include the above copyright notice in every modified copy of this work
* and to display that copyright notice when the software starts running; see COPYRIGHT in
* <https://www.pcjs.org/modules/shared/lib/defines.js>.
*
* Some PCjs files also attempt to load external resource files, such as character-image files,
* ROM files, and disk image files. Those external resource files are not considered part of PCjs
* for purposes of the GNU General Public License, and the author does not claim any copyright
* as to their contents.
*/
"use strict";
var fs = require("fs");
var Defines = require("../../../modules/shared/lib/defines");
var Str = require("../../../modules/shared/lib/strlib");
/**
* printf(format, ...args)
*
* @param {string} format
* @param {...} args
* @return {number}
*/
function printf(format, ...args)
{
console.log(Str.sprintf(format, ...args).replace(/\s*$/, ""));
}
/**
* processFile(sFile)
*
* @param {string} sFile
*/
function processFile(sFile) {
try {
let sXML = fs.readFileSync(sFile, "utf8");
let maxMatch = sXML.match(/max-width:\s*([0-9]+)/);
if (!maxMatch) {
printf("unable to find max width\n");
return;
}
let maxWidth = +maxMatch[1];
let re = /(<control\s+type="key")([^>]*)>(.*?)(<\/control>)/g;
let controlMatch;
while (controlMatch = re.exec(sXML)) {
let attrs = controlMatch[2];
let bindMatch = attrs.match(/ binding="([^"]+)"/);
let topMatch = attrs.match(/ top="([0-9]+)(px|%|)"/);
let leftMatch = attrs.match(/ left="([0-9]+)(px|%|)"/);
let widthMatch = attrs.match(/ width="([0-9]+)(px|%|)"/);
let top = -1, left = -1, width = -1;
if (topMatch) top = +topMatch[1];
if (leftMatch) left = +leftMatch[1];
if (widthMatch) width = +widthMatch[1];
//
// Option to shift all existing rows down one row....
//
// if (top >= 0) {
// top += 42;
// topMatch[2] = 'px';
// }
//
// Option to shift all columns left...
//
// if (left >= 84 && !top) {
// left += 42;
// }
let sLeft = "0", sWidth = "0";
if (left > 0) {
sLeft = left.toString();
if (leftMatch[2] != '%') {
left = (left / maxWidth) * 100;
sLeft = left.toFixed(2);
leftMatch[2] = '%';
}
} else {
leftMatch[2] = "";
}
if (width > 0) {
sWidth = width.toString();
if (widthMatch[2] != '%') {
width = (width / maxWidth) * 100;
sWidth = width.toFixed(2);
widthMatch[2] = '%';
}
} else {
widthMatch[2] = "";
}
if (bindMatch) attrs = attrs.replace(bindMatch[0], "");
if (top >= 0) attrs = attrs.replace(topMatch[0], "");
if (left >= 0) attrs = attrs.replace(leftMatch[0], "");
if (width >= 0) attrs = attrs.replace(widthMatch[0], "");
let controlNew = controlMatch[1];
if (bindMatch) controlNew += bindMatch[0];
if (top >= 0) controlNew += ' top="' + top + (top? topMatch[2] : '') + '"';
if (left >= 0) controlNew += ' left="' + sLeft + leftMatch[2] + '"';
if (width >= 0) controlNew += ' width="' + sWidth + widthMatch[2] + '"';
controlNew += attrs + '>' + controlMatch[3] + controlMatch[4];
if (controlMatch[0] != controlNew) {
// printf("changing '%s' to '%s'\n", controlMatch[0], controlNew);
sXML = sXML.replace(controlMatch[0], controlNew);
re.lastIndex += (controlNew.length - controlMatch[0].length);
}
}
printf("%s", sXML);
}
catch (err) {
console.log(err.message);
}
}
if (process.argv.length <= 2) {
console.log("usage: node kbadjust [us83-softkeys-fixed.xml | us84-softkeys-fixed.xml]");
process.exit();
}
processFile(process.argv[2]);