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>
|
|
|
|
|
2023-06-19 21:42:19 +00:00
|
|
|
#include <algorithm>
|
2023-07-10 20:02:03 +00:00
|
|
|
#include <filesystem>
|
|
|
|
#include <optional>
|
2022-10-18 11:18:43 +00:00
|
|
|
#include <regex>
|
2023-06-26 21:18:49 +00:00
|
|
|
#include <util/sanitize_str.hpp>
|
2023-06-20 00:23:03 +00:00
|
|
|
#include <vector>
|
2022-10-09 17:13:54 +00:00
|
|
|
|
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"
|
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"
|
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)
|
2023-07-10 20:02:03 +00:00
|
|
|
: AIconLabel(config, "window", id, "{title}", 0, true), bar_(bar) {
|
2022-07-01 10:46:28 +00:00
|
|
|
modulesReady = true;
|
2023-06-19 21:42:19 +00:00
|
|
|
separate_outputs = config["separate-outputs"].asBool();
|
2022-07-01 10:46:28 +00:00
|
|
|
|
2023-07-10 20:02:03 +00:00
|
|
|
// Icon size
|
|
|
|
if (config["icon-size"].isUInt()) {
|
|
|
|
app_icon_size_ = config["icon-size"].asUInt();
|
|
|
|
}
|
|
|
|
image_.set_pixel_size(app_icon_size_);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-07-10 20:02:03 +00:00
|
|
|
std::optional<std::string> getDesktopFilePath(const std::string& app_class) {
|
|
|
|
const auto data_dirs = Glib::get_system_data_dirs();
|
|
|
|
for (const auto& data_dir : data_dirs) {
|
|
|
|
const auto data_app_dir = data_dir + "applications/";
|
|
|
|
auto desktop_file_path = data_app_dir + app_class + ".desktop";
|
|
|
|
if (std::filesystem::exists(desktop_file_path)) {
|
|
|
|
return desktop_file_path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<Glib::ustring> getIconName(const std::string& app_class) {
|
|
|
|
const auto desktop_file_path = getDesktopFilePath(app_class);
|
|
|
|
if (!desktop_file_path.has_value()) {
|
|
|
|
// Try some heuristics to find a matching icon
|
|
|
|
|
|
|
|
if (DefaultGtkIconThemeWrapper::has_icon(app_class)) {
|
|
|
|
return app_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto app_identifier_desktop = app_class + "-desktop";
|
|
|
|
if (DefaultGtkIconThemeWrapper::has_icon(app_identifier_desktop)) {
|
|
|
|
return app_identifier_desktop;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto to_lower = [](const std::string& str) {
|
|
|
|
auto str_cpy = str;
|
|
|
|
std::transform(str_cpy.begin(), str_cpy.end(), str_cpy.begin(),
|
|
|
|
[](unsigned char c) { return std::tolower(c); });
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto first_space = app_class.find_first_of(' ');
|
|
|
|
if (first_space != std::string::npos) {
|
|
|
|
const auto first_word = to_lower(app_class.substr(0, first_space));
|
|
|
|
if (DefaultGtkIconThemeWrapper::has_icon(first_word)) {
|
|
|
|
return first_word;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto first_dash = app_class.find_first_of('-');
|
|
|
|
if (first_dash != std::string::npos) {
|
|
|
|
const auto first_word = to_lower(app_class.substr(0, first_dash));
|
|
|
|
if (DefaultGtkIconThemeWrapper::has_icon(first_word)) {
|
|
|
|
return first_word;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Glib::KeyFile desktop_file;
|
|
|
|
desktop_file.load_from_file(desktop_file_path.value());
|
|
|
|
return desktop_file.get_string("Desktop Entry", "Icon");
|
|
|
|
} catch (Glib::FileError& error) {
|
|
|
|
spdlog::warn("Error while loading desktop file {}: {}", desktop_file_path.value(),
|
|
|
|
error.what().c_str());
|
|
|
|
} catch (Glib::KeyFileError& error) {
|
|
|
|
spdlog::warn("Error while loading desktop file {}: {}", desktop_file_path.value(),
|
|
|
|
error.what().c_str());
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::updateAppIconName() {
|
|
|
|
if (!iconEnabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto icon_name = getIconName(window_data_.class_name);
|
|
|
|
if (icon_name.has_value()) {
|
|
|
|
app_icon_name_ = icon_name.value();
|
|
|
|
} else {
|
|
|
|
app_icon_name_ = "";
|
|
|
|
}
|
|
|
|
update_app_icon_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::updateAppIcon() {
|
|
|
|
if (update_app_icon_) {
|
|
|
|
update_app_icon_ = false;
|
|
|
|
if (app_icon_name_.empty()) {
|
|
|
|
image_.set_visible(false);
|
|
|
|
} else {
|
|
|
|
image_.set_from_icon_name(app_icon_name_, Gtk::ICON_SIZE_INVALID);
|
|
|
|
image_.set_visible(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2023-07-08 19:42:28 +00:00
|
|
|
std::string window_address = workspace_.last_window;
|
2023-06-19 20:08:03 +00:00
|
|
|
|
2023-07-08 20:05:15 +00:00
|
|
|
if (window_name != window_data_.title) {
|
2023-06-19 20:08:03 +00:00
|
|
|
if (window_name.empty()) {
|
|
|
|
label_.get_style_context()->add_class("empty");
|
|
|
|
} else {
|
|
|
|
label_.get_style_context()->remove_class("empty");
|
|
|
|
}
|
2023-07-08 20:05:15 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-06-19 20:08:03 +00:00
|
|
|
setClass("empty", workspace_.windows == 0);
|
2023-06-20 00:23:03 +00:00
|
|
|
setClass("solo", solo_);
|
2023-06-19 21:42:19 +00:00
|
|
|
setClass("floating", all_floating_);
|
2023-07-02 17:58:42 +00:00
|
|
|
setClass("hidden", hidden_);
|
|
|
|
setClass("fullscreen", fullscreen_);
|
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
|
|
|
|
2023-07-10 20:02:03 +00:00
|
|
|
updateAppIcon();
|
|
|
|
|
2022-08-17 19:54:23 +00:00
|
|
|
ALabel::update();
|
|
|
|
}
|
|
|
|
|
2023-06-19 20:08:03 +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);
|
2023-06-19 20:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
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-07-01 10:37:46 +00:00
|
|
|
const auto workspaces = gIPC->getSocket1JsonReply("workspaces");
|
|
|
|
assert(workspaces.isArray());
|
2023-07-06 01:01:24 +00:00
|
|
|
auto workspace = std::find_if(workspaces.begin(), workspaces.end(),
|
2023-06-19 21:42:19 +00:00
|
|
|
[&](Json::Value workspace) { return workspace["id"] == id; });
|
2023-07-06 01:01:24 +00:00
|
|
|
if (workspace == std::end(workspaces)) {
|
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 {
|
2023-06-26 21:18:49 +00:00
|
|
|
return Workspace{value["id"].asInt(), value["windows"].asInt(), value["lastwindow"].asString(),
|
|
|
|
value["lastwindowtitle"].asString()};
|
2023-06-19 20:08:03 +00:00
|
|
|
}
|
|
|
|
|
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()};
|
|
|
|
}
|
|
|
|
|
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) {
|
2023-07-02 17:58:42 +00:00
|
|
|
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;
|
|
|
|
});
|
2023-07-02 17:58:42 +00:00
|
|
|
if (active_window == std::end(clients)) {
|
2023-06-19 21:42:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-08 20:05:15 +00:00
|
|
|
window_data_ = WindowData::parse(*active_window);
|
2023-07-10 20:02:03 +00:00
|
|
|
updateAppIconName();
|
2023-06-20 00:23:03 +00:00
|
|
|
std::vector<Json::Value> workspace_windows;
|
2023-07-02 17:58:42 +00:00
|
|
|
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();
|
|
|
|
});
|
2023-07-02 17:58:42 +00:00
|
|
|
hidden_ = std::any_of(workspace_windows.begin(), workspace_windows.end(),
|
2023-07-05 00:38:38 +00:00
|
|
|
[&](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(); });
|
2023-06-19 21:42:19 +00:00
|
|
|
fullscreen_ = (*active_window)["fullscreen"].asBool();
|
2023-07-02 17:58:42 +00:00
|
|
|
|
|
|
|
if (fullscreen_) {
|
|
|
|
solo_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (solo_) {
|
2023-07-08 20:05:15 +00:00
|
|
|
solo_class_ = window_data_.class_name;
|
2023-07-02 17:58:42 +00:00
|
|
|
} else {
|
|
|
|
solo_class_ = "";
|
|
|
|
}
|
2023-06-01 11:38:27 +00:00
|
|
|
} else {
|
2023-07-08 20:05:15 +00:00
|
|
|
window_data_ = WindowData{};
|
2023-06-19 21:42:19 +00:00
|
|
|
all_floating_ = false;
|
2023-07-02 17:58:42 +00:00
|
|
|
hidden_ = false;
|
2023-06-19 21:42:19 +00:00
|
|
|
fullscreen_ = false;
|
2023-07-02 17:58:42 +00:00
|
|
|
solo_ = false;
|
|
|
|
solo_class_ = "";
|
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
|