2022-07-01 10:46:28 +00:00
|
|
|
#include "modules/hyprland/window.hpp"
|
|
|
|
|
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
|
2022-10-18 11:18:43 +00:00
|
|
|
#include <regex>
|
2022-10-09 17:13:54 +00:00
|
|
|
#include <util/sanitize_str.hpp>
|
|
|
|
|
2022-08-17 20:03:49 +00:00
|
|
|
#include "modules/hyprland/backend.hpp"
|
2022-10-13 21:41:56 +00:00
|
|
|
#include "util/command.hpp"
|
|
|
|
#include "util/json.hpp"
|
2022-10-19 11:25:08 +00:00
|
|
|
#include "util/rewrite_title.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;
|
2022-10-13 21:41:56 +00:00
|
|
|
separate_outputs = config["separate-outputs"].as<bool>();
|
2022-07-01 10:46:28 +00:00
|
|
|
|
|
|
|
if (!gIPC.get()) {
|
|
|
|
gIPC = std::make_unique<IPC>();
|
|
|
|
}
|
|
|
|
|
|
|
|
label_.hide();
|
|
|
|
ALabel::update();
|
|
|
|
|
|
|
|
// register for hyprland ipc
|
2022-11-09 08:34:19 +00:00
|
|
|
gIPC->registerForIPC("activewindow", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
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_);
|
|
|
|
|
|
|
|
if (!format_.empty()) {
|
|
|
|
label_.show();
|
2022-10-19 11:30:28 +00:00
|
|
|
label_.set_markup(
|
|
|
|
fmt::format(format_, waybar::util::rewriteTitle(lastView, config_["rewrite"])));
|
2022-08-17 19:54:23 +00:00
|
|
|
} else {
|
|
|
|
label_.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
ALabel::update();
|
|
|
|
}
|
|
|
|
|
2022-11-24 01:16:44 +00:00
|
|
|
int Window::getActiveWorkspaceID(std::string monitorName) {
|
2022-10-13 21:41:56 +00:00
|
|
|
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 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)) {
|
2022-11-09 08:34:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-11-24 01:16:44 +00:00
|
|
|
return (*monitor)["activeWorkspace"]["id"].as<int>();
|
2022-10-13 21:41:56 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 01:16:44 +00:00
|
|
|
std::string Window::getLastWindowTitle(int workspaceID) {
|
2022-10-13 21:41:56 +00:00
|
|
|
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 07:01:45 +00:00
|
|
|
auto workspace = std::find_if(json.begin(), json.end(), [&](Json::Value workspace) {
|
2022-11-24 01:16:44 +00:00
|
|
|
return workspace["id"].as<int>() == workspaceID;
|
2022-10-13 21:41:56 +00:00
|
|
|
});
|
2022-10-18 16:36:00 +00:00
|
|
|
|
2022-10-19 11:25:08 +00:00
|
|
|
if (workspace == std::end(json)) {
|
2022-10-18 16:36:00 +00:00
|
|
|
return "";
|
|
|
|
}
|
2022-10-13 21:41:56 +00:00
|
|
|
return (*workspace)["lastwindowtitle"].as<std::string>();
|
|
|
|
}
|
|
|
|
|
2022-07-01 10:46:28 +00:00
|
|
|
void Window::onEvent(const std::string& ev) {
|
2022-08-17 19:54:23 +00:00
|
|
|
std::lock_guard<std::mutex> lg(mutex_);
|
2022-10-13 21:41:56 +00:00
|
|
|
|
|
|
|
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 10:46:28 +00:00
|
|
|
|
2022-10-09 17:13:54 +00:00
|
|
|
windowName = waybar::util::sanitize_string(windowName);
|
2022-07-01 10:46:28 +00:00
|
|
|
|
|
|
|
if (windowName == lastView) return;
|
|
|
|
|
|
|
|
lastView = windowName;
|
|
|
|
|
|
|
|
spdlog::debug("hyprland window onevent with {}", windowName);
|
|
|
|
|
2022-08-17 19:54:23 +00:00
|
|
|
dp.emit();
|
2022-07-01 10:46:28 +00:00
|
|
|
}
|
2022-11-09 08:34:19 +00:00
|
|
|
} // namespace waybar::modules::hyprland
|