event: refactor HookSystem into a typed event bus (#13333)

Refactors the old HookSystem into a typed event bus with clear
separation,
discovery and types.
This commit is contained in:
Vaxry 2026-02-22 23:30:10 +00:00 committed by GitHub
parent b4ee4674f9
commit b88813c7ef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
58 changed files with 493 additions and 516 deletions

View file

@ -5,9 +5,9 @@
#include "config/ConfigManager.hpp"
#include "managers/animation/AnimationManager.hpp"
#include "../managers/EventManager.hpp"
#include "../managers/HookSystemManager.hpp"
#include "../layout/space/Space.hpp"
#include "../layout/supplementary/WorkspaceAlgoMatcher.hpp"
#include "../event/EventBus.hpp"
#include <hyprutils/animation/AnimatedVariable.hpp>
#include <hyprutils/string/String.hpp>
@ -37,10 +37,8 @@ void CWorkspace::init(PHLWORKSPACE self) {
if (RULEFORTHIS.defaultName.has_value())
m_name = RULEFORTHIS.defaultName.value();
m_focusedWindowHook = g_pHookSystem->hookDynamic("closeWindow", [this](void* self, SCallbackInfo& info, std::any param) {
const auto PWINDOW = std::any_cast<PHLWINDOW>(param);
if (PWINDOW == m_lastFocusedWindow.lock())
m_focusedWindowHook = Event::bus()->m_events.window.close.listen([this](PHLWINDOW pWindow) {
if (pWindow == m_lastFocusedWindow.lock())
m_lastFocusedWindow.reset();
});
@ -58,22 +56,19 @@ void CWorkspace::init(PHLWORKSPACE self) {
g_pEventManager->postEvent({.event = "createworkspace", .data = m_name});
g_pEventManager->postEvent({.event = "createworkspacev2", .data = std::format("{},{}", m_id, m_name)});
EMIT_HOOK_EVENT("createWorkspace", this);
Event::bus()->m_events.workspace.created.emit(self);
}
CWorkspace::~CWorkspace() {
Log::logger->log(Log::DEBUG, "Destroying workspace ID {}", m_id);
// check if g_pHookSystem and g_pEventManager exist, they might be destroyed as in when the compositor is closing.
if (g_pHookSystem)
g_pHookSystem->unhook(m_focusedWindowHook);
if (g_pEventManager) {
g_pEventManager->postEvent({.event = "destroyworkspace", .data = m_name});
g_pEventManager->postEvent({.event = "destroyworkspacev2", .data = std::format("{},{}", m_id, m_name)});
EMIT_HOOK_EVENT("destroyWorkspace", this);
}
Event::bus()->m_events.workspace.removed.emit(m_self);
m_events.destroy.emit();
}

View file

@ -93,13 +93,13 @@ class CWorkspace {
} m_events;
private:
void init(PHLWORKSPACE self);
void init(PHLWORKSPACE self);
SP<HOOK_CALLBACK_FN> m_focusedWindowHook;
bool m_inert = true;
CHyprSignalListener m_focusedWindowHook;
bool m_inert = true;
SP<CWorkspace> m_selfPersistent; // for persistent workspaces.
bool m_persistent = false;
SP<CWorkspace> m_selfPersistent; // for persistent workspaces.
bool m_persistent = false;
};
inline bool valid(const PHLWORKSPACE& ref) {

View file

@ -1,7 +1,7 @@
#include "WindowHistoryTracker.hpp"
#include "../../managers/HookSystemManager.hpp"
#include "../view/Window.hpp"
#include "../../event/EventBus.hpp"
using namespace Desktop;
using namespace Desktop::History;
@ -12,18 +12,12 @@ SP<CWindowHistoryTracker> History::windowTracker() {
}
CWindowHistoryTracker::CWindowHistoryTracker() {
static auto P = g_pHookSystem->hookDynamic("openWindowEarly", [this](void* self, SCallbackInfo& info, std::any data) {
auto window = std::any_cast<PHLWINDOW>(data);
static auto P = Event::bus()->m_events.window.openEarly.listen([this](PHLWINDOW pWindow) {
// add a last track
m_history.insert(m_history.begin(), window);
m_history.insert(m_history.begin(), pWindow);
});
static auto P1 = g_pHookSystem->hookDynamic("activeWindow", [this](void* self, SCallbackInfo& info, std::any data) {
auto window = std::any_cast<Desktop::View::SWindowActiveEvent>(data).window;
track(window);
});
static auto P1 = Event::bus()->m_events.window.active.listen([this](PHLWINDOW window, uint8_t reason) { track(window); });
}
void CWindowHistoryTracker::track(PHLWINDOW w) {

View file

@ -3,8 +3,8 @@
#include "../../helpers/Monitor.hpp"
#include "../Workspace.hpp"
#include "../state/FocusState.hpp"
#include "../../managers/HookSystemManager.hpp"
#include "../../managers/eventLoop/EventLoopManager.hpp"
#include "../../event/EventBus.hpp"
#include "../../config/ConfigValue.hpp"
#include <hyprutils/utils/ScopeGuard.hpp>
@ -18,14 +18,9 @@ SP<CWorkspaceHistoryTracker> History::workspaceTracker() {
}
CWorkspaceHistoryTracker::CWorkspaceHistoryTracker() {
static auto P = g_pHookSystem->hookDynamic("workspace", [this](void* self, SCallbackInfo& info, std::any data) {
auto workspace = std::any_cast<PHLWORKSPACE>(data);
track(workspace);
});
static auto P1 = g_pHookSystem->hookDynamic("focusedMon", [this](void* self, SCallbackInfo& info, std::any data) {
auto mon = std::any_cast<PHLMONITOR>(data);
static auto P = Event::bus()->m_events.workspace.active.listen([this](PHLWORKSPACE workspace) { track(workspace); });
static auto P1 = Event::bus()->m_events.monitor.focused.listen([this](PHLMONITOR mon) {
// This sucks ASS, but we have to do this because switching to a workspace on another mon will trigger a workspace event right afterwards and we don't
// want to remember the workspace that was not visible there
// TODO: do something about this

View file

@ -4,7 +4,7 @@
#include "../utils/SetUtils.hpp"
#include "../../view/Window.hpp"
#include "../../types/OverridableVar.hpp"
#include "../../../managers/HookSystemManager.hpp"
#include "../../../event/EventBus.hpp"
#include <hyprutils/string/String.hpp>
@ -634,5 +634,5 @@ void CWindowRuleApplicator::propertiesChanged(std::underlying_type_t<eRuleProper
g_pDecorationPositioner->forceRecalcFor(m_window.lock());
// for plugins
EMIT_HOOK_EVENT("windowUpdateRules", m_window.lock());
Event::bus()->m_events.window.updateRules.emit(m_window.lock());
}

View file

@ -4,13 +4,13 @@
#include "../../protocols/XDGShell.hpp"
#include "../../render/Renderer.hpp"
#include "../../managers/EventManager.hpp"
#include "../../managers/HookSystemManager.hpp"
#include "../../managers/input/InputManager.hpp"
#include "../../managers/SeatManager.hpp"
#include "../../xwayland/XSurface.hpp"
#include "../../protocols/PointerConstraints.hpp"
#include "managers/animation/DesktopAnimationManager.hpp"
#include "../../layout/LayoutManager.hpp"
#include "../../event/EventBus.hpp"
using namespace Desktop;
@ -133,7 +133,7 @@ void CFocusState::rawWindowFocus(PHLWINDOW pWindow, eFocusReason reason, SP<CWLS
g_pEventManager->postEvent(SHyprIPCEvent{"activewindow", ","});
g_pEventManager->postEvent(SHyprIPCEvent{"activewindowv2", ""});
EMIT_HOOK_EVENT("activeWindow", Desktop::View::SWindowActiveEvent{nullptr COMMA reason});
Event::bus()->m_events.window.active.emit(nullptr, reason);
m_focusSurface.reset();
@ -200,7 +200,7 @@ void CFocusState::rawWindowFocus(PHLWINDOW pWindow, eFocusReason reason, SP<CWLS
g_pEventManager->postEvent(SHyprIPCEvent{.event = "activewindow", .data = pWindow->m_class + "," + pWindow->m_title});
g_pEventManager->postEvent(SHyprIPCEvent{.event = "activewindowv2", .data = std::format("{:x}", rc<uintptr_t>(pWindow.get()))});
EMIT_HOOK_EVENT("activeWindow", Desktop::View::SWindowActiveEvent{pWindow COMMA reason});
Event::bus()->m_events.window.active.emit(pWindow, reason);
g_pInputManager->recheckIdleInhibitorStatus();
@ -233,7 +233,7 @@ void CFocusState::rawSurfaceFocus(SP<CWLSurfaceResource> pSurface, PHLWINDOW pWi
g_pSeatManager->setKeyboardFocus(nullptr);
g_pEventManager->postEvent(SHyprIPCEvent{.event = "activewindow", .data = ","});
g_pEventManager->postEvent(SHyprIPCEvent{.event = "activewindowv2", .data = ""});
EMIT_HOOK_EVENT("keyboardFocus", SP<CWLSurfaceResource>{nullptr});
Event::bus()->m_events.input.keyboard.focus.emit(nullptr);
m_focusSurface.reset();
return;
}
@ -249,7 +249,7 @@ void CFocusState::rawSurfaceFocus(SP<CWLSurfaceResource> pSurface, PHLWINDOW pWi
g_pXWaylandManager->activateSurface(pSurface, true);
m_focusSurface = pSurface;
EMIT_HOOK_EVENT("keyboardFocus", pSurface);
Event::bus()->m_events.input.keyboard.focus.emit(pSurface);
const auto SURF = Desktop::View::CWLSurface::fromResource(pSurface);
const auto OLDSURF = Desktop::View::CWLSurface::fromResource(PLASTSURF);
@ -278,7 +278,7 @@ void CFocusState::rawMonitorFocus(PHLMONITOR pMonitor) {
g_pEventManager->postEvent(SHyprIPCEvent{.event = "focusedmon", .data = pMonitor->m_name + "," + WORKSPACE_NAME});
g_pEventManager->postEvent(SHyprIPCEvent{.event = "focusedmonv2", .data = pMonitor->m_name + "," + WORKSPACE_ID});
EMIT_HOOK_EVENT("focusedMon", pMonitor);
Event::bus()->m_events.monitor.focused.emit(pMonitor);
m_focusMonitor = pMonitor;
}

View file

@ -1,7 +1,7 @@
#pragma once
#include "../DesktopTypes.hpp"
#include "../../SharedDefs.hpp"
#include "../../helpers/signal/Signal.hpp"
class CWLSurfaceResource;
@ -44,7 +44,7 @@ namespace Desktop {
PHLWINDOWREF m_focusWindow;
PHLMONITORREF m_focusMonitor;
SP<HOOK_CALLBACK_FN> m_windowOpen, m_windowClose;
CHyprSignalListener m_windowOpen, m_windowClose;
};
SP<CFocusState> focusState();

View file

@ -10,8 +10,8 @@
#include "../../config/ConfigManager.hpp"
#include "../../helpers/Monitor.hpp"
#include "../../managers/input/InputManager.hpp"
#include "../../managers/HookSystemManager.hpp"
#include "../../managers/EventManager.hpp"
#include "../../event/EventBus.hpp"
using namespace Desktop;
using namespace Desktop::View;
@ -222,7 +222,7 @@ void CLayerSurface::onMap() {
m_fadingOut = false;
g_pEventManager->postEvent(SHyprIPCEvent{.event = "openlayer", .data = m_namespace});
EMIT_HOOK_EVENT("openLayer", m_self.lock());
Event::bus()->m_events.layer.opened.emit(m_self.lock());
g_pCompositor->setPreferredScaleForSurface(m_wlSurface->resource(), PMONITOR->m_scale);
g_pCompositor->setPreferredTransformForSurface(m_wlSurface->resource(), PMONITOR->m_transform);
@ -232,7 +232,7 @@ void CLayerSurface::onUnmap() {
Log::logger->log(Log::DEBUG, "LayerSurface {:x} unmapped", rc<uintptr_t>(m_layerSurface.get()));
g_pEventManager->postEvent(SHyprIPCEvent{.event = "closelayer", .data = m_layerSurface->m_layerNamespace});
EMIT_HOOK_EVENT("closeLayer", m_self.lock());
Event::bus()->m_events.layer.closed.emit(m_self.lock());
std::erase_if(g_pInputManager->m_exclusiveLSes, [this](const auto& other) { return !other || other == m_self; });

View file

@ -39,7 +39,6 @@
#include "../../helpers/math/Expression.hpp"
#include "../../managers/XWaylandManager.hpp"
#include "../../render/Renderer.hpp"
#include "../../managers/HookSystemManager.hpp"
#include "../../managers/EventManager.hpp"
#include "../../managers/input/InputManager.hpp"
#include "../../managers/PointerManager.hpp"
@ -48,6 +47,7 @@
#include "../../layout/LayoutManager.hpp"
#include "../../layout/target/WindowTarget.hpp"
#include "../../layout/target/WindowGroupTarget.hpp"
#include "../../event/EventBus.hpp"
#include <hyprutils/string/String.hpp>
@ -521,7 +521,7 @@ void CWindow::moveToWorkspace(PHLWORKSPACE pWorkspace) {
if (valid(pWorkspace)) {
g_pEventManager->postEvent(SHyprIPCEvent{.event = "movewindow", .data = std::format("{:x},{}", rc<uintptr_t>(this), pWorkspace->m_name)});
g_pEventManager->postEvent(SHyprIPCEvent{.event = "movewindowv2", .data = std::format("{:x},{},{}", rc<uintptr_t>(this), pWorkspace->m_id, pWorkspace->m_name)});
EMIT_HOOK_EVENT("moveWindow", (std::vector<std::any>{m_self.lock(), pWorkspace}));
Event::bus()->m_events.window.moveToWorkspace.emit(m_self.lock(), pWorkspace);
}
if (const auto SWALLOWED = m_swallowed.lock()) {
@ -1037,7 +1037,7 @@ void CWindow::activate(bool force) {
m_isUrgent = true;
g_pEventManager->postEvent(SHyprIPCEvent{.event = "urgent", .data = std::format("{:x}", rc<uintptr_t>(this))});
EMIT_HOOK_EVENT("urgent", m_self.lock());
Event::bus()->m_events.window.urgent.emit(m_self.lock());
if (!force &&
(!m_ruleApplicator->focusOnActivate().valueOr(*PFOCUSONACTIVATE) || (m_suppressedEvents & SUPPRESS_ACTIVATE_FOCUSONLY) || (m_suppressedEvents & SUPPRESS_ACTIVATE)))
@ -1098,7 +1098,7 @@ void CWindow::onUpdateMeta() {
m_title = NEWTITLE;
g_pEventManager->postEvent(SHyprIPCEvent{.event = "windowtitle", .data = std::format("{:x}", rc<uintptr_t>(this))});
g_pEventManager->postEvent(SHyprIPCEvent{.event = "windowtitlev2", .data = std::format("{:x},{}", rc<uintptr_t>(this), m_title)});
EMIT_HOOK_EVENT("windowTitle", m_self.lock());
Event::bus()->m_events.window.title.emit(m_self.lock());
if (m_self == Desktop::focusState()->window()) { // if it's the active, let's post an event to update others
g_pEventManager->postEvent(SHyprIPCEvent{.event = "activewindow", .data = m_class + "," + m_title});
@ -1115,6 +1115,8 @@ void CWindow::onUpdateMeta() {
if (m_class != NEWCLASS) {
m_class = NEWCLASS;
Event::bus()->m_events.window.class_.emit(m_self.lock());
if (m_self == Desktop::focusState()->window()) { // if it's the active, let's post an event to update others
g_pEventManager->postEvent(SHyprIPCEvent{.event = "activewindow", .data = m_class + "," + m_title});
g_pEventManager->postEvent(SHyprIPCEvent{.event = "activewindowv2", .data = std::format("{:x}", rc<uintptr_t>(this))});
@ -1945,7 +1947,7 @@ void CWindow::mapWindow() {
// emit the IPC event before the layout might focus the window to avoid a focus event first
g_pEventManager->postEvent(SHyprIPCEvent{"openwindow", std::format("{:x},{},{},{}", m_self.lock(), PWORKSPACE->m_name, m_class, m_title)});
EMIT_HOOK_EVENT("openWindowEarly", m_self.lock());
Event::bus()->m_events.window.openEarly.emit(m_self.lock());
if (*PAUTOGROUP // auto_group enabled
&& Desktop::focusState()->window() // focused window exists
@ -2078,7 +2080,7 @@ void CWindow::mapWindow() {
Log::logger->log(Log::DEBUG, "Map request dispatched, monitor {}, window pos: {:5j}, window size: {:5j}", PMONITOR->m_name, m_realPosition->goal(), m_realSize->goal());
// emit the hook event here after basic stuff has been initialized
EMIT_HOOK_EVENT("openWindow", m_self.lock());
Event::bus()->m_events.window.open.emit(m_self.lock());
// apply data from default decos. Borders, shadows.
g_pDecorationPositioner->forceRecalcFor(m_self.lock());
@ -2136,7 +2138,7 @@ void CWindow::unmapWindow() {
m_events.unmap.emit();
g_pEventManager->postEvent(SHyprIPCEvent{"closewindow", std::format("{:x}", m_self.lock())});
EMIT_HOOK_EVENT("closeWindow", m_self.lock());
Event::bus()->m_events.window.close.emit(m_self.lock());
if (m_isFloating && !m_isX11 && m_ruleApplicator->persistentSize().valueOrDefault()) {
Log::logger->log(Log::DEBUG, "storing floating size {}x{} for window {}::{} on close", m_realSize->value().x, m_realSize->value().y, m_class, m_title);
@ -2250,7 +2252,7 @@ void CWindow::unmapWindow() {
g_pEventManager->postEvent(SHyprIPCEvent{"activewindow", ","});
g_pEventManager->postEvent(SHyprIPCEvent{"activewindowv2", ""});
EMIT_HOOK_EVENT("activeWindow", Desktop::View::SWindowActiveEvent{nullptr COMMA FOCUS_REASON_OTHER});
Event::bus()->m_events.window.active.emit(m_self.lock(), FOCUS_REASON_OTHER);
}
} else {
Log::logger->log(Log::DEBUG, "Unmapped was not focused, ignoring a refocus.");