Refactor merge code

This commit is contained in:
Simon Long 2024-01-08 10:39:03 +00:00
parent 1385ce20ad
commit 369e69113b
4 changed files with 77 additions and 65 deletions

View file

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

View file

@ -17,9 +17,13 @@ struct dir {
const char *path; const char *path;
}; };
static struct dir config_dirs[] = { static struct dir user_config_dirs[] = {
{ "XDG_CONFIG_HOME", "labwc" }, { "XDG_CONFIG_HOME", "labwc" },
{ "HOME", ".config/labwc" }, { "HOME", ".config/labwc" },
{ NULL, NULL }
};
static struct dir sys_config_dirs[] = {
{ "XDG_CONFIG_DIRS", "labwc" }, { "XDG_CONFIG_DIRS", "labwc" },
{ NULL, "/etc/xdg/labwc" }, { NULL, "/etc/xdg/labwc" },
{ NULL, NULL } { NULL, NULL }
@ -74,12 +78,12 @@ build_theme_path(struct ctx *ctx, char *prefix, const char *path)
} }
static char * static char *
find_dir(struct ctx *ctx, int n) find_dir(struct ctx *ctx)
{ {
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[n != -1 ? n : i]; struct dir d = ctx->dirs[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,9 +113,6 @@ find_dir(struct ctx *ctx, int n)
} }
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';
@ -119,7 +120,7 @@ find_dir(struct ctx *ctx, int n)
} }
char * char *
config_dir(void) user_config_dir(void)
{ {
static char buf[4096] = { 0 }; static char buf[4096] = { 0 };
if (buf[0] != '\0') { if (buf[0] != '\0') {
@ -129,26 +130,35 @@ config_dir(void)
.build_path_fn = build_config_path, .build_path_fn = build_config_path,
.buf = buf, .buf = buf,
.len = sizeof(buf), .len = sizeof(buf),
.dirs = config_dirs .dirs = user_config_dirs
}; };
return find_dir(&ctx, -1); return find_dir(&ctx);
} }
char * char *
config_dir_n(int n) sys_config_dir(void)
{ {
char buf[4096] = { 0 }; static char buf[4096] = { 0 };
if (buf[0] != '\0') {
return buf;
}
struct ctx ctx = { struct ctx ctx = {
.build_path_fn = build_config_path, .build_path_fn = build_config_path,
.buf = buf, .buf = buf,
.len = sizeof(buf), .len = sizeof(buf),
.dirs = config_dirs .dirs = sys_config_dirs
}; };
if (n == 0) { return find_dir(&ctx);
return find_dir(&ctx, getenv("XDG_CONFIG_HOME") ? 0 : 1);
} else {
return find_dir(&ctx, getenv("XDG_CONFIG_DIRS") ? 2 : 3);
} }
char *
config_dir(void)
{
char *res = user_config_dir ();
if (res[0] != '\0') {
return res;
}
return sys_config_dir ();
} }
char * char *
@ -162,5 +172,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, -1); return find_dir(&ctx);
} }

View file

@ -1453,39 +1453,14 @@ validate(void)
validate_actions(); validate_actions();
} }
void static void
rcxml_read(const char *filename) rcxml_read_file(const char *rcxml)
{ {
FILE *stream; FILE *stream;
char *line = NULL; char *line = NULL;
size_t len = 0; size_t len = 0;
struct buf b; struct buf b;
char rcxml[4096] = {0};
static char saved_filename[4096] = {0};
if (filename) {
snprintf(saved_filename, sizeof(rcxml), "%s", filename);
}
rcxml_init();
/*
* rcxml_read() can be called multiple times, but we only set rcxml[]
* the first time. The specified 'filename' is only respected the first
* time.
*/
for (int i = 1; i >= 0; i--) {
line = NULL;
len = 0;
if (filename) {
snprintf(rcxml, sizeof(rcxml), "%s", filename);
i = 0;
} else if (saved_filename[0] != 0) {
snprintf(rcxml, sizeof(rcxml), "%s", saved_filename);
i = 0;
} else {
snprintf(rcxml, sizeof(rcxml), "%s/rc.xml", config_dir_n(i));
}
/* Reading file into buffer before parsing - better for unit tests */ /* Reading file into buffer before parsing - better for unit tests */
stream = fopen(rcxml, "r"); stream = fopen(rcxml, "r");
if (!stream) { if (!stream) {
@ -1506,6 +1481,31 @@ rcxml_read(const char *filename)
free(b.buf); free(b.buf);
} }
} }
void
rcxml_read(const char *filename)
{
char *rcxml;
static char saved_filename[4096] = {0};
rcxml_init();
if (filename) {
snprintf(saved_filename, sizeof(saved_filename), "%s", filename);
rcxml_read_file(filename);
} else if (saved_filename[0] != 0) {
rcxml_read_file(saved_filename);
} else {
/* merge user config with system config */
rcxml = strdup_printf("%s/rc.xml", sys_config_dir());
rcxml_read_file (rcxml);
free (rcxml);
rcxml = strdup_printf("%s/rc.xml", user_config_dir());
rcxml_read_file (rcxml);
free (rcxml);
}
post_processing(); post_processing();
validate(); validate();
} }

View file

@ -3,12 +3,12 @@
#include <signal.h> #include <signal.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <glib.h>
#include "common/dir.h" #include "common/dir.h"
#include "common/fd_util.h" #include "common/fd_util.h"
#include "common/font.h" #include "common/font.h"
#include "common/mem.h" #include "common/mem.h"
#include "common/spawn.h" #include "common/spawn.h"
#include "common/string-helpers.h"
#include "config/session.h" #include "config/session.h"
#include "labwc.h" #include "labwc.h"
#include "theme.h" #include "theme.h"
@ -22,7 +22,7 @@ static const struct option long_options[] = {
{"debug", no_argument, NULL, 'd'}, {"debug", no_argument, NULL, 'd'},
{"exit", no_argument, NULL, 'e'}, {"exit", no_argument, NULL, 'e'},
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{"mergeconfig", no_argument, NULL, 'm'}, {"merge-config", no_argument, NULL, 'm'},
{"reconfigure", no_argument, NULL, 'r'}, {"reconfigure", no_argument, NULL, 'r'},
{"startup", required_argument, NULL, 's'}, {"startup", required_argument, NULL, 's'},
{"version", no_argument, NULL, 'v'}, {"version", no_argument, NULL, 'v'},
@ -37,7 +37,7 @@ static const char labwc_usage[] =
" -d, --debug Enable full logging, including debug information\n" " -d, --debug Enable full logging, including debug information\n"
" -e, --exit Exit the compositor\n" " -e, --exit Exit the compositor\n"
" -h, --help Show help message and quit\n" " -h, --help Show help message and quit\n"
" -m, --mergeconfig Merge user rc.xml with system rc.xml\n" " -m, --merge-config Merge user rc.xml with system rc.xml\n"
" -r, --reconfigure Reload the compositor configuration\n" " -r, --reconfigure Reload the compositor configuration\n"
" -s, --startup <command> Run command on startup\n" " -s, --startup <command> Run command on startup\n"
" -v, --version Show version number and quit\n" " -v, --version Show version number and quit\n"
@ -143,11 +143,12 @@ main(int argc, char *argv[])
if (!rc.config_dir) { if (!rc.config_dir) {
rc.config_dir = config_dir(); rc.config_dir = config_dir();
if (!merge_rc && !config_file) { if (!merge_rc && !config_file) {
config_file = g_strdup_printf("%s/rc.xml", rc.config_dir); config_file = strdup_printf("%s/rc.xml", rc.config_dir);
} }
} else if (!config_file) { } else if (!config_file) {
config_file = g_strdup_printf("%s/rc.xml", rc.config_dir); config_file = strdup_printf("%s/rc.xml", rc.config_dir);
} }
wlr_log(WLR_INFO, "using config dir (%s)\n", rc.config_dir); wlr_log(WLR_INFO, "using config dir (%s)\n", rc.config_dir);
session_environment_init(rc.config_dir); session_environment_init(rc.config_dir);
rcxml_read(config_file); rcxml_read(config_file);