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