ryantimwilson / rpms / systemd

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