Waybar/src/modules/hyprland/window.cpp

209 lines
7.0 KiB
C++
Raw Normal View History

2022-07-01 10:46:28 +00:00
#include "modules/hyprland/window.hpp"
2023-07-10 20:02:03 +00:00
#include <glibmm/fileutils.h>
#include <glibmm/keyfile.h>
#include <glibmm/miscutils.h>
2022-07-01 10:46:28 +00:00
#include <spdlog/spdlog.h>
#include <algorithm>
2022-10-18 11:18:43 +00:00
#include <regex>
2023-06-26 21:18:49 +00:00
#include <util/sanitize_str.hpp>
#include <vector>
2022-08-17 20:03:49 +00:00
#include "modules/hyprland/backend.hpp"
2023-07-10 20:02:03 +00:00
#include "util/gtk_icon.hpp"
#include "util/json.hpp"
2023-03-25 16:33:01 +00:00
#include "util/rewrite_string.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)
: AAppIconLabel(config, "window", id, "{title}", 0, true), bar_(bar) {
2022-07-01 10:46:28 +00:00
modulesReady = true;
separate_outputs = config["separate-outputs"].asBool();
2022-07-01 10:46:28 +00:00
if (!gIPC.get()) {
gIPC = std::make_unique<IPC>();
}
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);
gIPC->registerForIPC("closewindow", this);
gIPC->registerForIPC("movewindow", this);
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_);
std::string window_name = waybar::util::sanitize_string(workspace_.last_window_title);
2023-07-08 19:42:28 +00:00
std::string window_address = workspace_.last_window;
if (window_name != window_data_.title) {
if (window_name.empty()) {
label_.get_style_context()->add_class("empty");
} else {
label_.get_style_context()->remove_class("empty");
}
window_data_.title = window_name;
2023-07-08 19:42:28 +00:00
}
2022-08-17 19:54:23 +00:00
if (!format_.empty()) {
label_.show();
2023-07-08 19:42:28 +00:00
label_.set_markup(waybar::util::rewriteString(
fmt::format(fmt::runtime(format_), fmt::arg("title", window_name),
2023-07-08 21:11:11 +00:00
fmt::arg("initialTitle", window_data_.initial_title),
2023-07-08 19:42:28 +00:00
fmt::arg("class", window_data_.class_name),
2023-07-08 21:11:11 +00:00
fmt::arg("initialClass", window_data_.initial_class_name)),
2023-07-08 19:42:28 +00:00
config_["rewrite"]));
2022-08-17 19:54:23 +00:00
} else {
label_.hide();
}
setClass("empty", workspace_.windows == 0);
setClass("solo", solo_);
setClass("floating", all_floating_);
setClass("hidden", hidden_);
setClass("fullscreen", fullscreen_);
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_);
}
last_solo_class_ = solo_class_;
AAppIconLabel::update();
2022-08-17 19:54:23 +00:00
}
auto Window::getActiveWorkspace() -> Workspace {
2023-07-01 10:37:46 +00:00
const auto workspace = gIPC->getSocket1JsonReply("activeworkspace");
assert(workspace.isObject());
return Workspace::parse(workspace);
}
auto Window::getActiveWorkspace(const std::string& monitorName) -> Workspace {
2023-07-01 10:37:46 +00:00
const auto monitors = gIPC->getSocket1JsonReply("monitors");
assert(monitors.isArray());
auto monitor = std::find_if(monitors.begin(), monitors.end(),
2022-10-18 07:01:45 +00:00
[&](Json::Value monitor) { return monitor["name"] == monitorName; });
2023-07-01 10:37:46 +00:00
if (monitor == std::end(monitors)) {
spdlog::warn("Monitor not found: {}", monitorName);
return Workspace{-1, 0, "", ""};
2022-11-09 08:34:19 +00:00
}
const int id = (*monitor)["activeWorkspace"]["id"].asInt();
2023-07-01 10:37:46 +00:00
const auto workspaces = gIPC->getSocket1JsonReply("workspaces");
assert(workspaces.isArray());
auto workspace = std::find_if(workspaces.begin(), workspaces.end(),
[&](Json::Value workspace) { return workspace["id"] == id; });
if (workspace == std::end(workspaces)) {
spdlog::warn("No workspace with id {}", id);
return Workspace{-1, 0, "", ""};
}
return Workspace::parse(*workspace);
}
auto Window::Workspace::parse(const Json::Value& value) -> Window::Workspace {
2023-06-26 21:18:49 +00:00
return Workspace{value["id"].asInt(), value["windows"].asInt(), value["lastwindow"].asString(),
value["lastwindowtitle"].asString()};
}
2023-07-08 19:42:28 +00:00
auto Window::WindowData::parse(const Json::Value& value) -> Window::WindowData {
return WindowData{value["floating"].asBool(), value["monitor"].asInt(),
value["class"].asString(), value["initialClass"].asString(),
value["title"].asString(), value["initialTitle"].asString()};
}
void Window::queryActiveWorkspace() {
2022-08-17 19:54:23 +00:00
std::lock_guard<std::mutex> lg(mutex_);
if (separate_outputs) {
workspace_ = getActiveWorkspace(this->bar_.output->name);
} else {
workspace_ = getActiveWorkspace();
}
2022-07-01 10:46:28 +00:00
if (workspace_.windows > 0) {
const auto clients = gIPC->getSocket1JsonReply("clients");
assert(clients.isArray());
auto active_window = std::find_if(clients.begin(), clients.end(), [&](Json::Value window) {
2023-06-26 21:18:49 +00:00
return window["address"] == workspace_.last_window;
});
if (active_window == std::end(clients)) {
return;
}
window_data_ = WindowData::parse(*active_window);
updateAppIconName(window_data_.class_name);
std::vector<Json::Value> workspace_windows;
std::copy_if(clients.begin(), clients.end(), std::back_inserter(workspace_windows),
2023-06-26 21:18:49 +00:00
[&](Json::Value window) {
return window["workspace"]["id"] == workspace_.id && window["mapped"].asBool();
});
hidden_ = std::any_of(workspace_windows.begin(), workspace_windows.end(),
[&](Json::Value window) { return window["hidden"].asBool(); });
std::vector<Json::Value> visible_windows;
std::copy_if(workspace_windows.begin(), workspace_windows.end(),
std::back_inserter(visible_windows),
[&](Json::Value window) { return !window["hidden"].asBool(); });
solo_ = 1 == std::count_if(visible_windows.begin(), visible_windows.end(),
[&](Json::Value window) { return !window["floating"].asBool(); });
all_floating_ = std::all_of(visible_windows.begin(), visible_windows.end(),
[&](Json::Value window) { return window["floating"].asBool(); });
fullscreen_ = (*active_window)["fullscreen"].asBool();
if (fullscreen_) {
solo_ = true;
}
if (solo_) {
solo_class_ = window_data_.class_name;
} else {
solo_class_ = "";
}
} else {
window_data_ = WindowData{};
all_floating_ = false;
hidden_ = false;
fullscreen_ = false;
solo_ = false;
solo_class_ = "";
}
}
2022-07-01 10:46:28 +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
}
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