diff --git a/include/common/dir.h b/include/common/dir.h index a9478acc..ced783c4 100644 --- a/include/common/dir.h +++ b/include/common/dir.h @@ -3,7 +3,8 @@ #define LABWC_DIR_H 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 diff --git a/src/common/dir.c b/src/common/dir.c index 09b8106b..c8bed13d 100644 --- a/src/common/dir.c +++ b/src/common/dir.c @@ -17,9 +17,13 @@ struct dir { const char *path; }; -static struct dir config_dirs[] = { +static struct dir user_config_dirs[] = { { "XDG_CONFIG_HOME", "labwc" }, { "HOME", ".config/labwc" }, + { NULL, NULL } +}; + +static struct dir sys_config_dirs[] = { { "XDG_CONFIG_DIRS", "labwc" }, { NULL, "/etc/xdg/labwc" }, { NULL, NULL } @@ -74,12 +78,12 @@ build_theme_path(struct ctx *ctx, char *prefix, const char *path) } static char * -find_dir(struct ctx *ctx, int n) +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[n != -1 ? n : i]; + struct dir d = ctx->dirs[i]; if (!d.prefix) { /* handle /etc/xdg... */ ctx->build_path_fn(ctx, NULL, d.path); @@ -109,9 +113,6 @@ find_dir(struct ctx *ctx, int n) } g_strfreev(prefixes); } - if (n != -1) { - break; - } } /* no directory was found */ ctx->buf[0] = '\0'; @@ -119,7 +120,7 @@ find_dir(struct ctx *ctx, int n) } char * -config_dir(void) +user_config_dir(void) { static char buf[4096] = { 0 }; if (buf[0] != '\0') { @@ -129,26 +130,35 @@ config_dir(void) .build_path_fn = build_config_path, .buf = buf, .len = sizeof(buf), - .dirs = config_dirs + .dirs = user_config_dirs }; - return find_dir(&ctx, -1); + return find_dir(&ctx); } 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 = { .build_path_fn = build_config_path, .buf = buf, .len = sizeof(buf), - .dirs = config_dirs + .dirs = sys_config_dirs }; - if (n == 0) { - return find_dir(&ctx, getenv("XDG_CONFIG_HOME") ? 0 : 1); - } else { - return find_dir(&ctx, getenv("XDG_CONFIG_DIRS") ? 2 : 3); + return find_dir(&ctx); +} + +char * +config_dir(void) +{ + char *res = user_config_dir (); + if (res[0] != '\0') { + return res; } + return sys_config_dir (); } char * @@ -162,5 +172,5 @@ theme_dir(const char *theme_name) .dirs = theme_dirs, .theme_name = theme_name }; - return find_dir(&ctx, -1); + return find_dir(&ctx); } diff --git a/src/config/rcxml.c b/src/config/rcxml.c index f89a8b30..fa0d5377 100644 --- a/src/config/rcxml.c +++ b/src/config/rcxml.c @@ -1453,59 +1453,59 @@ validate(void) validate_actions(); } -void -rcxml_read(const char *filename) +static void +rcxml_read_file(const char *rcxml) { FILE *stream; char *line = NULL; size_t len = 0; struct buf b; - char rcxml[4096] = {0}; - static char saved_filename[4096] = {0}; - if (filename) { - snprintf(saved_filename, sizeof(rcxml), "%s", filename); + /* 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); } +} + +void +rcxml_read(const char *filename) +{ + char *rcxml; + static char saved_filename[4096] = {0}; 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 */ - 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); - } + 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(); validate(); } diff --git a/src/main.c b/src/main.c index 2fe87f0a..04314327 100644 --- a/src/main.c +++ b/src/main.c @@ -3,12 +3,12 @@ #include #include #include -#include #include "common/dir.h" #include "common/fd_util.h" #include "common/font.h" #include "common/mem.h" #include "common/spawn.h" +#include "common/string-helpers.h" #include "config/session.h" #include "labwc.h" #include "theme.h" @@ -22,7 +22,7 @@ static const struct option long_options[] = { {"debug", no_argument, NULL, 'd'}, {"exit", no_argument, NULL, 'e'}, {"help", no_argument, NULL, 'h'}, - {"mergeconfig", no_argument, NULL, 'm'}, + {"merge-config", no_argument, NULL, 'm'}, {"reconfigure", no_argument, NULL, 'r'}, {"startup", required_argument, NULL, 's'}, {"version", no_argument, NULL, 'v'}, @@ -37,7 +37,7 @@ static const char labwc_usage[] = " -d, --debug Enable full logging, including debug information\n" " -e, --exit Exit the compositor\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" " -s, --startup Run command on startup\n" " -v, --version Show version number and quit\n" @@ -143,11 +143,12 @@ main(int argc, char *argv[]) if (!rc.config_dir) { rc.config_dir = config_dir(); 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) { - 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); session_environment_init(rc.config_dir); rcxml_read(config_file);