diff --git a/src/blacklist.cpp b/src/blacklist.cpp index 99dbe913..5cffee19 100644 --- a/src/blacklist.cpp +++ b/src/blacklist.cpp @@ -7,19 +7,13 @@ #include "file_utils.h" static std::string get_proc_name() { -#ifdef _GNU_SOURCE_OFF - std::string p(program_invocation_name); - std::string proc_name = p.substr(p.find_last_of("/\\") + 1); -#else - std::string p = get_exe_path(); - std::string proc_name; - if (ends_with(p, "wine-preloader") || ends_with(p, "wine64-preloader")) { - get_wine_exe_name(proc_name, true); - } else { - proc_name = p.substr(p.find_last_of("/\\") + 1); + // Note: It is possible to use GNU program_invocation_short_name. + const std::string proc_name = get_wine_exe_name(/*keep_ext=*/true); + if (!proc_name.empty()) { + return proc_name; } -#endif - return proc_name; + const std::string p = get_exe_path(); + return p.substr(p.find_last_of("/\\") + 1); } static std::vector blacklist { diff --git a/src/config.cpp b/src/config.cpp index 95407235..616594b3 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -3,13 +3,14 @@ #include #include #include +#include +#include #include "config.h" #include "file_utils.h" #include "string_utils.h" #include "hud_elements.h" -std::string program_name; -void parseConfigLine(std::string line, std::unordered_map& options) { +static void parseConfigLine(std::string line, std::unordered_map& options) { std::string param, value; if (line.find("#") != std::string::npos) @@ -30,42 +31,67 @@ void parseConfigLine(std::string line, std::unordered_map& paths) -{ +static std::string get_program_dir() { + const std::string exe_path = get_exe_path(); + if (exe_path.empty()) { + return std::string(); + } + const auto n = exe_path.find_last_of('/'); + if (n != std::string::npos) { + return exe_path.substr(0, n); + } + return std::string(); +} + +std::string get_program_name() { + const std::string exe_path = get_exe_path(); + std::string basename = "unknown"; + if (exe_path.empty()) { + return basename; + } + const auto n = exe_path.find_last_of('/'); + if (n == std::string::npos) { + return basename; + } + if (n < exe_path.size() - 1) { + // An executable's name. + basename = exe_path.substr(n + 1); + } + return basename; +} + +static void enumerate_config_files(std::vector& paths) { static const char *mangohud_dir = "/MangoHud/"; - std::string env_data = get_data_dir(); - std::string env_config = get_config_dir(); + const std::string data_dir = get_data_dir(); + const std::string config_dir = get_config_dir(); + + const std::string program_name = get_program_name(); + + if (config_dir.empty()) { + // If we can't find 'HOME' just abandon hope. + return; + } + + paths.push_back(config_dir + mangohud_dir + "MangoHud.conf"); - if (!env_config.empty()) - paths.push_back(env_config + mangohud_dir + "MangoHud.conf"); #ifdef _WIN32 paths.push_back("C:\\mangohud\\MangoHud.conf"); #endif - 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); - program_name = basename; - 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 (!env_config.empty() && (basename == "wine-preloader" || basename == "wine64-preloader")) { - - std::string name; - if (get_wine_exe_name(name)) { - paths.push_back(env_config + mangohud_dir + "wine-" + name + ".conf"); - } - program_name = name; - } + + if (!program_name.empty()) { + paths.push_back(config_dir + mangohud_dir + program_name + ".conf"); + } + + const std::string program_dir = get_program_dir(); + if (!program_dir.empty()) { + paths.push_back(program_dir + "/MangoHud.conf"); } - if (program_name.empty()) - program_name = "Unknown"; + + const std::string wine_program_name = get_wine_exe_name(); + if (!wine_program_name.empty()) { + paths.push_back(config_dir + mangohud_dir + "wine-" + wine_program_name + ".conf"); + } } void parseConfigFile(overlay_params& params) { diff --git a/src/config.h b/src/config.h index 4f37e531..ca1b5104 100644 --- a/src/config.h +++ b/src/config.h @@ -3,6 +3,9 @@ #define MANGOHUD_CONFIG_H #include "overlay_params.h" +#include + void parseConfigFile(overlay_params& p); -extern std::string program_name; +std::string get_program_name(); + #endif //MANGOHUD_CONFIG_H diff --git a/src/file_utils.cpp b/src/file_utils.cpp index b268971d..b7fc978d 100644 --- a/src/file_utils.cpp +++ b/src/file_utils.cpp @@ -5,9 +5,9 @@ #include #include #include -#include #include #include +#include std::string read_line(const std::string& filename) { @@ -112,13 +112,18 @@ std::string get_exe_path() return read_symlink("/proc/self/exe"); } -bool get_wine_exe_name(std::string& name, bool keep_ext) +std::string get_wine_exe_name(bool keep_ext) { + const std::string exe_path = get_exe_path(); + if (!ends_with(exe_path, "wine-preloader") && !ends_with(exe_path, "wine64-preloader")) { + return std::string(); + } + std::string line; std::ifstream cmdline("/proc/self/cmdline"); - auto n = std::string::npos; - while (std::getline(cmdline, line, '\0')) - { + // Iterate over arguments (separated by NUL byte). + while (std::getline(cmdline, line, '\0')) { + auto n = std::string::npos; if (!line.empty() && ((n = line.find_last_of("/\\")) != std::string::npos) && n < line.size() - 1) // have at least one character @@ -126,17 +131,15 @@ bool get_wine_exe_name(std::string& name, bool keep_ext) auto dot = keep_ext ? std::string::npos : line.find_last_of('.'); if (dot < n) dot = line.size(); - name = line.substr(n + 1, dot - n - 1); - return true; + return line.substr(n + 1, dot - n - 1); } else if (ends_with(line, ".exe", true)) { auto dot = keep_ext ? std::string::npos : line.find_last_of('.'); - name = line.substr(0, dot); - return true; + return line.substr(0, dot); } } - return false; + return std::string(); } std::string get_home_dir() diff --git a/src/file_utils.h b/src/file_utils.h index 43f9f920..f30164c9 100644 --- a/src/file_utils.h +++ b/src/file_utils.h @@ -20,7 +20,7 @@ bool file_exists(const std::string& path); bool dir_exists(const std::string& path); std::string read_symlink(const char * link); std::string get_exe_path(); -bool get_wine_exe_name(std::string& name, bool keep_ext = false); +std::string get_wine_exe_name(bool keep_ext = false); std::string get_home_dir(); std::string get_data_dir(); std::string get_config_dir(); diff --git a/src/file_utils_win32.cpp b/src/file_utils_win32.cpp index 211ddd89..7ec032c5 100644 --- a/src/file_utils_win32.cpp +++ b/src/file_utils_win32.cpp @@ -1,8 +1,7 @@ #include "file_utils.h" #include "string_utils.h" -#include #include -#include +#include std::string read_line(const std::string& filename) { @@ -43,9 +42,9 @@ std::string get_exe_path() return std::string(); } -bool get_wine_exe_name(std::string& name, bool keep_ext) +std::string get_wine_exe_name(bool keep_ext) { - return false; + return std::string(); } std::string get_home_dir() diff --git a/src/logging.cpp b/src/logging.cpp index b04fd9c6..d902d470 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -127,7 +127,7 @@ void Logger::stop_logging() { std::thread(calculate_benchmark_data, m_params).detach(); if(!m_params->output_folder.empty()) { - m_log_files.emplace_back(m_params->output_folder + "/" + program_name + "_" + get_log_suffix()); + m_log_files.emplace_back(m_params->output_folder + "/" + get_program_name() + "_" + get_log_suffix()); std::thread(writeFile, m_log_files.back()).detach(); } }