chiark / gitweb /
extras/keymap: add Everex Stepnote XT5000T
[elogind.git] / extras / keymap / keymap.c
1 /*
2  * keymap - dump keymap of an evdev device or set a new keymap from a file
3  *
4  * Based on keyfuzz by Lennart Poettering <mzqrovna@0pointer.net>
5  * Adapted for udev-extras by Martin Pitt <martin.pitt@ubuntu.com>
6  *
7  * Copyright (C) 2006, Lennart Poettering
8  * Copyright (C) 2009, Canonical Ltd.
9  *
10  * keymap is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * keymap is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with keymap; if not, write to the Free Software Foundation,
22  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <fcntl.h>
33 #include <getopt.h>
34 #include <sys/ioctl.h>
35 #include <linux/input.h>
36
37 const struct key* lookup_key (const char *str, unsigned int len);
38
39 #include "keys-from-name.h"
40 #include "keys-to-name.h"
41
42 #define MAX_SCANCODES 1024
43
44 static int evdev_open(const char *dev)
45 {
46         int fd;
47         char fn[PATH_MAX];
48
49         if (strncmp(dev, "/dev", 4) != 0) {
50                 snprintf(fn, sizeof(fn), "/dev/%s", dev);
51                 dev = fn;
52         }
53
54         if ((fd = open(dev, O_RDWR)) < 0) {
55                 fprintf(stderr, "error open('%s'): %m\n", dev);
56                 return -1;
57         }
58         return fd;
59 }
60
61 static int evdev_get_keycode(int fd, int scancode, int e)
62 {
63         int codes[2];
64
65         codes[0] = scancode;
66         if (ioctl(fd, EVIOCGKEYCODE, codes) < 0) {
67                 if (e && errno == EINVAL) {
68                         return -2;
69                 } else {
70                         fprintf(stderr, "EVIOCGKEYCODE: %m\n");
71                         return -1;
72                 }
73         }
74         return codes[1];
75 }
76
77 static int evdev_set_keycode(int fd, int scancode, int keycode)
78 {
79         int codes[2];
80
81         codes[0] = scancode;
82         codes[1] = keycode;
83
84         if (ioctl(fd, EVIOCSKEYCODE, codes) < 0) {
85                 fprintf(stderr, "EVIOCSKEYCODE: %m\n");
86                 return -1;
87         }
88         return 0;
89 }
90
91 static int evdev_driver_version(int fd, char *v, size_t l)
92 {
93         int version;
94
95         if (ioctl(fd, EVIOCGVERSION, &version)) {
96                 fprintf(stderr, "EVIOCGVERSION: %m\n");
97                 return -1;
98         }
99
100         snprintf(v, l, "%i.%i.%i.", version >> 16, (version >> 8) & 0xff, version & 0xff);
101         return 0;
102 }
103
104 static int evdev_device_name(int fd, char *n, size_t l)
105 {
106         if (ioctl(fd, EVIOCGNAME(l), n) < 0) {
107                 fprintf(stderr, "EVIOCGNAME: %m\n");
108                 return -1;
109         }
110         return 0;
111 }
112
113 /* Return a lower-case string with KEY_ prefix removed */
114 static const char* format_keyname(const char* key) {
115         static char result[101];
116         const char* s;
117         int len;
118
119         for (s = key+4, len = 0; *s && len < 100; ++len, ++s)
120                 result[len] = tolower(*s);
121         result[len] = '\0';
122         return result;
123 }
124
125 static int dump_table(int fd) {
126         char version[256], name[256];
127         int scancode, r = -1;
128
129         if (evdev_driver_version(fd, version, sizeof(version)) < 0)
130                 goto fail;
131
132         if (evdev_device_name(fd, name, sizeof(name)) < 0)
133                 goto fail;
134
135         printf("### evdev %s, driver '%s'\n", version, name);
136
137         r = 0;
138         for (scancode = 0; scancode < MAX_SCANCODES; scancode++) {
139                 int keycode;
140
141                 if ((keycode = evdev_get_keycode(fd, scancode, 1)) < 0) {
142                         if (keycode != -2)
143                                 r = -1;
144                         break;
145                 }
146
147                 if (keycode < KEY_MAX && key_names[keycode])
148                         printf("0x%03x %s\n", scancode, format_keyname(key_names[keycode]));
149                 else
150                         printf("0x%03x 0x%03x\n", scancode, keycode);
151         }
152 fail:
153         return r;
154 }
155
156 static int merge_table(int fd, const char *filename) {
157         int r = 0;
158         int line = 0;
159         FILE* f;
160
161         f = fopen(filename, "r");
162         if (!f) {
163                 perror(filename);
164                 r = -1;
165                 goto fail;
166         }
167
168         while (!feof(f)) {
169                 char s[256], *p;
170                 int scancode, new_keycode, old_keycode;
171
172                 if (!fgets(s, sizeof(s), f))
173                         break;
174
175                 line++;
176                 p = s+strspn(s, "\t ");
177                 if (*p == '#' || *p == '\n')
178                         continue;
179
180                 if (sscanf(p, "%i %i", &scancode, &new_keycode) != 2) {
181                         char t[105] = "KEY_UNKNOWN";
182                         const struct key *k;
183
184                         if (sscanf(p, "%i %100s", &scancode, t+4) != 2) {
185                                 fprintf(stderr, "WARNING: Parse failure at line %i, ignoring.\n", line);
186                                 r = -1;
187                                 continue;
188                         }
189
190                         if (!(k = lookup_key(t, strlen(t)))) {
191                                 fprintf(stderr, "WARNING: Unknown key '%s' at line %i, ignoring.\n", t, line);
192                                 r = -1;
193                                 continue;
194                         }
195
196                         new_keycode = k->id;
197                 }
198
199
200                 if ((old_keycode = evdev_get_keycode(fd, scancode, 0)) < 0) {
201                         r = -1;
202                         goto fail;
203                 }
204
205                 if (evdev_set_keycode(fd, scancode, new_keycode) < 0) {
206                         r = -1;
207                         goto fail;
208                 }
209
210                 if (new_keycode != old_keycode)
211                         fprintf(stderr, "Remapped scancode 0x%02x to 0x%02x (prior: 0x%02x)\n",
212                                 scancode, new_keycode, old_keycode);
213         }
214 fail:
215         return r;
216 }
217
218 static const char* default_keymap_path(const char* path)
219 {
220         static char result[PATH_MAX];
221
222         /* If keymap file is given without a path, assume udev diretory; must end with '/' * */
223         if (!strchr(path, '/')) {
224                 snprintf(result, sizeof(result), "%s%s", LIBEXECDIR "/keymaps/", path);
225                 return result;
226         }
227         return path;
228 }
229
230 static void print_key(struct input_event *event)
231 {
232         static int cur_scancode = 0;
233
234         /* save scan code for next EV_KEY event */
235         if (event->type == EV_MSC && event->code == MSC_SCAN)
236             cur_scancode = event->value;
237
238         /* key press */
239         if (event->type == EV_KEY && event->value)
240             printf("scan code: 0x%02X   key code: %s\n", cur_scancode,
241                     format_keyname(key_names[event->code]));
242 }
243
244 static void interactive(int fd)
245 {
246         struct input_event ev;
247         int run = 1;
248
249         /* grab input device */
250         ioctl(fd, EVIOCGRAB, 1);
251
252         puts("Press ESC to finish");
253         while (run) {
254                 switch (read(fd, &ev, sizeof(ev))) {
255                 case -1:
256                         perror("read");
257                         run = 0;
258                         break;
259                 case 0:
260                         run = 0;
261                         break;
262                 default:
263                         print_key(&ev);
264                         /* stop on Escape key release */
265                         if (ev.type == EV_KEY && ev.code == KEY_ESC && ev.value == 0)
266                                 run = 0;
267                         break;
268                 }
269         }
270
271         /* release input device */
272         ioctl(fd, EVIOCGRAB, 0);
273 }
274
275 int main(int argc, char **argv)
276 {
277         static const struct option options[] = {
278                 { "help", no_argument, NULL, 'h' },
279                 { "interactive", no_argument, NULL, 'i' },
280                 {}
281         };
282         int fd = -1;
283         int opt_interactive = 0;
284
285         while (1) {
286                 int option;
287
288                 option = getopt_long(argc, argv, "hi", options, NULL);
289                 if (option == -1)
290                         break;
291
292                 switch (option) {
293                 case 'h':
294                         printf("Usage: keymap <event device> [<map file>]\n\n");
295                         return 0;
296
297                 case 'i':
298                         opt_interactive = 1;
299                         break;
300                 default:
301                         return 1;
302                 }
303         }
304
305         if (argc < optind+1 || argc > optind+2) {
306                 fprintf(stderr, "Usage: keymap <event device> [<map file>]\n\n");
307                 return 2;
308         }
309
310         if ((fd = evdev_open(argv[optind])) < 0)
311                 return 3;
312
313         if (argc == optind+2)
314                 merge_table(fd, default_keymap_path(argv[optind+1]));
315         else {
316                 if (opt_interactive)
317                         interactive(fd);
318                 else
319                         dump_table(fd);
320         }
321         return 0;
322 }