1 From 52f533a6c32f8e1e376c5a335cc067da16d59b61 Mon Sep 17 00:00:00 2001
2 From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
3 Date: Wed, 26 Jan 2011 15:49:39 +0100
4 Subject: [PATCH] input-kbd - switch to using EVIOCGKEYCODE2 when available
6 [mchehab@redhat.com: Ported it to the -git version]
8 Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
9 Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
11 input-kbd.c | 110 +++++++++++++++++++++++++++++++++++++++--------------------
13 2 files changed, 75 insertions(+), 39 deletions(-)
15 diff --git a/input-kbd.c b/input-kbd.c
16 index c432d0d..aaf23b9 100644
23 +struct input_keymap_entry_v2 {
24 +#define KEYMAP_BY_INDEX (1 << 0)
29 + uint8_t scancode[32];
32 +#ifndef EVIOCGKEYCODE_V2
33 +#define EVIOCGKEYCODE_V2 _IOR('E', 0x04, struct input_keymap_entry_v2)
39 + unsigned int scancode;
40 + unsigned int keycode;
44 @@ -23,7 +36,7 @@ struct kbd_map {
46 /* ------------------------------------------------------------------ */
48 -static struct kbd_map* kbd_map_read(int fd)
49 +static struct kbd_map* kbd_map_read(int fd, unsigned int version)
51 struct kbd_entry entry;
53 @@ -32,17 +45,35 @@ static struct kbd_map* kbd_map_read(int fd)
54 map = malloc(sizeof(*map));
55 memset(map,0,sizeof(*map));
56 for (map->size = 0; map->size < 65536; map->size++) {
57 - entry.scancode = map->size;
58 - entry.keycode = KEY_RESERVED;
59 - rc = ioctl(fd, EVIOCGKEYCODE, &entry);
63 + if (version < 0x10001) {
64 + entry.scancode = map->size;
65 + entry.keycode = KEY_RESERVED;
66 + rc = ioctl(fd, EVIOCGKEYCODE, &entry);
72 + struct input_keymap_entry_v2 ke = {
74 + .flags = KEYMAP_BY_INDEX,
75 + .len = sizeof(uint32_t),
76 + .keycode = KEY_RESERVED,
79 + rc = ioctl(fd, EVIOCGKEYCODE_V2, &ke);
82 + memcpy(&entry.scancode, ke.scancode,
83 + sizeof(entry.scancode));
84 + entry.keycode = ke.keycode;
87 if (map->size >= map->alloc) {
89 map->map = realloc(map->map, map->alloc * sizeof(entry));
92 map->map[map->size] = entry;
94 if (KEY_RESERVED != entry.keycode)
95 @@ -156,37 +187,25 @@ static void kbd_print_bits(int fd)
99 -static void show_kbd(int nr)
100 +static void show_kbd(int fd, unsigned int protocol_version)
105 - fd = device_open(nr,1);
110 - map = kbd_map_read(fd);
112 - kbd_map_print(stdout,map,0);
114 + map = kbd_map_read(fd, protocol_version);
116 + kbd_map_print(stdout, map, 0);
124 -static int set_kbd(int nr, char *mapfile)
125 +static int set_kbd(int fd, unsigned int protocol_version, char *mapfile)
131 - fd = device_open(nr,1);
135 - map = kbd_map_read(fd);
136 + map = kbd_map_read(fd, protocol_version);
138 fprintf(stderr,"device has no map\n");
140 @@ -203,14 +222,12 @@ static int set_kbd(int nr, char *mapfile)
146 if (0 != kbd_map_parse(fp,map) ||
147 0 != kbd_map_write(fd,map)) {
156 @@ -224,8 +241,10 @@ static int usage(char *prog, int error)
158 int main(int argc, char *argv[])
162 char *mapfile = NULL;
163 + unsigned int protocol_version;
164 + int rc = EXIT_FAILURE;
167 if (-1 == (c = getopt(argc, argv, "hf:")))
168 @@ -245,12 +264,29 @@ int main(int argc, char *argv[])
171 devnr = atoi(argv[optind]);
173 - set_kbd(devnr,mapfile);
177 + fd = device_open(devnr, 1);
181 + if (ioctl(fd, EVIOCGVERSION, &protocol_version) < 0) {
183 + "Unable to query evdev protocol version: %s\n",
190 + set_kbd(fd, protocol_version, mapfile);
192 + show_kbd(fd, protocol_version);
202 /* ---------------------------------------------------------------------
203 diff --git a/input.c b/input.c
204 index d57a31e..a9bd5e8 100644
207 @@ -101,8 +101,8 @@ int device_open(int nr, int verbose)
211 - if (EV_VERSION != version) {
212 - fprintf(stderr, "protocol version mismatch (expected %d, got %d)\n",
213 + if (EV_VERSION > version) {
214 + fprintf(stderr, "protocol version mismatch (expected >= %d, got %d)\n",
215 EV_VERSION, version);