Merge branch 'Alexays:master' into hyprland/windowcount
This commit is contained in:
@ -11,7 +11,6 @@
|
||||
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
namespace waybar::modules::hyprland {
|
||||
|
||||
@ -44,71 +43,96 @@ std::filesystem::path IPC::getSocketFolder(const char* instanceSig) {
|
||||
return socketFolder_;
|
||||
}
|
||||
|
||||
void IPC::startIPC() {
|
||||
IPC::IPC() {
|
||||
// will start IPC and relay events to parseIPC
|
||||
ipcThread_ = std::thread([this]() { socketListener(); });
|
||||
}
|
||||
|
||||
std::thread([&]() {
|
||||
// check for hyprland
|
||||
const char* his = getenv("HYPRLAND_INSTANCE_SIGNATURE");
|
||||
|
||||
if (his == nullptr) {
|
||||
spdlog::warn("Hyprland is not running, Hyprland IPC will not be available.");
|
||||
return;
|
||||
IPC::~IPC() {
|
||||
running_ = false;
|
||||
spdlog::info("Hyprland IPC stopping...");
|
||||
if (socketfd_ != -1) {
|
||||
spdlog::trace("Shutting down socket");
|
||||
if (shutdown(socketfd_, SHUT_RDWR) == -1) {
|
||||
spdlog::error("Hyprland IPC: Couldn't shutdown socket");
|
||||
}
|
||||
|
||||
if (!modulesReady) return;
|
||||
|
||||
spdlog::info("Hyprland IPC starting");
|
||||
|
||||
struct sockaddr_un addr;
|
||||
int socketfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
|
||||
if (socketfd == -1) {
|
||||
spdlog::error("Hyprland IPC: socketfd failed");
|
||||
return;
|
||||
spdlog::trace("Closing socket");
|
||||
if (close(socketfd_) == -1) {
|
||||
spdlog::error("Hyprland IPC: Couldn't close socket");
|
||||
}
|
||||
}
|
||||
ipcThread_.join();
|
||||
}
|
||||
|
||||
addr.sun_family = AF_UNIX;
|
||||
IPC& IPC::inst() {
|
||||
static IPC ipc;
|
||||
return ipc;
|
||||
}
|
||||
|
||||
auto socketPath = IPC::getSocketFolder(his) / ".socket2.sock";
|
||||
strncpy(addr.sun_path, socketPath.c_str(), sizeof(addr.sun_path) - 1);
|
||||
void IPC::socketListener() {
|
||||
// check for hyprland
|
||||
const char* his = getenv("HYPRLAND_INSTANCE_SIGNATURE");
|
||||
|
||||
addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
|
||||
if (his == nullptr) {
|
||||
spdlog::warn("Hyprland is not running, Hyprland IPC will not be available.");
|
||||
return;
|
||||
}
|
||||
|
||||
int l = sizeof(struct sockaddr_un);
|
||||
if (!modulesReady) return;
|
||||
|
||||
if (connect(socketfd, (struct sockaddr*)&addr, l) == -1) {
|
||||
spdlog::error("Hyprland IPC: Unable to connect?");
|
||||
return;
|
||||
}
|
||||
spdlog::info("Hyprland IPC starting");
|
||||
|
||||
auto* file = fdopen(socketfd, "r");
|
||||
struct sockaddr_un addr;
|
||||
socketfd_ = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
|
||||
while (true) {
|
||||
std::array<char, 1024> buffer; // Hyprland socket2 events are max 1024 bytes
|
||||
if (socketfd_ == -1) {
|
||||
spdlog::error("Hyprland IPC: socketfd failed");
|
||||
return;
|
||||
}
|
||||
|
||||
auto* receivedCharPtr = fgets(buffer.data(), buffer.size(), file);
|
||||
addr.sun_family = AF_UNIX;
|
||||
|
||||
if (receivedCharPtr == nullptr) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
continue;
|
||||
}
|
||||
auto socketPath = IPC::getSocketFolder(his) / ".socket2.sock";
|
||||
strncpy(addr.sun_path, socketPath.c_str(), sizeof(addr.sun_path) - 1);
|
||||
|
||||
std::string messageReceived(buffer.data());
|
||||
messageReceived = messageReceived.substr(0, messageReceived.find_first_of('\n'));
|
||||
spdlog::debug("hyprland IPC received {}", messageReceived);
|
||||
addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
|
||||
|
||||
try {
|
||||
parseIPC(messageReceived);
|
||||
} catch (std::exception& e) {
|
||||
spdlog::warn("Failed to parse IPC message: {}, reason: {}", messageReceived, e.what());
|
||||
} catch (...) {
|
||||
throw;
|
||||
}
|
||||
int l = sizeof(struct sockaddr_un);
|
||||
|
||||
if (connect(socketfd_, (struct sockaddr*)&addr, l) == -1) {
|
||||
spdlog::error("Hyprland IPC: Unable to connect?");
|
||||
return;
|
||||
}
|
||||
auto* file = fdopen(socketfd_, "r");
|
||||
if (file == nullptr) {
|
||||
spdlog::error("Hyprland IPC: Couldn't open file descriptor");
|
||||
return;
|
||||
}
|
||||
while (running_) {
|
||||
std::array<char, 1024> buffer; // Hyprland socket2 events are max 1024 bytes
|
||||
|
||||
auto* receivedCharPtr = fgets(buffer.data(), buffer.size(), file);
|
||||
|
||||
if (receivedCharPtr == nullptr) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
continue;
|
||||
}
|
||||
}).detach();
|
||||
|
||||
std::string messageReceived(buffer.data());
|
||||
messageReceived = messageReceived.substr(0, messageReceived.find_first_of('\n'));
|
||||
spdlog::debug("hyprland IPC received {}", messageReceived);
|
||||
|
||||
try {
|
||||
parseIPC(messageReceived);
|
||||
} catch (std::exception& e) {
|
||||
spdlog::warn("Failed to parse IPC message: {}, reason: {}", messageReceived, e.what());
|
||||
} catch (...) {
|
||||
throw;
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
spdlog::debug("Hyprland IPC stopped");
|
||||
}
|
||||
|
||||
void IPC::parseIPC(const std::string& ev) {
|
||||
|
@ -10,13 +10,9 @@
|
||||
namespace waybar::modules::hyprland {
|
||||
|
||||
Language::Language(const std::string& id, const Bar& bar, const Json::Value& config)
|
||||
: ALabel(config, "language", id, "{}", 0, true), bar_(bar) {
|
||||
: ALabel(config, "language", id, "{}", 0, true), bar_(bar), m_ipc(IPC::inst()) {
|
||||
modulesReady = true;
|
||||
|
||||
if (!gIPC) {
|
||||
gIPC = std::make_unique<IPC>();
|
||||
}
|
||||
|
||||
// get the active layout when open
|
||||
initLanguage();
|
||||
|
||||
@ -24,11 +20,11 @@ Language::Language(const std::string& id, const Bar& bar, const Json::Value& con
|
||||
update();
|
||||
|
||||
// register for hyprland ipc
|
||||
gIPC->registerForIPC("activelayout", this);
|
||||
m_ipc.registerForIPC("activelayout", this);
|
||||
}
|
||||
|
||||
Language::~Language() {
|
||||
gIPC->unregisterForIPC(this);
|
||||
m_ipc.unregisterForIPC(this);
|
||||
// wait for possible event handler to finish
|
||||
std::lock_guard<std::mutex> lg(mutex_);
|
||||
}
|
||||
@ -85,7 +81,7 @@ void Language::onEvent(const std::string& ev) {
|
||||
}
|
||||
|
||||
void Language::initLanguage() {
|
||||
const auto inputDevices = gIPC->getSocket1Reply("devices");
|
||||
const auto inputDevices = m_ipc.getSocket1Reply("devices");
|
||||
|
||||
const auto kbName = config_["keyboard-name"].asString();
|
||||
|
||||
|
@ -7,15 +7,11 @@
|
||||
namespace waybar::modules::hyprland {
|
||||
|
||||
Submap::Submap(const std::string& id, const Bar& bar, const Json::Value& config)
|
||||
: ALabel(config, "submap", id, "{}", 0, true), bar_(bar) {
|
||||
: ALabel(config, "submap", id, "{}", 0, true), bar_(bar), m_ipc(IPC::inst()) {
|
||||
modulesReady = true;
|
||||
|
||||
parseConfig(config);
|
||||
|
||||
if (!gIPC) {
|
||||
gIPC = std::make_unique<IPC>();
|
||||
}
|
||||
|
||||
label_.hide();
|
||||
ALabel::update();
|
||||
|
||||
@ -27,12 +23,12 @@ Submap::Submap(const std::string& id, const Bar& bar, const Json::Value& config)
|
||||
}
|
||||
|
||||
// register for hyprland ipc
|
||||
gIPC->registerForIPC("submap", this);
|
||||
m_ipc.registerForIPC("submap", this);
|
||||
dp.emit();
|
||||
}
|
||||
|
||||
Submap::~Submap() {
|
||||
gIPC->unregisterForIPC(this);
|
||||
m_ipc.unregisterForIPC(this);
|
||||
// wait for possible event handler to finish
|
||||
std::lock_guard<std::mutex> lg(mutex_);
|
||||
}
|
||||
|
@ -6,37 +6,30 @@
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <shared_mutex>
|
||||
#include <vector>
|
||||
|
||||
#include "modules/hyprland/backend.hpp"
|
||||
#include "util/rewrite_string.hpp"
|
||||
#include "util/sanitize_str.hpp"
|
||||
|
||||
#include <shared_mutex>
|
||||
#include <thread>
|
||||
|
||||
namespace waybar::modules::hyprland {
|
||||
|
||||
std::shared_mutex windowIpcSmtx;
|
||||
|
||||
Window::Window(const std::string& id, const Bar& bar, const Json::Value& config)
|
||||
: AAppIconLabel(config, "window", id, "{title}", 0, true), bar_(bar) {
|
||||
|
||||
: AAppIconLabel(config, "window", id, "{title}", 0, true), bar_(bar), m_ipc(IPC::inst()) {
|
||||
std::unique_lock<std::shared_mutex> windowIpcUniqueLock(windowIpcSmtx);
|
||||
|
||||
modulesReady = true;
|
||||
separateOutputs_ = config["separate-outputs"].asBool();
|
||||
|
||||
if (!gIPC) {
|
||||
gIPC = std::make_unique<IPC>();
|
||||
}
|
||||
|
||||
// register for hyprland ipc
|
||||
gIPC->registerForIPC("activewindow", this);
|
||||
gIPC->registerForIPC("closewindow", this);
|
||||
gIPC->registerForIPC("movewindow", this);
|
||||
gIPC->registerForIPC("changefloatingmode", this);
|
||||
gIPC->registerForIPC("fullscreen", this);
|
||||
m_ipc.registerForIPC("activewindow", this);
|
||||
m_ipc.registerForIPC("closewindow", this);
|
||||
m_ipc.registerForIPC("movewindow", this);
|
||||
m_ipc.registerForIPC("changefloatingmode", this);
|
||||
m_ipc.registerForIPC("fullscreen", this);
|
||||
|
||||
windowIpcUniqueLock.unlock();
|
||||
|
||||
@ -47,11 +40,10 @@ Window::Window(const std::string& id, const Bar& bar, const Json::Value& config)
|
||||
|
||||
Window::~Window() {
|
||||
std::unique_lock<std::shared_mutex> windowIpcUniqueLock(windowIpcSmtx);
|
||||
gIPC->unregisterForIPC(this);
|
||||
m_ipc.unregisterForIPC(this);
|
||||
}
|
||||
|
||||
auto Window::update() -> void {
|
||||
|
||||
std::shared_lock<std::shared_mutex> windowIpcShareLock(windowIpcSmtx);
|
||||
|
||||
std::string windowName = waybar::util::sanitize_string(workspace_.last_window_title);
|
||||
@ -59,18 +51,36 @@ auto Window::update() -> void {
|
||||
|
||||
windowData_.title = windowName;
|
||||
|
||||
std::string label_text;
|
||||
if (!format_.empty()) {
|
||||
label_.show();
|
||||
label_.set_markup(waybar::util::rewriteString(
|
||||
label_text = waybar::util::rewriteString(
|
||||
fmt::format(fmt::runtime(format_), fmt::arg("title", windowName),
|
||||
fmt::arg("initialTitle", windowData_.initial_title),
|
||||
fmt::arg("class", windowData_.class_name),
|
||||
fmt::arg("initialClass", windowData_.initial_class_name)),
|
||||
config_["rewrite"]));
|
||||
config_["rewrite"]);
|
||||
label_.set_markup(label_text);
|
||||
} else {
|
||||
label_.hide();
|
||||
}
|
||||
|
||||
if (tooltipEnabled()) {
|
||||
std::string tooltip_format;
|
||||
if (config_["tooltip-format"].isString()) {
|
||||
tooltip_format = config_["tooltip-format"].asString();
|
||||
}
|
||||
if (!tooltip_format.empty()) {
|
||||
label_.set_tooltip_text(
|
||||
fmt::format(fmt::runtime(tooltip_format), fmt::arg("title", windowName),
|
||||
fmt::arg("initialTitle", windowData_.initial_title),
|
||||
fmt::arg("class", windowData_.class_name),
|
||||
fmt::arg("initialClass", windowData_.initial_class_name)));
|
||||
} else if (!label_text.empty()) {
|
||||
label_.set_tooltip_text(label_text);
|
||||
}
|
||||
}
|
||||
|
||||
if (focused_) {
|
||||
image_.show();
|
||||
} else {
|
||||
@ -100,7 +110,7 @@ auto Window::update() -> void {
|
||||
}
|
||||
|
||||
auto Window::getActiveWorkspace() -> Workspace {
|
||||
const auto workspace = gIPC->getSocket1JsonReply("activeworkspace");
|
||||
const auto workspace = IPC::inst().getSocket1JsonReply("activeworkspace");
|
||||
|
||||
if (workspace.isObject()) {
|
||||
return Workspace::parse(workspace);
|
||||
@ -110,24 +120,33 @@ auto Window::getActiveWorkspace() -> Workspace {
|
||||
}
|
||||
|
||||
auto Window::getActiveWorkspace(const std::string& monitorName) -> Workspace {
|
||||
const auto monitors = gIPC->getSocket1JsonReply("monitors");
|
||||
const auto monitors = IPC::inst().getSocket1JsonReply("monitors");
|
||||
if (monitors.isArray()) {
|
||||
auto monitor = std::find_if(monitors.begin(), monitors.end(), [&](Json::Value monitor) {
|
||||
return monitor["name"] == monitorName;
|
||||
});
|
||||
auto monitor = std::ranges::find_if(
|
||||
monitors, [&](Json::Value monitor) { return monitor["name"] == monitorName; });
|
||||
if (monitor == std::end(monitors)) {
|
||||
spdlog::warn("Monitor not found: {}", monitorName);
|
||||
return Workspace{-1, 0, "", ""};
|
||||
return Workspace{
|
||||
.id = -1,
|
||||
.windows = 0,
|
||||
.last_window = "",
|
||||
.last_window_title = "",
|
||||
};
|
||||
}
|
||||
const int id = (*monitor)["activeWorkspace"]["id"].asInt();
|
||||
|
||||
const auto workspaces = gIPC->getSocket1JsonReply("workspaces");
|
||||
const auto workspaces = IPC::inst().getSocket1JsonReply("workspaces");
|
||||
if (workspaces.isArray()) {
|
||||
auto workspace = std::find_if(workspaces.begin(), workspaces.end(),
|
||||
[&](Json::Value workspace) { return workspace["id"] == id; });
|
||||
auto workspace = std::ranges::find_if(
|
||||
workspaces, [&](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{
|
||||
.id = -1,
|
||||
.windows = 0,
|
||||
.last_window = "",
|
||||
.last_window_title = "",
|
||||
};
|
||||
}
|
||||
return Workspace::parse(*workspace);
|
||||
};
|
||||
@ -138,22 +157,25 @@ auto Window::getActiveWorkspace(const std::string& monitorName) -> Workspace {
|
||||
|
||||
auto Window::Workspace::parse(const Json::Value& value) -> Window::Workspace {
|
||||
return Workspace{
|
||||
value["id"].asInt(),
|
||||
value["windows"].asInt(),
|
||||
value["lastwindow"].asString(),
|
||||
value["lastwindowtitle"].asString(),
|
||||
.id = value["id"].asInt(),
|
||||
.windows = value["windows"].asInt(),
|
||||
.last_window = value["lastwindow"].asString(),
|
||||
.last_window_title = value["lastwindowtitle"].asString(),
|
||||
};
|
||||
}
|
||||
|
||||
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(),
|
||||
value["fullscreen"].asBool(), !value["grouped"].empty()};
|
||||
return WindowData{.floating = value["floating"].asBool(),
|
||||
.monitor = value["monitor"].asInt(),
|
||||
.class_name = value["class"].asString(),
|
||||
.initial_class_name = value["initialClass"].asString(),
|
||||
.title = value["title"].asString(),
|
||||
.initial_title = value["initialTitle"].asString(),
|
||||
.fullscreen = value["fullscreen"].asBool(),
|
||||
.grouped = !value["grouped"].empty()};
|
||||
}
|
||||
|
||||
void Window::queryActiveWorkspace() {
|
||||
|
||||
std::shared_lock<std::shared_mutex> windowIpcShareLock(windowIpcSmtx);
|
||||
|
||||
if (separateOutputs_) {
|
||||
@ -164,11 +186,10 @@ void Window::queryActiveWorkspace() {
|
||||
|
||||
focused_ = true;
|
||||
if (workspace_.windows > 0) {
|
||||
const auto clients = gIPC->getSocket1JsonReply("clients");
|
||||
const auto clients = m_ipc.getSocket1JsonReply("clients");
|
||||
if (clients.isArray()) {
|
||||
auto activeWindow = std::find_if(clients.begin(), clients.end(), [&](Json::Value window) {
|
||||
return window["address"] == workspace_.last_window;
|
||||
});
|
||||
auto activeWindow = std::ranges::find_if(
|
||||
clients, [&](Json::Value window) { return window["address"] == workspace_.last_window; });
|
||||
|
||||
if (activeWindow == std::end(clients)) {
|
||||
focused_ = false;
|
||||
@ -178,22 +199,19 @@ void Window::queryActiveWorkspace() {
|
||||
windowData_ = WindowData::parse(*activeWindow);
|
||||
updateAppIconName(windowData_.class_name, windowData_.initial_class_name);
|
||||
std::vector<Json::Value> workspaceWindows;
|
||||
std::copy_if(clients.begin(), clients.end(), std::back_inserter(workspaceWindows),
|
||||
[&](Json::Value window) {
|
||||
return window["workspace"]["id"] == workspace_.id && window["mapped"].asBool();
|
||||
});
|
||||
swallowing_ =
|
||||
std::any_of(workspaceWindows.begin(), workspaceWindows.end(), [&](Json::Value window) {
|
||||
return !window["swallowing"].isNull() && window["swallowing"].asString() != "0x0";
|
||||
});
|
||||
std::ranges::copy_if(clients, std::back_inserter(workspaceWindows), [&](Json::Value window) {
|
||||
return window["workspace"]["id"] == workspace_.id && window["mapped"].asBool();
|
||||
});
|
||||
swallowing_ = std::ranges::any_of(workspaceWindows, [&](Json::Value window) {
|
||||
return !window["swallowing"].isNull() && window["swallowing"].asString() != "0x0";
|
||||
});
|
||||
std::vector<Json::Value> visibleWindows;
|
||||
std::copy_if(workspaceWindows.begin(), workspaceWindows.end(),
|
||||
std::back_inserter(visibleWindows),
|
||||
[&](Json::Value window) { return !window["hidden"].asBool(); });
|
||||
std::ranges::copy_if(workspaceWindows, std::back_inserter(visibleWindows),
|
||||
[&](Json::Value window) { return !window["hidden"].asBool(); });
|
||||
solo_ = 1 == std::count_if(visibleWindows.begin(), visibleWindows.end(),
|
||||
[&](Json::Value window) { return !window["floating"].asBool(); });
|
||||
allFloating_ = std::all_of(visibleWindows.begin(), visibleWindows.end(),
|
||||
[&](Json::Value window) { return window["floating"].asBool(); });
|
||||
allFloating_ = std::ranges::all_of(
|
||||
visibleWindows, [&](Json::Value window) { return window["floating"].asBool(); });
|
||||
fullscreen_ = windowData_.fullscreen;
|
||||
|
||||
// Fullscreen windows look like they are solo
|
||||
@ -206,7 +224,7 @@ void Window::queryActiveWorkspace() {
|
||||
} else {
|
||||
soloClass_ = "";
|
||||
}
|
||||
};
|
||||
}
|
||||
} else {
|
||||
focused_ = false;
|
||||
windowData_ = WindowData{};
|
||||
|
@ -18,7 +18,8 @@ Workspace::Workspace(const Json::Value &workspace_data, Workspaces &workspace_ma
|
||||
m_windows(workspace_data["windows"].asInt()),
|
||||
m_isActive(true),
|
||||
m_isPersistentRule(workspace_data["persistent-rule"].asBool()),
|
||||
m_isPersistentConfig(workspace_data["persistent-config"].asBool()) {
|
||||
m_isPersistentConfig(workspace_data["persistent-config"].asBool()),
|
||||
m_ipc(IPC::inst()) {
|
||||
if (m_name.starts_with("name:")) {
|
||||
m_name = m_name.substr(5);
|
||||
} else if (m_name.starts_with("special")) {
|
||||
@ -58,20 +59,20 @@ bool Workspace::handleClicked(GdkEventButton *bt) const {
|
||||
try {
|
||||
if (id() > 0) { // normal
|
||||
if (m_workspaceManager.moveToMonitor()) {
|
||||
gIPC->getSocket1Reply("dispatch focusworkspaceoncurrentmonitor " + std::to_string(id()));
|
||||
m_ipc.getSocket1Reply("dispatch focusworkspaceoncurrentmonitor " + std::to_string(id()));
|
||||
} else {
|
||||
gIPC->getSocket1Reply("dispatch workspace " + std::to_string(id()));
|
||||
m_ipc.getSocket1Reply("dispatch workspace " + std::to_string(id()));
|
||||
}
|
||||
} else if (!isSpecial()) { // named (this includes persistent)
|
||||
if (m_workspaceManager.moveToMonitor()) {
|
||||
gIPC->getSocket1Reply("dispatch focusworkspaceoncurrentmonitor name:" + name());
|
||||
m_ipc.getSocket1Reply("dispatch focusworkspaceoncurrentmonitor name:" + name());
|
||||
} else {
|
||||
gIPC->getSocket1Reply("dispatch workspace name:" + name());
|
||||
m_ipc.getSocket1Reply("dispatch workspace name:" + name());
|
||||
}
|
||||
} else if (id() != -99) { // named special
|
||||
gIPC->getSocket1Reply("dispatch togglespecialworkspace " + name());
|
||||
m_ipc.getSocket1Reply("dispatch togglespecialworkspace " + name());
|
||||
} else { // special
|
||||
gIPC->getSocket1Reply("dispatch togglespecialworkspace");
|
||||
m_ipc.getSocket1Reply("dispatch togglespecialworkspace");
|
||||
}
|
||||
return true;
|
||||
} catch (const std::exception &e) {
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
@ -13,7 +14,10 @@
|
||||
namespace waybar::modules::hyprland {
|
||||
|
||||
Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value &config)
|
||||
: AModule(config, "workspaces", id, false, false), m_bar(bar), m_box(bar.orientation, 0) {
|
||||
: AModule(config, "workspaces", id, false, false),
|
||||
m_bar(bar),
|
||||
m_box(bar.orientation, 0),
|
||||
m_ipc(IPC::inst()) {
|
||||
modulesReady = true;
|
||||
parseConfig(config);
|
||||
|
||||
@ -24,23 +28,19 @@ Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value
|
||||
m_box.get_style_context()->add_class(MODULE_CLASS);
|
||||
event_box_.add(m_box);
|
||||
|
||||
if (!gIPC) {
|
||||
gIPC = std::make_unique<IPC>();
|
||||
}
|
||||
|
||||
setCurrentMonitorId();
|
||||
init();
|
||||
registerIpc();
|
||||
}
|
||||
|
||||
Workspaces::~Workspaces() {
|
||||
gIPC->unregisterForIPC(this);
|
||||
m_ipc.unregisterForIPC(this);
|
||||
// wait for possible event handler to finish
|
||||
std::lock_guard<std::mutex> lg(m_mutex);
|
||||
}
|
||||
|
||||
void Workspaces::init() {
|
||||
m_activeWorkspaceName = (gIPC->getSocket1JsonReply("activeworkspace"))["name"].asString();
|
||||
m_activeWorkspaceId = m_ipc.getSocket1JsonReply("activeworkspace")["id"].asInt();
|
||||
|
||||
initializeWorkspaces();
|
||||
dp.emit();
|
||||
@ -50,13 +50,12 @@ Json::Value Workspaces::createMonitorWorkspaceData(std::string const &name,
|
||||
std::string const &monitor) {
|
||||
spdlog::trace("Creating persistent workspace: {} on monitor {}", name, monitor);
|
||||
Json::Value workspaceData;
|
||||
try {
|
||||
// numbered persistent workspaces get the name as ID
|
||||
workspaceData["id"] = name == "special" ? -99 : std::stoi(name);
|
||||
} catch (const std::exception &e) {
|
||||
// named persistent workspaces start with ID=0
|
||||
workspaceData["id"] = 0;
|
||||
|
||||
auto workspaceId = parseWorkspaceId(name);
|
||||
if (!workspaceId.has_value()) {
|
||||
workspaceId = 0;
|
||||
}
|
||||
workspaceData["id"] = *workspaceId;
|
||||
workspaceData["name"] = name;
|
||||
workspaceData["monitor"] = monitor;
|
||||
workspaceData["windows"] = 0;
|
||||
@ -69,9 +68,8 @@ void Workspaces::createWorkspace(Json::Value const &workspace_data,
|
||||
spdlog::debug("Creating workspace {}", workspaceName);
|
||||
|
||||
// avoid recreating existing workspaces
|
||||
auto workspace = std::find_if(
|
||||
m_workspaces.begin(), m_workspaces.end(),
|
||||
[workspaceName](std::unique_ptr<Workspace> const &w) {
|
||||
auto workspace =
|
||||
std::ranges::find_if(m_workspaces, [workspaceName](std::unique_ptr<Workspace> const &w) {
|
||||
return (workspaceName.starts_with("special:") && workspaceName.substr(8) == w->name()) ||
|
||||
workspaceName == w->name();
|
||||
});
|
||||
@ -81,14 +79,14 @@ void Workspaces::createWorkspace(Json::Value const &workspace_data,
|
||||
const auto keys = workspace_data.getMemberNames();
|
||||
|
||||
const auto *k = "persistent-rule";
|
||||
if (std::find(keys.begin(), keys.end(), k) != keys.end()) {
|
||||
if (std::ranges::find(keys, k) != keys.end()) {
|
||||
spdlog::debug("Set dynamic persistency of workspace {} to: {}", workspaceName,
|
||||
workspace_data[k].asBool() ? "true" : "false");
|
||||
(*workspace)->setPersistentRule(workspace_data[k].asBool());
|
||||
}
|
||||
|
||||
k = "persistent-config";
|
||||
if (std::find(keys.begin(), keys.end(), k) != keys.end()) {
|
||||
if (std::ranges::find(keys, k) != keys.end()) {
|
||||
spdlog::debug("Set config persistency of workspace {} to: {}", workspaceName,
|
||||
workspace_data[k].asBool() ? "true" : "false");
|
||||
(*workspace)->setPersistentConfig(workspace_data[k].asBool());
|
||||
@ -159,18 +157,18 @@ std::string Workspaces::getRewrite(std::string window_class, std::string window_
|
||||
fmt::arg("title", window_title));
|
||||
}
|
||||
|
||||
std::vector<std::string> Workspaces::getVisibleWorkspaces() {
|
||||
std::vector<std::string> visibleWorkspaces;
|
||||
auto monitors = gIPC->getSocket1JsonReply("monitors");
|
||||
std::vector<int> Workspaces::getVisibleWorkspaces() {
|
||||
std::vector<int> visibleWorkspaces;
|
||||
auto monitors = IPC::inst().getSocket1JsonReply("monitors");
|
||||
for (const auto &monitor : monitors) {
|
||||
auto ws = monitor["activeWorkspace"];
|
||||
if (ws.isObject() && ws["name"].isString()) {
|
||||
visibleWorkspaces.push_back(ws["name"].asString());
|
||||
if (ws.isObject() && ws["id"].isInt()) {
|
||||
visibleWorkspaces.push_back(ws["id"].asInt());
|
||||
}
|
||||
auto sws = monitor["specialWorkspace"];
|
||||
auto name = sws["name"].asString();
|
||||
if (sws.isObject() && sws["name"].isString() && !name.empty()) {
|
||||
visibleWorkspaces.push_back(!name.starts_with("special:") ? name : name.substr(8));
|
||||
if (sws.isObject() && sws["id"].isInt() && !name.empty()) {
|
||||
visibleWorkspaces.push_back(sws["id"].asInt());
|
||||
}
|
||||
}
|
||||
return visibleWorkspaces;
|
||||
@ -181,12 +179,12 @@ void Workspaces::initializeWorkspaces() {
|
||||
|
||||
// if the workspace rules changed since last initialization, make sure we reset everything:
|
||||
for (auto &workspace : m_workspaces) {
|
||||
m_workspacesToRemove.push_back(workspace->name());
|
||||
m_workspacesToRemove.push_back(std::to_string(workspace->id()));
|
||||
}
|
||||
|
||||
// get all current workspaces
|
||||
auto const workspacesJson = gIPC->getSocket1JsonReply("workspaces");
|
||||
auto const clientsJson = gIPC->getSocket1JsonReply("clients");
|
||||
auto const workspacesJson = m_ipc.getSocket1JsonReply("workspaces");
|
||||
auto const clientsJson = m_ipc.getSocket1JsonReply("clients");
|
||||
|
||||
for (Json::Value workspaceJson : workspacesJson) {
|
||||
std::string workspaceName = workspaceJson["name"].asString();
|
||||
@ -233,7 +231,7 @@ void Workspaces::loadPersistentWorkspacesFromConfig(Json::Value const &clientsJs
|
||||
std::vector<std::string> persistentWorkspacesToCreate;
|
||||
|
||||
const std::string currentMonitor = m_bar.output->name;
|
||||
const bool monitorInConfig = std::find(keys.begin(), keys.end(), currentMonitor) != keys.end();
|
||||
const bool monitorInConfig = std::ranges::find(keys, currentMonitor) != keys.end();
|
||||
for (const std::string &key : keys) {
|
||||
// only add if either:
|
||||
// 1. key is the current monitor name
|
||||
@ -248,7 +246,7 @@ void Workspaces::loadPersistentWorkspacesFromConfig(Json::Value const &clientsJs
|
||||
int amount = value.asInt();
|
||||
spdlog::debug("Creating {} persistent workspaces for monitor {}", amount, currentMonitor);
|
||||
for (int i = 0; i < amount; i++) {
|
||||
persistentWorkspacesToCreate.emplace_back(std::to_string(m_monitorId * amount + i + 1));
|
||||
persistentWorkspacesToCreate.emplace_back(std::to_string((m_monitorId * amount) + i + 1));
|
||||
}
|
||||
}
|
||||
} else if (value.isArray() && !value.empty()) {
|
||||
@ -285,7 +283,7 @@ void Workspaces::loadPersistentWorkspacesFromConfig(Json::Value const &clientsJs
|
||||
void Workspaces::loadPersistentWorkspacesFromWorkspaceRules(const Json::Value &clientsJson) {
|
||||
spdlog::info("Loading persistent workspaces from Hyprland workspace rules");
|
||||
|
||||
auto const workspaceRules = gIPC->getSocket1JsonReply("workspacerules");
|
||||
auto const workspaceRules = m_ipc.getSocket1JsonReply("workspacerules");
|
||||
for (Json::Value const &rule : workspaceRules) {
|
||||
if (!rule["workspaceString"].isString()) {
|
||||
spdlog::warn("Workspace rules: invalid workspaceString, skipping: {}", rule);
|
||||
@ -294,7 +292,8 @@ void Workspaces::loadPersistentWorkspacesFromWorkspaceRules(const Json::Value &c
|
||||
if (!rule["persistent"].asBool()) {
|
||||
continue;
|
||||
}
|
||||
auto const &workspace = rule["workspaceString"].asString();
|
||||
auto const &workspace = rule.isMember("defaultName") ? rule["defaultName"].asString()
|
||||
: rule["workspaceString"].asString();
|
||||
auto const &monitor = rule["monitor"].asString();
|
||||
// create this workspace persistently if:
|
||||
// 1. the allOutputs config option is enabled
|
||||
@ -306,6 +305,7 @@ void Workspaces::loadPersistentWorkspacesFromWorkspaceRules(const Json::Value &c
|
||||
workspaceData["persistent-rule"] = true;
|
||||
m_workspacesToCreate.emplace_back(workspaceData, clientsJson);
|
||||
} else {
|
||||
// This can be any workspace selector.
|
||||
m_workspacesToRemove.emplace_back(workspace);
|
||||
}
|
||||
}
|
||||
@ -316,29 +316,29 @@ void Workspaces::onEvent(const std::string &ev) {
|
||||
std::string eventName(begin(ev), begin(ev) + ev.find_first_of('>'));
|
||||
std::string payload = ev.substr(eventName.size() + 2);
|
||||
|
||||
if (eventName == "workspace") {
|
||||
if (eventName == "workspacev2") {
|
||||
onWorkspaceActivated(payload);
|
||||
} else if (eventName == "activespecial") {
|
||||
onSpecialWorkspaceActivated(payload);
|
||||
} else if (eventName == "destroyworkspace") {
|
||||
} else if (eventName == "destroyworkspacev2") {
|
||||
onWorkspaceDestroyed(payload);
|
||||
} else if (eventName == "createworkspace") {
|
||||
} else if (eventName == "createworkspacev2") {
|
||||
onWorkspaceCreated(payload);
|
||||
} else if (eventName == "focusedmon") {
|
||||
} else if (eventName == "focusedmonv2") {
|
||||
onMonitorFocused(payload);
|
||||
} else if (eventName == "moveworkspace") {
|
||||
} else if (eventName == "moveworkspacev2") {
|
||||
onWorkspaceMoved(payload);
|
||||
} else if (eventName == "openwindow") {
|
||||
onWindowOpened(payload);
|
||||
} else if (eventName == "closewindow") {
|
||||
onWindowClosed(payload);
|
||||
} else if (eventName == "movewindow") {
|
||||
} else if (eventName == "movewindowv2") {
|
||||
onWindowMoved(payload);
|
||||
} else if (eventName == "urgent") {
|
||||
setUrgentWorkspace(payload);
|
||||
} else if (eventName == "renameworkspace") {
|
||||
onWorkspaceRenamed(payload);
|
||||
} else if (eventName == "windowtitle") {
|
||||
} else if (eventName == "windowtitlev2") {
|
||||
onWindowTitleEvent(payload);
|
||||
} else if (eventName == "configreloaded") {
|
||||
onConfigReloaded();
|
||||
@ -348,7 +348,11 @@ void Workspaces::onEvent(const std::string &ev) {
|
||||
}
|
||||
|
||||
void Workspaces::onWorkspaceActivated(std::string const &payload) {
|
||||
m_activeWorkspaceName = payload;
|
||||
const auto [workspaceIdStr, workspaceName] = splitDoublePayload(payload);
|
||||
const auto workspaceId = parseWorkspaceId(workspaceIdStr);
|
||||
if (workspaceId.has_value()) {
|
||||
m_activeWorkspaceId = *workspaceId;
|
||||
}
|
||||
}
|
||||
|
||||
void Workspaces::onSpecialWorkspaceActivated(std::string const &payload) {
|
||||
@ -357,39 +361,55 @@ void Workspaces::onSpecialWorkspaceActivated(std::string const &payload) {
|
||||
}
|
||||
|
||||
void Workspaces::onWorkspaceDestroyed(std::string const &payload) {
|
||||
if (!isDoubleSpecial(payload)) {
|
||||
m_workspacesToRemove.push_back(payload);
|
||||
const auto [workspaceId, workspaceName] = splitDoublePayload(payload);
|
||||
if (!isDoubleSpecial(workspaceName)) {
|
||||
m_workspacesToRemove.push_back(workspaceId);
|
||||
}
|
||||
}
|
||||
|
||||
void Workspaces::onWorkspaceCreated(std::string const &workspaceName,
|
||||
Json::Value const &clientsData) {
|
||||
spdlog::debug("Workspace created: {}", workspaceName);
|
||||
auto const workspacesJson = gIPC->getSocket1JsonReply("workspaces");
|
||||
void Workspaces::onWorkspaceCreated(std::string const &payload, Json::Value const &clientsData) {
|
||||
spdlog::debug("Workspace created: {}", payload);
|
||||
|
||||
if (!isWorkspaceIgnored(workspaceName)) {
|
||||
auto const workspaceRules = gIPC->getSocket1JsonReply("workspacerules");
|
||||
for (Json::Value workspaceJson : workspacesJson) {
|
||||
std::string name = workspaceJson["name"].asString();
|
||||
if (name == workspaceName) {
|
||||
if ((allOutputs() || m_bar.output->name == workspaceJson["monitor"].asString()) &&
|
||||
(showSpecial() || !name.starts_with("special")) && !isDoubleSpecial(workspaceName)) {
|
||||
for (Json::Value const &rule : workspaceRules) {
|
||||
if (rule["workspaceString"].asString() == workspaceName) {
|
||||
workspaceJson["persistent-rule"] = rule["persistent"].asBool();
|
||||
break;
|
||||
}
|
||||
}
|
||||
const auto [workspaceIdStr, _] = splitDoublePayload(payload);
|
||||
|
||||
m_workspacesToCreate.emplace_back(workspaceJson, clientsData);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
extendOrphans(workspaceJson["id"].asInt(), clientsData);
|
||||
const auto workspaceId = parseWorkspaceId(workspaceIdStr);
|
||||
if (!workspaceId.has_value()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto const workspaceRules = m_ipc.getSocket1JsonReply("workspacerules");
|
||||
auto const workspacesJson = m_ipc.getSocket1JsonReply("workspaces");
|
||||
|
||||
for (Json::Value workspaceJson : workspacesJson) {
|
||||
const auto currentId = workspaceJson["id"].asInt();
|
||||
if (currentId == *workspaceId) {
|
||||
std::string workspaceName = workspaceJson["name"].asString();
|
||||
// This workspace name is more up-to-date than the one in the event payload.
|
||||
if (isWorkspaceIgnored(workspaceName)) {
|
||||
spdlog::trace("Not creating workspace because it is ignored: id={} name={}", *workspaceId,
|
||||
workspaceName);
|
||||
break;
|
||||
}
|
||||
|
||||
if ((allOutputs() || m_bar.output->name == workspaceJson["monitor"].asString()) &&
|
||||
(showSpecial() || !workspaceName.starts_with("special")) &&
|
||||
!isDoubleSpecial(workspaceName)) {
|
||||
for (Json::Value const &rule : workspaceRules) {
|
||||
auto ruleWorkspaceName = rule.isMember("defaultName")
|
||||
? rule["defaultName"].asString()
|
||||
: rule["workspaceString"].asString();
|
||||
if (ruleWorkspaceName == workspaceName) {
|
||||
workspaceJson["persistent-rule"] = rule["persistent"].asBool();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
m_workspacesToCreate.emplace_back(workspaceJson, clientsData);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
extendOrphans(*workspaceId, clientsData);
|
||||
}
|
||||
} else {
|
||||
spdlog::trace("Not creating workspace because it is ignored: {}", workspaceName);
|
||||
}
|
||||
}
|
||||
|
||||
@ -397,32 +417,34 @@ void Workspaces::onWorkspaceMoved(std::string const &payload) {
|
||||
spdlog::debug("Workspace moved: {}", payload);
|
||||
|
||||
// Update active workspace
|
||||
m_activeWorkspaceName = (gIPC->getSocket1JsonReply("activeworkspace"))["name"].asString();
|
||||
m_activeWorkspaceId = (m_ipc.getSocket1JsonReply("activeworkspace"))["id"].asInt();
|
||||
|
||||
if (allOutputs()) return;
|
||||
|
||||
std::string workspaceName = payload.substr(0, payload.find(','));
|
||||
std::string monitorName = payload.substr(payload.find(',') + 1);
|
||||
const auto [workspaceIdStr, workspaceName, monitorName] = splitTriplePayload(payload);
|
||||
|
||||
const auto subPayload = makePayload(workspaceIdStr, workspaceName);
|
||||
|
||||
if (m_bar.output->name == monitorName) {
|
||||
Json::Value clientsData = gIPC->getSocket1JsonReply("clients");
|
||||
onWorkspaceCreated(workspaceName, clientsData);
|
||||
Json::Value clientsData = m_ipc.getSocket1JsonReply("clients");
|
||||
onWorkspaceCreated(subPayload, clientsData);
|
||||
} else {
|
||||
spdlog::debug("Removing workspace because it was moved to another monitor: {}");
|
||||
onWorkspaceDestroyed(workspaceName);
|
||||
spdlog::debug("Removing workspace because it was moved to another monitor: {}", subPayload);
|
||||
onWorkspaceDestroyed(subPayload);
|
||||
}
|
||||
}
|
||||
|
||||
void Workspaces::onWorkspaceRenamed(std::string const &payload) {
|
||||
spdlog::debug("Workspace renamed: {}", payload);
|
||||
std::string workspaceIdStr = payload.substr(0, payload.find(','));
|
||||
int workspaceId = workspaceIdStr == "special" ? -99 : std::stoi(workspaceIdStr);
|
||||
std::string newName = payload.substr(payload.find(',') + 1);
|
||||
const auto [workspaceIdStr, newName] = splitDoublePayload(payload);
|
||||
|
||||
const auto workspaceId = parseWorkspaceId(workspaceIdStr);
|
||||
if (!workspaceId.has_value()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto &workspace : m_workspaces) {
|
||||
if (workspace->id() == workspaceId) {
|
||||
if (workspace->name() == m_activeWorkspaceName) {
|
||||
m_activeWorkspaceName = newName;
|
||||
}
|
||||
if (workspace->id() == *workspaceId) {
|
||||
workspace->setName(newName);
|
||||
break;
|
||||
}
|
||||
@ -432,11 +454,19 @@ void Workspaces::onWorkspaceRenamed(std::string const &payload) {
|
||||
|
||||
void Workspaces::onMonitorFocused(std::string const &payload) {
|
||||
spdlog::trace("Monitor focused: {}", payload);
|
||||
m_activeWorkspaceName = payload.substr(payload.find(',') + 1);
|
||||
|
||||
for (Json::Value &monitor : gIPC->getSocket1JsonReply("monitors")) {
|
||||
if (monitor["name"].asString() == payload.substr(0, payload.find(','))) {
|
||||
auto name = monitor["specialWorkspace"]["name"].asString();
|
||||
const auto [monitorName, workspaceIdStr] = splitDoublePayload(payload);
|
||||
|
||||
const auto workspaceId = parseWorkspaceId(workspaceIdStr);
|
||||
if (!workspaceId.has_value()) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_activeWorkspaceId = *workspaceId;
|
||||
|
||||
for (Json::Value &monitor : m_ipc.getSocket1JsonReply("monitors")) {
|
||||
if (monitor["name"].asString() == monitorName) {
|
||||
const auto name = monitor["specialWorkspace"]["name"].asString();
|
||||
m_activeSpecialWorkspaceName = !name.starts_with("special:") ? name : name.substr(8);
|
||||
}
|
||||
}
|
||||
@ -475,11 +505,7 @@ void Workspaces::onWindowClosed(std::string const &addr) {
|
||||
void Workspaces::onWindowMoved(std::string const &payload) {
|
||||
spdlog::trace("Window moved: {}", payload);
|
||||
updateWindowCount();
|
||||
size_t lastCommaIdx = 0;
|
||||
size_t nextCommaIdx = payload.find(',');
|
||||
std::string windowAddress = payload.substr(lastCommaIdx, nextCommaIdx - lastCommaIdx);
|
||||
|
||||
std::string workspaceName = payload.substr(nextCommaIdx + 1, payload.length() - nextCommaIdx);
|
||||
auto [windowAddress, _, workspaceName] = splitTriplePayload(payload);
|
||||
|
||||
std::string windowRepr;
|
||||
|
||||
@ -515,13 +541,15 @@ void Workspaces::onWindowTitleEvent(std::string const &payload) {
|
||||
spdlog::trace("Window title changed: {}", payload);
|
||||
std::optional<std::function<void(WindowCreationPayload)>> inserter;
|
||||
|
||||
const auto [windowAddress, _] = splitDoublePayload(payload);
|
||||
|
||||
// If the window was an orphan, rename it at the orphan's vector
|
||||
if (m_orphanWindowMap.contains(payload)) {
|
||||
if (m_orphanWindowMap.contains(windowAddress)) {
|
||||
inserter = [this](WindowCreationPayload wcp) { this->registerOrphanWindow(std::move(wcp)); };
|
||||
} else {
|
||||
auto windowWorkspace =
|
||||
std::find_if(m_workspaces.begin(), m_workspaces.end(),
|
||||
[payload](auto &workspace) { return workspace->containsWindow(payload); });
|
||||
auto windowWorkspace = std::ranges::find_if(m_workspaces, [windowAddress](auto &workspace) {
|
||||
return workspace->containsWindow(windowAddress);
|
||||
});
|
||||
|
||||
// If the window exists on a workspace, rename it at the workspace's window
|
||||
// map
|
||||
@ -530,9 +558,9 @@ void Workspaces::onWindowTitleEvent(std::string const &payload) {
|
||||
(*windowWorkspace)->insertWindow(std::move(wcp));
|
||||
};
|
||||
} else {
|
||||
auto queuedWindow = std::find_if(
|
||||
m_windowsToCreate.begin(), m_windowsToCreate.end(),
|
||||
[payload](auto &windowPayload) { return windowPayload.getAddress() == payload; });
|
||||
auto queuedWindow = std::ranges::find_if(m_windowsToCreate, [payload](auto &windowPayload) {
|
||||
return windowPayload.getAddress() == payload;
|
||||
});
|
||||
|
||||
// If the window was queued, rename it in the queue
|
||||
if (queuedWindow != m_windowsToCreate.end()) {
|
||||
@ -542,15 +570,14 @@ void Workspaces::onWindowTitleEvent(std::string const &payload) {
|
||||
}
|
||||
|
||||
if (inserter.has_value()) {
|
||||
Json::Value clientsData = gIPC->getSocket1JsonReply("clients");
|
||||
Json::Value clientsData = m_ipc.getSocket1JsonReply("clients");
|
||||
std::string jsonWindowAddress = fmt::format("0x{}", payload);
|
||||
|
||||
auto client =
|
||||
std::find_if(clientsData.begin(), clientsData.end(), [jsonWindowAddress](auto &client) {
|
||||
return client["address"].asString() == jsonWindowAddress;
|
||||
});
|
||||
auto client = std::ranges::find_if(clientsData, [jsonWindowAddress](auto &client) {
|
||||
return client["address"].asString() == jsonWindowAddress;
|
||||
});
|
||||
|
||||
if (!client->empty()) {
|
||||
if (client != clientsData.end() && !client->empty()) {
|
||||
(*inserter)({*client});
|
||||
}
|
||||
}
|
||||
@ -590,8 +617,8 @@ auto Workspaces::populateIconsMap(const Json::Value &formatIcons) -> void {
|
||||
m_iconsMap.emplace("", "");
|
||||
}
|
||||
|
||||
auto Workspaces::populateBoolConfig(const Json::Value &config, const std::string &key,
|
||||
bool &member) -> void {
|
||||
auto Workspaces::populateBoolConfig(const Json::Value &config, const std::string &key, bool &member)
|
||||
-> void {
|
||||
const auto &configValue = config[key];
|
||||
if (configValue.isBool()) {
|
||||
member = configValue.asBool();
|
||||
@ -660,40 +687,58 @@ void Workspaces::registerOrphanWindow(WindowCreationPayload create_window_payloa
|
||||
}
|
||||
|
||||
auto Workspaces::registerIpc() -> void {
|
||||
gIPC->registerForIPC("workspace", this);
|
||||
gIPC->registerForIPC("activespecial", this);
|
||||
gIPC->registerForIPC("createworkspace", this);
|
||||
gIPC->registerForIPC("destroyworkspace", this);
|
||||
gIPC->registerForIPC("focusedmon", this);
|
||||
gIPC->registerForIPC("moveworkspace", this);
|
||||
gIPC->registerForIPC("renameworkspace", this);
|
||||
gIPC->registerForIPC("openwindow", this);
|
||||
gIPC->registerForIPC("closewindow", this);
|
||||
gIPC->registerForIPC("movewindow", this);
|
||||
gIPC->registerForIPC("urgent", this);
|
||||
gIPC->registerForIPC("configreloaded", this);
|
||||
m_ipc.registerForIPC("workspacev2", this);
|
||||
m_ipc.registerForIPC("activespecial", this);
|
||||
m_ipc.registerForIPC("createworkspacev2", this);
|
||||
m_ipc.registerForIPC("destroyworkspacev2", this);
|
||||
m_ipc.registerForIPC("focusedmonv2", this);
|
||||
m_ipc.registerForIPC("moveworkspacev2", this);
|
||||
m_ipc.registerForIPC("renameworkspace", this);
|
||||
m_ipc.registerForIPC("openwindow", this);
|
||||
m_ipc.registerForIPC("closewindow", this);
|
||||
m_ipc.registerForIPC("movewindowv2", this);
|
||||
m_ipc.registerForIPC("urgent", this);
|
||||
m_ipc.registerForIPC("configreloaded", this);
|
||||
|
||||
if (windowRewriteConfigUsesTitle()) {
|
||||
spdlog::info(
|
||||
"Registering for Hyprland's 'windowtitle' events because a user-defined window "
|
||||
"Registering for Hyprland's 'windowtitlev2' events because a user-defined window "
|
||||
"rewrite rule uses the 'title' field.");
|
||||
gIPC->registerForIPC("windowtitle", this);
|
||||
m_ipc.registerForIPC("windowtitlev2", this);
|
||||
}
|
||||
}
|
||||
|
||||
void Workspaces::removeWorkspacesToRemove() {
|
||||
for (const auto &workspaceName : m_workspacesToRemove) {
|
||||
removeWorkspace(workspaceName);
|
||||
for (const auto &workspaceString : m_workspacesToRemove) {
|
||||
removeWorkspace(workspaceString);
|
||||
}
|
||||
m_workspacesToRemove.clear();
|
||||
}
|
||||
|
||||
void Workspaces::removeWorkspace(std::string const &name) {
|
||||
spdlog::debug("Removing workspace {}", name);
|
||||
auto workspace =
|
||||
std::find_if(m_workspaces.begin(), m_workspaces.end(), [&](std::unique_ptr<Workspace> &x) {
|
||||
return (name.starts_with("special:") && name.substr(8) == x->name()) || name == x->name();
|
||||
});
|
||||
void Workspaces::removeWorkspace(std::string const &workspaceString) {
|
||||
spdlog::debug("Removing workspace {}", workspaceString);
|
||||
|
||||
// If this succeeds, we have a workspace ID.
|
||||
const auto workspaceId = parseWorkspaceId(workspaceString);
|
||||
|
||||
std::string name;
|
||||
// TODO: At some point we want to support all workspace selectors
|
||||
// This is just a subset.
|
||||
// https://wiki.hyprland.org/Configuring/Workspace-Rules/#workspace-selectors
|
||||
if (workspaceString.starts_with("special:")) {
|
||||
name = workspaceString.substr(8);
|
||||
} else if (workspaceString.starts_with("name:")) {
|
||||
name = workspaceString.substr(5);
|
||||
} else {
|
||||
name = workspaceString;
|
||||
}
|
||||
|
||||
const auto workspace = std::ranges::find_if(m_workspaces, [&](std::unique_ptr<Workspace> &x) {
|
||||
if (workspaceId.has_value()) {
|
||||
return *workspaceId == x->id();
|
||||
}
|
||||
return name == x->name();
|
||||
});
|
||||
|
||||
if (workspace == m_workspaces.end()) {
|
||||
// happens when a workspace on another monitor is destroyed
|
||||
@ -701,7 +746,8 @@ void Workspaces::removeWorkspace(std::string const &name) {
|
||||
}
|
||||
|
||||
if ((*workspace)->isPersistentConfig()) {
|
||||
spdlog::trace("Not removing config persistent workspace {}", name);
|
||||
spdlog::trace("Not removing config persistent workspace id={} name={}", (*workspace)->id(),
|
||||
(*workspace)->name());
|
||||
return;
|
||||
}
|
||||
|
||||
@ -712,10 +758,10 @@ void Workspaces::removeWorkspace(std::string const &name) {
|
||||
void Workspaces::setCurrentMonitorId() {
|
||||
// get monitor ID from name (used by persistent workspaces)
|
||||
m_monitorId = 0;
|
||||
auto monitors = gIPC->getSocket1JsonReply("monitors");
|
||||
auto currentMonitor = std::find_if(
|
||||
monitors.begin(), monitors.end(),
|
||||
[this](const Json::Value &m) { return m["name"].asString() == m_bar.output->name; });
|
||||
auto monitors = m_ipc.getSocket1JsonReply("monitors");
|
||||
auto currentMonitor = std::ranges::find_if(monitors, [this](const Json::Value &m) {
|
||||
return m["name"].asString() == m_bar.output->name;
|
||||
});
|
||||
if (currentMonitor == monitors.end()) {
|
||||
spdlog::error("Monitor '{}' does not have an ID? Using 0", m_bar.output->name);
|
||||
} else {
|
||||
@ -725,62 +771,63 @@ void Workspaces::setCurrentMonitorId() {
|
||||
}
|
||||
|
||||
void Workspaces::sortWorkspaces() {
|
||||
std::sort(m_workspaces.begin(), m_workspaces.end(),
|
||||
[&](std::unique_ptr<Workspace> &a, std::unique_ptr<Workspace> &b) {
|
||||
// Helper comparisons
|
||||
auto isIdLess = a->id() < b->id();
|
||||
auto isNameLess = a->name() < b->name();
|
||||
std::ranges::sort( //
|
||||
m_workspaces, [&](std::unique_ptr<Workspace> &a, std::unique_ptr<Workspace> &b) {
|
||||
// Helper comparisons
|
||||
auto isIdLess = a->id() < b->id();
|
||||
auto isNameLess = a->name() < b->name();
|
||||
|
||||
switch (m_sortBy) {
|
||||
case SortMethod::ID:
|
||||
return isIdLess;
|
||||
case SortMethod::NAME:
|
||||
return isNameLess;
|
||||
case SortMethod::NUMBER:
|
||||
try {
|
||||
return std::stoi(a->name()) < std::stoi(b->name());
|
||||
} catch (const std::invalid_argument &) {
|
||||
// Handle the exception if necessary.
|
||||
break;
|
||||
}
|
||||
case SortMethod::DEFAULT:
|
||||
default:
|
||||
// Handle the default case here.
|
||||
// normal -> named persistent -> named -> special -> named special
|
||||
switch (m_sortBy) {
|
||||
case SortMethod::ID:
|
||||
return isIdLess;
|
||||
case SortMethod::NAME:
|
||||
return isNameLess;
|
||||
case SortMethod::NUMBER:
|
||||
try {
|
||||
return std::stoi(a->name()) < std::stoi(b->name());
|
||||
} catch (const std::invalid_argument &) {
|
||||
// Handle the exception if necessary.
|
||||
break;
|
||||
}
|
||||
case SortMethod::DEFAULT:
|
||||
default:
|
||||
// Handle the default case here.
|
||||
// normal -> named persistent -> named -> special -> named special
|
||||
|
||||
// both normal (includes numbered persistent) => sort by ID
|
||||
if (a->id() > 0 && b->id() > 0) {
|
||||
return isIdLess;
|
||||
}
|
||||
// both normal (includes numbered persistent) => sort by ID
|
||||
if (a->id() > 0 && b->id() > 0) {
|
||||
return isIdLess;
|
||||
}
|
||||
|
||||
// one normal, one special => normal first
|
||||
if ((a->isSpecial()) ^ (b->isSpecial())) {
|
||||
return b->isSpecial();
|
||||
}
|
||||
// one normal, one special => normal first
|
||||
if ((a->isSpecial()) ^ (b->isSpecial())) {
|
||||
return b->isSpecial();
|
||||
}
|
||||
|
||||
// only one normal, one named
|
||||
if ((a->id() > 0) ^ (b->id() > 0)) {
|
||||
return a->id() > 0;
|
||||
}
|
||||
// only one normal, one named
|
||||
if ((a->id() > 0) ^ (b->id() > 0)) {
|
||||
return a->id() > 0;
|
||||
}
|
||||
|
||||
// both special
|
||||
if (a->isSpecial() && b->isSpecial()) {
|
||||
// if one is -99 => put it last
|
||||
if (a->id() == -99 || b->id() == -99) {
|
||||
return b->id() == -99;
|
||||
}
|
||||
// both are 0 (not yet named persistents) / named specials (-98 <= ID <= -1)
|
||||
return isNameLess;
|
||||
}
|
||||
|
||||
// sort non-special named workspaces by name (ID <= -1377)
|
||||
return isNameLess;
|
||||
break;
|
||||
// both special
|
||||
if (a->isSpecial() && b->isSpecial()) {
|
||||
// if one is -99 => put it last
|
||||
if (a->id() == -99 || b->id() == -99) {
|
||||
return b->id() == -99;
|
||||
}
|
||||
// both are 0 (not yet named persistents) / named specials
|
||||
// (-98 <= ID <= -1)
|
||||
return isNameLess;
|
||||
}
|
||||
|
||||
// Return a default value if none of the cases match.
|
||||
return isNameLess; // You can adjust this to your specific needs.
|
||||
});
|
||||
// sort non-special named workspaces by name (ID <= -1377)
|
||||
return isNameLess;
|
||||
break;
|
||||
}
|
||||
|
||||
// Return a default value if none of the cases match.
|
||||
return isNameLess; // You can adjust this to your specific needs.
|
||||
});
|
||||
|
||||
for (size_t i = 0; i < m_workspaces.size(); ++i) {
|
||||
m_box.reorder_child(m_workspaces[i]->button(), i);
|
||||
@ -788,7 +835,7 @@ void Workspaces::sortWorkspaces() {
|
||||
}
|
||||
|
||||
void Workspaces::setUrgentWorkspace(std::string const &windowaddress) {
|
||||
const Json::Value clientsJson = gIPC->getSocket1JsonReply("clients");
|
||||
const Json::Value clientsJson = m_ipc.getSocket1JsonReply("clients");
|
||||
int workspaceId = -1;
|
||||
|
||||
for (Json::Value clientJson : clientsJson) {
|
||||
@ -798,9 +845,9 @@ void Workspaces::setUrgentWorkspace(std::string const &windowaddress) {
|
||||
}
|
||||
}
|
||||
|
||||
auto workspace =
|
||||
std::find_if(m_workspaces.begin(), m_workspaces.end(),
|
||||
[workspaceId](std::unique_ptr<Workspace> &x) { return x->id() == workspaceId; });
|
||||
auto workspace = std::ranges::find_if(m_workspaces, [workspaceId](std::unique_ptr<Workspace> &x) {
|
||||
return x->id() == workspaceId;
|
||||
});
|
||||
if (workspace != m_workspaces.end()) {
|
||||
workspace->get()->setUrgent();
|
||||
}
|
||||
@ -812,13 +859,12 @@ auto Workspaces::update() -> void {
|
||||
}
|
||||
|
||||
void Workspaces::updateWindowCount() {
|
||||
const Json::Value workspacesJson = gIPC->getSocket1JsonReply("workspaces");
|
||||
const Json::Value workspacesJson = m_ipc.getSocket1JsonReply("workspaces");
|
||||
for (auto &workspace : m_workspaces) {
|
||||
auto workspaceJson =
|
||||
std::find_if(workspacesJson.begin(), workspacesJson.end(), [&](Json::Value const &x) {
|
||||
return x["name"].asString() == workspace->name() ||
|
||||
(workspace->isSpecial() && x["name"].asString() == "special:" + workspace->name());
|
||||
});
|
||||
auto workspaceJson = std::ranges::find_if(workspacesJson, [&](Json::Value const &x) {
|
||||
return x["name"].asString() == workspace->name() ||
|
||||
(workspace->isSpecial() && x["name"].asString() == "special:" + workspace->name());
|
||||
});
|
||||
uint32_t count = 0;
|
||||
if (workspaceJson != workspacesJson.end()) {
|
||||
try {
|
||||
@ -858,26 +904,26 @@ bool Workspaces::updateWindowsToCreate() {
|
||||
}
|
||||
|
||||
void Workspaces::updateWorkspaceStates() {
|
||||
const std::vector<std::string> visibleWorkspaces = getVisibleWorkspaces();
|
||||
auto updatedWorkspaces = gIPC->getSocket1JsonReply("workspaces");
|
||||
const std::vector<int> visibleWorkspaces = getVisibleWorkspaces();
|
||||
auto updatedWorkspaces = m_ipc.getSocket1JsonReply("workspaces");
|
||||
for (auto &workspace : m_workspaces) {
|
||||
workspace->setActive(workspace->name() == m_activeWorkspaceName ||
|
||||
workspace->name() == m_activeSpecialWorkspaceName);
|
||||
if (workspace->name() == m_activeWorkspaceName && workspace->isUrgent()) {
|
||||
workspace->setActive(
|
||||
workspace->id() == m_activeWorkspaceId ||
|
||||
(workspace->isSpecial() && workspace->name() == m_activeSpecialWorkspaceName));
|
||||
if (workspace->isActive() && workspace->isUrgent()) {
|
||||
workspace->setUrgent(false);
|
||||
}
|
||||
workspace->setVisible(std::find(visibleWorkspaces.begin(), visibleWorkspaces.end(),
|
||||
workspace->name()) != visibleWorkspaces.end());
|
||||
workspace->setVisible(std::ranges::find(visibleWorkspaces, workspace->id()) !=
|
||||
visibleWorkspaces.end());
|
||||
std::string &workspaceIcon = m_iconsMap[""];
|
||||
if (m_withIcon) {
|
||||
workspaceIcon = workspace->selectIcon(m_iconsMap);
|
||||
}
|
||||
auto updatedWorkspace = std::find_if(
|
||||
updatedWorkspaces.begin(), updatedWorkspaces.end(), [&workspace](const auto &w) {
|
||||
auto wNameRaw = w["name"].asString();
|
||||
auto wName = wNameRaw.starts_with("special:") ? wNameRaw.substr(8) : wNameRaw;
|
||||
return wName == workspace->name();
|
||||
});
|
||||
auto updatedWorkspace = std::ranges::find_if(updatedWorkspaces, [&workspace](const auto &w) {
|
||||
auto wNameRaw = w["name"].asString();
|
||||
auto wName = wNameRaw.starts_with("special:") ? wNameRaw.substr(8) : wNameRaw;
|
||||
return wName == workspace->name();
|
||||
});
|
||||
if (updatedWorkspace != updatedWorkspaces.end()) {
|
||||
workspace->setOutput((*updatedWorkspace)["monitor"].asString());
|
||||
}
|
||||
@ -905,4 +951,39 @@ int Workspaces::windowRewritePriorityFunction(std::string const &window_rule) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
std::string Workspaces::makePayload(Args const &...args) {
|
||||
std::ostringstream result;
|
||||
bool first = true;
|
||||
((result << (first ? "" : ",") << args, first = false), ...);
|
||||
return result.str();
|
||||
}
|
||||
|
||||
std::pair<std::string, std::string> Workspaces::splitDoublePayload(std::string const &payload) {
|
||||
const std::string part1 = payload.substr(0, payload.find(','));
|
||||
const std::string part2 = payload.substr(part1.size() + 1);
|
||||
return {part1, part2};
|
||||
}
|
||||
|
||||
std::tuple<std::string, std::string, std::string> Workspaces::splitTriplePayload(
|
||||
std::string const &payload) {
|
||||
const size_t firstComma = payload.find(',');
|
||||
const size_t secondComma = payload.find(',', firstComma + 1);
|
||||
|
||||
const std::string part1 = payload.substr(0, firstComma);
|
||||
const std::string part2 = payload.substr(firstComma + 1, secondComma - (firstComma + 1));
|
||||
const std::string part3 = payload.substr(secondComma + 1);
|
||||
|
||||
return {part1, part2, part3};
|
||||
}
|
||||
|
||||
std::optional<int> Workspaces::parseWorkspaceId(std::string const &workspaceIdStr) {
|
||||
try {
|
||||
return workspaceIdStr == "special" ? -99 : std::stoi(workspaceIdStr);
|
||||
} catch (std::exception const &e) {
|
||||
spdlog::error("Failed to parse workspace ID: {}", e.what());
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace waybar::modules::hyprland
|
||||
|
Reference in New Issue
Block a user