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