65878a
From 354d8234f177bca65eddf1451b180772cdbf7611 Mon Sep 17 00:00:00 2001
65878a
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
65878a
Date: Mon, 18 Nov 2013 13:42:57 -0500
65878a
Subject: [PATCH] localed: match converted keymaps before legacy
65878a
65878a
Before, X11 keymap fr-pc105-oss would be converted to fr,
65878a
even though fr-oss exists. Now, if
65878a
/usr/lib/kbd/keymaps/xkb/<layout>[-<variant>].map[.gz] exists,
65878a
<layout>[-<variant>] will be used as the console keymap,
65878a
falling back to the legacy mappings otherwise.
65878a
65878a
% sudo localectl set-x11-keymap pl pc105
65878a
% localectl
65878a
   System Locale: LANG=en_US.UTF-8
65878a
       VC Keymap: pl                      (was pl2 before)
65878a
      X11 Layout: pl
65878a
       X11 Model: pc105
65878a
% sudo localectl set-x11-keymap fr pc105 oss
65878a
% localectl
65878a
   System Locale: LANG=en_US.UTF-8
65878a
       VC Keymap: fr-oss                  (was fr before)
65878a
      X11 Layout: fr
65878a
       X11 Model: pc105
65878a
     X11 Variant: oss
65878a
% sudo localectl set-x11-keymap fr pc105
65878a
% localectl
65878a
   System Locale: LANG=en_US.UTF-8
65878a
       VC Keymap: fr
65878a
      X11 Layout: fr
65878a
       X11 Model: pc105
65878a
% sudo localectl set-x11-keymap gb
65878a
% localectl
65878a
   System Locale: LANG=en_US.UTF-8
65878a
       VC Keymap: gb                     (was uk before)
65878a
      X11 Layout: gb
65878a
65878a
Conflicts:
65878a
	src/locale/localed.c
65878a
	src/shared/def.h
65878a
---
65878a
 src/locale/localectl.c |   8 +--
65878a
 src/locale/localed.c   | 189 ++++++++++++++++++++++++++++---------------------
65878a
 src/shared/def.h       |  13 ++++
65878a
 3 files changed, 126 insertions(+), 84 deletions(-)
65878a
65878a
diff --git a/src/locale/localectl.c b/src/locale/localectl.c
65878a
index 8259c0a..d3c6152 100644
65878a
--- a/src/locale/localectl.c
65878a
+++ b/src/locale/localectl.c
65878a
@@ -38,6 +38,7 @@
65878a
 #include "set.h"
65878a
 #include "path-util.h"
65878a
 #include "utf8.h"
65878a
+#include "def.h"
65878a
 
65878a
 static bool arg_no_pager = false;
65878a
 static enum transport {
65878a
@@ -533,15 +534,14 @@ static int nftw_cb(
65878a
 
65878a
 static int list_vconsole_keymaps(DBusConnection *bus, char **args, unsigned n) {
65878a
         _cleanup_strv_free_ char **l = NULL;
65878a
+        const char *dir;
65878a
 
65878a
         keymaps = set_new(string_hash_func, string_compare_func);
65878a
         if (!keymaps)
65878a
                 return log_oom();
65878a
 
65878a
-        nftw("/usr/share/keymaps/", nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
65878a
-        nftw("/usr/share/kbd/keymaps/", nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
65878a
-        nftw("/usr/lib/kbd/keymaps/", nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
65878a
-        nftw("/lib/kbd/keymaps/", nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
65878a
+        NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS)
65878a
+                nftw(dir, nftw_cb, 20, FTW_MOUNT|FTW_PHYS);
65878a
 
65878a
         l = set_get_strv(keymaps);
65878a
         if (!l) {
65878a
diff --git a/src/locale/localed.c b/src/locale/localed.c
65878a
index e160c04..b9b98f4 100644
65878a
--- a/src/locale/localed.c
65878a
+++ b/src/locale/localed.c
65878a
@@ -792,105 +792,135 @@ static int convert_vconsole_to_x11(DBusConnection *connection) {
65878a
         return 0;
65878a
 }
65878a
 
65878a
-static int convert_x11_to_vconsole(DBusConnection *connection) {
65878a
-        bool modified = false;
65878a
+static int find_converted_keymap(char **new_keymap) {
65878a
+        const char *dir;
65878a
+        _cleanup_free_ char *n;
65878a
+
65878a
+        if (state.x11_variant)
65878a
+                n = strjoin(state.x11_layout, "-", state.x11_variant, NULL);
65878a
+        else
65878a
+                n = strdup(state.x11_layout);
65878a
+        if (!n)
65878a
+                return -ENOMEM;
65878a
 
65878a
-        assert(connection);
65878a
+        NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
65878a
+                _cleanup_free_ char *p = NULL, *pz = NULL;
65878a
 
65878a
-        if (isempty(state.x11_layout)) {
65878a
+                p = strjoin(dir, "xkb/", n, ".map", NULL);
65878a
+                pz = strjoin(dir, "xkb/", n, ".map.gz", NULL);
65878a
+                if (!p || !pz)
65878a
+                        return -ENOMEM;
65878a
 
65878a
-                modified =
65878a
-                        !isempty(state.vc_keymap) ||
65878a
-                        !isempty(state.vc_keymap_toggle);
65878a
+                if (access(p, F_OK) == 0 || access(pz, F_OK) == 0) {
65878a
+                        *new_keymap = n;
65878a
+                        n = NULL;
65878a
+                        return 1;
65878a
+                }
65878a
+        }
65878a
 
65878a
-                free_data_x11();
65878a
-        } else {
65878a
-                FILE *f;
65878a
-                unsigned n = 0;
65878a
-                unsigned best_matching = 0;
65878a
-                char *new_keymap = NULL;
65878a
+        return 0;
65878a
+}
65878a
 
65878a
-                f = fopen(SYSTEMD_KBD_MODEL_MAP, "re");
65878a
-                if (!f)
65878a
-                        return -errno;
65878a
+static int find_legacy_keymap(char **new_keymap) {
65878a
+        _cleanup_fclose_ FILE *f;
65878a
+        unsigned n = 0;
65878a
+        unsigned best_matching = 0;
65878a
 
65878a
-                for (;;) {
65878a
-                        char **a;
65878a
-                        unsigned matching = 0;
65878a
-                        int r;
65878a
 
65878a
-                        r = read_next_mapping(f, &n, &a);
65878a
-                        if (r < 0) {
65878a
-                                fclose(f);
65878a
-                                return r;
65878a
-                        }
65878a
+        f = fopen(SYSTEMD_KBD_MODEL_MAP, "re");
65878a
+        if (!f)
65878a
+                return -errno;
65878a
 
65878a
-                        if (r == 0)
65878a
-                                break;
65878a
+        for (;;) {
65878a
+                _cleanup_strv_free_ char **a = NULL;
65878a
+                unsigned matching = 0;
65878a
+                int r;
65878a
 
65878a
-                        /* Determine how well matching this entry is */
65878a
-                        if (streq_ptr(state.x11_layout, a[1]))
65878a
-                                /* If we got an exact match, this is best */
65878a
-                                matching = 10;
65878a
-                        else {
65878a
-                                size_t x;
65878a
-
65878a
-                                x = strcspn(state.x11_layout, ",");
65878a
-
65878a
-                                /* We have multiple X layouts, look
65878a
-                                 * for an entry that matches our key
65878a
-                                 * with the everything but the first
65878a
-                                 * layout stripped off. */
65878a
-                                if (x > 0 &&
65878a
-                                    strlen(a[1]) == x &&
65878a
-                                    strneq(state.x11_layout, a[1], x))
65878a
-                                        matching = 5;
65878a
-                                else  {
65878a
-                                        size_t w;
65878a
-
65878a
-                                        /* If that didn't work, strip
65878a
-                                         * off the other layouts from
65878a
-                                         * the entry, too */
65878a
-
65878a
-                                        w = strcspn(a[1], ",");
65878a
-
65878a
-                                        if (x > 0 && x == w &&
65878a
-                                            memcmp(state.x11_layout, a[1], x) == 0)
65878a
-                                                matching = 1;
65878a
-                                }
65878a
+                r = read_next_mapping(f, &n, &a);
65878a
+                if (r < 0)
65878a
+                        return r;
65878a
+                if (r == 0)
65878a
+                        break;
65878a
+
65878a
+                /* Determine how well matching this entry is */
65878a
+                if (streq_ptr(state.x11_layout, a[1]))
65878a
+                        /* If we got an exact match, this is best */
65878a
+                        matching = 10;
65878a
+                else {
65878a
+                        size_t x;
65878a
+
65878a
+                        x = strcspn(state.x11_layout, ",");
65878a
+
65878a
+                        /* We have multiple X layouts, look for an
65878a
+                         * entry that matches our key with everything
65878a
+                         * but the first layout stripped off. */
65878a
+                        if (x > 0 &&
65878a
+                            strlen(a[1]) == x &&
65878a
+                            strneq(state.x11_layout, a[1], x))
65878a
+                                matching = 5;
65878a
+                        else  {
65878a
+                                size_t w;
65878a
+
65878a
+                                /* If that didn't work, strip off the
65878a
+                                 * other layouts from the entry, too */
65878a
+                                w = strcspn(a[1], ",");
65878a
+
65878a
+                                if (x > 0 && x == w &&
65878a
+                                    memcmp(state.x11_layout, a[1], x) == 0)
65878a
+                                        matching = 1;
65878a
                         }
65878a
+                }
65878a
+
65878a
+                if (matching > 0 &&
65878a
+                    streq_ptr(state.x11_model, a[2])) {
65878a
+                        matching++;
65878a
 
65878a
-                        if (matching > 0 &&
65878a
-                            streq_ptr(state.x11_model, a[2])) {
65878a
+                        if (streq_ptr(state.x11_variant, a[3])) {
65878a
                                 matching++;
65878a
 
65878a
-                                if (streq_ptr(state.x11_variant, a[3])) {
65878a
+                                if (streq_ptr(state.x11_options, a[4]))
65878a
                                         matching++;
65878a
-
65878a
-                                        if (streq_ptr(state.x11_options, a[4]))
65878a
-                                                matching++;
65878a
-                                }
65878a
                         }
65878a
+                }
65878a
 
65878a
-                        /* The best matching entry so far, then let's
65878a
-                         * save that */
65878a
-                        if (matching > best_matching) {
65878a
-                                best_matching = matching;
65878a
+                /* The best matching entry so far, then let's save that */
65878a
+                if (matching > best_matching) {
65878a
+                        best_matching = matching;
65878a
 
65878a
-                                free(new_keymap);
65878a
-                                new_keymap = strdup(a[0]);
65878a
+                        free(*new_keymap);
65878a
+                        *new_keymap = strdup(a[0]);
65878a
+                        if (!*new_keymap)
65878a
+                                return -ENOMEM;
65878a
+                }
65878a
+        }
65878a
 
65878a
-                                if (!new_keymap) {
65878a
-                                        strv_free(a);
65878a
-                                        fclose(f);
65878a
-                                        return -ENOMEM;
65878a
-                                }
65878a
-                        }
65878a
+        return 0;
65878a
+}
65878a
 
65878a
-                        strv_free(a);
65878a
-                }
65878a
+static int convert_x11_to_vconsole(DBusConnection *connection) {
65878a
+        bool modified = false;
65878a
+        int r;
65878a
 
65878a
-                fclose(f);
65878a
+        assert(connection);
65878a
+
65878a
+        if (isempty(state.x11_layout)) {
65878a
+
65878a
+                modified =
65878a
+                        !isempty(state.vc_keymap) ||
65878a
+                        !isempty(state.vc_keymap_toggle);
65878a
+
65878a
+                free_data_x11();
65878a
+        } else {
65878a
+                char *new_keymap = NULL;
65878a
+
65878a
+                r = find_converted_keymap(&new_keymap);
65878a
+                if (r < 0)
65878a
+                        return r;
65878a
+                else if (r == 0) {
65878a
+                        r = find_legacy_keymap(&new_keymap);
65878a
+                        if (r < 0)
65878a
+                                return r;
65878a
+                }
65878a
 
65878a
                 if (!streq_ptr(state.vc_keymap, new_keymap)) {
65878a
                         free(state.vc_keymap);
65878a
@@ -907,7 +937,6 @@ static int convert_x11_to_vconsole(DBusConnection *connection) {
65878a
         if (modified) {
65878a
                 dbus_bool_t b;
65878a
                 DBusMessage *changed;
65878a
-                int r;
65878a
 
65878a
                 r = write_data_vconsole();
65878a
                 if (r < 0)
65878a
diff --git a/src/shared/def.h b/src/shared/def.h
65878a
index e4ef735..58c834c 100644
65878a
--- a/src/shared/def.h
65878a
+++ b/src/shared/def.h
65878a
@@ -41,3 +41,16 @@
65878a
 #define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz"
65878a
 #define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
65878a
 #define LETTERS LOWERCASE_LETTERS UPPERCASE_LETTERS
65878a
+
65878a
+#ifdef HAVE_SPLIT_USR
65878a
+#define KBD_KEYMAP_DIRS                         \
65878a
+        "/usr/share/keymaps/\0"                 \
65878a
+        "/usr/share/kbd/keymaps/\0"             \
65878a
+        "/usr/lib/kbd/keymaps/\0"               \
65878a
+        "/lib/kbd/keymaps/\0"
65878a
+#else
65878a
+#define KBD_KEYMAP_DIRS                         \
65878a
+        "/usr/share/keymaps/\0"                 \
65878a
+        "/usr/share/kbd/keymaps/\0"             \
65878a
+        "/usr/lib/kbd/keymaps/\0"
65878a
+#endif