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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <ui/KeycodeLabels.h>
#include <stdlib.h>
#include <ctype.h>
#include <map>
#include <string>
#include <utils/ByteOrder.h>
using namespace std;
enum {
LENDIAN,
BENDIAN
};
/*
* 1: KeyEvent name
* 2: display_label
* 3: number
* 4..7: base, shift, alt, shift-alt
*/
#define COLUMNS (3+4)
struct KeyRecord
{
int lineno;
int values[COLUMNS];
};
struct PropValue
{
PropValue() { lineno = -1; }
PropValue(const PropValue& that) { lineno=that.lineno; value=that.value; }
PropValue(int l, const string& v) { lineno = l; value = v; }
int lineno;
string value;
};
static int usage();
// 0 -- ok
// >0 -- error
static int parse_key_line(const char* filename, int lineno, char* line,
KeyRecord* out);
static int write_kr(int fd, const KeyRecord& kr);
int g_endian;
int
main(int argc, char** argv)
{
int err;
if (argc != 3) {
return usage();
}
const char* filename = argv[1];
const char* outfilename = argv[2];
int in = open(filename, O_RDONLY);
if (in == -1) {
fprintf(stderr, "kcm: error opening file for read: %s\n", filename);
return 1;
}
off_t size = lseek(in, 0, SEEK_END);
lseek(in, 0, SEEK_SET);
char* input = (char*)malloc(size+1);
read(in, input, size);
input[size] = '\0';
close(in);
in = -1;
map<string,PropValue> properties;
map<int,KeyRecord> keys;
int errorcount = 0;
int lineno = 1;
char *thisline = input;
while (*thisline) {
KeyRecord kr;
char *nextline = thisline;
while (*nextline != '\0' && *nextline != '\n' && *nextline != '\r') {
nextline++;
}
// eat whitespace, but not newlines
while (*thisline != '\0' && (*thisline == ' ' || *thisline == '\t')) {
thisline++;
}
// find the end of the line
char lineend = *nextline;
*nextline = '\0';
if (lineend == '\r' && nextline[1] == '\n') {
nextline++;
}
if (*thisline == '\0' || *thisline == '\r' || *thisline == '\n'
|| *thisline == '#') {
// comment or blank line
}
else if (*thisline == '[') {
// property - syntax [name=value]
// look for =
char* prop = thisline+1;
char* end = prop;
while (*end != '\0' && *end != '=') {
end++;
}
if (*end != '=') {
fprintf(stderr, "%s:%d: invalid property line: %s\n",
filename, lineno, thisline);
errorcount++;
} else {
*end = '\0';
char* value = end+1;
end = nextline;
while (end > prop && *end != ']') {
end--;
}
if (*end != ']') {
fprintf(stderr, "%s:%d: property missing closing ]: %s\n",
filename, lineno, thisline);
errorcount++;
} else {
*end = '\0';
properties[prop] = PropValue(lineno, value);
}
}
}
else {
// key
err = parse_key_line(filename, lineno, thisline, &kr);
if (err == 0) {
kr.lineno = lineno;
map<int,KeyRecord>::iterator old = keys.find(kr.values[0]);
if (old != keys.end()) {
fprintf(stderr, "%s:%d: keycode %d already defined\n",
filename, lineno, kr.values[0]);
fprintf(stderr, "%s:%d: previously defined here\n",
filename, old->second.lineno);
errorcount++;
}
keys[kr.values[0]] = kr;
}
else if (err > 0) {
errorcount += err;
}
}
lineno++;
nextline++;
thisline = nextline;
if (errorcount > 20) {
fprintf(stderr, "%s:%d: too many errors. stopping.\n", filename,
lineno);
return 1;
}
}
free(input);
map<string,PropValue>::iterator sit = properties.find("type");
if (sit == properties.end()) {
fprintf(stderr, "%s: key character map must contain type property.\n",
argv[0]);
errorcount++;
}
PropValue pv = sit->second;
unsigned char kbdtype = 0;
if (pv.value == "NUMERIC") {
kbdtype = 1;
}
else if (pv.value == "Q14") {
kbdtype = 2;
}
else if (pv.value == "QWERTY") {
kbdtype = 3;
}
else {
fprintf(stderr, "%s:%d: keyboard type must be one of NUMERIC, Q14 "
" or QWERTY, not %s\n", filename, pv.lineno, pv.value.c_str());
}
if (errorcount != 0) {
return 1;
}
int out = open(outfilename, O_RDWR|O_CREAT|O_TRUNC, 0664);
if (out == -1) {
fprintf(stderr, "kcm: error opening file for write: %s\n", outfilename);
return 1;
}
int count = keys.size();
map<int,KeyRecord>::iterator it;
int n;
/**
* File Format:
* Offset Description Value
* 0 magic string "keychar"
* 8 endian marker 0x12345678
* 12 version 0x00000002
* 16 key count number of key entries
* 20 keyboard type NUMERIC, Q14, QWERTY, etc.
* 21 padding 0
* 32 the keys
*/
err = write(out, "keychar", 8);
if (err == -1) goto bad_write;
n = htodl(0x12345678);
err = write(out, &n, 4);
if (err == -1) goto bad_write;
n = htodl(0x00000002);
err = write(out, &n, 4);
if (err == -1) goto bad_write;
n = htodl(count);
err = write(out, &n, 4);
if (err == -1) goto bad_write;
err = write(out, &kbdtype, 1);
if (err == -1) goto bad_write;
char zero[11];
memset(zero, 0, 11);
err = write(out, zero, 11);
if (err == -1) goto bad_write;
for (it = keys.begin(); it != keys.end(); it++) {
const KeyRecord& kr = it->second;
/*
printf("%2d/ [%d] [%d] [%d] [%d] [%d] [%d] [%d]\n", kr.lineno,
kr.values[0], kr.values[1], kr.values[2], kr.values[3],
kr.values[4], kr.values[5], kr.values[6]);
*/
err = write_kr(out, kr);
if (err == -1) goto bad_write;
}
close(out);
return 0;
bad_write:
fprintf(stderr, "kcm: fatal error writing to file: %s\n", outfilename);
close(out);
unlink(outfilename);
return 1;
}
static int usage()
{
fprintf(stderr,
"usage: kcm INPUT OUTPUT\n"
"\n"
"INPUT keycharmap file\n"
"OUTPUT compiled keycharmap file\n"
);
return 1;
}
static int
is_whitespace(const char* p)
{
while (*p) {
if (!isspace(*p)) {
return 0;
}
p++;
}
return 1;
}
static int
parse_keycode(const char* filename, int lineno, char* str, int* value)
{
const KeycodeLabel *list = KEYCODES;
while (list->literal) {
if (0 == strcmp(str, list->literal)) {
*value = list->value;
return 0;
}
list++;
}
char* endptr;
*value = strtol(str, &endptr, 0);
if (*endptr != '\0') {
fprintf(stderr, "%s:%d: expected keycode label or number near: "
"%s\n", filename, lineno, str);
return 1;
}
if (*value == 0) {
fprintf(stderr, "%s:%d: 0 is not a valid keycode.\n",
filename, lineno);
return 1;
}
return 0;
}
static int
parse_number(const char* filename, int lineno, char* str, int* value)
{
int len = strlen(str);
if (len == 3 && str[0] == '\'' && str[2] == '\'') {
if (str[1] > 0 && str[1] < 127) {
*value = (int)str[1];
return 0;
} else {
fprintf(stderr, "%s:%d: only low ascii characters are allowed in"
" quotes near: %s\n", filename, lineno, str);
return 1;
}
}
char* endptr;
*value = strtol(str, &endptr, 0);
if (*endptr != '\0') {
fprintf(stderr, "%s:%d: expected number or quoted ascii but got: %s\n",
filename, lineno, str);
return 1;
}
if (*value >= 0xfffe || *value < 0) {
fprintf(stderr, "%s:%d: unicode char out of range (no negatives, "
"nothing larger than 0xfffe): %s\n", filename, lineno, str);
return 1;
}
return 0;
}
static int
parse_key_line(const char* filename, int lineno, char* line, KeyRecord* out)
{
char* p = line;
int len = strlen(line);
char* s[COLUMNS];
for (int i=0; i<COLUMNS; i++) {
s[i] = (char*)malloc(len+1);
}
for (int i = 0; i < COLUMNS; i++) {
while (*p != '\0' && isspace(*p)) {
p++;
}
if (*p == '\0') {
fprintf(stderr, "%s:%d: not enough on this line: %s\n", filename,
lineno, line);
return 1;
}
char *p1 = p;
while (*p != '\0' && !isspace(*p)) {
p++;
}
memcpy(s[i], p1, p - p1);
s[i][p - p1] = '\0';
}
while (*p != '\0' && isspace(*p)) {
*p++;
}
if (*p != '\0') {
fprintf(stderr, "%s:%d: too much on one line near: %s\n", filename,
lineno, p);
fprintf(stderr, "%s:%d: -->%s<--\n", filename, lineno, line);
return 1;
}
int errorcount = parse_keycode(filename, lineno, s[0], &out->values[0]);
for (int i=1; i<COLUMNS && errorcount == 0; i++) {
errorcount += parse_number(filename, lineno, s[i], &out->values[i]);
}
return errorcount;
}
struct WrittenRecord
{
unsigned int keycode; // 4 bytes
unsigned short values[COLUMNS - 1]; // 6*2 bytes = 12
// 16 bytes total
};
static int
write_kr(int fd, const KeyRecord& kr)
{
WrittenRecord wr;
wr.keycode = htodl(kr.values[0]);
for (int i=0; i<COLUMNS - 1; i++) {
wr.values[i] = htods(kr.values[i+1]);
}
return write(fd, &wr, sizeof(WrittenRecord));
}
|