From 94b1bc038782248f762e80bf5ecec14923aaf0f5 Mon Sep 17 00:00:00 2001 From: Yuxuan Shui Date: Sat, 15 Dec 2018 19:44:11 +0000 Subject: [PATCH] Use libxdg-basedir for finding config files Also add a new config file path: {$XDG_CONFIG_DIRS,$XDG_CONFIG_HOME}/compton/compton.conf (For those not familiar with xdg: now compton will look for ~/.config/compton/compton.conf too) Closes #62 Signed-off-by: Yuxuan Shui --- README.md | 3 +- src/config_libconfig.c | 99 +++++++++++++++--------------------------- src/diagnostic.c | 1 + src/meson.build | 5 ++- 4 files changed, 40 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index 5cde882..b9e141b 100644 --- a/README.md +++ b/README.md @@ -26,10 +26,11 @@ Assuming you already have all the usual building tools installed (e.g. gcc, meso * xcb-composite * xcb-image * xcb-present -* xcb-xinerama (optional, disable with `-Dxinerama=false` meson configure flag) +* xcb-xinerama (optional, disable with the `-Dxinerama=false` meson configure flag) * pixman * libdbus (optional, disable with the `-Ddbus=false` meson configure flag) * libconfig (optional, disable with the `-Dconfig_file=false` meson configure flag) +* libxdg-basedir (optional, disable with the `-Dconfig_file=false` meson configure flag) * libGL (optional, disable with the `-Dopengl=false` meson configure flag) * libpcre (optional, disable with the `-Dregex=false` meson configure flag) * libev diff --git a/src/config_libconfig.c b/src/config_libconfig.c index 201f4d3..c9e9910 100644 --- a/src/config_libconfig.c +++ b/src/config_libconfig.c @@ -7,6 +7,7 @@ #include #include +#include #include "common.h" #include "config.h" @@ -36,79 +37,47 @@ lcfg_lookup_bool(const config_t *config, const char *path, bool *value) { */ FILE * open_config_file(char *cpath, char **ppath) { - static const char *config_filename = "/compton.conf"; - static const char *config_filename_legacy = "/.compton.conf"; - static const char *config_home_suffix = "/.config"; - static const char *config_system_dir = "/etc/xdg"; + static const char *config_paths[] = { + "/compton.conf", + "/compton/compton.conf" + }; + static const char config_filename_legacy[] = "/.compton.conf"; - char *dir = NULL, *home = NULL; - char *path = cpath; - FILE *f = NULL; - - if (path) { - f = fopen(path, "r"); - if (f && ppath) - *ppath = path; - return f; + if (cpath) { + FILE *ret = fopen(cpath, "r"); + if (ret && ppath) + *ppath = cpath; + return ret; } - // Check user configuration file in $XDG_CONFIG_HOME firstly - if (!((dir = getenv("XDG_CONFIG_HOME")) && strlen(dir))) { - if (!((home = getenv("HOME")) && strlen(home))) - return NULL; - - path = mstrjoin3(home, config_home_suffix, config_filename); - } - else - path = mstrjoin(dir, config_filename); - - f = fopen(path, "r"); - - if (f && ppath) - *ppath = path; - else + for (size_t i = 0; i < ARR_SIZE(config_paths); i++) { + char *path = xdgConfigFind(config_paths[i], NULL); + FILE *ret = fopen(path, "r"); + if (ret) { + printf_errf("(): file is %s", path); + if (ppath) { + *ppath = strdup(path); + } + } free(path); - if (f) - return f; - - // Then check user configuration file in $HOME - if ((home = getenv("HOME")) && strlen(home)) { - path = mstrjoin(home, config_filename_legacy); - f = fopen(path, "r"); - if (f && ppath) - *ppath = path; - else - free(path); - if (f) - return f; - } - - // Check system configuration file in $XDG_CONFIG_DIRS at last - if ((dir = getenv("XDG_CONFIG_DIRS")) && strlen(dir)) { - char *part = strtok(dir, ":"); - while (part) { - path = mstrjoin(part, config_filename); - f = fopen(path, "r"); - if (f && ppath) - *ppath = path; - else - free(path); - if (f) - return f; - part = strtok(NULL, ":"); + if (ret) { + return ret; } } - else { - path = mstrjoin(config_system_dir, config_filename); - f = fopen(path, "r"); - if (f && ppath) + + // Fall back to legacy config file names + const char *home = getenv("HOME"); + if (home && strlen(home)) { + auto path = ccalloc(strlen(home)+strlen(config_filename_legacy)+1, char); + strcpy(path, home); + strcpy(path+strlen(home), config_filename_legacy); + FILE *ret = fopen(path, "r"); + if (ret && ppath) *ppath = path; else free(path); - if (f) - return f; + return ret; } - return NULL; } @@ -199,7 +168,7 @@ void parse_config_libconfig(session_t *ps, bool *shadow_enable, int read_result = config_read(&cfg, f); fclose(f); f = NULL; - if (CONFIG_FALSE == read_result) { + if (read_result == CONFIG_FALSE) { printf("Error when reading configuration file \"%s\", line %d: %s\n", path, config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); @@ -210,7 +179,7 @@ void parse_config_libconfig(session_t *ps, bool *shadow_enable, config_set_auto_convert(&cfg, 1); if (path != ps->o.config_file) { - free(ps->o.config_file); + assert(ps->o.config_file == NULL); ps->o.config_file = path; } diff --git a/src/diagnostic.c b/src/diagnostic.c index 85d971a..0a37dd4 100644 --- a/src/diagnostic.c +++ b/src/diagnostic.c @@ -17,6 +17,7 @@ void print_diagnostics(session_t *ps) { #ifdef __FAST_MATH__ printf("* Fast Math: Yes\n"); #endif + printf("* Config file used: %s\n", ps->o.config_file ?: "None"); } // vim: set noet sw=8 ts=8 : diff --git a/src/meson.build b/src/meson.build index bd874c4..58f323e 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,7 +1,7 @@ deps = [ cc.find_library('m'), cc.find_library('ev'), - dependency('xcb', version: '>=1.9.2') + dependency('xcb', version: '>=1.9.2'), ] srcs = ['compton.c', 'win.c', 'c2.c', 'x.c', 'config.c', 'diagnostic.c', 'string_utils.c'] @@ -25,7 +25,8 @@ if get_option('xinerama') endif if get_option('config_file') - deps += [dependency('libconfig', version: '>=1.4', required: true)] + deps += [dependency('libconfig', version: '>=1.4', required: true), + dependency('libxdg-basedir', required: true)] cflags += ['-DCONFIG_LIBCONFIG'] srcs += [ 'config_libconfig.c' ] endif