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
|
#define _GNU_SOURCE
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include "utils.h"
#include "rcm-server-main.h"
#include "rcm-server-keymap.h"
enum ini_section {
INI_SECTION_UNDEFINED,
INI_SECTION_DESCRIPTION,
INI_SECTION_KEYMAP,
INI_SECTION_LAYOUT,
};
struct inis {
const char *name;
enum ini_section value;
};
struct inis iniss[] = {
{ .name = "Description", .value = INI_SECTION_DESCRIPTION },
{ .name = "Keymap", .value = INI_SECTION_KEYMAP },
{ .name = "Layout", .value = INI_SECTION_LAYOUT },
{ .name = NULL, .value = INI_SECTION_UNDEFINED },
};
static int strtol_strict(const char *str, int *result)
{
char *end;
long val;
errno = 0;
val = strtol(str, &end, 10);
if (errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
return -EINVAL;
if (errno != 0 && val == 0)
return -EINVAL;
if (end == str)
return -EINVAL;
if (*end != '\0')
return -EINVAL;
if (val < 1 || val > UINT16_MAX)
return -EINVAL;
*result = val;
return 0;
}
static int strtoull_strict(const char *str, uint64_t *result)
{
char *end;
unsigned long long val;
errno = 0;
val = strtoull(str, &end, 16);
if (errno == ERANGE && (val == ULLONG_MAX || val == 0))
return -EINVAL;
if (errno != 0 && val == 0)
return -EINVAL;
if (end == str)
return -EINVAL;
if (*end != '\0')
return -EINVAL;
if (val > UINT64_MAX)
return -EINVAL;
*result = val;
return 0;
}
static int
keymap_parse(FILE *fp, char **line, size_t *buf_size, struct keymap *keymap,
uint16_t *rows_return, uint16_t *cols_return,
unsigned *keycode_count_return)
{
unsigned keycode_count = 0;
unsigned layout_count = 0;
int rows = -1;
int cols = -1;
unsigned i;
enum ini_section current_section = INI_SECTION_UNDEFINED;
int r;
rewind(fp);
while (getline(line, buf_size, fp) != -1) {
char *p, *tmp;
p = strchr(*line, '\n');
if (!p)
continue;
*p = '\0';
p = *line;
while (isspace(*p))
p++;
if (*p == '\0' || *p == ';' || *p == '#')
continue;
if (*p == '[') {
p++;
tmp = strchr(p, ']');
if (!tmp)
continue;
*tmp = '\0';
for (i = 0; iniss[i].name; i++) {
if (strcasecmp(iniss[i].name, p))
continue;
current_section = iniss[i].value;
break;
}
if (!iniss[i].name)
current_section = INI_SECTION_UNDEFINED;
continue;
}
tmp = strchr(p, '=');
if (!tmp)
continue;
*tmp++ = '\0';
while (isspace(*tmp))
tmp++;
if (*tmp == '\0')
continue;
/* FIXME: eat RHS whitespace for p */
switch (current_section) {
case INI_SECTION_DESCRIPTION:
if (!keymap)
continue;
if (!strcasecmp(p, "Name")) {
keymap->name = strdup(tmp);
if (!keymap->name)
return -ENOMEM;
} else if (!strcasecmp(p, "Description")) {
keymap->desc = strdup(tmp);
if (!keymap->desc)
return -ENOMEM;
}
break;
case INI_SECTION_KEYMAP:
if (!strcasecmp(p, "Map")) {
char *protocol;
char *scanstr;
char *keycode;
char *end;
uint64_t scancode;
printf("Map %s\n", tmp);
protocol = strtok(tmp, " :");
scanstr = strtok(NULL, " :");
keycode = strtok(NULL, " :");
end = strtok(NULL, " :");
if (!protocol || !scancode || !keycode || end) {
fprintf(stderr, "Invalid map directive\n");
return -ENOMEM;
}
r = strtoull_strict(scanstr, &scancode);
if (r < 0) {
fprintf(stderr, "Invalid scancode value\n");
return r;
}
printf("Map means protocol %s scancode 0x%08" PRIx64 " keycode %s\n",
protocol, scancode, keycode);
if (keymap) {
/* FIXME: leaks */
keymap->keycodes[keycode_count].protocol = strdup(protocol);
keymap->keycodes[keycode_count].scancode = scancode;
keymap->keycodes[keycode_count].keycode = strdup(keycode);
}
keycode_count++;
} else {
fprintf(stderr, "Unknown parameter: %s\n", p);
}
break;
case INI_SECTION_LAYOUT:
if (!strcasecmp(p, "Rows")) {
if (rows >= 0) {
fprintf(stderr, "Multiple rows specifications\n");
return -EINVAL;
}
r = strtol_strict(tmp, &rows);
if (r < 0) {
fprintf(stderr, "Invalid rows specification\n");
return -EINVAL;
}
} else if (!strcasecmp(p, "Cols")) {
if (cols >= 0) {
fprintf(stderr, "Multiple cols specifications\n");
return -EINVAL;
}
r = strtol_strict(tmp, &cols);
if (r < 0) {
fprintf(stderr, "Invalid cols specification\n");
return -EINVAL;
}
} else if (!strcasecmp(p, "Button")) {
if (keymap) {
if (!strcasecmp(tmp, "Blank"))
keymap->layout[layout_count] = NULL;
else
keymap->layout[layout_count] = &keymap->keycodes[1];
}
layout_count++;
} else {
fprintf(stderr, "Unknown parameter: %s\n", p);
}
break;
case INI_SECTION_UNDEFINED:
break;
default:
printf("Line: %i - %s = %s\n", current_section, p, tmp);
break;
}
}
if (rows < 0) {
fprintf(stderr, "Rows specification missing\n");
return -EINVAL;
}
if (cols < 0) {
fprintf(stderr, "Cols specification missing\n");
return -EINVAL;
}
if (keycode_count < 1) {
fprintf(stderr, "No keycodes found\n");
return -EINVAL;
}
if (layout_count < 1) {
fprintf(stderr, "No layout found\n");
return -EINVAL;
}
if (rows * cols != layout_count) {
fprintf(stderr, "Layout (%u) does not match rows x cols (%ux%u)\n",
layout_count, rows, cols);
return -EINVAL;
}
if (keymap) {
keymap->rows = rows;
keymap->cols = cols;
keymap->keycode_count = keycode_count;
}
if (rows_return)
*rows_return = rows;
if (cols_return)
*cols_return = cols;
if (keycode_count_return)
*keycode_count_return = keycode_count;
return 0;
}
static struct keymap *
keymap_read(int dfd, const char *name)
{
_cleanup_close_ int fd = -1;
_cleanup_fclose_ FILE *fp = NULL;
_cleanup_free_ char *line = NULL;
struct keymap *keymap = NULL;
uint16_t cols, rows;
unsigned keycode_count;
size_t buf_size = 0;
int r;
if (dfd < 0 || !name)
return NULL;
fd = openat(dfd, name, O_RDONLY);
if (fd < 0)
return NULL;
fp = fdopen(fd, "r");
if (!fp)
return NULL;
fd = -1;
r = keymap_parse(fp, &line, &buf_size, NULL, &rows, &cols, &keycode_count);
if (r < 0) {
fprintf(stderr, "Invalid keymap: %s\n", name);
return NULL;
}
keymap = zmalloc(sizeof(*keymap) + sizeof(struct keycode) * keycode_count);
if (!keymap)
return NULL;
keymap->layout = zmalloc(sizeof(struct keycode *) * cols * rows);
if (!keymap->layout) {
free(keymap);
return NULL;
}
keymap_parse(fp, &line, &buf_size, keymap, NULL, NULL, NULL);
return keymap;
}
static int
keymaps_load_dir(struct device *device, const char *path)
{
_cleanup_closedir_ DIR *dir = NULL;
int dfd;
struct dirent *dent;
struct keymap *keymap;
dir = opendir(path);
if (!dir)
return -1;
dfd = dirfd(dir);
if (dfd < 0)
return -1;
while ((dent = readdir(dir))) {
switch (dent->d_type) {
case DT_REG:
case DT_LNK:
case DT_UNKNOWN:
keymap = find_keymap_by_name(device, dent->d_name);
if (keymap)
continue;
keymap = keymap_read(dfd, dent->d_name);
if (keymap) {
printf("Keymap added (0x%p)\n", keymap);
printf("\tDescription: %s\n", keymap->desc);
printf("\tName: %s\n", keymap->name);
list_add(&keymap->list, &device->keymaps);
}
break;
default:
break;
}
}
return 0;
}
int
keymaps_load(struct device *device)
{
char pdpath[strlen("./keymaps/per-device/") + strlen(device->name) + 1];
sprintf(pdpath, "./keymaps/per-device/%s", device->name);
printf("Loading per-device keymaps...\n");
keymaps_load_dir(device, pdpath);
printf("Loading default keymaps...\n");
keymaps_load_dir(device, "./keymaps/default");
return 0;
}
void
keymap_free(struct keymap *keymap)
{
free(keymap->layout);
free(keymap->name);
free(keymap->desc);
free(keymap);
}
struct keymap *
find_keymap_by_name(struct device *dev, const char *name)
{
struct keymap *keymap;
list_for_each_entry(keymap, &dev->keymaps, list)
if (!strcmp(keymap->name, name))
return keymap;
return NULL;
}
|