[package] input-utils: update to make it works with new kernels.
[openwrt/svn-archive/archive.git] / utils / input-utils / patches / 001-EVIOCGKEYCODE2.patch
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
5
6 [mchehab@redhat.com: Ported it to the -git version]
7
8 Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
9 Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
10 ---
11 input-kbd.c | 110 +++++++++++++++++++++++++++++++++++++++--------------------
12 input.c | 4 +-
13 2 files changed, 75 insertions(+), 39 deletions(-)
14
15 diff --git a/input-kbd.c b/input-kbd.c
16 index c432d0d..aaf23b9 100644
17 --- a/input-kbd.c
18 +++ b/input-kbd.c
19 @@ -9,9 +9,22 @@
20
21 #include "input.h"
22
23 +struct input_keymap_entry_v2 {
24 +#define KEYMAP_BY_INDEX (1 << 0)
25 + uint8_t flags;
26 + uint8_t len;
27 + uint16_t index;
28 + uint32_t keycode;
29 + uint8_t scancode[32];
30 +};
31 +
32 +#ifndef EVIOCGKEYCODE_V2
33 +#define EVIOCGKEYCODE_V2 _IOR('E', 0x04, struct input_keymap_entry_v2)
34 +#endif
35 +
36 struct kbd_entry {
37 - int scancode;
38 - int keycode;
39 + unsigned int scancode;
40 + unsigned int keycode;
41 };
42
43 struct kbd_map {
44 @@ -23,7 +36,7 @@ struct kbd_map {
45
46 /* ------------------------------------------------------------------ */
47
48 -static struct kbd_map* kbd_map_read(int fd)
49 +static struct kbd_map* kbd_map_read(int fd, unsigned int version)
50 {
51 struct kbd_entry entry;
52 struct kbd_map *map;
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);
60 - if (rc < 0) {
61 - map->size--;
62 - break;
63 + if (version < 0x10001) {
64 + entry.scancode = map->size;
65 + entry.keycode = KEY_RESERVED;
66 + rc = ioctl(fd, EVIOCGKEYCODE, &entry);
67 + if (rc < 0) {
68 + map->size--;
69 + break;
70 + }
71 + } else {
72 + struct input_keymap_entry_v2 ke = {
73 + .index = map->size,
74 + .flags = KEYMAP_BY_INDEX,
75 + .len = sizeof(uint32_t),
76 + .keycode = KEY_RESERVED,
77 + };
78 +
79 + rc = ioctl(fd, EVIOCGKEYCODE_V2, &ke);
80 + if (rc < 0)
81 + break;
82 + memcpy(&entry.scancode, ke.scancode,
83 + sizeof(entry.scancode));
84 + entry.keycode = ke.keycode;
85 }
86 +
87 if (map->size >= map->alloc) {
88 map->alloc += 64;
89 map->map = realloc(map->map, map->alloc * sizeof(entry));
90 }
91 +
92 map->map[map->size] = entry;
93
94 if (KEY_RESERVED != entry.keycode)
95 @@ -156,37 +187,25 @@ static void kbd_print_bits(int fd)
96 }
97 }
98
99 -static void show_kbd(int nr)
100 +static void show_kbd(int fd, unsigned int protocol_version)
101 {
102 struct kbd_map *map;
103 - int fd;
104
105 - fd = device_open(nr,1);
106 - if (-1 == fd)
107 - return;
108 device_info(fd);
109
110 - map = kbd_map_read(fd);
111 - if (NULL != map) {
112 - kbd_map_print(stdout,map,0);
113 - } else {
114 + map = kbd_map_read(fd, protocol_version);
115 + if (map)
116 + kbd_map_print(stdout, map, 0);
117 + else
118 kbd_print_bits(fd);
119 - }
120 -
121 - close(fd);
122 }
123
124 -static int set_kbd(int nr, char *mapfile)
125 +static int set_kbd(int fd, unsigned int protocol_version, char *mapfile)
126 {
127 struct kbd_map *map;
128 FILE *fp;
129 - int fd;
130
131 - fd = device_open(nr,1);
132 - if (-1 == fd)
133 - return -1;
134 -
135 - map = kbd_map_read(fd);
136 + map = kbd_map_read(fd, protocol_version);
137 if (NULL == map) {
138 fprintf(stderr,"device has no map\n");
139 close(fd);
140 @@ -203,14 +222,12 @@ static int set_kbd(int nr, char *mapfile)
141 return -1;
142 }
143 }
144 -
145 +
146 if (0 != kbd_map_parse(fp,map) ||
147 0 != kbd_map_write(fd,map)) {
148 - close(fd);
149 return -1;
150 }
151
152 - close(fd);
153 return 0;
154 }
155
156 @@ -224,8 +241,10 @@ static int usage(char *prog, int error)
157
158 int main(int argc, char *argv[])
159 {
160 - int c,devnr;
161 + int c, devnr, fd;
162 char *mapfile = NULL;
163 + unsigned int protocol_version;
164 + int rc = EXIT_FAILURE;
165
166 for (;;) {
167 if (-1 == (c = getopt(argc, argv, "hf:")))
168 @@ -245,12 +264,29 @@ int main(int argc, char *argv[])
169 usage(argv[0],1);
170
171 devnr = atoi(argv[optind]);
172 - if (mapfile) {
173 - set_kbd(devnr,mapfile);
174 - } else {
175 - show_kbd(devnr);
176 +
177 + fd = device_open(devnr, 1);
178 + if (fd < 0)
179 + goto out;
180 +
181 + if (ioctl(fd, EVIOCGVERSION, &protocol_version) < 0) {
182 + fprintf(stderr,
183 + "Unable to query evdev protocol version: %s\n",
184 + strerror(errno));
185 + goto out_close;
186 }
187 - return 0;
188 +
189 + if (mapfile)
190 + set_kbd(fd, protocol_version, mapfile);
191 + else
192 + show_kbd(fd, protocol_version);
193 +
194 + rc = EXIT_SUCCESS;
195 +
196 +out_close:
197 + close(fd);
198 +out:
199 + return rc;
200 }
201
202 /* ---------------------------------------------------------------------
203 diff --git a/input.c b/input.c
204 index d57a31e..a9bd5e8 100644
205 --- a/input.c
206 +++ b/input.c
207 @@ -101,8 +101,8 @@ int device_open(int nr, int verbose)
208 close(fd);
209 return -1;
210 }
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);
216 close(fd);
217 return -1;
218 --
219 1.7.2.3
220