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/limits.h>
36 #include <linux/input.h>
38 const struct key* lookup_key (const char *str, unsigned int len);
40 #include "keys-from-name.h"
41 #include "keys-to-name.h"
43 #define MAX_SCANCODES 1024
45 static int evdev_open(const char *dev)
50 if (strncmp(dev, "/dev", 4) != 0) {
51 snprintf(fn, sizeof(fn), "/dev/%s", dev);
55 if ((fd = open(dev, O_RDWR)) < 0) {
56 fprintf(stderr, "error open('%s'): %m\n", dev);
62 static int evdev_get_keycode(int fd, int scancode, int e)
67 if (ioctl(fd, EVIOCGKEYCODE, codes) < 0) {
68 if (e && errno == EINVAL) {
71 fprintf(stderr, "EVIOCGKEYCODE: %m\n");
78 static int evdev_set_keycode(int fd, int scancode, int keycode)
85 if (ioctl(fd, EVIOCSKEYCODE, codes) < 0) {
86 fprintf(stderr, "EVIOCSKEYCODE: %m\n");
92 static int evdev_driver_version(int fd, char *v, size_t l)
96 if (ioctl(fd, EVIOCGVERSION, &version)) {
97 fprintf(stderr, "EVIOCGVERSION: %m\n");
101 snprintf(v, l, "%i.%i.%i.", version >> 16, (version >> 8) & 0xff, version & 0xff);
105 static int evdev_device_name(int fd, char *n, size_t l)
107 if (ioctl(fd, EVIOCGNAME(l), n) < 0) {
108 fprintf(stderr, "EVIOCGNAME: %m\n");
114 /* Return a lower-case string with KEY_ prefix removed */
115 static const char* format_keyname(const char* key) {
116 static char result[101];
120 for (s = key+4, len = 0; *s && len < 100; ++len, ++s)
121 result[len] = tolower(*s);
126 static int dump_table(int fd) {
127 char version[256], name[256];
128 int scancode, r = -1;
130 if (evdev_driver_version(fd, version, sizeof(version)) < 0)
133 if (evdev_device_name(fd, name, sizeof(name)) < 0)
136 printf("### evdev %s, driver '%s'\n", version, name);
139 for (scancode = 0; scancode < MAX_SCANCODES; scancode++) {
142 if ((keycode = evdev_get_keycode(fd, scancode, 1)) < 0) {
148 if (keycode < KEY_MAX && key_names[keycode])
149 printf("0x%03x %s\n", scancode, format_keyname(key_names[keycode]));
151 printf("0x%03x 0x%03x\n", scancode, keycode);
157 static void set_key(int fd, const char* scancode_str, const char* keyname)
161 char t[105] = "KEY_UNKNOWN";
164 scancode = (unsigned) strtol(scancode_str, &endptr, 0);
165 if (*endptr != '\0') {
166 fprintf(stderr, "ERROR: Invalid scancode\n");
170 snprintf(t, sizeof(t), "KEY_%s", keyname);
172 if (!(k = lookup_key(t, strlen(t)))) {
173 fprintf(stderr, "ERROR: Unknown key name '%s'\n", keyname);
177 if (evdev_set_keycode(fd, scancode, k->id) < 0)
178 fprintf(stderr, "setting scancode 0x%2X to key code %i failed\n",
181 printf("setting scancode 0x%2X to key code %i\n",
185 static int merge_table(int fd, const char *filename) {
190 f = fopen(filename, "r");
199 int scancode, new_keycode, old_keycode;
201 if (!fgets(s, sizeof(s), f))
205 p = s+strspn(s, "\t ");
206 if (*p == '#' || *p == '\n')
209 if (sscanf(p, "%i %i", &scancode, &new_keycode) != 2) {
210 char t[105] = "KEY_UNKNOWN";
213 if (sscanf(p, "%i %100s", &scancode, t+4) != 2) {
214 fprintf(stderr, "WARNING: Parse failure at line %i, ignoring.\n", line);
219 if (!(k = lookup_key(t, strlen(t)))) {
220 fprintf(stderr, "WARNING: Unknown key '%s' at line %i, ignoring.\n", t, line);
229 if ((old_keycode = evdev_get_keycode(fd, scancode, 0)) < 0) {
234 if (evdev_set_keycode(fd, scancode, new_keycode) < 0) {
239 if (new_keycode != old_keycode)
240 fprintf(stderr, "Remapped scancode 0x%02x to 0x%02x (prior: 0x%02x)\n",
241 scancode, new_keycode, old_keycode);
247 static const char* default_keymap_path(const char* path)
249 static char result[PATH_MAX];
251 /* If keymap file is given without a path, assume udev directory; must end with '/' * */
252 if (!strchr(path, '/')) {
253 snprintf(result, sizeof(result), "%s%s", LIBEXECDIR "/keymaps/", path);
259 static void print_key(struct input_event *event)
261 static int cur_scancode = 0;
264 /* save scan code for next EV_KEY event */
265 if (event->type == EV_MSC && event->code == MSC_SCAN)
266 cur_scancode = event->value;
269 if (event->type == EV_KEY && event->value) {
270 keyname = key_names[event->code];
272 printf("scan code: 0x%02X key code: %s\n", cur_scancode,
273 format_keyname(key_names[event->code]));
275 printf("scan code: 0x%02X key code: %03X\n", cur_scancode,
280 static void interactive(int fd)
282 struct input_event ev;
285 /* grab input device */
286 ioctl(fd, EVIOCGRAB, 1);
288 puts("Press ESC to finish");
290 switch (read(fd, &ev, sizeof(ev))) {
300 /* stop on Escape key release */
301 if (ev.type == EV_KEY && ev.code == KEY_ESC && ev.value == 0)
307 /* release input device */
308 ioctl(fd, EVIOCGRAB, 0);
311 static void help(int error)
313 const char* h = "Usage: keymap <event device> [<map file>]\n"
314 " keymap <event device> scancode keyname [...]\n"
315 " keymap -i <event device>\n";
325 int main(int argc, char **argv)
327 static const struct option options[] = {
328 { "help", no_argument, NULL, 'h' },
329 { "interactive", no_argument, NULL, 'i' },
333 int opt_interactive = 0;
339 option = getopt_long(argc, argv, "hi", options, NULL);
358 if ((fd = evdev_open(argv[optind])) < 0)
361 /* one argument (device): dump or interactive */
362 if (argc == optind+1) {
370 /* two arguments (device, mapfile): set map file */
371 if (argc == optind+2) {
372 merge_table(fd, default_keymap_path(argv[optind+1]));
376 /* more arguments (device, scancode/keyname pairs): set keys directly */
377 if ((argc - optind - 1) % 2 == 0) {
378 for (i = optind+1; i < argc; i += 2)
379 set_key(fd, argv[i], argv[i+1]);
383 /* invalid number of arguments */
385 return 1; /* not reached */