923a60
From 3b4e03492fd157ab87ea625a8ad1eb91cef7396b Mon Sep 17 00:00:00 2001
923a60
From: Peter Hutterer <peter.hutterer@who-t.net>
923a60
Date: Fri, 20 Mar 2015 13:17:20 +1000
923a60
Subject: [PATCH] udev: builtin-keyboard: move actual key mapping to a helper
923a60
 function
923a60
923a60
No changes in the mapping, but previously we opened the device only on
923a60
successful parsing. Now we open the mapping as soon as we have a value that
923a60
looks interesting. Since errors are supposed to be the exception, not the
923a60
rule, this is probably fine.
923a60
923a60
(cherry picked from commit c9a8e34094733018727677fee44be2c2952224c0)
923a60
923a60
Resolves: #1500119
923a60
---
923a60
 src/udev/udev-builtin-keyboard.c | 58 ++++++++++++++++++--------------
923a60
 1 file changed, 33 insertions(+), 25 deletions(-)
923a60
923a60
diff --git a/src/udev/udev-builtin-keyboard.c b/src/udev/udev-builtin-keyboard.c
923a60
index 515edd45ce..f33401790c 100644
923a60
--- a/src/udev/udev-builtin-keyboard.c
923a60
+++ b/src/udev/udev-builtin-keyboard.c
923a60
@@ -66,12 +66,41 @@ static int install_force_release(struct udev_device *dev, const unsigned *releas
923a60
         return ret;
923a60
 }
923a60
 
923a60
-static int builtin_keyboard(struct udev_device *dev, int argc, char *argv[], bool test) {
923a60
-        struct udev_list_entry *entry;
923a60
+static void map_keycode(int fd, const char *devnode, int scancode, const char *keycode)
923a60
+{
923a60
         struct {
923a60
                 unsigned scan;
923a60
                 unsigned key;
923a60
         } map;
923a60
+        char *endptr;
923a60
+        const struct key *k;
923a60
+        unsigned keycode_num;
923a60
+
923a60
+        /* translate identifier to key code */
923a60
+        k = keyboard_lookup_key(keycode, strlen(keycode));
923a60
+        if (k) {
923a60
+                keycode_num = k->id;
923a60
+        } else {
923a60
+                /* check if it's a numeric code already */
923a60
+                keycode_num = strtoul(keycode, &endptr, 0);
923a60
+                if (endptr[0] !='\0') {
923a60
+                        log_error("Error, unknown key identifier '%s'", keycode);
923a60
+                        return;
923a60
+                }
923a60
+        }
923a60
+
923a60
+        map.scan = scancode;
923a60
+        map.key = keycode_num;
923a60
+
923a60
+        log_debug("keyboard: mapping scan code %d (0x%x) to key code %d (0x%x)",
923a60
+                  map.scan, map.scan, map.key, map.key);
923a60
+
923a60
+        if (ioctl(fd, EVIOCSKEYCODE, &map) < 0)
923a60
+                log_error_errno(errno, "Error calling EVIOCSKEYCODE on device node '%s' (scan code 0x%x, key code %d): %m", devnode, map.scan, map.key);
923a60
+}
923a60
+
923a60
+static int builtin_keyboard(struct udev_device *dev, int argc, char *argv[], bool test) {
923a60
+        struct udev_list_entry *entry;
923a60
         unsigned release[1024];
923a60
         unsigned release_count = 0;
923a60
         _cleanup_close_ int fd = -1;
923a60
@@ -85,10 +114,9 @@ static int builtin_keyboard(struct udev_device *dev, int argc, char *argv[], boo
923a60
 
923a60
         udev_list_entry_foreach(entry, udev_device_get_properties_list_entry(dev)) {
923a60
                 const char *key;
923a60
-                unsigned scancode, keycode_num;
923a60
                 char *endptr;
923a60
+                unsigned scancode;
923a60
                 const char *keycode;
923a60
-                const struct key *k;
923a60
 
923a60
                 key = udev_list_entry_get_name(entry);
923a60
                 if (!startswith(key, "KEYBOARD_KEY_"))
923a60
@@ -115,19 +143,6 @@ static int builtin_keyboard(struct udev_device *dev, int argc, char *argv[], boo
923a60
                                 continue;
923a60
                 }
923a60
 
923a60
-                /* translate identifier to key code */
923a60
-                k = keyboard_lookup_key(keycode, strlen(keycode));
923a60
-                if (k) {
923a60
-                        keycode_num = k->id;
923a60
-                } else {
923a60
-                        /* check if it's a numeric code already */
923a60
-                        keycode_num = strtoul(keycode, &endptr, 0);
923a60
-                        if (endptr[0] !='\0') {
923a60
-                                log_error("Error, unknown key identifier '%s'", keycode);
923a60
-                                continue;
923a60
-                        }
923a60
-                }
923a60
-
923a60
                 if (fd == -1) {
923a60
                         fd = open(node, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
923a60
                         if (fd < 0) {
923a60
@@ -136,14 +151,7 @@ static int builtin_keyboard(struct udev_device *dev, int argc, char *argv[], boo
923a60
                         }
923a60
                 }
923a60
 
923a60
-                map.scan = scancode;
923a60
-                map.key = keycode_num;
923a60
-
923a60
-                log_debug("keyboard: mapping scan code %d (0x%x) to key code %d (0x%x)",
923a60
-                          map.scan, map.scan, map.key, map.key);
923a60
-
923a60
-                if (ioctl(fd, EVIOCSKEYCODE, &map) < 0)
923a60
-                        log_error_errno(errno, "Error calling EVIOCSKEYCODE on device node '%s' (scan code 0x%x, key code %d): %m", node, map.scan, map.key);
923a60
+                map_keycode(fd, node, scancode, keycode);
923a60
         }
923a60
 
923a60
         /* install list of force-release codes */