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:
Johan Malm 2024-01-09 22:00:45 +00:00 committed by Johan Malm
parent d0aff49c81
commit 698c7ace07
14 changed files with 330 additions and 240 deletions

View file

@ -23,6 +23,7 @@
#include "common/font.h"
#include "common/graphic-helpers.h"
#include "common/match.h"
#include "common/mem.h"
#include "common/string-helpers.h"
#include "config/rcxml.h"
#include "button/button-png.h"
@ -664,60 +665,36 @@ process_line(struct theme *theme, char *line)
}
static void
theme_read(struct theme *theme, const char *theme_name)
theme_read(struct theme *theme, struct wl_list *paths)
{
FILE *stream = NULL;
char *line = NULL;
size_t len = 0;
char themerc[4096];
bool should_merge_config = rc.merge_config;
struct wl_list *(*iter)(struct wl_list *list);
iter = should_merge_config ? paths_get_prev : paths_get_next;
if (strlen(theme_dir(theme_name))) {
snprintf(themerc, sizeof(themerc), "%s/themerc",
theme_dir(theme_name));
stream = fopen(themerc, "r");
}
if (!stream) {
if (theme_name) {
wlr_log(WLR_INFO, "cannot find theme %s", theme_name);
for (struct wl_list *elm = iter(paths); elm != paths; elm = iter(elm)) {
struct path *path = wl_container_of(elm, path, link);
FILE *stream = fopen(path->string, "r");
if (!stream) {
continue;
}
return;
}
wlr_log(WLR_INFO, "read theme %s", themerc);
while (getline(&line, &len, stream) != -1) {
char *p = strrchr(line, '\n');
if (p) {
*p = '\0';
wlr_log(WLR_INFO, "read theme %s", path->string);
char *line = NULL;
size_t len = 0;
while (getline(&line, &len, stream) != -1) {
char *p = strrchr(line, '\n');
if (p) {
*p = '\0';
}
process_line(theme, line);
}
process_line(theme, line);
}
free(line);
fclose(stream);
}
static void
theme_read_override(struct theme *theme)
{
char f[4096] = { 0 };
snprintf(f, sizeof(f), "%s/themerc-override", rc.config_dir);
FILE *stream = fopen(f, "r");
if (!stream) {
wlr_log(WLR_INFO, "no theme override '%s'", f);
return;
}
wlr_log(WLR_INFO, "read theme-override %s", f);
char *line = NULL;
size_t len = 0;
while (getline(&line, &len, stream) != -1) {
char *p = strrchr(line, '\n');
if (p) {
*p = '\0';
zfree(line);
fclose(stream);
if (!should_merge_config) {
break;
}
process_line(theme, line);
}
free(line);
fclose(stream);
}
struct rounded_corner_ctx {
@ -1001,10 +978,15 @@ theme_init(struct theme *theme, const char *theme_name)
theme_builtin(theme);
/* Read <data-dir>/share/themes/$theme_name/openbox-3/themerc */
theme_read(theme, theme_name);
struct wl_list paths;
paths_theme_create(&paths, theme_name, "themerc");
theme_read(theme, &paths);
paths_destroy(&paths);
/* Read <config-dir>/labwc/themerc-override */
theme_read_override(theme);
paths_config_create(&paths, "themerc-override");
theme_read(theme, &paths);
paths_destroy(&paths);
post_processing(theme);
create_corners(theme);