chiark / gitweb /
eabd1e44d823f768703cc74de0bb2a363b3c3280
[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 <stdint.h>
29 #include <ctype.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <fcntl.h>
34 #include <getopt.h>
35 #include <sys/ioctl.h>
36 #include <linux/limits.h>
37 #include <linux/input.h>
38
39 const struct key* lookup_key (const char *str, unsigned int len);
40
41 #include "keys-from-name.h"
42 #include "keys-to-name.h"
43
44 #define MAX_SCANCODES 1024
45
46 static int evdev_open(const char *dev)
47 {
48         int fd;
49         char fn[PATH_MAX];
50
51         if (strncmp(dev, "/dev", 4) != 0) {
52                 snprintf(fn, sizeof(fn), "/dev/%s", dev);
53                 dev = fn;
54         }
55
56         if ((fd = open(dev, O_RDWR)) < 0) {
57                 fprintf(stderr, "error open('%s'): %m\n", dev);
58                 return -1;
59         }
60         return fd;
61 }
62
63 static int evdev_get_keycode(int fd, int scancode, int e)
64 {
65         int codes[2];
66
67         codes[0] = scancode;
68         if (ioctl(fd, EVIOCGKEYCODE, codes) < 0) {
69                 if (e && errno == EINVAL) {
70                         return -2;
71                 } else {
72                         fprintf(stderr, "EVIOCGKEYCODE: %m\n");
73                         return -1;
74                 }
75         }
76         return codes[1];
77 }
78
79 static int evdev_set_keycode(int fd, int scancode, int keycode)
80 {
81         int codes[2];
82
83         codes[0] = scancode;
84         codes[1] = keycode;
85
86         if (ioctl(fd, EVIOCSKEYCODE, codes) < 0) {
87                 fprintf(stderr, "EVIOCSKEYCODE: %m\n");
88                 return -1;
89         }
90         return 0;
91 }
92
93 static int evdev_driver_version(int fd, char *v, size_t l)
94 {
95         int version;
96
97         if (ioctl(fd, EVIOCGVERSION, &version)) {
98                 fprintf(stderr, "EVIOCGVERSION: %m\n");
99                 return -1;
100         }
101
102         snprintf(v, l, "%i.%i.%i.", version >> 16, (version >> 8) & 0xff, version & 0xff);
103         return 0;
104 }
105
106 static int evdev_device_name(int fd, char *n, size_t l)
107 {
108         if (ioctl(fd, EVIOCGNAME(l), n) < 0) {
109                 fprintf(stderr, "EVIOCGNAME: %m\n");
110                 return -1;
111         }
112         return 0;
113 }
114
115 /* Return a lower-case string with KEY_ prefix removed */
116 static const char* format_keyname(const char* key) {
117         static char result[101];
118         const char* s;
119         int len;
120
121         for (s = key+4, len = 0; *s && len < 100; ++len, ++s)
122                 result[len] = tolower(*s);
123         result[len] = '\0';
124         return result;
125 }
126
127 static int dump_table(int fd) {
128         char version[256], name[256];
129         int scancode, r = -1;
130
131         if (evdev_driver_version(fd, version, sizeof(version)) < 0)
132                 goto fail;
133
134         if (evdev_device_name(fd, name, sizeof(name)) < 0)
135                 goto fail;
136
137         printf("### evdev %s, driver '%s'\n", version, name);
138
139         r = 0;
140         for (scancode = 0; scancode < MAX_SCANCODES; scancode++) {
141                 int keycode;
142
143                 if ((keycode = evdev_get_keycode(fd, scancode, 1)) < 0) {
144                         if (keycode == -2)
145                                 continue;
146                         r = -1;
147                         break;
148                 }
149
150                 if (keycode < KEY_MAX && key_names[keycode])
151                         printf("0x%03x %s\n", scancode, format_keyname(key_names[keycode]));
152                 else
153                         printf("0x%03x 0x%03x\n", scancode, keycode);
154         }
155 fail:
156         return r;
157 }
158
159 static void set_key(int fd, const char* scancode_str, const char* keyname)
160 {
161         unsigned scancode;
162         char *endptr;
163         char t[105] = "KEY_UNKNOWN";
164         const struct key *k;
165
166         scancode = (unsigned) strtol(scancode_str, &endptr, 0);
167         if (*endptr != '\0') {
168                 fprintf(stderr, "ERROR: Invalid scancode\n");
169                 exit(1);
170         }
171
172         snprintf(t, sizeof(t), "KEY_%s", keyname);
173
174         if (!(k = lookup_key(t, strlen(t)))) {
175                 fprintf(stderr, "ERROR: Unknown key name '%s'\n", keyname);
176                 exit(1);
177         }
178
179         if (evdev_set_keycode(fd, scancode, k->id) < 0)
180                 fprintf(stderr, "setting scancode 0x%2X to key code %i failed\n", 
181                         scancode, k->id);
182         else
183                 printf("setting scancode 0x%2X to key code %i\n", 
184                         scancode, k->id);
185 }
186
187 static int merge_table(int fd, const char *filename) {
188         int r = 0;
189         int line = 0;
190         FILE* f;
191
192         f = fopen(filename, "r");
193         if (!f) {
194                 perror(filename);
195                 r = -1;
196                 goto fail;
197         }
198
199         while (!feof(f)) {
200                 char s[256], *p;
201                 int scancode, new_keycode, old_keycode;
202
203                 if (!fgets(s, sizeof(s), f))
204                         break;
205
206                 line++;
207                 p = s+strspn(s, "\t ");
208                 if (*p == '#' || *p == '\n')
209                         continue;
210
211                 if (sscanf(p, "%i %i", &scancode, &new_keycode) != 2) {
212                         char t[105] = "KEY_UNKNOWN";
213                         const struct key *k;
214
215                         if (sscanf(p, "%i %100s", &scancode, t+4) != 2) {
216                                 fprintf(stderr, "WARNING: Parse failure at line %i, ignoring.\n", line);
217                                 r = -1;
218                                 continue;
219                         }
220
221                         if (!(k = lookup_key(t, strlen(t)))) {
222                                 fprintf(stderr, "WARNING: Unknown key '%s' at line %i, ignoring.\n", t, line);
223                                 r = -1;
224                                 continue;
225                         }
226
227                         new_keycode = k->id;
228                 }
229
230
231                 if ((old_keycode = evdev_get_keycode(fd, scancode, 0)) < 0) {
232                         r = -1;
233                         goto fail;
234                 }
235
236                 if (evdev_set_keycode(fd, scancode, new_keycode) < 0) {
237                         r = -1;
238                         goto fail;
239                 }
240
241                 if (new_keycode != old_keycode)
242                         fprintf(stderr, "Remapped scancode 0x%02x to 0x%02x (prior: 0x%02x)\n",
243                                 scancode, new_keycode, old_keycode);
244         }
245 fail:
246         return r;
247 }
248
249 static const char* default_keymap_path(const char* path)
250 {
251         static char result[PATH_MAX];
252
253         /* If keymap file is given without a path, assume udev directory; must end with '/' * */
254         if (!strchr(path, '/')) {
255                 snprintf(result, sizeof(result), "%s%s", LIBEXECDIR "/keymaps/", path);
256                 return result;
257         }
258         return path;
259 }
260
261 /* read one event; return 1 if valid */
262 static int read_event(int fd, struct input_event* ev)
263 {
264         int ret;
265         ret = read(fd, ev, sizeof(struct input_event));
266
267         if (ret < 0) {
268                 perror("read");
269                 return 0;
270         }
271         if (ret != sizeof(struct input_event)) {
272                 fprintf(stderr, "did not get enough data for event struct, aborting\n");
273                 return 0;
274         }
275
276         return 1;
277 }
278
279 static void print_key(uint32_t scancode, uint16_t keycode, int has_scan, int has_key)
280 {
281         const char *keyname;
282
283         /* ignore key release events */
284         if (has_key == 1)
285                 return;
286
287         if (has_key == 0 && has_scan != 0) {
288                 fprintf(stderr, "got scan code event 0x%02X without a key code event\n",
289                         scancode);
290                 return;
291         }
292
293         if (has_scan != 0)
294                 printf("scan code: 0x%02X   ", scancode);
295         else
296                 printf("(no scan code received)  ");
297
298         keyname = key_names[keycode];
299         if (keyname != NULL)
300                 printf("key code: %s\n", format_keyname(keyname));
301         else
302                 printf("key code: %03X\n", keycode);
303 }
304
305 static void interactive(int fd)
306 {
307         struct input_event ev;
308         uint32_t last_scan = 0;
309         uint16_t last_key = 0;
310         int has_scan; /* boolean */
311         int has_key; /* 0: none, 1: release, 2: press */
312
313         /* grab input device */
314         ioctl(fd, EVIOCGRAB, 1);
315         puts("Press ESC to finish, or Control-C if this device is not your primary keyboard");
316
317         has_scan = has_key = 0;
318         while (read_event(fd, &ev)) {
319                 /* Drivers usually send the scan code first, then the key code,
320                  * then a SYN. Some drivers (like thinkpad_acpi) send the key
321                  * code first, and some drivers might not send SYN events, so
322                  * keep a robust state machine which can deal with any of those
323                  */
324
325                 if (ev.type == EV_MSC && ev.code == MSC_SCAN) {
326                         if (has_scan) {
327                                 fputs("driver did not send SYN event in between key events; previous event:\n",
328                                       stderr);
329                                 print_key(last_scan, last_key, has_scan, has_key);
330                                 has_key = 0;
331                         }
332
333                         last_scan = ev.value;
334                         has_scan = 1;
335                         /*printf("--- got scan %u; has scan %i key %i\n", last_scan, has_scan, has_key); */
336                 }
337                 else if (ev.type == EV_KEY) {
338                         if (has_key) {
339                                 fputs("driver did not send SYN event in between key events; previous event:\n",
340                                       stderr);
341                                 print_key(last_scan, last_key, has_scan, has_key);
342                                 has_scan = 0;
343                         }
344
345                         last_key = ev.code;
346                         has_key = 1 + ev.value;
347                         /*printf("--- got key %hu; has scan %i key %i\n", last_key, has_scan, has_key);*/
348
349                         /* Stop on ESC */
350                         if (ev.code == KEY_ESC && ev.value == 0)
351                                 break;
352                 }
353                 else if (ev.type == EV_SYN) {
354                         /*printf("--- got SYN; has scan %i key %i\n", has_scan, has_key);*/
355                         print_key(last_scan, last_key, has_scan, has_key);
356
357                         has_scan = has_key = 0;
358                 }
359
360         }
361
362         /* release input device */
363         ioctl(fd, EVIOCGRAB, 0);
364 }
365
366 static void help(int error)
367 {
368         const char* h = "Usage: keymap <event device> [<map file>]\n"
369                         "       keymap <event device> scancode keyname [...]\n"
370                         "       keymap -i <event device>\n";
371         if (error) {
372                 fputs(h, stderr);
373                 exit(2);
374         } else {
375                 fputs(h, stdout);
376                 exit(0);
377         }
378 }
379
380 int main(int argc, char **argv)
381 {
382         static const struct option options[] = {
383                 { "help", no_argument, NULL, 'h' },
384                 { "interactive", no_argument, NULL, 'i' },
385                 {}
386         };
387         int fd = -1;
388         int opt_interactive = 0;
389         int i;
390
391         while (1) {
392                 int option;
393
394                 option = getopt_long(argc, argv, "hi", options, NULL);
395                 if (option == -1)
396                         break;
397
398                 switch (option) {
399                 case 'h':
400                         help(0);
401
402                 case 'i':
403                         opt_interactive = 1;
404                         break;
405                 default:
406                         return 1;
407                 }
408         }
409
410         if (argc < optind+1)
411                 help (1);
412
413         if ((fd = evdev_open(argv[optind])) < 0)
414                 return 3;
415
416         /* one argument (device): dump or interactive */
417         if (argc == optind+1) {
418                 if (opt_interactive)
419                         interactive(fd);
420                 else
421                         dump_table(fd);
422                 return 0;
423         }
424
425         /* two arguments (device, mapfile): set map file */
426         if (argc == optind+2) {
427                 merge_table(fd, default_keymap_path(argv[optind+1]));
428                 return 0;
429         }
430
431         /* more arguments (device, scancode/keyname pairs): set keys directly */
432         if ((argc - optind - 1) % 2 == 0) {
433                 for (i = optind+1; i < argc; i += 2)
434                         set_key(fd, argv[i], argv[i+1]);        
435                 return 0;
436         }
437
438         /* invalid number of arguments */
439         help(1);
440         return 1; /* not reached */
441 }