mirror of
https://github.com/labwc/labwc.git
synced 2025-11-04 13:30:07 -05:00
config: support merging multiple config files
Add the -m|--merge-config command line option to iterate backwards over XDG Base Dir paths and read config/theme files multiple times. For example if both ~/.config/labwc/rc.xml and /etc/xdg/labwc/rc.xml exist, the latter will be read first and then the former (if --merge-config is enabled). When $XDG_CONFIG_HOME is defined, make it replace (not augment) $HOME/.config. Similarly, make $XDG_CONFIG_DIRS replace /etc/xdg when defined. XDG Base Dir Spec does not specify whether or not an application (or a compositor!) should (a) define that only the file under the most important base directory should be used, or (b) define rules for merging the information from the different files. ref: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html In the case of labwc there is a use-case for both positions, just to be clear, the default behaviour, described by position (a) above, does NOT change. This change affects the following config/theme files: - rc.xml - menu.xml - autostart - environment - themerc - themerc-override - Theme buttons, for example max.xbm Instead of caching global config/theme directories, create lists of paths (e.g. '/home/foo/.config/labwc/rc.xml', '/etc/xdg/labwc/rc.xml', etc). This creates more common parsing logic and just reversing the direction of iteration and breaks early if config-merge is not wanted. Enable better fallback for themes. For example if a particular theme does not exist in $HOME/.local/share/themes, it will be searched for in ~/.themes/ and so on. This also applies to theme buttons which now fallback on an individual basis. Avoid using stat() in most situations and just go straight to fopen(). Fixes #1406
This commit is contained in:
parent
d0aff49c81
commit
698c7ace07
14 changed files with 330 additions and 240 deletions
193
src/common/dir.c
193
src/common/dir.c
|
|
@ -4,143 +4,190 @@
|
|||
*
|
||||
* Copyright Johan Malm 2020
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <glib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include "common/dir.h"
|
||||
#include "common/buf.h"
|
||||
#include "common/list.h"
|
||||
#include "common/mem.h"
|
||||
#include "common/string-helpers.h"
|
||||
#include "labwc.h"
|
||||
|
||||
struct dir {
|
||||
const char *prefix;
|
||||
const char *default_prefix;
|
||||
const char *path;
|
||||
};
|
||||
|
||||
static struct dir config_dirs[] = {
|
||||
{ "XDG_CONFIG_HOME", "labwc" },
|
||||
{ "HOME", ".config/labwc" },
|
||||
{ "XDG_CONFIG_DIRS", "labwc" },
|
||||
{ NULL, "/etc/xdg/labwc" },
|
||||
{ NULL, NULL }
|
||||
{
|
||||
.prefix = "XDG_CONFIG_HOME",
|
||||
.default_prefix = "$HOME/.config",
|
||||
.path = "labwc"
|
||||
}, {
|
||||
.prefix = "XDG_CONFIG_DIRS",
|
||||
.default_prefix = "/etc/xdg",
|
||||
.path = "labwc",
|
||||
}, {
|
||||
.path = NULL,
|
||||
}
|
||||
};
|
||||
|
||||
static struct dir theme_dirs[] = {
|
||||
{ "XDG_DATA_HOME", "themes" },
|
||||
{ "HOME", ".local/share/themes" },
|
||||
{ "HOME", ".themes" },
|
||||
{ "XDG_DATA_DIRS", "themes" },
|
||||
{ NULL, "/usr/share/themes" },
|
||||
{ NULL, "/usr/local/share/themes" },
|
||||
{ NULL, "/opt/share/themes" },
|
||||
{ NULL, NULL }
|
||||
{
|
||||
.prefix = "XDG_DATA_HOME",
|
||||
.default_prefix = "$HOME/.local/share",
|
||||
.path = "themes",
|
||||
}, {
|
||||
.prefix = "HOME",
|
||||
.path = ".themes",
|
||||
}, {
|
||||
.prefix = "XDG_DATA_DIRS",
|
||||
.default_prefix = "/usr/share:/usr/local/share:/opt/share",
|
||||
.path = "themes",
|
||||
}, {
|
||||
.path = NULL,
|
||||
}
|
||||
};
|
||||
|
||||
static bool
|
||||
isdir(const char *path)
|
||||
{
|
||||
struct stat st;
|
||||
return (!stat(path, &st) && S_ISDIR(st.st_mode));
|
||||
}
|
||||
|
||||
struct ctx {
|
||||
void (*build_path_fn)(struct ctx *ctx, char *prefix, const char *path);
|
||||
const char *filename;
|
||||
char *buf;
|
||||
size_t len;
|
||||
struct dir *dirs;
|
||||
const char *theme_name;
|
||||
struct wl_list *list;
|
||||
};
|
||||
|
||||
struct wl_list *paths_get_prev(struct wl_list *elm) { return elm->prev; }
|
||||
struct wl_list *paths_get_next(struct wl_list *elm) { return elm->next; }
|
||||
|
||||
static void
|
||||
build_config_path(struct ctx *ctx, char *prefix, const char *path)
|
||||
{
|
||||
if (!prefix) {
|
||||
snprintf(ctx->buf, ctx->len, "%s", path);
|
||||
} else {
|
||||
snprintf(ctx->buf, ctx->len, "%s/%s", prefix, path);
|
||||
}
|
||||
assert(prefix);
|
||||
snprintf(ctx->buf, ctx->len, "%s/%s/%s", prefix, path, ctx->filename);
|
||||
}
|
||||
|
||||
static void
|
||||
build_theme_path(struct ctx *ctx, char *prefix, const char *path)
|
||||
{
|
||||
if (!prefix) {
|
||||
snprintf(ctx->buf, ctx->len, "%s/%s/openbox-3", path,
|
||||
ctx->theme_name);
|
||||
} else {
|
||||
snprintf(ctx->buf, ctx->len, "%s/%s/%s/openbox-3", prefix, path,
|
||||
ctx->theme_name);
|
||||
}
|
||||
assert(prefix);
|
||||
snprintf(ctx->buf, ctx->len, "%s/%s/%s/openbox-3/%s", prefix, path,
|
||||
ctx->theme_name, ctx->filename);
|
||||
}
|
||||
|
||||
static char *
|
||||
static void
|
||||
find_dir(struct ctx *ctx)
|
||||
{
|
||||
char *debug = getenv("LABWC_DEBUG_DIR_CONFIG_AND_THEME");
|
||||
|
||||
for (int i = 0; ctx->dirs[i].path; i++) {
|
||||
struct dir d = ctx->dirs[i];
|
||||
if (!d.prefix) {
|
||||
/* handle /etc/xdg... */
|
||||
ctx->build_path_fn(ctx, NULL, d.path);
|
||||
struct buf prefix;
|
||||
buf_init(&prefix);
|
||||
|
||||
/*
|
||||
* Replace (rather than augment) $HOME/.config with
|
||||
* $XDG_CONFIG_HOME if defined, and so on for the other
|
||||
* XDG Base Directories.
|
||||
*/
|
||||
char *pfxenv = getenv(d.prefix);
|
||||
buf_add(&prefix, pfxenv ? pfxenv : d.default_prefix);
|
||||
if (!prefix.len) {
|
||||
free(prefix.buf);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Handle .default_prefix shell variables such as $HOME */
|
||||
buf_expand_shell_variables(&prefix);
|
||||
|
||||
/*
|
||||
* Respect that $XDG_DATA_DIRS can contain multiple colon
|
||||
* separated paths and that we have structured the
|
||||
* .default_prefix in the same way.
|
||||
*/
|
||||
gchar * *prefixes;
|
||||
prefixes = g_strsplit(prefix.buf, ":", -1);
|
||||
for (gchar * *p = prefixes; *p; p++) {
|
||||
ctx->build_path_fn(ctx, *p, d.path);
|
||||
if (debug) {
|
||||
fprintf(stderr, "%s\n", ctx->buf);
|
||||
}
|
||||
if (isdir(ctx->buf)) {
|
||||
return ctx->buf;
|
||||
}
|
||||
} else {
|
||||
/* handle $HOME/.config/... and $XDG_* */
|
||||
char *prefix = getenv(d.prefix);
|
||||
if (!prefix) {
|
||||
continue;
|
||||
}
|
||||
gchar * *prefixes;
|
||||
prefixes = g_strsplit(prefix, ":", -1);
|
||||
for (gchar * *p = prefixes; *p; p++) {
|
||||
ctx->build_path_fn(ctx, *p, d.path);
|
||||
if (debug) {
|
||||
fprintf(stderr, "%s\n", ctx->buf);
|
||||
}
|
||||
if (isdir(ctx->buf)) {
|
||||
g_strfreev(prefixes);
|
||||
return ctx->buf;
|
||||
}
|
||||
}
|
||||
g_strfreev(prefixes);
|
||||
|
||||
/*
|
||||
* TODO: We could stat() and continue here if we really
|
||||
* wanted to only respect only the first hit, but feels
|
||||
* like it is probably overkill.
|
||||
*/
|
||||
struct path *path = znew(*path);
|
||||
path->string = xstrdup(ctx->buf);
|
||||
wl_list_append(ctx->list, &path->link);
|
||||
}
|
||||
g_strfreev(prefixes);
|
||||
free(prefix.buf);
|
||||
}
|
||||
/* no directory was found */
|
||||
ctx->buf[0] = '\0';
|
||||
return ctx->buf;
|
||||
}
|
||||
|
||||
char *
|
||||
config_dir(void)
|
||||
void
|
||||
paths_config_create(struct wl_list *paths, const char *filename)
|
||||
{
|
||||
static char buf[4096] = { 0 };
|
||||
if (buf[0] != '\0') {
|
||||
return buf;
|
||||
char buf[4096] = { 0 };
|
||||
wl_list_init(paths);
|
||||
|
||||
/*
|
||||
* If user provided a config directory with the -C command line option,
|
||||
* then that trumps everything else and we do not create the
|
||||
* XDG-Base-Dir list.
|
||||
*/
|
||||
if (rc.config_dir) {
|
||||
struct path *path = znew(*path);
|
||||
path->string = strdup_printf("%s/%s", rc.config_dir, filename);
|
||||
wl_list_append(paths, &path->link);
|
||||
return;
|
||||
}
|
||||
|
||||
struct ctx ctx = {
|
||||
.build_path_fn = build_config_path,
|
||||
.filename = filename,
|
||||
.buf = buf,
|
||||
.len = sizeof(buf),
|
||||
.dirs = config_dirs
|
||||
.dirs = config_dirs,
|
||||
.list = paths,
|
||||
};
|
||||
return find_dir(&ctx);
|
||||
find_dir(&ctx);
|
||||
}
|
||||
|
||||
char *
|
||||
theme_dir(const char *theme_name)
|
||||
void
|
||||
paths_theme_create(struct wl_list *paths, const char *theme_name,
|
||||
const char *filename)
|
||||
{
|
||||
static char buf[4096] = { 0 };
|
||||
wl_list_init(paths);
|
||||
struct ctx ctx = {
|
||||
.build_path_fn = build_theme_path,
|
||||
.filename = filename,
|
||||
.buf = buf,
|
||||
.len = sizeof(buf),
|
||||
.dirs = theme_dirs,
|
||||
.theme_name = theme_name
|
||||
.theme_name = theme_name,
|
||||
.list = paths,
|
||||
};
|
||||
return find_dir(&ctx);
|
||||
find_dir(&ctx);
|
||||
}
|
||||
|
||||
void
|
||||
paths_destroy(struct wl_list *paths)
|
||||
{
|
||||
struct path *path, *next;
|
||||
wl_list_for_each_safe(path, next, paths, link) {
|
||||
free(path->string);
|
||||
wl_list_remove(&path->link);
|
||||
free(path);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue