config file: respect XDG dirs

Other half of #37
pull/41/head
jackun 4 years ago
parent ab04249782
commit 65b90fc01b
No known key found for this signature in database
GPG Key ID: 119DB3F1D05A9ED3

@ -38,30 +38,30 @@ void parseConfigLine(std::string line){
void parseConfigFile() {
std::vector<std::string> paths;
std::string home;
static const char *config_dir = "/.config/MangoHud/";
static const char *mangohud_dir = "/MangoHud/";
const char *env_home = std::getenv("HOME");
if (env_home)
home = env_home;
if (!home.empty()) {
paths.push_back(home + "/.local/share/MangoHud/MangoHud.conf");
paths.push_back(home + config_dir + "MangoHud.conf");
}
std::string env_data = get_data_dir();
std::string env_config = get_config_dir();
if (!env_data.empty())
paths.push_back(env_data + mangohud_dir + "MangoHud.conf");
if (!env_config.empty())
paths.push_back(env_config + mangohud_dir + "MangoHud.conf");
std::string exe_path = get_exe_path();
auto n = exe_path.find_last_of('/');
if (!exe_path.empty() && n != std::string::npos && n < exe_path.size() - 1) {
// as executable's name
std::string basename = exe_path.substr(n + 1);
if (!home.empty())
paths.push_back(home + config_dir + basename + ".conf");
if (!env_config.empty())
paths.push_back(env_config + mangohud_dir + basename + ".conf");
// in executable's folder though not much sense in /usr/bin/
paths.push_back(exe_path.substr(0, n) + "/MangoHud.conf");
// find executable's path when run in Wine
if (!home.empty() && (basename == "wine-preloader" || basename == "wine64-preloader")) {
if (!env_config.empty() && (basename == "wine-preloader" || basename == "wine64-preloader")) {
std::string line;
std::ifstream stream("/proc/self/cmdline");
while (std::getline(stream, line, '\0'))
@ -73,7 +73,7 @@ void parseConfigFile() {
auto dot = line.find_last_of('.');
if (dot < n)
dot = line.size();
paths.push_back(home + config_dir + "wine-" + line.substr(n + 1, dot - n - 1) + ".conf");
paths.push_back(env_config + mangohud_dir + "wine-" + line.substr(n + 1, dot - n - 1) + ".conf");
break;
}
}
@ -98,4 +98,4 @@ void parseConfigFile() {
std::cerr << " [ ok ]" << std::endl;
}
}
}

@ -105,3 +105,37 @@ std::string get_exe_path()
ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
return std::string(result, (count > 0) ? count : 0);
}
std::string get_home_dir()
{
std::string path;
const char* p = getenv("HOME");
if (p)
path = p;
return path;
}
std::string get_data_dir()
{
const char* p = getenv("XDG_DATA_HOME");
if (p)
return p;
std::string path = get_home_dir();
if (!path.empty())
path += "/.local/share";
return path;
}
std::string get_config_dir()
{
const char* p = getenv("XDG_CONFIG_HOME");
if (p)
return p;
std::string path = get_home_dir();
if (!path.empty())
path += "/.config";
return path;
}

@ -16,3 +16,6 @@ std::vector<std::string> ls(const char* root, const char* prefix = nullptr, LS_F
bool file_exists(const std::string& path);
bool dir_exists(const std::string& path);
std::string get_exe_path();
std::string get_home_dir();
std::string get_data_dir();
std::string get_config_dir();

Loading…
Cancel
Save