2 * keymap - dump keymap of an evdev device or set a new keymap from a file
4 * Based on keyfuzz by Lennart Poettering <mzqrovna@0pointer.net>
5 * Adapted for udev-extras by Martin Pitt <martin.pitt@ubuntu.com>
7 * Copyright (C) 2006, Lennart Poettering
8 * Copyright (C) 2009, Canonical Ltd.
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.
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.
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.
35 #include <sys/ioctl.h>
36 #include <linux/limits.h>
37 #include <linux/input.h>
39 const struct key* lookup_key (const char *str, unsigned int len);
41 #include "keys-from-name.h"
42 #include "keys-to-name.h"
45 #define MAX_SCANCODES 1024
47 static int evdev_open(const char *dev)
52 if (!startswith(dev, "/dev")) {
53 snprintf(fn, sizeof(fn), "/dev/%s", dev);
57 if ((fd = open(dev, O_RDWR)) < 0) {
58 fprintf(stderr, "error open('%s'): %m\n", dev);
64 static int evdev_get_keycode(int fd, int scancode, int e)
69 if (ioctl(fd, EVIOCGKEYCODE, codes) < 0) {
70 if (e && errno == EINVAL) {
73 fprintf(stderr, "EVIOCGKEYCODE: %m\n");
80 static int evdev_set_keycode(int fd, int scancode, int keycode)
87 if (ioctl(fd, EVIOCSKEYCODE, codes) < 0) {
88 fprintf(stderr, "EVIOCSKEYCODE: %m\n");
94 static int evdev_driver_version(int fd, char *v, size_t l)
98 if (ioctl(fd, EVIOCGVERSION, &version)) {
99 fprintf(stderr, "EVIOCGVERSION: %m\n");
103 snprintf(v, l, "%i.%i.%i.", version >> 16, (version >> 8) & 0xff, version & 0xff);
107 static int evdev_device_name(int fd, char *n, size_t l)
109 if (ioctl(fd, EVIOCGNAME(l), n) < 0) {
110 fprintf(stderr, "EVIOCGNAME: %m\n");
116 /* Return a lower-case string with KEY_ prefix removed */
117 static const char* format_keyname(const char* key) {
118 static char result[101];
122 for (s = key+4, len = 0; *s && len < 100; ++len, ++s)
123 result[len] = tolower(*s);
128 static int dump_table(int fd) {
129 char version[256], name[256];
130 int scancode, r = -1;
132 if (evdev_driver_version(fd, version, sizeof(version)) < 0)
135 if (evdev_device_name(fd, name, sizeof(name)) < 0)
138 printf("### evdev %s, driver '%s'\n", version, name);
141 for (scancode = 0; scancode < MAX_SCANCODES; scancode++) {
144 if ((keycode = evdev_get_keycode(fd, scancode, 1)) < 0) {
151 if (keycode < KEY_MAX && key_names[keycode])
152 printf("0x%03x %s\n", scancode, format_keyname(key_names[keycode]));
154 printf("0x%03x 0x%03x\n", scancode, keycode);
160 static void set_key(int fd, const char* scancode_str, const char* keyname)
164 char t[105] = "KEY_UNKNOWN";
167 scancode = (unsigned) strtol(scancode_str, &endptr, 0);
168 if (*endptr != '\0') {
169 fprintf(stderr, "ERROR: Invalid scancode\n");
173 snprintf(t, sizeof(t), "KEY_%s", keyname);
175 if (!(k = lookup_key(t, strlen(t)))) {
176 fprintf(stderr, "ERROR: Unknown key name '%s'\n", keyname);
180 if (evdev_set_keycode(fd, scancode, k->id) < 0)
181 fprintf(stderr, "setting scancode 0x%2X to key code %i failed\n",
184 printf("setting scancode 0x%2X to key code %i\n",
188 static int merge_table(int fd, FILE *f) {
194 int scancode, new_keycode, old_keycode;
196 if (!fgets(s, sizeof(s), f))
200 p = s+strspn(s, "\t ");
201 if (*p == '#' || *p == '\n')
204 if (sscanf(p, "%i %i", &scancode, &new_keycode) != 2) {
205 char t[105] = "KEY_UNKNOWN";
208 if (sscanf(p, "%i %100s", &scancode, t+4) != 2) {
209 fprintf(stderr, "WARNING: Parse failure at line %i, ignoring.\n", line);
214 if (!(k = lookup_key(t, strlen(t)))) {
215 fprintf(stderr, "WARNING: Unknown key '%s' at line %i, ignoring.\n", t, line);
224 if ((old_keycode = evdev_get_keycode(fd, scancode, 0)) < 0) {
229 if (evdev_set_keycode(fd, scancode, new_keycode) < 0) {
234 if (new_keycode != old_keycode)
235 fprintf(stderr, "Remapped scancode 0x%02x to 0x%02x (prior: 0x%02x)\n",
236 scancode, new_keycode, old_keycode);
244 /* read one event; return 1 if valid */
245 static int read_event(int fd, struct input_event* ev)
248 ret = read(fd, ev, sizeof(struct input_event));
254 if (ret != sizeof(struct input_event)) {
255 fprintf(stderr, "did not get enough data for event struct, aborting\n");
262 static void print_key(uint32_t scancode, uint16_t keycode, int has_scan, int has_key)
266 /* ignore key release events */
270 if (has_key == 0 && has_scan != 0) {
271 fprintf(stderr, "got scan code event 0x%02X without a key code event\n",
277 printf("scan code: 0x%02X ", scancode);
279 printf("(no scan code received) ");
281 keyname = key_names[keycode];
283 printf("key code: %s\n", format_keyname(keyname));
285 printf("key code: %03X\n", keycode);
288 static void interactive(int fd)
290 struct input_event ev;
291 uint32_t last_scan = 0;
292 uint16_t last_key = 0;
293 int has_scan; /* boolean */
294 int has_key; /* 0: none, 1: release, 2: press */
296 /* grab input device */
297 ioctl(fd, EVIOCGRAB, 1);
298 puts("Press ESC to finish, or Control-C if this device is not your primary keyboard");
300 has_scan = has_key = 0;
301 while (read_event(fd, &ev)) {
302 /* Drivers usually send the scan code first, then the key code,
303 * then a SYN. Some drivers (like thinkpad_acpi) send the key
304 * code first, and some drivers might not send SYN events, so
305 * keep a robust state machine which can deal with any of those
308 if (ev.type == EV_MSC && ev.code == MSC_SCAN) {
310 fputs("driver did not send SYN event in between key events; previous event:\n",
312 print_key(last_scan, last_key, has_scan, has_key);
316 last_scan = ev.value;
318 /*printf("--- got scan %u; has scan %i key %i\n", last_scan, has_scan, has_key); */
320 else if (ev.type == EV_KEY) {
322 fputs("driver did not send SYN event in between key events; previous event:\n",
324 print_key(last_scan, last_key, has_scan, has_key);
329 has_key = 1 + ev.value;
330 /*printf("--- got key %hu; has scan %i key %i\n", last_key, has_scan, has_key);*/
333 if (ev.code == KEY_ESC && ev.value == 0)
336 else if (ev.type == EV_SYN) {
337 /*printf("--- got SYN; has scan %i key %i\n", has_scan, has_key);*/
338 print_key(last_scan, last_key, has_scan, has_key);
340 has_scan = has_key = 0;
345 /* release input device */
346 ioctl(fd, EVIOCGRAB, 0);
349 static void help(int error)
351 const char* h = "Usage: keymap <event device> [<map file>]\n"
352 " keymap <event device> scancode keyname [...]\n"
353 " keymap -i <event device>\n";
363 int main(int argc, char **argv)
365 static const struct option options[] = {
366 { "help", no_argument, NULL, 'h' },
367 { "interactive", no_argument, NULL, 'i' },
371 int opt_interactive = 0;
377 option = getopt_long(argc, argv, "hi", options, NULL);
396 if ((fd = evdev_open(argv[optind])) < 0)
399 /* one argument (device): dump or interactive */
400 if (argc == optind+1) {
408 /* two arguments (device, mapfile): set map file */
409 if (argc == optind+2) {
410 const char *filearg = argv[optind+1];
411 if (strchr(filearg, '/')) {
412 /* Keymap file argument is a path */
413 FILE *f = fopen(filearg, "re");
419 /* Keymap file argument is a filename */
420 /* Open override file if present, otherwise default file */
421 char keymap_path[PATH_MAX];
423 snprintf(keymap_path, sizeof(keymap_path), "%s%s", SYSCONFDIR "/udev/keymaps/", filearg);
424 f = fopen(keymap_path, "re");
428 snprintf(keymap_path, sizeof(keymap_path), "%s%s", UDEVLIBEXECDIR "/keymaps/", filearg);
429 f = fopen(keymap_path, "re");
439 /* more arguments (device, scancode/keyname pairs): set keys directly */
440 if ((argc - optind - 1) % 2 == 0) {
441 for (i = optind+1; i < argc; i += 2)
442 set_key(fd, argv[i], argv[i+1]);
446 /* invalid number of arguments */
448 return 1; /* not reached */