From 0233e0eeec03f9eb00b98bbf04301852431c0cdd Mon Sep 17 00:00:00 2001 From: Andreas Backx Date: Fri, 25 Dec 2020 20:54:38 +0000 Subject: [PATCH 1/3] Added waybar_output.identifier support. Resolves #602. --- include/bar.hpp | 1 + include/client.hpp | 15 ++++++++------- src/client.cpp | 48 ++++++++++++++++++++++++++++------------------ 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/include/bar.hpp b/include/bar.hpp index 8aab8f7..d6cd895 100644 --- a/include/bar.hpp +++ b/include/bar.hpp @@ -17,6 +17,7 @@ class Factory; struct waybar_output { Glib::RefPtr monitor; std::string name; + std::string identifier; std::unique_ptr xdg_output = { nullptr, &zxdg_output_v1_destroy}; diff --git a/include/client.hpp b/include/client.hpp index 05215cc..f533942 100644 --- a/include/client.hpp +++ b/include/client.hpp @@ -6,6 +6,7 @@ #include #include #include + #include "bar.hpp" struct zwlr_layer_shell_v1; @@ -33,18 +34,18 @@ class Client { std::tuple getConfigs(const std::string &config, const std::string &style) const; void bindInterfaces(); - const std::string getValidPath(const std::vector &paths) const; - void handleOutput(struct waybar_output &output); - bool isValidOutput(const Json::Value &config, struct waybar_output &output); - auto setupConfig(const std::string &config_file) -> void; - auto setupCss(const std::string &css_file) -> void; - struct waybar_output &getOutput(void *); + const std::string getValidPath(const std::vector &paths) const; + void handleOutput(struct waybar_output &output); + bool isValidOutput(const Json::Value &config, struct waybar_output &output); + auto setupConfig(const std::string &config_file) -> void; + auto setupCss(const std::string &css_file) -> void; + struct waybar_output & getOutput(void *); std::vector getOutputConfigs(struct waybar_output &output); static void handleGlobal(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version); static void handleGlobalRemove(void *data, struct wl_registry *registry, uint32_t name); - static void handleOutputName(void *, struct zxdg_output_v1 *, const char *); + static void handleOutputDescription(void *, struct zxdg_output_v1 *, const char *); void handleMonitorAdded(Glib::RefPtr monitor); void handleMonitorRemoved(Glib::RefPtr monitor); diff --git a/src/client.cpp b/src/client.cpp index 005761e..627904c 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1,12 +1,14 @@ #include "client.hpp" + #include #include + #include #include -#include "util/clara.hpp" -#include "util/json.hpp" #include "idle-inhibit-unstable-v1-client-protocol.h" +#include "util/clara.hpp" +#include "util/json.hpp" #include "wlr-layer-shell-unstable-v1-client-protocol.h" waybar::Client *waybar::Client::inst() { @@ -59,8 +61,8 @@ void waybar::Client::handleOutput(struct waybar_output &output) { .logical_position = [](void *, struct zxdg_output_v1 *, int32_t, int32_t) {}, .logical_size = [](void *, struct zxdg_output_v1 *, int32_t, int32_t) {}, .done = [](void *, struct zxdg_output_v1 *) {}, - .name = &handleOutputName, - .description = [](void *, struct zxdg_output_v1 *, const char *) {}, + .name = [](void *, struct zxdg_output_v1 *, const char *) {}, + .description = &handleOutputDescription, }; // owned by output->monitor; no need to destroy auto wl_output = gdk_wayland_monitor_get_wl_output(output.monitor->gobj()); @@ -71,18 +73,21 @@ void waybar::Client::handleOutput(struct waybar_output &output) { bool waybar::Client::isValidOutput(const Json::Value &config, struct waybar_output &output) { if (config["output"].isArray()) { for (auto const &output_conf : config["output"]) { - if (output_conf.isString() && output_conf.asString() == output.name) { + if (output_conf.isString() && + (output_conf.asString() == output.name || output_conf.asString() == output.identifier)) { + std::cout << output_conf.asString() << std::endl; return true; } } return false; } else if (config["output"].isString()) { - auto config_output_name = config["output"].asString(); - if (!config_output_name.empty()) { - if (config_output_name.substr(0, 1) == "!") { - return config_output_name.substr(1) != output.name; + auto config_output = config["output"].asString(); + if (!config_output.empty()) { + if (config_output.substr(0, 1) == "!") { + return config_output.substr(1) != output.name || + config_output.substr(1) != output.identifier; } - return config_output_name == output.name; + return config_output == output.name || config_output == output.identifier; } } @@ -112,16 +117,20 @@ std::vector waybar::Client::getOutputConfigs(struct waybar_output & return configs; } -void waybar::Client::handleOutputName(void * data, struct zxdg_output_v1 * /*xdg_output*/, - const char *name) { +void waybar::Client::handleOutputDescription(void *data, struct zxdg_output_v1 * /*xdg_output*/, + const char *description) { auto client = waybar::Client::inst(); try { - auto &output = client->getOutput(data); - output.name = name; - spdlog::debug("Output detected: {} ({} {})", - name, - output.monitor->get_manufacturer(), - output.monitor->get_model()); + auto & output = client->getOutput(data); + const char *open_paren = strrchr(description, '('); + const char *close_paren = strrchr(description, ')'); + + // Description format: "identifier (name)" + size_t identifier_length = open_paren - description; + output.identifier = std::string(description, identifier_length - 1); + output.name = std::string(description + identifier_length + 1, close_paren - open_paren - 1); + + spdlog::debug("Output detected: {}", description); auto configs = client->getOutputConfigs(output); if (configs.empty()) { output.xdg_output.reset(); @@ -260,7 +269,8 @@ int waybar::Client::main(int argc, char *argv[]) { if (!log_level.empty()) { spdlog::set_level(spdlog::level::from_str(log_level)); } - gtk_app = Gtk::Application::create(argc, argv, "fr.arouillard.waybar", Gio::APPLICATION_HANDLES_COMMAND_LINE); + gtk_app = Gtk::Application::create( + argc, argv, "fr.arouillard.waybar", Gio::APPLICATION_HANDLES_COMMAND_LINE); gdk_display = Gdk::Display::get_default(); if (!gdk_display) { throw std::runtime_error("Can't find display"); From e5684c6127d2fb063a7171a35cd52e03d5187ab3 Mon Sep 17 00:00:00 2001 From: Andreas Backx Date: Fri, 25 Dec 2020 23:03:01 +0000 Subject: [PATCH 2/3] Separated name and description setup and moved bar creation to done callback. --- include/client.hpp | 2 ++ src/client.cpp | 51 ++++++++++++++++++++++++++++++++-------------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/include/client.hpp b/include/client.hpp index f533942..f2eafb1 100644 --- a/include/client.hpp +++ b/include/client.hpp @@ -45,6 +45,8 @@ class Client { static void handleGlobal(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version); static void handleGlobalRemove(void *data, struct wl_registry *registry, uint32_t name); + static void handleOutputDone(void *, struct zxdg_output_v1 *); + static void handleOutputName(void *, struct zxdg_output_v1 *, const char *); static void handleOutputDescription(void *, struct zxdg_output_v1 *, const char *); void handleMonitorAdded(Glib::RefPtr monitor); void handleMonitorRemoved(Glib::RefPtr monitor); diff --git a/src/client.cpp b/src/client.cpp index 627904c..4240f09 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -60,8 +60,8 @@ void waybar::Client::handleOutput(struct waybar_output &output) { static const struct zxdg_output_v1_listener xdgOutputListener = { .logical_position = [](void *, struct zxdg_output_v1 *, int32_t, int32_t) {}, .logical_size = [](void *, struct zxdg_output_v1 *, int32_t, int32_t) {}, - .done = [](void *, struct zxdg_output_v1 *) {}, - .name = [](void *, struct zxdg_output_v1 *, const char *) {}, + .done = &handleOutputDone, + .name = &handleOutputName, .description = &handleOutputDescription, }; // owned by output->monitor; no need to destroy @@ -75,7 +75,6 @@ bool waybar::Client::isValidOutput(const Json::Value &config, struct waybar_outp for (auto const &output_conf : config["output"]) { if (output_conf.isString() && (output_conf.asString() == output.name || output_conf.asString() == output.identifier)) { - std::cout << output_conf.asString() << std::endl; return true; } } @@ -84,7 +83,7 @@ bool waybar::Client::isValidOutput(const Json::Value &config, struct waybar_outp auto config_output = config["output"].asString(); if (!config_output.empty()) { if (config_output.substr(0, 1) == "!") { - return config_output.substr(1) != output.name || + return config_output.substr(1) != output.name && config_output.substr(1) != output.identifier; } return config_output == output.name || config_output == output.identifier; @@ -117,20 +116,12 @@ std::vector waybar::Client::getOutputConfigs(struct waybar_output & return configs; } -void waybar::Client::handleOutputDescription(void *data, struct zxdg_output_v1 * /*xdg_output*/, - const char *description) { +void waybar::Client::handleOutputDone(void *data, struct zxdg_output_v1 * /*xdg_output*/) { auto client = waybar::Client::inst(); try { - auto & output = client->getOutput(data); - const char *open_paren = strrchr(description, '('); - const char *close_paren = strrchr(description, ')'); + auto &output = client->getOutput(data); + spdlog::debug("Output detection done: {} ({})", output.name, output.identifier); - // Description format: "identifier (name)" - size_t identifier_length = open_paren - description; - output.identifier = std::string(description, identifier_length - 1); - output.name = std::string(description + identifier_length + 1, close_paren - open_paren - 1); - - spdlog::debug("Output detected: {}", description); auto configs = client->getOutputConfigs(output); if (configs.empty()) { output.xdg_output.reset(); @@ -148,6 +139,36 @@ void waybar::Client::handleOutputDescription(void *data, struct zxdg_output_v1 * } } +void waybar::Client::handleOutputName(void * data, struct zxdg_output_v1 * /*xdg_output*/, + const char *name) { + auto client = waybar::Client::inst(); + try { + auto &output = client->getOutput(data); + spdlog::debug("Output detected with name: {} ({} {})", + name, + output.monitor->get_manufacturer(), + output.monitor->get_model()); + output.name = name; + } catch (const std::exception &e) { + std::cerr << e.what() << std::endl; + } +} + +void waybar::Client::handleOutputDescription(void *data, struct zxdg_output_v1 * /*xdg_output*/, + const char *description) { + auto client = waybar::Client::inst(); + try { + auto & output = client->getOutput(data); + const char *open_paren = strrchr(description, '('); + + // Description format: "identifier (name)" + size_t identifier_length = open_paren - description; + output.identifier = std::string(description, identifier_length - 1); + } catch (const std::exception &e) { + std::cerr << e.what() << std::endl; + } +} + void waybar::Client::handleMonitorAdded(Glib::RefPtr monitor) { auto &output = outputs_.emplace_back(); output.monitor = monitor; From 3fbbbf85416cb6755cd0b6fb81f84e8938b58658 Mon Sep 17 00:00:00 2001 From: Andreas Backx Date: Fri, 25 Dec 2020 23:31:29 +0000 Subject: [PATCH 3/3] Removed redundant log line. --- src/client.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index 4240f09..ad281d5 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -144,10 +144,6 @@ void waybar::Client::handleOutputName(void * data, struct zxdg_output_v1 * auto client = waybar::Client::inst(); try { auto &output = client->getOutput(data); - spdlog::debug("Output detected with name: {} ({} {})", - name, - output.monitor->get_manufacturer(), - output.monitor->get_model()); output.name = name; } catch (const std::exception &e) { std::cerr << e.what() << std::endl;