From 59ff79c3a307f99f016e33df348ca0727e94afd7 Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Thu, 14 May 2009 23:36:00 +0200 Subject: keymap: add --interactive mode Add -i/--interactive mode to keymap tool, for easy debugging of unknown and wrong keymaps. Use like sudo /lib/udev/keymap -i input/event4 and note down the wrong/missing keys. --- keymap/keymap.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 7 deletions(-) diff --git a/keymap/keymap.c b/keymap/keymap.c index 516dbc0..c48d934 100644 --- a/keymap/keymap.c +++ b/keymap/keymap.c @@ -228,18 +228,65 @@ static const char* default_keymap_path(const char* path) return path; } +static void print_key(struct input_event *event) +{ + static int cur_scancode = 0; + + /* save scan code for next EV_KEY event */ + if (event->type == EV_MSC && event->code == MSC_SCAN) + cur_scancode = event->value; + + /* key press */ + if (event->type == EV_KEY && event->value) + printf("scan code: 0x%02X key code: %s\n", cur_scancode, + format_keyname(key_names[event->code])); +} + +static void interactive(int fd) +{ + struct input_event ev; + int run = 1; + + /* grab input device */ + ioctl(fd, EVIOCGRAB, 1); + + puts("Press ESC to finish"); + while (run) { + switch (read(fd, &ev, sizeof(ev))) { + case -1: + perror("read"); + run = 0; + break; + case 0: + run = 0; + break; + default: + print_key(&ev); + /* stop on Escape key release */ + if (ev.type == EV_KEY && ev.code == KEY_ESC && ev.value == 0) + run = 0; + break; + } + } + + /* release input device */ + ioctl(fd, EVIOCGRAB, 0); +} + int main(int argc, char **argv) { static const struct option options[] = { { "help", no_argument, NULL, 'h' }, + { "interactive", no_argument, NULL, 'i' }, {} }; int fd = -1; + int opt_interactive = 0; while (1) { int option; - option = getopt_long(argc, argv, "h", options, NULL); + option = getopt_long(argc, argv, "hi", options, NULL); if (option == -1) break; @@ -247,22 +294,30 @@ int main(int argc, char **argv) case 'h': printf("Usage: keymap []\n\n"); return 0; + + case 'i': + opt_interactive = 1; + break; default: return 1; } } - if (argc < 2 || argc > 3) { + if (argc < optind+1 || argc > optind+2) { fprintf(stderr, "Usage: keymap []\n\n"); return 2; } - if ((fd = evdev_open(argv[1])) < 0) + if ((fd = evdev_open(argv[optind])) < 0) return 3; - if (argc == 3) - merge_table(fd, default_keymap_path(argv[2])); - else - dump_table(fd); + if (argc == optind+2) + merge_table(fd, default_keymap_path(argv[optind+1])); + else { + if (opt_interactive) + interactive(fd); + else + dump_table(fd); + } return 0; } -- cgit