waybar/src/modules/hyprland/window.cpp

87 lines
2.4 KiB
C++
Raw Normal View History

2022-07-01 12:46:28 +02:00
#include "modules/hyprland/window.hpp"
#include <spdlog/spdlog.h>
#include <util/sanitize_str.hpp>
2022-08-17 22:03:49 +02:00
#include "modules/hyprland/backend.hpp"
#include "util/command.hpp"
#include "util/json.hpp"
2022-08-17 22:03:49 +02:00
2022-07-01 12:46:28 +02: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;
separate_outputs = config["separate-outputs"].as<bool>();
2022-07-01 12:46:28 +02:00
if (!gIPC.get()) {
gIPC = std::make_unique<IPC>();
}
label_.hide();
ALabel::update();
// register for hyprland ipc
gIPC->registerForIPC("activewindow", [&](const std::string& ev) { this->onEvent(ev); });
}
2022-08-17 21:54:23 +02:00
auto Window::update() -> void {
// fix ampersands
std::lock_guard<std::mutex> lg(mutex_);
if (!format_.empty()) {
label_.show();
label_.set_markup(fmt::format(format_, lastView));
} else {
label_.hide();
}
ALabel::update();
}
uint Window::getActiveWorkspaceID(std::string monitorName) {
auto cmd = waybar::util::command::exec("hyprctl monitors -j");
assert(cmd.exit_code == 0);
Json::Value json = parser_.parse(cmd.out);
assert(json.isArray());
2022-10-18 09:01:45 +02:00
auto monitor = std::find_if(json.begin(), json.end(),
[&](Json::Value monitor) { return monitor["name"] == monitorName; });
assert(monitor != std::end(json));
return (*monitor)["activeWorkspace"]["id"].as<uint>();
}
std::string Window::getLastWindowTitle(uint workspaceID) {
auto cmd = waybar::util::command::exec("hyprctl workspaces -j");
assert(cmd.exit_code == 0);
Json::Value json = parser_.parse(cmd.out);
assert(json.isArray());
2022-10-18 09:01:45 +02:00
auto workspace = std::find_if(json.begin(), json.end(), [&](Json::Value workspace) {
return workspace["id"].as<uint>() == workspaceID;
});
assert(workspace != std::end(json));
return (*workspace)["lastwindowtitle"].as<std::string>();
}
2022-07-01 12:46:28 +02:00
void Window::onEvent(const std::string& ev) {
2022-08-17 21:54:23 +02:00
std::lock_guard<std::mutex> lg(mutex_);
std::string windowName;
if (separate_outputs) {
windowName = getLastWindowTitle(getActiveWorkspaceID(this->bar_.output->name));
} else {
windowName = ev.substr(ev.find_first_of(',') + 1).substr(0, 256);
}
2022-07-01 12:46:28 +02:00
windowName = waybar::util::sanitize_string(windowName);
2022-07-01 12:46:28 +02:00
if (windowName == lastView) return;
lastView = windowName;
spdlog::debug("hyprland window onevent with {}", windowName);
2022-08-17 21:54:23 +02:00
dp.emit();
2022-07-01 12:46:28 +02:00
}
2022-08-17 22:03:49 +02:00
} // namespace waybar::modules::hyprland