Merge pull request #2529 from Syndelis/feat/hyprland-window-workspaces

Feature: Hyprland dynamic window names on workspaces
This commit is contained in:
Alexis Rouillard
2023-10-02 19:17:42 +02:00
committed by GitHub
8 changed files with 290 additions and 15 deletions

View File

@ -244,7 +244,9 @@ void waybar::modules::Backlight::upsert_device(ForwardIt first, ForwardIt last,
check_nn(name);
const char *actual_brightness_attr =
strncmp(name, "amdgpu_bl", 9) == 0 || strcmp(name, "apple-panel-bl") == 0 ? "brightness" : "actual_brightness";
strncmp(name, "amdgpu_bl", 9) == 0 || strcmp(name, "apple-panel-bl") == 0
? "brightness"
: "actual_brightness";
const char *actual = udev_device_get_sysattr_value(dev, actual_brightness_attr);
const char *max = udev_device_get_sysattr_value(dev, "max_brightness");

View File

@ -256,7 +256,7 @@ const std::tuple<uint8_t, float, std::string, float> waybar::modules::Battery::g
std::string _status;
/* Check for adapter status if battery is not available */
if(!std::ifstream(bat / "status")) {
if (!std::ifstream(bat / "status")) {
std::getline(std::ifstream(adapter_ / "status"), _status);
} else {
std::getline(std::ifstream(bat / "status"), _status);

View File

@ -17,7 +17,7 @@ waybar::modules::Custom::Custom(const std::string& name, const std::string& id,
delayWorker();
} else if (config_["exec"].isString()) {
continuousWorker();
}
}
}
waybar::modules::Custom::~Custom() {

View File

@ -1,13 +1,17 @@
#include "modules/hyprland/workspaces.hpp"
#include <fmt/ostream.h>
#include <json/value.h>
#include <spdlog/spdlog.h>
#include <algorithm>
#include <charconv>
#include <memory>
#include <optional>
#include <string>
#include "util/rewrite_string.hpp"
namespace waybar::modules::hyprland {
Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value &config)
@ -68,6 +72,16 @@ auto Workspaces::parse_config(const Json::Value &config) -> void {
g_warning("Invalid string representation for sort-by. Falling back to default sort method.");
}
}
Json::Value format_window_separator = config["format-window-separator"];
format_window_separator_ =
format_window_separator.isString() ? format_window_separator.asString() : " ";
window_rewrite_rules_ = config["window-rewrite"];
Json::Value window_rewrite_default = config["window-rewrite-default"];
window_rewrite_default_ =
window_rewrite_default.isString() ? window_rewrite_default.asString() : "?";
}
auto Workspaces::register_ipc() -> void {
@ -184,8 +198,15 @@ void Workspaces::onEvent(const std::string &ev) {
} else {
workspaces_to_remove_.push_back(workspace);
}
} else if (eventName == "openwindow" || eventName == "closewindow" || eventName == "movewindow") {
} else if (eventName == "openwindow") {
update_window_count();
on_window_opened(payload);
} else if (eventName == "closewindow") {
update_window_count();
on_window_closed(payload);
} else if (eventName == "movewindow") {
update_window_count();
on_window_moved(payload);
} else if (eventName == "urgent") {
set_urgent_workspace(payload);
} else if (eventName == "renameworkspace") {
@ -206,6 +227,67 @@ void Workspaces::onEvent(const std::string &ev) {
dp.emit();
}
void Workspaces::on_window_opened(std::string payload) {
size_t last_comma_idx = 0;
size_t next_comma_idx = payload.find(',');
std::string window_address = payload.substr(last_comma_idx, next_comma_idx - last_comma_idx);
last_comma_idx = next_comma_idx;
next_comma_idx = payload.find(',', next_comma_idx + 1);
std::string workspace_name =
payload.substr(last_comma_idx + 1, next_comma_idx - last_comma_idx - 1);
last_comma_idx = next_comma_idx;
next_comma_idx = payload.find(',', next_comma_idx + 1);
std::string window_class =
payload.substr(last_comma_idx + 1, next_comma_idx - last_comma_idx - 1);
std::string window_title = payload.substr(next_comma_idx + 1, payload.length() - next_comma_idx);
for (auto &workspace : workspaces_) {
if (workspace->on_window_opened(window_address, workspace_name, window_class, window_title)) {
break;
}
}
}
void Workspaces::on_window_closed(std::string addr) {
for (auto &workspace : workspaces_) {
if (workspace->on_window_closed(addr)) {
break;
}
}
}
void Workspaces::on_window_moved(std::string payload) {
size_t last_comma_idx = 0;
size_t next_comma_idx = payload.find(',');
std::string window_address = payload.substr(last_comma_idx, next_comma_idx - last_comma_idx);
std::string workspace_name =
payload.substr(next_comma_idx + 1, payload.length() - next_comma_idx);
std::string window_repr;
// Take the window's representation from the old workspace...
for (auto &workspace : workspaces_) {
try {
window_repr = workspace->on_window_closed(window_address).value();
break;
} catch (const std::bad_optional_access &e) {
// window was not found in this workspace
continue;
}
}
// ...and add it to the new workspace
for (auto &workspace : workspaces_) {
if (workspace->on_window_opened(window_address, workspace_name, window_repr)) {
break;
}
}
}
void Workspaces::update_window_count() {
const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces");
for (auto &workspace : workspaces_) {
@ -224,22 +306,86 @@ void Workspaces::update_window_count() {
}
}
void Workspaces::create_workspace(Json::Value &value) {
void Workspaces::initialize_window_maps() {
Json::Value clients_data = gIPC->getSocket1JsonReply("clients");
for (auto &workspace : workspaces_) {
workspace->initialize_window_map(clients_data);
}
}
void Workspace::initialize_window_map(const Json::Value &clients_data) {
window_map_.clear();
for (auto client : clients_data) {
if (client["workspace"]["id"].asInt() == id()) {
// substr(2, ...) is necessary because Hyprland's JSON follows this format:
// 0x{ADDR}
// While Hyprland's IPC follows this format:
// {ADDR}
WindowAddress client_address = client["address"].asString();
client_address = client_address.substr(2, client_address.length() - 2);
insert_window(client_address, client["class"].asString());
}
}
}
void Workspace::insert_window(WindowAddress addr, std::string window_class) {
auto window_repr = workspace_manager_.get_rewrite(window_class);
if (!window_repr.empty()) {
window_map_.emplace(addr, window_repr);
}
};
std::string Workspace::remove_window(WindowAddress addr) {
std::string window_repr = window_map_[addr];
window_map_.erase(addr);
return window_repr;
}
bool Workspace::on_window_opened(WindowAddress &addr, std::string &workspace_name,
std::string window_repr) {
if (workspace_name == name()) {
window_map_.emplace(addr, window_repr);
return true;
} else {
return false;
}
}
bool Workspace::on_window_opened(WindowAddress &addr, std::string &workspace_name,
std::string &window_class, std::string &window_title) {
if (workspace_name == name()) {
insert_window(addr, window_class);
return true;
} else {
return false;
}
}
std::optional<std::string> Workspace::on_window_closed(WindowAddress &addr) {
if (window_map_.contains(addr)) {
return remove_window(addr);
} else {
return {};
}
}
void Workspaces::create_workspace(Json::Value &workspace_data, const Json::Value &clients_data) {
// replace the existing persistent workspace if it exists
auto workspace = std::find_if(
workspaces_.begin(), workspaces_.end(), [&](std::unique_ptr<Workspace> const &x) {
auto name = value["name"].asString();
auto name = workspace_data["name"].asString();
return x->is_persistent() &&
((name.starts_with("special:") && name.substr(8) == x->name()) || name == x->name());
});
if (workspace != workspaces_.end()) {
// replace workspace, but keep persistent flag
workspaces_.erase(workspace);
value["persistent"] = true;
workspace_data["persistent"] = true;
}
// create new workspace
workspaces_.emplace_back(std::make_unique<Workspace>(value, *this));
workspaces_.emplace_back(std::make_unique<Workspace>(workspace_data, *this, clients_data));
Gtk::Button &new_workspace_button = workspaces_.back()->button();
box_.pack_start(new_workspace_button, false, false);
sort_workspaces();
@ -362,10 +508,12 @@ void Workspaces::init() {
create_persistent_workspaces();
const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces");
const Json::Value clients_json = gIPC->getSocket1JsonReply("clients");
for (Json::Value workspace_json : workspaces_json) {
if ((all_outputs() || bar_.output->name == workspace_json["monitor"].asString()) &&
(!workspace_json["name"].asString().starts_with("special") || show_special())) {
create_workspace(workspace_json);
create_workspace(workspace_json, clients_json);
}
}
@ -382,7 +530,8 @@ Workspaces::~Workspaces() {
std::lock_guard<std::mutex> lg(mutex_);
}
Workspace::Workspace(const Json::Value &workspace_data, Workspaces &workspace_manager)
Workspace::Workspace(const Json::Value &workspace_data, Workspaces &workspace_manager,
const Json::Value &clients_data)
: workspace_manager_(workspace_manager),
id_(workspace_data["id"].asInt()),
name_(workspace_data["name"].asString()),
@ -407,6 +556,8 @@ Workspace::Workspace(const Json::Value &workspace_data, Workspaces &workspace_ma
button_.set_relief(Gtk::RELIEF_NONE);
content_.set_center_widget(label_);
button_.add(content_);
initialize_window_map(clients_data);
}
void add_or_remove_class(const Glib::RefPtr<Gtk::StyleContext> &context, bool condition,
@ -440,8 +591,22 @@ void Workspace::update(const std::string &format, const std::string &icon) {
add_or_remove_class(style_context, is_urgent(), "urgent");
add_or_remove_class(style_context, is_visible(), "visible");
std::string windows;
auto window_separator = workspace_manager_.get_window_separator();
bool is_not_first = false;
for (auto &[_pid, window_repr] : window_map_) {
if (is_not_first) {
windows.append(window_separator);
}
is_not_first = true;
windows.append(window_repr);
}
label_.set_markup(fmt::format(fmt::runtime(format), fmt::arg("id", id()),
fmt::arg("name", name()), fmt::arg("icon", icon)));
fmt::arg("name", name()), fmt::arg("icon", icon),
fmt::arg("windows", windows)));
}
void Workspaces::sort_workspaces() {
@ -601,4 +766,23 @@ void Workspaces::set_urgent_workspace(std::string windowaddress) {
}
}
std::string Workspaces::get_rewrite(std::string window_class) {
if (regex_cache_.contains(window_class)) {
return regex_cache_[window_class];
}
bool matched_any;
std::string window_class_rewrite =
waybar::util::rewriteStringOnce(window_class, window_rewrite_rules_, matched_any);
if (!matched_any) {
window_class_rewrite = window_rewrite_default_;
}
regex_cache_.emplace(window_class, window_class_rewrite);
return window_class_rewrite;
}
} // namespace waybar::modules::hyprland

View File

@ -1,5 +1,6 @@
#include "util/rewrite_string.hpp"
#include <fmt/core.h>
#include <spdlog/spdlog.h>
#include <regex>
@ -17,7 +18,7 @@ std::string rewriteString(const std::string& value, const Json::Value& rules) {
try {
// malformated regexes will cause an exception.
// in this case, log error and try the next rule.
const std::regex rule{it.key().asString()};
const std::regex rule{it.key().asString(), std::regex_constants::icase};
if (std::regex_match(value, rule)) {
res = std::regex_replace(res, rule, it->asString());
}
@ -29,4 +30,31 @@ std::string rewriteString(const std::string& value, const Json::Value& rules) {
return res;
}
std::string rewriteStringOnce(const std::string& value, const Json::Value& rules,
bool& matched_any) {
if (!rules.isObject()) {
return value;
}
matched_any = false;
std::string res = value;
for (auto it = rules.begin(); it != rules.end(); ++it) {
if (it.key().isString() && it->isString()) {
try {
const std::regex rule{it.key().asString(), std::regex_constants::icase};
if (std::regex_match(value, rule)) {
matched_any = true;
return std::regex_replace(res, rule, it->asString());
}
} catch (const std::regex_error& e) {
spdlog::error("Invalid rule {}: {}", it.key().asString(), e.what());
}
}
}
return value;
}
} // namespace waybar::util