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.
34 #include <sys/ioctl.h>
35 #include <linux/input.h>
37 const struct key* lookup_key (const char *str, unsigned int len);
39 #include "keys-from-name.h"
40 #include "keys-to-name.h"
42 #define MAX_SCANCODES 1024
44 static int evdev_open(const char *dev)
49 if (strncmp(dev, "/dev", 4) != 0) {
50 snprintf(fn, sizeof(fn), "/dev/%s", dev);
54 if ((fd = open(dev, O_RDWR)) < 0) {
55 fprintf(stderr, "error open('%s'): %m\n", dev);
61 static int evdev_get_keycode(int fd, int scancode, int e)
66 if (ioctl(fd, EVIOCGKEYCODE, codes) < 0) {
67 if (e && errno == EINVAL) {
70 fprintf(stderr, "EVIOCGKEYCODE: %m\n");
77 static int evdev_set_keycode(int fd, int scancode, int keycode)
84 if (ioctl(fd, EVIOCSKEYCODE, codes) < 0) {
85 fprintf(stderr, "EVIOCSKEYCODE: %m\n");
91 static int evdev_driver_version(int fd, char *v, size_t l)
95 if (ioctl(fd, EVIOCGVERSION, &version)) {
96 fprintf(stderr, "EVIOCGVERSION: %m\n");
100 snprintf(v, l, "%i.%i.%i.", version >> 16, (version >> 8) & 0xff, version & 0xff);
104 static int evdev_device_name(int fd, char *n, size_t l)
106 if (ioctl(fd, EVIOCGNAME(l), n) < 0) {
107 fprintf(stderr, "EVIOCGNAME: %m\n");
113 /* Return a lower-case string with KEY_ prefix removed */
114 static const char* format_keyname(const char* key) {
115 static char result[101];
119 for (s = key+4, len = 0; *s && len < 100; ++len, ++s)
120 result[len] = tolower(*s);
125 static int dump_table(int fd) {
126 char version[256], name[256];
127 int scancode, r = -1;
129 if (evdev_driver_version(fd, version, sizeof(version)) < 0)
132 if (evdev_device_name(fd, name, sizeof(name)) < 0)
135 printf("### evdev %s, driver '%s'\n", version, name);
138 for (scancode = 0; scancode < MAX_SCANCODES; scancode++) {
141 if ((keycode = evdev_get_keycode(fd, scancode, 1)) < 0) {
147 if (keycode < KEY_MAX && key_names[keycode])
148 printf("0x%03x %s\n", scancode, format_keyname(key_names[keycode]));
150 printf("0x%03x 0x%03x\n", scancode, keycode);
156 static int merge_table(int fd, const char *filename) {
161 f = fopen(filename, "r");
170 int scancode, new_keycode, old_keycode;
172 if (!fgets(s, sizeof(s), f))
176 p = s+strspn(s, "\t ");
177 if (*p == '#' || *p == '\n')
180 if (sscanf(p, "%i %i", &scancode, &new_keycode) != 2) {
181 char t[105] = "KEY_UNKNOWN";
184 if (sscanf(p, "%i %100s", &scancode, t+4) != 2) {
185 fprintf(stderr, "WARNING: Parse failure at line %i, ignoring.\n", line);
190 if (!(k = lookup_key(t, strlen(t)))) {
191 fprintf(stderr, "WARNING: Unknown key '%s' at line %i, ignoring.\n", t, line);
200 if ((old_keycode = evdev_get_keycode(fd, scancode, 0)) < 0) {
205 if (evdev_set_keycode(fd, scancode, new_keycode) < 0) {
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);
218 static const char* default_keymap_path(const char* path)
220 static char result[PATH_MAX];
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);
230 static void print_key(struct input_event *event)
232 static int cur_scancode = 0;
234 /* save scan code for next EV_KEY event */
235 if (event->type == EV_MSC && event->code == MSC_SCAN)
236 cur_scancode = event->value;
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]));
244 static void interactive(int fd)
246 struct input_event ev;
249 /* grab input device */
250 ioctl(fd, EVIOCGRAB, 1);
252 puts("Press ESC to finish");
254 switch (read(fd, &ev, sizeof(ev))) {
264 /* stop on Escape key release */
265 if (ev.type == EV_KEY && ev.code == KEY_ESC && ev.value == 0)
271 /* release input device */
272 ioctl(fd, EVIOCGRAB, 0);
275 int main(int argc, char **argv)
277 static const struct option options[] = {
278 { "help", no_argument, NULL, 'h' },
279 { "interactive", no_argument, NULL, 'i' },
283 int opt_interactive = 0;
288 option = getopt_long(argc, argv, "hi", options, NULL);
294 printf("Usage: keymap <event device> [<map file>]\n\n");
305 if (argc < optind+1 || argc > optind+2) {
306 fprintf(stderr, "Usage: keymap <event device> [<map file>]\n\n");
310 if ((fd = evdev_open(argv[optind])) < 0)
313 if (argc == optind+2)
314 merge_table(fd, default_keymap_path(argv[optind+1]));