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 void set_key(int fd, const char* scancode_str, const char* keyname)
160 char t[105] = "KEY_UNKNOWN";
163 scancode = (unsigned) strtol(scancode_str, &endptr, 0);
164 if (*endptr != '\0') {
165 fprintf(stderr, "ERROR: Invalid scancode\n");
169 snprintf(t, sizeof(t), "KEY_%s", keyname);
171 if (!(k = lookup_key(t, strlen(t)))) {
172 fprintf(stderr, "ERROR: Unknown key name '%s'\n", keyname);
176 if (evdev_set_keycode(fd, scancode, k->id) < 0)
177 fprintf(stderr, "setting scancode 0x%2X to key code %i failed\n",
180 printf("setting scancode 0x%2X to key code %i\n",
184 static int merge_table(int fd, const char *filename) {
189 f = fopen(filename, "r");
198 int scancode, new_keycode, old_keycode;
200 if (!fgets(s, sizeof(s), f))
204 p = s+strspn(s, "\t ");
205 if (*p == '#' || *p == '\n')
208 if (sscanf(p, "%i %i", &scancode, &new_keycode) != 2) {
209 char t[105] = "KEY_UNKNOWN";
212 if (sscanf(p, "%i %100s", &scancode, t+4) != 2) {
213 fprintf(stderr, "WARNING: Parse failure at line %i, ignoring.\n", line);
218 if (!(k = lookup_key(t, strlen(t)))) {
219 fprintf(stderr, "WARNING: Unknown key '%s' at line %i, ignoring.\n", t, line);
228 if ((old_keycode = evdev_get_keycode(fd, scancode, 0)) < 0) {
233 if (evdev_set_keycode(fd, scancode, new_keycode) < 0) {
238 if (new_keycode != old_keycode)
239 fprintf(stderr, "Remapped scancode 0x%02x to 0x%02x (prior: 0x%02x)\n",
240 scancode, new_keycode, old_keycode);
246 static const char* default_keymap_path(const char* path)
248 static char result[PATH_MAX];
250 /* If keymap file is given without a path, assume udev directory; must end with '/' * */
251 if (!strchr(path, '/')) {
252 snprintf(result, sizeof(result), "%s%s", LIBEXECDIR "/keymaps/", path);
258 static void print_key(struct input_event *event)
260 static int cur_scancode = 0;
263 /* save scan code for next EV_KEY event */
264 if (event->type == EV_MSC && event->code == MSC_SCAN)
265 cur_scancode = event->value;
268 if (event->type == EV_KEY && event->value) {
269 keyname = key_names[event->code];
271 printf("scan code: 0x%02X key code: %s\n", cur_scancode,
272 format_keyname(key_names[event->code]));
274 printf("scan code: 0x%02X key code: %03X\n", cur_scancode,
279 static void interactive(int fd)
281 struct input_event ev;
284 /* grab input device */
285 ioctl(fd, EVIOCGRAB, 1);
287 puts("Press ESC to finish");
289 switch (read(fd, &ev, sizeof(ev))) {
299 /* stop on Escape key release */
300 if (ev.type == EV_KEY && ev.code == KEY_ESC && ev.value == 0)
306 /* release input device */
307 ioctl(fd, EVIOCGRAB, 0);
310 static void help(int error)
312 const char* h = "Usage: keymap <event device> [<map file>]\n"
313 " keymap <event device> scancode keyname [...]\n"
314 " keymap -i <event device>\n";
324 int main(int argc, char **argv)
326 static const struct option options[] = {
327 { "help", no_argument, NULL, 'h' },
328 { "interactive", no_argument, NULL, 'i' },
332 int opt_interactive = 0;
338 option = getopt_long(argc, argv, "hi", options, NULL);
357 if ((fd = evdev_open(argv[optind])) < 0)
360 /* one argument (device): dump or interactive */
361 if (argc == optind+1) {
369 /* two arguments (device, mapfile): set map file */
370 if (argc == optind+2) {
371 merge_table(fd, default_keymap_path(argv[optind+1]));
375 /* more arguments (device, scancode/keyname pairs): set keys directly */
376 if ((argc - optind - 1) % 2 == 0) {
377 for (i = optind+1; i < argc; i += 2)
378 set_key(fd, argv[i], argv[i+1]);
382 /* invalid number of arguments */
384 return 1; /* not reached */