From e717e0e7ad03c9b9319a3799a76521fce67a1361 Mon Sep 17 00:00:00 2001 From: scorpion-26 Date: Sun, 21 Jan 2024 17:13:45 +0100 Subject: [PATCH] Add a CLI option to override the config dir The CLI option -c/--config overrides the search path, and appends itself to the front of the CSS search path. Partially implements https://github.com/scorpion-26/gBar/issues/75 --- src/CSS.cpp | 23 ++++++++++++++--------- src/CSS.h | 3 ++- src/Config.cpp | 8 ++++++-- src/Config.h | 2 +- src/System.cpp | 4 ++-- src/System.h | 2 +- src/Window.cpp | 6 +++--- src/Window.h | 2 +- src/gBar.cpp | 29 +++++++++++++++++++---------- 9 files changed, 49 insertions(+), 30 deletions(-) diff --git a/src/CSS.cpp b/src/CSS.cpp index 13f26e6..b723cc1 100644 --- a/src/CSS.cpp +++ b/src/CSS.cpp @@ -9,31 +9,36 @@ namespace CSS { static GtkCssProvider* sProvider; - void Load() + void Load(const std::string& overrideConfigLocation) { sProvider = gtk_css_provider_new(); std::vector locations; const char* home = std::getenv("HOME"); + if (overrideConfigLocation != "") + { + locations.push_back(overrideConfigLocation); + } + const char* configHome = std::getenv("XDG_CONFIG_HOME"); if (configHome && strlen(configHome) != 0) { - locations.push_back(configHome); + locations.push_back(std::string(configHome) + "/gBar"); } else if (home) { - locations.push_back(std::string(home) + "/.config"); + locations.push_back(std::string(home) + "/.config/gBar"); } const char* dataHome = std::getenv("XDG_DATA_HOME"); if (dataHome && strlen(dataHome) != 0) { - locations.push_back(dataHome); + locations.push_back(std::string(dataHome) + "/gBar"); } else if (home) { - locations.push_back(std::string(home) + "/.local/share"); + locations.push_back(std::string(home) + "/.local/share/gBar"); } const char* dataDirs = std::getenv("XDG_DATA_DIRS"); @@ -42,18 +47,18 @@ namespace CSS std::stringstream ss(dataDirs); std::string dir; while (std::getline(ss, dir, ':')) - locations.push_back(dir); + locations.push_back(dir + "/gBar"); } else { - locations.push_back("/usr/local/share"); - locations.push_back("/usr/share"); + locations.push_back("/usr/local/share/gBar"); + locations.push_back("/usr/share/gBar"); } GError* err = nullptr; for (auto& dir : locations) { - std::string file = dir + "/gBar/style.css"; + std::string file = dir + "/style.css"; if (!std::ifstream(file).is_open()) { diff --git a/src/CSS.h b/src/CSS.h index db9cc05..94bb945 100644 --- a/src/CSS.h +++ b/src/CSS.h @@ -1,8 +1,9 @@ #pragma once #include +#include namespace CSS { - void Load(); + void Load(const std::string& overrideConfigLocation); GtkCssProvider* GetProvider(); } diff --git a/src/Config.cpp b/src/Config.cpp index d448e31..765008a 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -197,11 +197,15 @@ void AddConfigVar(const std::string& propertyName, MapLike& propertyToSet, std:: } } -void Config::Load() +void Config::Load(const std::string& overrideConfigLocation) { const char* xdgConfigHome = getenv("XDG_CONFIG_HOME"); std::ifstream file; - if (xdgConfigHome) + if (overrideConfigLocation != "") + { + file = std::ifstream(overrideConfigLocation + "/config"); + } + else if (xdgConfigHome) { file = std::ifstream(std::string(xdgConfigHome) + "/gBar/config"); } diff --git a/src/Config.h b/src/Config.h index 06d7240..0e0d432 100644 --- a/src/Config.h +++ b/src/Config.h @@ -91,7 +91,7 @@ public: double audioMinVolume = 0.f; // Map the minimum volume to this value double audioMaxVolume = 100.f; // Map the maximum volume to this value - static void Load(); + static void Load(const std::string& overrideConfigLocation); static const Config& Get(); }; diff --git a/src/System.cpp b/src/System.cpp index e845c64..fb04af5 100644 --- a/src/System.cpp +++ b/src/System.cpp @@ -658,11 +658,11 @@ namespace System system(Config::Get().suspendCommand.c_str()); } - void Init() + void Init(const std::string& overrideConfigLocation) { Logging::Init(); - Config::Load(); + Config::Load(overrideConfigLocation); Wayland::Init(); diff --git a/src/System.h b/src/System.h index 7061544..af03a48 100644 --- a/src/System.h +++ b/src/System.h @@ -118,6 +118,6 @@ namespace System void Lock(); void Suspend(); - void Init(); + void Init(const std::string& overrideConfigLocation); void FreeResources(); } diff --git a/src/Window.cpp b/src/Window.cpp index bfe725f..d21b096 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -19,12 +19,12 @@ Window::~Window() } } -void Window::Init(int argc, char** argv) +void Window::Init(const std::string& overideConfigLocation) { - gtk_init(&argc, &argv); + gtk_init(NULL, NULL); // Style - CSS::Load(); + CSS::Load(overideConfigLocation); gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), (GtkStyleProvider*)CSS::GetProvider(), GTK_STYLE_PROVIDER_PRIORITY_USER); diff --git a/src/Window.h b/src/Window.h index 32cf7f2..e11fef4 100644 --- a/src/Window.h +++ b/src/Window.h @@ -27,7 +27,7 @@ public: Window& operator=(Window&& other) noexcept = default; ~Window(); - void Init(int argc, char** argv); + void Init(const std::string& overrideConfigLocation); void Run(); void Close(); diff --git a/src/gBar.cpp b/src/gBar.cpp index bb5dc2a..16c1f09 100644 --- a/src/gBar.cpp +++ b/src/gBar.cpp @@ -55,24 +55,27 @@ void PrintHelp() "\tgBar [OPTIONS...] WIDGET [MONITOR]\n" "\n" "Sample usage:\n" - "\tgBar bar 0\tOpens the status bar on monitor 0\n" - "\tgBar audio\tOpens the audio flyin on the current monitor\n" + "\tgBar bar 0 \tOpens the status bar on monitor 0\n" + "\tgBar audio \tOpens the audio flyin on the current monitor\n" "\n" "All options:\n" - "\t--help/-h \tPrints this help page and exits afterwards\n" + "\t--help/-h \tPrints this help page and exits afterwards\n" + "\t--config/-c DIR\tOverrides the config search path to DIR and appends DIR to the CSS search path.\n" + "\t \t DIR cannot contain path shorthands like e.g. \"~\"" "\n" "All available widgets:\n" - "\tbar \tThe main status bar\n" - "\taudio \tAn audio volume slider flyin\n" - "\tmic \tA microphone volume slider flyin\n" - "\tbluetooth \tA bluetooth connection widget\n" - "\t[plugin] \tTries to open and run the plugin lib[plugin].so\n"); + "\tbar \tThe main status bar\n" + "\taudio \tAn audio volume slider flyin\n" + "\tmic \tA microphone volume slider flyin\n" + "\tbluetooth \tA bluetooth connection widget\n" + "\t[plugin] \tTries to open and run the plugin lib[plugin].so\n"); } int main(int argc, char** argv) { std::string widget; int32_t monitor = -1; + std::string overrideConfigLocation = ""; // Arg parsing for (int i = 1; i < argc; i++) @@ -103,6 +106,12 @@ int main(int argc, char** argv) PrintHelp(); return 0; } + else if (arg == "-c" || arg == "--config") + { + ASSERT(i + 1 < argc, "Not enough arguments provided for -c/--config!"); + overrideConfigLocation = argv[i + 1]; + i += 1; + } else { LOG("Warning: Unknown CLI option \"" << arg << "\"") @@ -122,10 +131,10 @@ int main(int argc, char** argv) } signal(SIGINT, CloseTmpFiles); - System::Init(); + System::Init(overrideConfigLocation); Window window(monitor); - window.Init(argc, argv); + window.Init(overrideConfigLocation); if (widget == "bar") { Bar::Create(window, monitor);