2022-07-01 10:46:28 +00:00
|
|
|
#include "modules/hyprland/window.hpp"
|
|
|
|
|
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
|
2023-06-19 21:42:19 +00:00
|
|
|
#include <algorithm>
|
2022-10-18 11:18:43 +00:00
|
|
|
#include <regex>
|
2022-10-09 17:13:54 +00:00
|
|
|
|
2022-08-17 20:03:49 +00:00
|
|
|
#include "modules/hyprland/backend.hpp"
|
2022-10-13 21:41:56 +00:00
|
|
|
#include "util/json.hpp"
|
2023-03-25 16:33:01 +00:00
|
|
|
#include "util/rewrite_string.hpp"
|
2023-06-19 21:42:19 +00:00
|
|
|
#include <util/sanitize_str.hpp>
|
2022-08-17 20:03:49 +00:00
|
|
|
|
2022-07-01 10:46:28 +00:00
|
|
|
namespace waybar::modules::hyprland {
|
|
|
|
|
|
|
|
Window::Window(const std::string& id, const Bar& bar, const Json::Value& config)
|
|
|
|
: ALabel(config, "window", id, "{}", 0, true), bar_(bar) {
|
|
|
|
modulesReady = true;
|
2023-06-19 21:42:19 +00:00
|
|
|
separate_outputs = config["separate-outputs"].asBool();
|
2022-07-01 10:46:28 +00:00
|
|
|
|
|
|
|
if (!gIPC.get()) {
|
|
|
|
gIPC = std::make_unique<IPC>();
|
|
|
|
}
|
|
|
|
|
2023-06-19 20:08:03 +00:00
|
|
|
queryActiveWorkspace();
|
|
|
|
update();
|
2022-07-01 10:46:28 +00:00
|
|
|
|
|
|
|
// register for hyprland ipc
|
2022-11-09 08:34:19 +00:00
|
|
|
gIPC->registerForIPC("activewindow", this);
|
2023-06-19 20:08:03 +00:00
|
|
|
gIPC->registerForIPC("closewindow", this);
|
|
|
|
gIPC->registerForIPC("movewindow", this);
|
2023-06-19 21:42:19 +00:00
|
|
|
gIPC->registerForIPC("changefloatingmode", this);
|
|
|
|
gIPC->registerForIPC("fullscreen", this);
|
2022-11-09 08:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Window::~Window() {
|
|
|
|
gIPC->unregisterForIPC(this);
|
|
|
|
// wait for possible event handler to finish
|
|
|
|
std::lock_guard<std::mutex> lg(mutex_);
|
2022-07-01 10:46:28 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 19:54:23 +00:00
|
|
|
auto Window::update() -> void {
|
|
|
|
// fix ampersands
|
|
|
|
std::lock_guard<std::mutex> lg(mutex_);
|
|
|
|
|
2023-06-19 20:08:03 +00:00
|
|
|
std::string window_name = waybar::util::sanitize_string(workspace_.last_window_title);
|
|
|
|
|
|
|
|
if (window_name != last_title_) {
|
|
|
|
if (window_name.empty()) {
|
|
|
|
label_.get_style_context()->add_class("empty");
|
|
|
|
} else {
|
|
|
|
label_.get_style_context()->remove_class("empty");
|
|
|
|
}
|
|
|
|
last_title_ = window_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-17 19:54:23 +00:00
|
|
|
if (!format_.empty()) {
|
|
|
|
label_.show();
|
2023-01-16 21:24:55 +00:00
|
|
|
label_.set_markup(fmt::format(fmt::runtime(format_),
|
2023-06-19 20:08:03 +00:00
|
|
|
waybar::util::rewriteString(window_name, config_["rewrite"])));
|
2022-08-17 19:54:23 +00:00
|
|
|
} else {
|
|
|
|
label_.hide();
|
|
|
|
}
|
|
|
|
|
2023-06-19 20:08:03 +00:00
|
|
|
|
|
|
|
setClass("empty", workspace_.windows == 0);
|
|
|
|
setClass("solo", workspace_.windows == 1);
|
2023-06-19 21:42:19 +00:00
|
|
|
setClass("fullscreen", fullscreen_);
|
|
|
|
setClass("floating", all_floating_);
|
2023-06-19 20:08:03 +00:00
|
|
|
|
|
|
|
if (!last_solo_class_.empty() && solo_class_ != last_solo_class_) {
|
|
|
|
if (bar_.window.get_style_context()->has_class(last_solo_class_)) {
|
|
|
|
bar_.window.get_style_context()->remove_class(last_solo_class_);
|
|
|
|
spdlog::trace("Removing solo class: {}", last_solo_class_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!solo_class_.empty() && solo_class_ != last_solo_class_) {
|
|
|
|
bar_.window.get_style_context()->add_class(solo_class_);
|
|
|
|
spdlog::trace("Adding solo class: {}", solo_class_);
|
|
|
|
}
|
2023-06-19 21:42:19 +00:00
|
|
|
last_solo_class_ = solo_class_;
|
2023-06-19 20:08:03 +00:00
|
|
|
|
2022-08-17 19:54:23 +00:00
|
|
|
ALabel::update();
|
|
|
|
}
|
|
|
|
|
2023-06-19 20:08:03 +00:00
|
|
|
auto Window::getActiveWorkspace() -> Workspace {
|
|
|
|
const auto workspace = gIPC->getSocket1Reply("j/activeworkspace");
|
|
|
|
Json::Value json = parser_.parse(workspace);
|
|
|
|
assert(json.isObject());
|
|
|
|
return Workspace::parse(json);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Window::getActiveWorkspace(const std::string& monitorName) -> Workspace {
|
|
|
|
const auto monitors = gIPC->getSocket1Reply("j/monitors");
|
|
|
|
Json::Value json = parser_.parse(monitors);
|
2022-10-13 21:41:56 +00:00
|
|
|
assert(json.isArray());
|
2022-10-18 07:01:45 +00:00
|
|
|
auto monitor = std::find_if(json.begin(), json.end(),
|
|
|
|
[&](Json::Value monitor) { return monitor["name"] == monitorName; });
|
2022-11-10 08:19:49 +00:00
|
|
|
if (monitor == std::end(json)) {
|
2023-06-19 20:08:03 +00:00
|
|
|
spdlog::warn("Monitor not found: {}", monitorName);
|
2023-06-19 21:42:19 +00:00
|
|
|
return Workspace{-1, 0, "", ""};
|
2022-11-09 08:34:19 +00:00
|
|
|
}
|
2023-06-19 21:42:19 +00:00
|
|
|
const int id = (*monitor)["activeWorkspace"]["id"].asInt();
|
2022-10-13 21:41:56 +00:00
|
|
|
|
2023-06-19 20:08:03 +00:00
|
|
|
const auto workspaces = gIPC->getSocket1Reply("j/workspaces");
|
|
|
|
json = parser_.parse(workspaces);
|
2022-10-13 21:41:56 +00:00
|
|
|
assert(json.isArray());
|
2023-06-19 20:08:03 +00:00
|
|
|
auto workspace = std::find_if(json.begin(), json.end(),
|
2023-06-19 21:42:19 +00:00
|
|
|
[&](Json::Value workspace) { return workspace["id"] == id; });
|
2022-10-19 11:25:08 +00:00
|
|
|
if (workspace == std::end(json)) {
|
2023-06-19 20:08:03 +00:00
|
|
|
spdlog::warn("No workspace with id {}", id);
|
2023-06-19 21:42:19 +00:00
|
|
|
return Workspace{-1, 0, "", ""};
|
2023-06-19 20:08:03 +00:00
|
|
|
}
|
|
|
|
return Workspace::parse(*workspace);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Window::Workspace::parse(const Json::Value& value) -> Window::Workspace {
|
|
|
|
return Workspace{
|
2023-06-19 21:42:19 +00:00
|
|
|
value["id"].asInt(),
|
|
|
|
value["windows"].asInt(),
|
|
|
|
value["lastwindow"].asString(),
|
|
|
|
value["lastwindowtitle"].asString()
|
2023-06-19 20:08:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::queryActiveWorkspace() {
|
2022-08-17 19:54:23 +00:00
|
|
|
std::lock_guard<std::mutex> lg(mutex_);
|
2022-10-13 21:41:56 +00:00
|
|
|
|
|
|
|
if (separate_outputs) {
|
2023-06-19 20:08:03 +00:00
|
|
|
workspace_ = getActiveWorkspace(this->bar_.output->name);
|
2022-10-13 21:41:56 +00:00
|
|
|
} else {
|
2023-06-19 20:08:03 +00:00
|
|
|
workspace_ = getActiveWorkspace();
|
2022-10-13 21:41:56 +00:00
|
|
|
}
|
2022-07-01 10:46:28 +00:00
|
|
|
|
2023-06-19 21:42:19 +00:00
|
|
|
|
|
|
|
if (workspace_.windows > 0) {
|
|
|
|
const auto clients = gIPC->getSocket1Reply("j/clients");
|
|
|
|
Json::Value json = parser_.parse(clients);
|
|
|
|
assert(json.isArray());
|
|
|
|
auto active_window = std::find_if(json.begin(), json.end(),
|
|
|
|
[&](Json::Value window) { return window["address"] == workspace_.last_window; });
|
|
|
|
if (active_window == std::end(json)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (workspace_.windows == 1 && !(*active_window)["floating"].asBool()) {
|
|
|
|
solo_class_ = (*active_window)["class"].asString();
|
|
|
|
} else {
|
|
|
|
solo_class_ = "";
|
|
|
|
}
|
|
|
|
all_floating_ = std::all_of(json.begin(), json.end(),
|
|
|
|
[&](Json::Value window) { return window["floating"].asBool() ||
|
|
|
|
window["workspace"]["id"] != workspace_.id; });
|
|
|
|
fullscreen_ = (*active_window)["fullscreen"].asBool();
|
2023-06-01 11:38:27 +00:00
|
|
|
} else {
|
2023-06-19 20:08:03 +00:00
|
|
|
solo_class_ = "";
|
2023-06-19 21:42:19 +00:00
|
|
|
all_floating_ = false;
|
|
|
|
fullscreen_ = false;
|
2023-06-01 11:38:27 +00:00
|
|
|
}
|
2023-06-19 20:08:03 +00:00
|
|
|
}
|
2022-07-01 10:46:28 +00:00
|
|
|
|
2023-06-19 20:08:03 +00:00
|
|
|
void Window::onEvent(const std::string& ev) {
|
|
|
|
queryActiveWorkspace();
|
2022-07-01 10:46:28 +00:00
|
|
|
|
2022-08-17 19:54:23 +00:00
|
|
|
dp.emit();
|
2022-07-01 10:46:28 +00:00
|
|
|
}
|
2023-06-19 20:08:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
void Window::setClass(const std::string& classname, bool enable) {
|
|
|
|
if (enable) {
|
|
|
|
if (!bar_.window.get_style_context()->has_class(classname)) {
|
|
|
|
bar_.window.get_style_context()->add_class(classname);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bar_.window.get_style_context()->remove_class(classname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 08:34:19 +00:00
|
|
|
} // namespace waybar::modules::hyprland
|