naccyde / rpms / systemd

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