[mutter] compositor: use XDG_CONFIG_HOME as initial lookup path for xkb



commit f71238732508d91bdfcb581c84697a516499a1eb
Author: Peter Hutterer <peter hutterer who-t net>
Date:   Thu Nov 14 12:59:25 2019 +1000

    compositor: use XDG_CONFIG_HOME as initial lookup path for xkb
    
    Using XDG_CONFIG_HOME allows users to place their keyboard configuration into
    their home directory and have them loaded automatically.
    libxkbcommon now defaults to XDG_CONFIG_HOME/xkb/ first, see
    https://github.com/xkbcommon/libxkbcommon/pull/117
    
    However - libxkbcommon uses secure_getenv() to obtain XDG_CONFIG_HOME and thus
    fails to load this for the mutter context which has cap_sys_nice.
    We need to manually add that search path as lookup path.
    
    As we can only append paths to libxkbcommon's context, we need to start with
    an empty search path set, add our custom path, then append the default search
    paths.
    
    The net effect is nil where a user doesn't have XDG_CONFIG_HOME/xkb/.
    
    https://gitlab.gnome.org/GNOME/mutter/merge_requests/936

 src/backends/meta-keymap-utils.c          | 56 +++++++++++++++++++++++++++++++
 src/backends/meta-keymap-utils.h          | 28 ++++++++++++++++
 src/backends/native/meta-backend-native.c |  3 +-
 src/backends/native/meta-keymap-native.c  |  3 +-
 src/backends/x11/meta-backend-x11.c       |  3 +-
 src/core/keybindings.c                    |  3 +-
 src/meson.build                           |  2 ++
 7 files changed, 94 insertions(+), 4 deletions(-)
---
diff --git a/src/backends/meta-keymap-utils.c b/src/backends/meta-keymap-utils.c
new file mode 100644
index 0000000000..8b1e754118
--- /dev/null
+++ b/src/backends/meta-keymap-utils.c
@@ -0,0 +1,56 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/*
+ * Utilities for use with libxkbcommon
+ *
+ * Copyright 2019 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "backends/meta-keymap-utils.h"
+
+#include <glib.h>
+#include <limits.h>
+
+struct xkb_context *
+meta_create_xkb_context (void)
+{
+  struct xkb_context *ctx;
+  char xdg[PATH_MAX] = {0};
+  const char *env;
+
+  /*
+   * We can only append search paths in libxkbcommon, so we start with an
+   * emtpy set, then add the XDG dir, then add the default search paths.
+   */
+  ctx = xkb_context_new (XKB_CONTEXT_NO_DEFAULT_INCLUDES);
+
+  env = g_getenv ("XDG_CONFIG_HOME");
+  if (env)
+    {
+      g_snprintf (xdg, sizeof xdg, "%s/xkb", env);
+    }
+  else if ((env = g_getenv ("HOME")))
+    {
+      g_snprintf (xdg, sizeof xdg, "%s/.config/xkb", env);
+    }
+
+  if (env)
+    xkb_context_include_path_append (ctx, xdg);
+  xkb_context_include_path_append_default (ctx);
+
+  return ctx;
+}
diff --git a/src/backends/meta-keymap-utils.h b/src/backends/meta-keymap-utils.h
new file mode 100644
index 0000000000..3806824e48
--- /dev/null
+++ b/src/backends/meta-keymap-utils.h
@@ -0,0 +1,28 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/*
+ * Utilities for use with libxkbcommon
+ *
+ * Copyright 2020 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef META_KEYMAP_UTILS_H
+#define META_KEYMAP_UTILS_H
+
+#include <xkbcommon/xkbcommon.h>
+
+struct xkb_context * meta_create_xkb_context (void);
+
+#endif /* META_KEYMAP_UTILS_H */
diff --git a/src/backends/native/meta-backend-native.c b/src/backends/native/meta-backend-native.c
index 4e97ba7863..12f2812b21 100644
--- a/src/backends/native/meta-backend-native.c
+++ b/src/backends/native/meta-backend-native.c
@@ -42,6 +42,7 @@
 
 #include "backends/meta-cursor-tracker-private.h"
 #include "backends/meta-idle-monitor-private.h"
+#include "backends/meta-keymap-utils.h"
 #include "backends/meta-logical-monitor.h"
 #include "backends/meta-monitor-manager-private.h"
 #include "backends/meta-pointer-constraint.h"
@@ -437,7 +438,7 @@ meta_backend_native_set_keymap (MetaBackend *backend,
   names.variant = variants;
   names.options = options;
 
-  context = xkb_context_new (XKB_CONTEXT_NO_FLAGS);
+  context = meta_create_xkb_context ();
   keymap = xkb_keymap_new_from_names (context, &names, XKB_KEYMAP_COMPILE_NO_FLAGS);
   xkb_context_unref (context);
 
diff --git a/src/backends/native/meta-keymap-native.c b/src/backends/native/meta-keymap-native.c
index 622cfd354f..f55cd059f8 100644
--- a/src/backends/native/meta-keymap-native.c
+++ b/src/backends/native/meta-keymap-native.c
@@ -21,6 +21,7 @@
 
 #include "config.h"
 
+#include "backends/meta-keymap-utils.h"
 #include "backends/native/meta-keymap-native.h"
 #include "backends/native/meta-seat-native.h"
 
@@ -111,7 +112,7 @@ meta_keymap_native_init (MetaKeymapNative *keymap)
   names.variant = option_xkb_variant;
   names.options = option_xkb_options;
 
-  ctx = xkb_context_new (XKB_CONTEXT_NO_FLAGS);
+  ctx = meta_create_xkb_context ();
   g_assert (ctx);
   keymap->keymap = xkb_keymap_new_from_names (ctx, &names, 0);
   xkb_context_unref (ctx);
diff --git a/src/backends/x11/meta-backend-x11.c b/src/backends/x11/meta-backend-x11.c
index 7bb8ff3d1e..69e4ed85f9 100644
--- a/src/backends/x11/meta-backend-x11.c
+++ b/src/backends/x11/meta-backend-x11.c
@@ -43,6 +43,7 @@
 #include <xkbcommon/xkbcommon-x11.h>
 
 #include "backends/meta-idle-monitor-private.h"
+#include "backends/meta-keymap-utils.h"
 #include "backends/meta-stage-private.h"
 #include "backends/x11/meta-clutter-backend-x11.h"
 #include "backends/x11/meta-event-x11.h"
@@ -704,7 +705,7 @@ meta_backend_x11_get_keymap (MetaBackend *backend)
 
   if (priv->keymap == NULL)
     {
-      struct xkb_context *context = xkb_context_new (XKB_CONTEXT_NO_FLAGS);
+      struct xkb_context *context = meta_create_xkb_context ();
       priv->keymap = xkb_x11_keymap_new_from_device (context,
                                                      priv->xcb,
                                                      xkb_x11_get_core_keyboard_device_id (priv->xcb),
diff --git a/src/core/keybindings.c b/src/core/keybindings.c
index 1189e716e2..48d6935774 100644
--- a/src/core/keybindings.c
+++ b/src/core/keybindings.c
@@ -30,6 +30,7 @@
 #include "config.h"
 
 #include "backends/meta-backend-private.h"
+#include "backends/meta-keymap-utils.h"
 #include "backends/meta-logical-monitor.h"
 #include "backends/meta-monitor-manager-private.h"
 #include "backends/x11/meta-backend-x11.h"
@@ -741,7 +742,7 @@ create_us_layout (void)
   names.variant = "";
   names.options = "";
 
-  context = xkb_context_new (XKB_CONTEXT_NO_FLAGS);
+  context = meta_create_xkb_context ();
   keymap = xkb_keymap_new_from_names (context, &names, XKB_KEYMAP_COMPILE_NO_FLAGS);
   xkb_context_unref (context);
 
diff --git a/src/meson.build b/src/meson.build
index 2b5b902173..ba8b63cdf3 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -195,6 +195,8 @@ mutter_sources = [
   'backends/meta-input-mapper-private.h',
   'backends/meta-input-settings.c',
   'backends/meta-input-settings-private.h',
+  'backends/meta-keymap-utils.c',
+  'backends/meta-keymap-utils.h',
   'backends/meta-logical-monitor.c',
   'backends/meta-logical-monitor.h',
   'backends/meta-monitor.c',


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]