2023-01-29 15:47:50 +00:00
|
|
|
#include "Config.h"
|
|
|
|
#include "Common.h"
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
static Config config;
|
|
|
|
|
|
|
|
const Config& Config::Get()
|
|
|
|
{
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2023-01-29 16:38:40 +00:00
|
|
|
template<typename T>
|
|
|
|
void ApplyProperty(T& propertyToSet, const std::string_view& value);
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void ApplyProperty<std::string>(std::string& propertyToSet, const std::string_view& value)
|
|
|
|
{
|
|
|
|
propertyToSet = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void ApplyProperty<uint32_t>(uint32_t& propertyToSet, const std::string_view& value)
|
|
|
|
{
|
|
|
|
// Why, C++?
|
|
|
|
std::string valStr = std::string(value);
|
|
|
|
propertyToSet = atoi(valStr.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void ApplyProperty<bool>(bool& propertyToSet, const std::string_view& value)
|
|
|
|
{
|
|
|
|
// Why, C++?
|
|
|
|
if (value == "true")
|
|
|
|
{
|
|
|
|
propertyToSet = true;
|
|
|
|
}
|
|
|
|
else if (value == "false")
|
|
|
|
{
|
|
|
|
propertyToSet = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG("Invalid value for bool property: " << value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void AddConfigVar(const std::string& propertyName, T& propertyToSet, std::string_view line, bool& setConfig)
|
|
|
|
{
|
|
|
|
const char* whitespace = " \t";
|
|
|
|
if (setConfig)
|
|
|
|
{
|
|
|
|
// Don't bother, already found something else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip empty space at the beginning
|
|
|
|
size_t firstChar = line.find_last_not_of(whitespace);
|
|
|
|
if (firstChar == std::string::npos)
|
|
|
|
{
|
|
|
|
// Line is empty, don't need to check anymore
|
|
|
|
setConfig = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
line = line.substr(line.find_first_not_of(whitespace));
|
|
|
|
|
|
|
|
// Check if line starts with [propertyName]:
|
|
|
|
if (line.find(propertyName + ":") != 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
size_t colon = line.find_first_of(":");
|
|
|
|
|
|
|
|
// Now get the value part
|
|
|
|
std::string_view value = line.substr(colon + 1);
|
|
|
|
size_t beginValue = value.find_first_not_of(whitespace);
|
|
|
|
size_t endValue = value.find_last_not_of(whitespace);
|
|
|
|
value = value.substr(beginValue, endValue - beginValue + 1);
|
|
|
|
|
|
|
|
// Set value
|
|
|
|
ApplyProperty<T>(propertyToSet, value);
|
|
|
|
LOG("Set value for " << propertyName << ": " << value);
|
|
|
|
setConfig = true;
|
|
|
|
}
|
|
|
|
|
2023-01-29 15:47:50 +00:00
|
|
|
void Config::Load()
|
|
|
|
{
|
|
|
|
const char* xdgConfigHome = getenv("XDG_CONFIG_HOME");
|
|
|
|
std::ifstream file;
|
|
|
|
if (xdgConfigHome)
|
|
|
|
{
|
|
|
|
file = std::ifstream(std::string(xdgConfigHome) + "/gBar/config");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string home = getenv("HOME");
|
|
|
|
file = std::ifstream(home + "/.config/gBar/config");
|
|
|
|
}
|
|
|
|
if (!file.is_open())
|
|
|
|
{
|
|
|
|
LOG("Failed opening config!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::string line;
|
|
|
|
while (std::getline(file, line))
|
|
|
|
{
|
2023-01-29 16:38:40 +00:00
|
|
|
std::string_view lineView = {line};
|
|
|
|
// Strip comments
|
|
|
|
size_t comment = line.find_first_of('#');
|
|
|
|
if (comment == 0)
|
2023-01-29 15:47:50 +00:00
|
|
|
{
|
2023-01-29 16:38:40 +00:00
|
|
|
continue;
|
2023-01-29 15:47:50 +00:00
|
|
|
}
|
2023-01-29 16:38:40 +00:00
|
|
|
if (comment != std::string_view::npos)
|
2023-01-29 15:47:50 +00:00
|
|
|
{
|
2023-01-29 16:38:40 +00:00
|
|
|
lineView = lineView.substr(comment - 1);
|
2023-01-29 15:47:50 +00:00
|
|
|
}
|
2023-01-29 16:38:40 +00:00
|
|
|
|
|
|
|
bool foundProperty = false;
|
|
|
|
AddConfigVar("CPUThermalZone", config.cpuThermalZone, lineView, foundProperty);
|
|
|
|
AddConfigVar("SuspendCommand", config.suspendCommand, lineView, foundProperty);
|
|
|
|
AddConfigVar("LockCommand", config.lockCommand, lineView, foundProperty);
|
|
|
|
AddConfigVar("ExitCommand", config.exitCommand, lineView, foundProperty);
|
|
|
|
AddConfigVar("BatteryFolder", config.batteryFolder, lineView, foundProperty);
|
|
|
|
AddConfigVar("DefaultWorkspaceSymbol", config.defaultWorkspaceSymbol, lineView, foundProperty);
|
|
|
|
for (int i = 1; i < 10; i++)
|
2023-01-29 15:47:50 +00:00
|
|
|
{
|
2023-01-29 16:38:40 +00:00
|
|
|
// Subtract 1 to index from 1 to 9 rather than 0 to 8
|
|
|
|
AddConfigVar("WorkspaceSymbol-" + std::to_string(i), config.workspaceSymbols[i - 1], lineView, foundProperty);
|
2023-01-29 15:47:50 +00:00
|
|
|
}
|
2023-01-29 16:38:40 +00:00
|
|
|
|
2023-02-04 14:07:05 +00:00
|
|
|
AddConfigVar("CenterTime", config.centerTime, lineView, foundProperty);
|
|
|
|
AddConfigVar("AudioRevealer", config.audioRevealer, lineView, foundProperty);
|
|
|
|
|
2023-01-29 16:38:40 +00:00
|
|
|
if (foundProperty == false)
|
2023-01-29 15:47:50 +00:00
|
|
|
{
|
|
|
|
LOG("Warning: unknown config var: " << line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-30 15:57:15 +00:00
|
|
|
|
|
|
|
RuntimeConfig& RuntimeConfig::Get()
|
|
|
|
{
|
|
|
|
static RuntimeConfig config;
|
|
|
|
return config;
|
|
|
|
}
|