Skip to content

Commit

Permalink
Honor the XDG basedir specification
Browse files Browse the repository at this point in the history
If $XDG_CONFIG_DIRS is set, check if the config is present in one of
those directories. Otherwise, fallback to /etc/xdg, as in the spec:

https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html

Finally, fallback to SYSTEM_CONFIG_FILE_PATH for backwards compatibility.

Fix #477 and #525
  • Loading branch information
JoseExposito committed Oct 17, 2021
1 parent 02a39c6 commit b90e078
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/utils/paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#include <unistd.h>

#include <stdexcept>
#include <string>
#include <vector>

#include "utils/string.h"

std::filesystem::path Paths::getHomePath() {
// $HOME should be checked first
Expand Down Expand Up @@ -76,6 +80,24 @@ std::filesystem::path Paths::getUserLockFilePath() {
}

std::filesystem::path Paths::getSystemConfigFilePath() {
// If $XDG_CONFIG_DIRS is set, check if the config is present in one of those
// directories. Otherwise, fallback to /etc/xdg, as in the spec:
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
// Finally, fallback to SYSTEM_CONFIG_FILE_PATH for backwards compatibility.
const char *xdgConfigDirsEnvVar = getenv("XDG_CONFIG_DIRS");
std::vector<std::string> xdgPaths = (xdgConfigDirsEnvVar != nullptr)
? split(xdgConfigDirsEnvVar, ':')
: std::vector<std::string>();
xdgPaths.emplace_back("/etc/xdg");

for (const std::string &path : xdgPaths) {
std::filesystem::path configPath = std::filesystem::path{path};
configPath = configPath / "touchegg" / "touchegg.conf";
if (std::filesystem::exists(configPath)) {
return configPath;
}
}

return std::filesystem::path{SYSTEM_CONFIG_FILE_PATH};
}

Expand Down

0 comments on commit b90e078

Please sign in to comment.