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
|
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/select.h>
#include "input.h"
struct input_keymap_entry_v2 {
#define KEYMAP_BY_INDEX (1 << 0)
uint8_t flags;
uint8_t len;
uint16_t index;
uint32_t keycode;
uint8_t scancode[32];
};
#ifndef EVIOCGKEYCODE_V2
#define EVIOCGKEYCODE_V2 _IOR('E', 0x04, struct input_keymap_entry_v2)
#endif
struct kbd_entry {
unsigned int scancode;
unsigned int keycode;
};
struct kbd_map {
struct kbd_entry *map;
int keys;
int size;
int alloc;
};
/* ------------------------------------------------------------------ */
static struct kbd_map* kbd_map_read(int fd, unsigned int version)
{
struct kbd_entry entry;
struct kbd_map *map;
int rc;
map = malloc(sizeof(*map));
memset(map,0,sizeof(*map));
for (map->size = 0; map->size < 65536; map->size++) {
if (version < 0x10001) {
entry.scancode = map->size;
entry.keycode = KEY_RESERVED;
rc = ioctl(fd, EVIOCGKEYCODE, &entry);
if (rc < 0) {
break;
}
} else {
struct input_keymap_entry_v2 ke = {
.index = map->size,
.flags = KEYMAP_BY_INDEX,
.len = sizeof(uint32_t),
.keycode = KEY_RESERVED,
};
rc = ioctl(fd, EVIOCGKEYCODE_V2, &ke);
if (rc < 0)
break;
memcpy(&entry.scancode, ke.scancode,
sizeof(entry.scancode));
entry.keycode = ke.keycode;
}
if (map->size >= map->alloc) {
map->alloc += 64;
map->map = realloc(map->map, map->alloc * sizeof(entry));
}
map->map[map->size] = entry;
if (KEY_RESERVED != entry.keycode)
map->keys++;
}
if (map->keys) {
fprintf(stderr,"map: %d keys, size: %d/%d\n",
map->keys, map->size, map->alloc);
return map;
} else {
free(map);
return NULL;
}
}
static int kbd_map_write(int fh, struct kbd_map *map)
{
int i,rc;
for (i = 0; i < map->size; i++) {
rc = ioctl(fh, EVIOCSKEYCODE, &map->map[i]);
if (0 != rc) {
fprintf(stderr,"ioctl EVIOCSKEYCODE(%d,%d): %s\n",
map->map[i].scancode,map->map[i].keycode,
strerror(errno));
return -1;
}
}
return 0;
}
static void kbd_key_print(FILE *fp, int scancode, int keycode)
{
if ( keycode < 10 )
fprintf(fp, "0x%04x = 0x%x # %s\n",
scancode, keycode, ev_type_name(EV_KEY, keycode));
else
fprintf(fp, "0x%04x = %3d # %s\n",
scancode, keycode, ev_type_name(EV_KEY, keycode));
}
static void kbd_map_print(FILE *fp, struct kbd_map *map, int complete)
{
int i;
for (i = 0; i < map->size; i++) {
if (!complete && KEY_RESERVED == map->map[i].keycode)
continue;
kbd_key_print(fp,map->map[i].scancode,map->map[i].keycode);
}
}
static struct kbd_map* kbd_map_parse(FILE *fp)
{
struct kbd_entry entry;
struct kbd_map *map;
char line[1024],scancode[80],keycode[80];
char *c;
int i;
map = malloc(sizeof(*map));
memset(map,0,sizeof(*map));
while (NULL != fgets(line,sizeof(line),fp)) {
c = strchr(line,'#');
if (c)
*c = 0;
c = line;
while (*c == ' ' || *c == '\t')
c++;
if (!*c || *c == '\n')
continue; // Blank or comment line
if (2 != sscanf(line," %79s = %79s", scancode, keycode)) {
fprintf(stderr,"parse error: %s",line);
return NULL;
}
/* parse scancode */
entry.scancode = strtol(scancode, NULL, 0);
if (entry.scancode < 0) {
fprintf(stderr,"scancode %d invalid\n",
entry.scancode);
return NULL;
}
/* parse keycode */
for (i = 0; i < KEY_MAX; i++) {
if (!EV_TYPE_NAME[EV_KEY][i])
continue;
if (0 == strcmp(keycode,EV_TYPE_NAME[EV_KEY][i]))
break;
}
if (i == KEY_MAX)
entry.keycode = strtol(keycode, NULL, 0);
else
entry.keycode = i;
fprintf(stderr,"set: ");
kbd_key_print(stderr,entry.scancode,entry.keycode);
if (map->size >= map->alloc) {
map->alloc += 64;
map->map = realloc(map->map, map->alloc * sizeof(entry));
}
map->map[map->size] = entry;
map->size++;
}
return map;
}
/* ------------------------------------------------------------------ */
static void kbd_print_bits(int fd)
{
BITFIELD bits[KEY_CNT/sizeof(BITFIELD)];
int rc,bit;
rc = ioctl(fd,EVIOCGBIT(EV_KEY,sizeof(bits)),bits);
if (rc < 0)
return;
for (bit = 0; bit < rc*8 && bit < KEY_MAX; bit++) {
if (!test_bit(bit,bits))
continue;
if (EV_TYPE_NAME[EV_KEY][bit]) {
fprintf(stderr,"bits: %s\n", EV_TYPE_NAME[EV_KEY][bit]);
} else {
fprintf(stderr,"bits: unknown [%d]\n", bit);
}
}
}
static void show_kbd(int nr, int fd, unsigned int protocol_version)
{
struct kbd_map *map;
device_info(nr, fd, true);
map = kbd_map_read(fd, protocol_version);
if (map)
kbd_map_print(stdout, map, 0);
else
kbd_print_bits(fd);
}
static int set_kbd(int fd, unsigned int protocol_version, char *mapfile)
{
struct kbd_map *map;
FILE *fp;
if (0 == strcmp(mapfile,"-"))
fp = stdin;
else {
fp = fopen(mapfile,"r");
if (NULL == fp) {
fprintf(stderr,"open %s: %s\n",mapfile,strerror(errno));
close(fd);
return -1;
}
}
map = kbd_map_parse(fp);
if (map == NULL ||
0 != kbd_map_write(fd,map)) {
return -1;
}
return 0;
}
static int usage(char *prog, int error)
{
fprintf(error ? stderr : stdout,
"usage: %s [ -f file ] devnr\n",
prog);
exit(error);
}
int main(int argc, char *argv[])
{
int c, devnr, fd;
char *mapfile = NULL;
unsigned int protocol_version;
int rc = EXIT_FAILURE;
for (;;) {
if (-1 == (c = getopt(argc, argv, "hf:")))
break;
switch (c) {
case 'f':
mapfile = optarg;
break;
case 'h':
usage(argv[0],0);
default:
usage(argv[0],1);
}
}
if (optind == argc)
usage(argv[0],1);
devnr = atoi(argv[optind]);
fd = device_open(devnr, 1);
if (fd < 0)
goto out;
if (ioctl(fd, EVIOCGVERSION, &protocol_version) < 0) {
fprintf(stderr,
"Unable to query evdev protocol version: %s\n",
strerror(errno));
goto out_close;
}
if (mapfile)
set_kbd(fd, protocol_version, mapfile);
else
show_kbd(devnr, fd, protocol_version);
rc = EXIT_SUCCESS;
out_close:
close(fd);
out:
return rc;
}
/* ---------------------------------------------------------------------
* Local variables:
* c-basic-offset: 8
* End:
*/
|