Skip to content

Commit 24d391b

Browse files
committed
feat(hyprland): support workspacev2
1 parent a878dd2 commit 24d391b

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

include/modules/hyprland/workspaces.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class Workspaces : public AModule, public EventHandler {
138138

139139
bool m_withIcon;
140140
uint64_t m_monitorId;
141-
std::string m_activeWorkspaceName;
141+
int m_activeWorkspaceId;
142142
std::string m_activeSpecialWorkspaceName;
143143
std::vector<std::unique_ptr<Workspace>> m_workspaces;
144144
std::vector<std::pair<Json::Value, Json::Value>> m_workspacesToCreate;

src/modules/hyprland/workspaces.cpp

+25-15
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Workspaces::~Workspaces() {
4040
}
4141

4242
void Workspaces::init() {
43-
m_activeWorkspaceName = (gIPC->getSocket1JsonReply("activeworkspace"))["name"].asString();
43+
m_activeWorkspaceId = (gIPC->getSocket1JsonReply("activeworkspace"))["id"].asInt();
4444

4545
initializeWorkspaces();
4646
dp.emit();
@@ -306,6 +306,7 @@ void Workspaces::loadPersistentWorkspacesFromWorkspaceRules(const Json::Value &c
306306
workspaceData["persistent-rule"] = true;
307307
m_workspacesToCreate.emplace_back(workspaceData, clientsJson);
308308
} else {
309+
// This can be any workspace selector.
309310
m_workspacesToRemove.emplace_back(workspace);
310311
}
311312
}
@@ -316,7 +317,7 @@ void Workspaces::onEvent(const std::string &ev) {
316317
std::string eventName(begin(ev), begin(ev) + ev.find_first_of('>'));
317318
std::string payload = ev.substr(eventName.size() + 2);
318319

319-
if (eventName == "workspace") {
320+
if (eventName == "workspacev2") {
320321
onWorkspaceActivated(payload);
321322
} else if (eventName == "activespecial") {
322323
onSpecialWorkspaceActivated(payload);
@@ -348,7 +349,8 @@ void Workspaces::onEvent(const std::string &ev) {
348349
}
349350

350351
void Workspaces::onWorkspaceActivated(std::string const &payload) {
351-
m_activeWorkspaceName = payload;
352+
std::string workspaceIdStr = payload.substr(0, payload.find(','));
353+
m_activeWorkspaceId = std::stoi(workspaceIdStr);
352354
}
353355

354356
void Workspaces::onSpecialWorkspaceActivated(std::string const &payload) {
@@ -406,7 +408,7 @@ void Workspaces::onWorkspaceMoved(std::string const &payload) {
406408
spdlog::debug("Workspace moved: {}", payload);
407409

408410
// Update active workspace
409-
m_activeWorkspaceName = (gIPC->getSocket1JsonReply("activeworkspace"))["name"].asString();
411+
m_activeWorkspaceId = (gIPC->getSocket1JsonReply("activeworkspace"))["id"].asInt();
410412

411413
if (allOutputs()) return;
412414

@@ -429,9 +431,6 @@ void Workspaces::onWorkspaceRenamed(std::string const &payload) {
429431
std::string newName = payload.substr(payload.find(',') + 1);
430432
for (auto &workspace : m_workspaces) {
431433
if (workspace->id() == workspaceId) {
432-
if (workspace->name() == m_activeWorkspaceName) {
433-
m_activeWorkspaceName = newName;
434-
}
435434
workspace->setName(newName);
436435
break;
437436
}
@@ -441,7 +440,16 @@ void Workspaces::onWorkspaceRenamed(std::string const &payload) {
441440

442441
void Workspaces::onMonitorFocused(std::string const &payload) {
443442
spdlog::trace("Monitor focused: {}", payload);
444-
m_activeWorkspaceName = payload.substr(payload.find(',') + 1);
443+
444+
std::string workspaceName = payload.substr(payload.find(',') + 1);
445+
446+
// TODO this will be in the payload when we upgrade to focusedmonv2
447+
for (auto &workspace : m_workspaces) {
448+
if (workspace->name() == workspaceName) {
449+
m_activeWorkspaceId = workspace->id();
450+
break;
451+
}
452+
}
445453

446454
for (Json::Value &monitor : gIPC->getSocket1JsonReply("monitors")) {
447455
if (monitor["name"].asString() == payload.substr(0, payload.find(','))) {
@@ -669,7 +677,7 @@ void Workspaces::registerOrphanWindow(WindowCreationPayload create_window_payloa
669677
}
670678

671679
auto Workspaces::registerIpc() -> void {
672-
gIPC->registerForIPC("workspace", this);
680+
gIPC->registerForIPC("workspacev2", this);
673681
gIPC->registerForIPC("activespecial", this);
674682
gIPC->registerForIPC("createworkspacev2", this);
675683
gIPC->registerForIPC("destroyworkspacev2", this);
@@ -700,14 +708,16 @@ void Workspaces::removeWorkspacesToRemove() {
700708
void Workspaces::removeWorkspace(std::string const &workspaceString) {
701709
spdlog::debug("Removing workspace {}", workspaceString);
702710

703-
int id = -100; // workspace IDs range from -99 upwards, so -100 is a good "invalid" value
711+
int id;
704712
std::string name;
705713

706-
// TODO: we need to support workspace selectors here
707-
// https://wiki.hyprland.org/Configuring/Workspace-Rules/#workspace-selectors
708714
try {
715+
// If this succeeds, we have a workspace ID.
709716
id = std::stoi(workspaceString);
710717
} catch (const std::exception &e) {
718+
// TODO: At some point we want to support all workspace selectors
719+
// This is just a subset.
720+
// https://wiki.hyprland.org/Configuring/Workspace-Rules/#workspace-selectors
711721
if (workspaceString.starts_with("special:")) {
712722
name = workspaceString.substr(8);
713723
} else if (workspaceString.starts_with("name:")) {
@@ -731,7 +741,7 @@ void Workspaces::removeWorkspace(std::string const &workspaceString) {
731741
}
732742

733743
if ((*workspace)->isPersistentConfig()) {
734-
spdlog::trace("Not removing config persistent workspace {}", (*workspace)->name());
744+
spdlog::trace("Not removing config persistent workspace id={} name={}", (*workspace)->id(), (*workspace)->name());
735745
return;
736746
}
737747

@@ -891,9 +901,9 @@ void Workspaces::updateWorkspaceStates() {
891901
const std::vector<std::string> visibleWorkspaces = getVisibleWorkspaces();
892902
auto updatedWorkspaces = gIPC->getSocket1JsonReply("workspaces");
893903
for (auto &workspace : m_workspaces) {
894-
workspace->setActive(workspace->name() == m_activeWorkspaceName ||
904+
workspace->setActive(workspace->id() == m_activeWorkspaceId ||
895905
workspace->name() == m_activeSpecialWorkspaceName);
896-
if (workspace->name() == m_activeWorkspaceName && workspace->isUrgent()) {
906+
if (workspace->id() == m_activeWorkspaceId && workspace->isUrgent()) {
897907
workspace->setUrgent(false);
898908
}
899909
workspace->setVisible(std::find(visibleWorkspaces.begin(), visibleWorkspaces.end(),

0 commit comments

Comments
 (0)