chiark / gitweb /
rules: do not preprocess 80-drivers.rules + 75-probe_mtd.rules
[elogind.git] / extras / keymap / keymap.c
index ed6b69d5f141d4b59c9bae21d473b1feb3864fca..fadd7a31d270043f3b793faf68afb80a05f1a961 100644 (file)
@@ -141,8 +141,9 @@ static int dump_table(int fd) {
                int keycode;
 
                if ((keycode = evdev_get_keycode(fd, scancode, 1)) < 0) {
-                       if (keycode != -2)
-                               r = -1;
+                       if (keycode == -2)
+                               continue;
+                       r = -1;
                        break;
                }
 
@@ -183,17 +184,9 @@ static void set_key(int fd, const char* scancode_str, const char* keyname)
                        scancode, k->id);
 }
 
-static int merge_table(int fd, const char *filename) {
+static int merge_table(int fd, FILE *f) {
        int r = 0;
        int line = 0;
-       FILE* f;
-
-       f = fopen(filename, "r");
-       if (!f) {
-               perror(filename);
-               r = -1;
-               goto fail;
-       }
 
        while (!feof(f)) {
                char s[256], *p;
@@ -242,20 +235,10 @@ static int merge_table(int fd, const char *filename) {
                                scancode, new_keycode, old_keycode);
        }
 fail:
+       fclose(f);
        return r;
 }
 
-static const char* default_keymap_path(const char* path)
-{
-       static char result[PATH_MAX];
-
-       /* If keymap file is given without a path, assume udev directory; must end with '/' * */
-       if (!strchr(path, '/')) {
-               snprintf(result, sizeof(result), "%s%s", LIBEXECDIR "/keymaps/", path);
-               return result;
-       }
-       return path;
-}
 
 /* read one event; return 1 if valid */
 static int read_event(int fd, struct input_event* ev)
@@ -311,7 +294,7 @@ static void interactive(int fd)
 
        /* grab input device */
        ioctl(fd, EVIOCGRAB, 1);
-       puts("Press ESC to finish");
+       puts("Press ESC to finish, or Control-C if this device is not your primary keyboard");
 
        has_scan = has_key = 0;
        while (read_event(fd, &ev)) {
@@ -423,7 +406,31 @@ int main(int argc, char **argv)
 
        /* two arguments (device, mapfile): set map file */
        if (argc == optind+2) {
-               merge_table(fd, default_keymap_path(argv[optind+1]));
+               const char *filearg = argv[optind+1];
+               if (strchr(filearg, '/')) {
+                       /* Keymap file argument is a path */
+                       FILE *f = fopen(filearg, "r");
+                       if (f)
+                               merge_table(fd, f);
+                       else
+                               perror(filearg);
+               } else {
+                       /* Keymap file argument is a filename */
+                       /* Open override file if present, otherwise default file */
+                       char keymap_path[PATH_MAX];
+                       snprintf(keymap_path, sizeof(keymap_path), "%s%s", SYSCONFDIR "/udev/keymaps/", filearg);
+                       FILE *f = fopen(keymap_path, "r");
+                       if (f) {
+                               merge_table(fd, f);
+                       } else {
+                               snprintf(keymap_path, sizeof(keymap_path), "%s%s", LIBEXECDIR "/keymaps/", filearg);
+                               f = fopen(keymap_path, "r");
+                               if (f)
+                                       merge_table(fd, f);
+                               else
+                                       perror(keymap_path);
+                       }
+               }
                return 0;
        }