Merge branch 'Alexays:master' into clock_fix

This commit is contained in:
Christopher Shore
2023-10-07 18:39:56 -04:00
committed by GitHub
13 changed files with 356 additions and 24 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 ? "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

@ -100,9 +100,11 @@ void waybar::modules::Battery::refreshBatteries() {
}
auto dir_name = node.path().filename();
auto bat_defined = config_["bat"].isString();
bool bat_compatibility = config_["bat-compatibility"].asBool();
if (((bat_defined && dir_name == config_["bat"].asString()) || !bat_defined) &&
(fs::exists(node.path() / "capacity") || fs::exists(node.path() / "charge_now")) &&
fs::exists(node.path() / "uevent") && fs::exists(node.path() / "status") &&
fs::exists(node.path() / "uevent") &&
(fs::exists(node.path() / "status") || bat_compatibility) &&
fs::exists(node.path() / "type")) {
std::string type;
std::ifstream(node.path() / "type") >> type;
@ -252,7 +254,13 @@ const std::tuple<uint8_t, float, std::string, float> waybar::modules::Battery::g
for (auto const& item : batteries_) {
auto bat = item.first;
std::string _status;
std::getline(std::ifstream(bat / "status"), _status);
/* Check for adapter status if battery is not available */
if (!std::ifstream(bat / "status")) {
std::getline(std::ifstream(adapter_ / "status"), _status);
} else {
std::getline(std::ifstream(bat / "status"), _status);
}
// Some battery will report current and charge in μA/μAh.
// Scale these by the voltage to get μW/μWh.

View File

@ -11,7 +11,9 @@ waybar::modules::Custom::Custom(const std::string& name, const std::string& id,
fp_(nullptr),
pid_(-1) {
dp.emit();
if (interval_.count() > 0) {
if (!config_["signal"].empty() && config_["interval"].empty()) {
waitingWorker();
} else if (interval_.count() > 0) {
delayWorker();
} else if (config_["exec"].isString()) {
continuousWorker();
@ -92,6 +94,26 @@ void waybar::modules::Custom::continuousWorker() {
};
}
void waybar::modules::Custom::waitingWorker() {
thread_ = [this] {
bool can_update = true;
if (config_["exec-if"].isString()) {
output_ = util::command::execNoRead(config_["exec-if"].asString());
if (output_.exit_code != 0) {
can_update = false;
dp.emit();
}
}
if (can_update) {
if (config_["exec"].isString()) {
output_ = util::command::exec(config_["exec"].asString());
}
dp.emit();
}
thread_.sleep();
};
}
void waybar::modules::Custom::refresh(int sig) {
if (sig == SIGRTMIN + config_["signal"].asInt()) {
thread_.wake_up();

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 {
@ -134,6 +148,14 @@ auto Workspaces::update() -> void {
AModule::update();
}
bool isDoubleSpecial(std::string &workspace_name) {
// Hyprland's IPC sometimes reports the creation of workspaces strangely named
// `special:special:<some_name>`. This function checks for that and is used
// to avoid creating (and then removing) such workspaces.
// See hyprwm/Hyprland#3424 for more info.
return workspace_name.find("special:special:") != std::string::npos;
}
void Workspaces::onEvent(const std::string &ev) {
std::lock_guard<std::mutex> lock(mutex_);
std::string eventName(begin(ev), begin(ev) + ev.find_first_of('>'));
@ -143,15 +165,16 @@ void Workspaces::onEvent(const std::string &ev) {
active_workspace_name_ = payload;
} else if (eventName == "destroyworkspace") {
workspaces_to_remove_.push_back(payload);
if (!isDoubleSpecial(payload)) {
workspaces_to_remove_.push_back(payload);
}
} else if (eventName == "createworkspace") {
const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces");
for (Json::Value workspace_json : workspaces_json) {
std::string name = workspace_json["name"].asString();
if (name == payload &&
(all_outputs() || bar_.output->name == workspace_json["monitor"].asString()) &&
(show_special() || !name.starts_with("special"))) {
(show_special() || !name.starts_with("special")) && !isDoubleSpecial(payload)) {
workspaces_to_create_.push_back(workspace_json);
break;
}
@ -175,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") {
@ -197,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_) {
@ -215,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();
@ -238,8 +393,10 @@ void Workspaces::create_workspace(Json::Value &value) {
}
void Workspaces::remove_workspace(std::string name) {
auto workspace = std::find_if(workspaces_.begin(), workspaces_.end(),
[&](std::unique_ptr<Workspace> &x) { return x->name() == name; });
auto workspace =
std::find_if(workspaces_.begin(), workspaces_.end(), [&](std::unique_ptr<Workspace> &x) {
return (name.starts_with("special:") && name.substr(8) == x->name()) || name == x->name();
});
if (workspace == workspaces_.end()) {
// happens when a workspace on another monitor is destroyed
@ -351,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);
}
}
@ -371,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()),
@ -396,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,
@ -429,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() {
@ -549,7 +725,8 @@ std::string &Workspace::select_icon(std::map<std::string, std::string> &icons_ma
if (default_icon_it != icons_map.end()) {
return default_icon_it->second;
}
return icons_map[""];
return name_;
}
auto Workspace::handle_clicked(GdkEventButton *bt) -> bool {
@ -589,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