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 <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2018-12-15 19:44:11 +00:00
parent 68873efaa2
commit 94b1bc0387
No known key found for this signature in database
GPG Key ID: 37C999F617EA1A47
4 changed files with 40 additions and 68 deletions

View File

@ -26,10 +26,11 @@ Assuming you already have all the usual building tools installed (e.g. gcc, meso
* xcb-composite * xcb-composite
* xcb-image * xcb-image
* xcb-present * xcb-present
* xcb-xinerama (optional, disable with `-Dxinerama=false` meson configure flag) * xcb-xinerama (optional, disable with the `-Dxinerama=false` meson configure flag)
* pixman * pixman
* libdbus (optional, disable with the `-Ddbus=false` meson configure flag) * libdbus (optional, disable with the `-Ddbus=false` meson configure flag)
* libconfig (optional, disable with the `-Dconfig_file=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) * libGL (optional, disable with the `-Dopengl=false` meson configure flag)
* libpcre (optional, disable with the `-Dregex=false` meson configure flag) * libpcre (optional, disable with the `-Dregex=false` meson configure flag)
* libev * libev

View File

@ -7,6 +7,7 @@
#include <libgen.h> #include <libgen.h>
#include <libconfig.h> #include <libconfig.h>
#include <basedir_fs.h>
#include "common.h" #include "common.h"
#include "config.h" #include "config.h"
@ -36,79 +37,47 @@ lcfg_lookup_bool(const config_t *config, const char *path, bool *value) {
*/ */
FILE * FILE *
open_config_file(char *cpath, char **ppath) { open_config_file(char *cpath, char **ppath) {
static const char *config_filename = "/compton.conf"; static const char *config_paths[] = {
static const char *config_filename_legacy = "/.compton.conf"; "/compton.conf",
static const char *config_home_suffix = "/.config"; "/compton/compton.conf"
static const char *config_system_dir = "/etc/xdg"; };
static const char config_filename_legacy[] = "/.compton.conf";
char *dir = NULL, *home = NULL; if (cpath) {
char *path = cpath; FILE *ret = fopen(cpath, "r");
FILE *f = NULL; if (ret && ppath)
*ppath = cpath;
if (path) { return ret;
f = fopen(path, "r");
if (f && ppath)
*ppath = path;
return f;
} }
// Check user configuration file in $XDG_CONFIG_HOME firstly for (size_t i = 0; i < ARR_SIZE(config_paths); i++) {
if (!((dir = getenv("XDG_CONFIG_HOME")) && strlen(dir))) { char *path = xdgConfigFind(config_paths[i], NULL);
if (!((home = getenv("HOME")) && strlen(home))) FILE *ret = fopen(path, "r");
return NULL; if (ret) {
printf_errf("(): file is %s", path);
path = mstrjoin3(home, config_home_suffix, config_filename); if (ppath) {
} *ppath = strdup(path);
else }
path = mstrjoin(dir, config_filename); }
f = fopen(path, "r");
if (f && ppath)
*ppath = path;
else
free(path); free(path);
if (f) if (ret) {
return f; return ret;
// 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, ":");
} }
} }
else {
path = mstrjoin(config_system_dir, config_filename); // Fall back to legacy config file names
f = fopen(path, "r"); const char *home = getenv("HOME");
if (f && ppath) 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; *ppath = path;
else else
free(path); free(path);
if (f) return ret;
return f;
} }
return NULL; return NULL;
} }
@ -199,7 +168,7 @@ void parse_config_libconfig(session_t *ps, bool *shadow_enable,
int read_result = config_read(&cfg, f); int read_result = config_read(&cfg, f);
fclose(f); fclose(f);
f = NULL; f = NULL;
if (CONFIG_FALSE == read_result) { if (read_result == CONFIG_FALSE) {
printf("Error when reading configuration file \"%s\", line %d: %s\n", printf("Error when reading configuration file \"%s\", line %d: %s\n",
path, config_error_line(&cfg), config_error_text(&cfg)); path, config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg); config_destroy(&cfg);
@ -210,7 +179,7 @@ void parse_config_libconfig(session_t *ps, bool *shadow_enable,
config_set_auto_convert(&cfg, 1); config_set_auto_convert(&cfg, 1);
if (path != ps->o.config_file) { if (path != ps->o.config_file) {
free(ps->o.config_file); assert(ps->o.config_file == NULL);
ps->o.config_file = path; ps->o.config_file = path;
} }

View File

@ -17,6 +17,7 @@ void print_diagnostics(session_t *ps) {
#ifdef __FAST_MATH__ #ifdef __FAST_MATH__
printf("* Fast Math: Yes\n"); printf("* Fast Math: Yes\n");
#endif #endif
printf("* Config file used: %s\n", ps->o.config_file ?: "None");
} }
// vim: set noet sw=8 ts=8 : // vim: set noet sw=8 ts=8 :

View File

@ -1,7 +1,7 @@
deps = [ deps = [
cc.find_library('m'), cc.find_library('m'),
cc.find_library('ev'), 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'] 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 endif
if get_option('config_file') 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'] cflags += ['-DCONFIG_LIBCONFIG']
srcs += [ 'config_libconfig.c' ] srcs += [ 'config_libconfig.c' ]
endif endif