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 #define LABWC_DIR_H
char *config_dir(void); char *config_dir(void);
char *config_dir_n(int n);
/** /**
* theme_dir - find theme directory containing theme @theme_name * 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 * static char *
find_dir(struct ctx *ctx) find_dir(struct ctx *ctx, int n)
{ {
char *debug = getenv("LABWC_DEBUG_DIR_CONFIG_AND_THEME"); char *debug = getenv("LABWC_DEBUG_DIR_CONFIG_AND_THEME");
for (int i = 0; ctx->dirs[i].path; i++) { 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) { if (!d.prefix) {
/* handle /etc/xdg... */ /* handle /etc/xdg... */
ctx->build_path_fn(ctx, NULL, d.path); ctx->build_path_fn(ctx, NULL, d.path);
@ -109,6 +109,9 @@ find_dir(struct ctx *ctx)
} }
g_strfreev(prefixes); g_strfreev(prefixes);
} }
if (n != -1) {
break;
}
} }
/* no directory was found */ /* no directory was found */
ctx->buf[0] = '\0'; ctx->buf[0] = '\0';
@ -128,7 +131,20 @@ config_dir(void)
.len = sizeof(buf), .len = sizeof(buf),
.dirs = config_dirs .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 * char *
@ -142,5 +158,5 @@ theme_dir(const char *theme_name)
.dirs = theme_dirs, .dirs = theme_dirs,
.theme_name = theme_name .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/box.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
#include "action.h" #include "action.h"
#include "common/dir.h"
#include "common/list.h" #include "common/list.h"
#include "common/macros.h" #include "common/macros.h"
#include "common/mem.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 * the first time. The specified 'filename' is only respected the first
* time. * time.
*/ */
if (rcxml[0] == '\0') { for (int i = 3; i >= 0; i--) {
find_config_file(rcxml, sizeof(rcxml), filename); line = NULL;
} len = 0;
if (rcxml[0] == '\0') { if (filename) {
wlr_log(WLR_INFO, "cannot find rc.xml config file"); sprintf(rcxml, "%s", filename);
goto no_config; 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"); /* Reading file into buffer before parsing - better for unit tests */
if (!stream) { stream = fopen(rcxml, "r");
wlr_log(WLR_ERROR, "cannot read (%s)", rcxml); if (!stream) {
goto no_config; wlr_log(WLR_ERROR, "cannot read (%s)", rcxml);
} }
wlr_log(WLR_INFO, "read config file %s", rcxml); else
buf_init(&b); {
while (getline(&line, &len, stream) != -1) { wlr_log(WLR_INFO, "read config file %s", rcxml);
char *p = strrchr(line, '\n'); buf_init(&b);
if (p) { while (getline(&line, &len, stream) != -1) {
*p = '\0'; 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(); post_processing();
validate(); validate();
} }