chiark / gitweb /
localed: match converted keymaps before legacy
[elogind.git] / src / locale / localed.c
index 5275ef3e5ba7b95dead1193f8d30e477ba3f2e96..1248f20c9be9a67a6660ee691039888bfb02679b 100644 (file)
@@ -633,8 +633,114 @@ static int vconsole_convert_to_x11(Context *c, sd_bus *bus) {
         return 0;
 }
 
+static int find_converted_keymap(Context *c, char **new_keymap) {
+        const char *dir;
+        _cleanup_free_ char *n;
+
+        if (c->x11_variant)
+                n = strjoin(c->x11_layout, "-", c->x11_variant, NULL);
+        else
+                n = strdup(c->x11_layout);
+        if (!n)
+                return -ENOMEM;
+
+        NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
+                _cleanup_free_ char *p = NULL, *pz = NULL;
+
+                p = strjoin(dir, "xkb/", n, ".map", NULL);
+                pz = strjoin(dir, "xkb/", n, ".map.gz", NULL);
+                if (!p || !pz)
+                        return -ENOMEM;
+
+                if (access(p, F_OK) == 0 || access(pz, F_OK) == 0) {
+                        *new_keymap = n;
+                        n = NULL;
+                        return 1;
+                }
+        }
+
+        return 0;
+}
+
+static int find_legacy_keymap(Context *c, char **new_keymap) {
+        _cleanup_fclose_ FILE *f;
+        unsigned n = 0;
+        unsigned best_matching = 0;
+
+
+        f = fopen(SYSTEMD_KBD_MODEL_MAP, "re");
+        if (!f)
+                return -errno;
+
+        for (;;) {
+                _cleanup_strv_free_ char **a = NULL;
+                unsigned matching = 0;
+                int r;
+
+                r = read_next_mapping(f, &n, &a);
+                if (r < 0)
+                        return r;
+                if (r == 0)
+                        break;
+
+                /* Determine how well matching this entry is */
+                if (streq_ptr(c->x11_layout, a[1]))
+                        /* If we got an exact match, this is best */
+                        matching = 10;
+                else {
+                        size_t x;
+
+                        x = strcspn(c->x11_layout, ",");
+
+                        /* We have multiple X layouts, look for an
+                         * entry that matches our key with everything
+                         * but the first layout stripped off. */
+                        if (x > 0 &&
+                            strlen(a[1]) == x &&
+                            strneq(c->x11_layout, a[1], x))
+                                matching = 5;
+                        else  {
+                                size_t w;
+
+                                /* If that didn't work, strip off the
+                                 * other layouts from the entry, too */
+                                w = strcspn(a[1], ",");
+
+                                if (x > 0 && x == w &&
+                                    memcmp(c->x11_layout, a[1], x) == 0)
+                                        matching = 1;
+                        }
+                }
+
+                if (matching > 0 &&
+                    streq_ptr(c->x11_model, a[2])) {
+                        matching++;
+
+                        if (streq_ptr(c->x11_variant, a[3])) {
+                                matching++;
+
+                                if (streq_ptr(c->x11_options, a[4]))
+                                        matching++;
+                        }
+                }
+
+                /* The best matching entry so far, then let's save that */
+                if (matching > best_matching) {
+                        best_matching = matching;
+
+                        free(*new_keymap);
+                        *new_keymap = strdup(a[0]);
+                        if (!*new_keymap)
+                                return -ENOMEM;
+                }
+        }
+
+        return 0;
+}
+
 static int x11_convert_to_vconsole(Context *c, sd_bus *bus) {
         bool modified = false;
+        int r;
 
         assert(bus);
 
@@ -646,79 +752,15 @@ static int x11_convert_to_vconsole(Context *c, sd_bus *bus) {
 
                 context_free_x11(c);
         } else {
-                _cleanup_fclose_ FILE *f;
-                unsigned n = 0;
-                unsigned best_matching = 0;
                 char *new_keymap = NULL;
 
-                f = fopen(SYSTEMD_KBD_MODEL_MAP, "re");
-                if (!f)
-                        return -errno;
-
-                for (;;) {
-                        _cleanup_strv_free_ char **a = NULL;
-                        unsigned matching = 0;
-                        int r;
-
-                        r = read_next_mapping(f, &n, &a);
+                r = find_converted_keymap(c, &new_keymap);
+                if (r < 0)
+                        return r;
+                else if (r == 0) {
+                        r = find_legacy_keymap(c, &new_keymap);
                         if (r < 0)
                                 return r;
-                        if (r == 0)
-                                break;
-
-                        /* Determine how well matching this entry is */
-                        if (streq_ptr(c->x11_layout, a[1]))
-                                /* If we got an exact match, this is best */
-                                matching = 10;
-                        else {
-                                size_t x;
-
-                                x = strcspn(c->x11_layout, ",");
-
-                                /* We have multiple X layouts, look
-                                 * for an entry that matches our key
-                                 * with the everything but the first
-                                 * layout stripped off. */
-                                if (x > 0 &&
-                                    strlen(a[1]) == x &&
-                                    strneq(c->x11_layout, a[1], x))
-                                        matching = 5;
-                                else  {
-                                        size_t w;
-
-                                        /* If that didn't work, strip
-                                         * off the other layouts from
-                                         * the entry, too */
-                                        w = strcspn(a[1], ",");
-
-                                        if (x > 0 && x == w &&
-                                            memcmp(c->x11_layout, a[1], x) == 0)
-                                                matching = 1;
-                                }
-                        }
-
-                        if (matching > 0 &&
-                            streq_ptr(c->x11_model, a[2])) {
-                                matching++;
-
-                                if (streq_ptr(c->x11_variant, a[3])) {
-                                        matching++;
-
-                                        if (streq_ptr(c->x11_options, a[4]))
-                                                matching++;
-                                }
-                        }
-
-                        /* The best matching entry so far, then let's
-                         * save that */
-                        if (matching > best_matching) {
-                                best_matching = matching;
-
-                                free(new_keymap);
-                                new_keymap = strdup(a[0]);
-                                if (!new_keymap)
-                                        return -ENOMEM;
-                        }
                 }
 
                 if (!streq_ptr(c->vc_keymap, new_keymap)) {
@@ -730,8 +772,6 @@ static int x11_convert_to_vconsole(Context *c, sd_bus *bus) {
         }
 
         if (modified) {
-                int r;
-
                 r = vconsole_write_data(c);
                 if (r < 0)
                         log_error("Failed to set virtual console keymap: %s", strerror(-r));