Skip to content

Commit

Permalink
Use libxdg-basedir for finding config files
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
yshui committed Dec 15, 2018
1 parent 68873ef commit 94b1bc0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 68 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
99 changes: 34 additions & 65 deletions src/config_libconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <libgen.h>
#include <libconfig.h>
#include <basedir_fs.h>

#include "common.h"
#include "config.h"
Expand Down Expand Up @@ -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";

char *dir = NULL, *home = NULL;
char *path = cpath;
FILE *f = NULL;

if (path) {
f = fopen(path, "r");
if (f && ppath)
*ppath = path;
return f;
}

// 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;
static const char *config_paths[] = {
"/compton.conf",
"/compton/compton.conf"
};
static const char config_filename_legacy[] = "/.compton.conf";

path = mstrjoin3(home, config_home_suffix, config_filename);
if (cpath) {
FILE *ret = fopen(cpath, "r");
if (ret && ppath)
*ppath = cpath;
return ret;
}
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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/diagnostic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
5 changes: 3 additions & 2 deletions src/meson.build
Original file line number Diff line number Diff line change
@@ -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']
Expand All @@ -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
Expand Down

0 comments on commit 94b1bc0

Please sign in to comment.