Commit 17ad0df14ad3414de3d69e75f448a1f4acb637c3

Emmanuel Gil Peyrot 2020-07-16T12:06:49

compose: add xdg base directory support Before reading ~/.XCompose, try to read $XDG_CONFIG_HOME/XCompose (falling back to ~/.config/XCompose). This helps unclutter the home directory of users who want that.

diff --git a/src/compose/paths.c b/src/compose/paths.c
index 19eafa4..aab4507 100644
--- a/src/compose/paths.c
+++ b/src/compose/paths.c
@@ -150,6 +150,23 @@ get_xcomposefile_path(void)
 }
 
 char *
+get_xdg_xcompose_file_path(void)
+{
+    const char *xdg_config_home;
+    const char *home;
+
+    xdg_config_home = secure_getenv("XDG_CONFIG_HOME");
+    if (!xdg_config_home || xdg_config_home[0] != '/') {
+        home = secure_getenv("HOME");
+        if (!home)
+            return NULL;
+        return asprintf_safe("%s/.config/XCompose", home);
+    }
+
+    return asprintf_safe("%s/XCompose", xdg_config_home);
+}
+
+char *
 get_home_xcompose_file_path(void)
 {
     const char *home;
diff --git a/src/compose/paths.h b/src/compose/paths.h
index 1d719af..53d7415 100644
--- a/src/compose/paths.h
+++ b/src/compose/paths.h
@@ -34,6 +34,9 @@ const char *
 get_xcomposefile_path(void);
 
 char *
+get_xdg_xcompose_file_path(void);
+
+char *
 get_home_xcompose_file_path(void);
 
 char *
diff --git a/src/compose/table.c b/src/compose/table.c
index bdfb907..1843c46 100644
--- a/src/compose/table.c
+++ b/src/compose/table.c
@@ -183,6 +183,15 @@ xkb_compose_table_new_from_locale(struct xkb_context *ctx,
             goto found_path;
     }
 
+    cpath = path = get_xdg_xcompose_file_path();
+    if (path) {
+        file = fopen(path, "rb");
+        if (file)
+            goto found_path;
+    }
+    free(path);
+    path = NULL;
+
     cpath = path = get_home_xcompose_file_path();
     if (path) {
         file = fopen(path, "rb");
diff --git a/xkbcommon/xkbcommon-compose.h b/xkbcommon/xkbcommon-compose.h
index 1f158bd..f3c367a 100644
--- a/xkbcommon/xkbcommon-compose.h
+++ b/xkbcommon/xkbcommon-compose.h
@@ -204,8 +204,15 @@ enum xkb_compose_format {
  * affected by the following environment variables:
  *
  * 1. `XCOMPOSEFILE` - see Compose(5).
- * 2. `HOME` - see Compose(5).
- * 3. `XLOCALEDIR` - if set, used as the base directory for the system's
+ * 2. `XDG_CONFIG_HOME` - before `$HOME/.XCompose` is checked,
+ *    `$XDG_CONFIG_HOME/XCompose` is checked (with a fall back to
+ *    `$HOME/.config/XCompose` if `XDG_CONFIG_HOME` is not defined).
+ *    This is a libxkbcommon extension to the search procedure in
+ *    Compose(5) (since libxkbcommon 0.11.0). Note that other
+ *    implementations, such as libX11, might not find a Compose file in
+ *    this path.
+ * 3. `HOME` - see Compose(5).
+ * 4. `XLOCALEDIR` - if set, used as the base directory for the system's
  *    X locale files, e.g. `/usr/share/X11/locale`, instead of the
  *    preconfigured directory.
  *