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-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

View File

@ -7,6 +7,7 @@
#include <libgen.h>
#include <libconfig.h>
#include <basedir_fs.h>
#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);
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 (ret) {
return ret;
}
}
else
path = mstrjoin(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;
// 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;
return ret;
}
// 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);
f = fopen(path, "r");
if (f && ppath)
*ppath = path;
else
free(path);
if (f)
return f;
}
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;
}

View File

@ -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 :

View File

@ -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