From 6f9fb72cdf691572c43f93e70592aa1b6dda7259 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Pla=C3=A7ais?= Date: Thu, 9 Aug 2018 14:04:48 +0200 Subject: [PATCH] Add different default paths for config and css files --- include/client.hpp | 7 +++++-- src/client.cpp | 32 +++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/include/client.hpp b/include/client.hpp index 81eabd2e..6e70b0dc 100644 --- a/include/client.hpp +++ b/include/client.hpp @@ -1,5 +1,8 @@ #pragma once +#include +#include + #include #include @@ -21,8 +24,8 @@ namespace waybar { struct Client { uint32_t height = 30; - std::string cssFile = "./resources/style.css"; - std::string configFile = "./resources/config"; + std::string cssFile; + std::string configFile; Gtk::Main gtk_main; diff --git a/src/client.cpp b/src/client.cpp index 30e36442..6fb779c8 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -36,7 +36,37 @@ waybar::Client::Client(int argc, char* argv[]) : gtk_main(argc, argv), gdk_display(Gdk::Display::get_default()), wlDisplay(gdk_wayland_display_get_wl_display(gdk_display->gobj())) -{} +{ + auto getFirstValidPath = [] (std::vector possiblePaths) { + wordexp_t p; + + for (std::string path: possiblePaths) { + if (wordexp(path.c_str(), &p, 0) == 0) { + if (access(p.we_wordv[0], F_OK) == 0) { + std::string result = p.we_wordv[0]; + wordfree(&p); + return result; + } else { + wordfree(&p); + } + } + } + + return std::string(); + }; + + configFile = getFirstValidPath({ + "$XDG_CONFIG_HOME/waybar/config", + "$HOME/waybar/config", + "./resources/config", + }); + cssFile = getFirstValidPath({ + "$XDG_CONFIG_HOME/waybar/style.css", + "$HOME/waybar/style.css", + "./resources/style.css", + }); + +} void waybar::Client::bind_interfaces() {