Load hierarchy of rc.xml files from directories

This commit is contained in:
Simon Long 2024-01-05 12:00:59 +00:00
parent 785a34e8ad
commit d2e891ab2e
3 changed files with 51 additions and 30 deletions

View file

@ -3,6 +3,7 @@
#define LABWC_DIR_H
char *config_dir(void);
char *config_dir_n(int n);
/**
* theme_dir - find theme directory containing theme @theme_name

View file

@ -74,12 +74,12 @@ build_theme_path(struct ctx *ctx, char *prefix, const char *path)
}
static char *
find_dir(struct ctx *ctx)
find_dir(struct ctx *ctx, int n)
{
char *debug = getenv("LABWC_DEBUG_DIR_CONFIG_AND_THEME");
for (int i = 0; ctx->dirs[i].path; i++) {
struct dir d = ctx->dirs[i];
struct dir d = ctx->dirs[n != -1 ? n : i];
if (!d.prefix) {
/* handle /etc/xdg... */
ctx->build_path_fn(ctx, NULL, d.path);
@ -109,6 +109,9 @@ find_dir(struct ctx *ctx)
}
g_strfreev(prefixes);
}
if (n != -1) {
break;
}
}
/* no directory was found */
ctx->buf[0] = '\0';
@ -128,7 +131,20 @@ config_dir(void)
.len = sizeof(buf),
.dirs = config_dirs
};
return find_dir(&ctx);
return find_dir(&ctx, -1);
}
char *
config_dir_n(int n)
{
char buf[4096] = { 0 };
struct ctx ctx = {
.build_path_fn = build_config_path,
.buf = buf,
.len = sizeof(buf),
.dirs = config_dirs
};
return find_dir(&ctx, n);
}
char *
@ -142,5 +158,5 @@ theme_dir(const char *theme_name)
.dirs = theme_dirs,
.theme_name = theme_name
};
return find_dir(&ctx);
return find_dir(&ctx, -1);
}

View file

@ -14,6 +14,7 @@
#include <wlr/util/box.h>
#include <wlr/util/log.h>
#include "action.h"
#include "common/dir.h"
#include "common/list.h"
#include "common/macros.h"
#include "common/mem.h"
@ -1487,34 +1488,37 @@ rcxml_read(const char *filename)
* the first time. The specified 'filename' is only respected the first
* time.
*/
if (rcxml[0] == '\0') {
find_config_file(rcxml, sizeof(rcxml), filename);
}
if (rcxml[0] == '\0') {
wlr_log(WLR_INFO, "cannot find rc.xml config file");
goto no_config;
}
/* Reading file into buffer before parsing - better for unit tests */
stream = fopen(rcxml, "r");
if (!stream) {
wlr_log(WLR_ERROR, "cannot read (%s)", rcxml);
goto no_config;
}
wlr_log(WLR_INFO, "read config file %s", rcxml);
buf_init(&b);
while (getline(&line, &len, stream) != -1) {
char *p = strrchr(line, '\n');
if (p) {
*p = '\0';
for (int i = 3; i >= 0; i--) {
line = NULL;
len = 0;
if (filename) {
sprintf(rcxml, "%s", filename);
i = 0;
} else {
sprintf(rcxml, "%s/rc.xml", config_dir_n(i));
}
/* Reading file into buffer before parsing - better for unit tests */
stream = fopen(rcxml, "r");
if (!stream) {
wlr_log(WLR_ERROR, "cannot read (%s)", rcxml);
}
else
{
wlr_log(WLR_INFO, "read config file %s", rcxml);
buf_init(&b);
while (getline(&line, &len, stream) != -1) {
char *p = strrchr(line, '\n');
if (p) {
*p = '\0';
}
buf_add(&b, line);
}
free(line);
fclose(stream);
rcxml_parse_xml(&b);
free(b.buf);
}
buf_add(&b, line);
}
free(line);
fclose(stream);
rcxml_parse_xml(&b);
free(b.buf);
no_config:
post_processing();
validate();
}