From a61eb7694df25a75f45502ed64b1536fda370c1d Mon Sep 17 00:00:00 2001 From: Vaxry Date: Sat, 21 Oct 2023 14:52:43 +0100 Subject: hooksystem: add callbackinfo struct and cancellable events --- src/managers/HookSystemManager.cpp | 6 +++--- src/managers/HookSystemManager.hpp | 17 ++++++++++++++--- src/managers/KeybindManager.cpp | 2 +- src/managers/input/InputManager.cpp | 10 +++++----- src/managers/input/InputMethodRelay.cpp | 2 +- 5 files changed, 24 insertions(+), 13 deletions(-) (limited to 'src/managers') diff --git a/src/managers/HookSystemManager.cpp b/src/managers/HookSystemManager.cpp index 567286d6..48c300cb 100644 --- a/src/managers/HookSystemManager.cpp +++ b/src/managers/HookSystemManager.cpp @@ -26,7 +26,7 @@ void CHookSystemManager::unhook(HOOK_CALLBACK_FN* fn) { } } -void CHookSystemManager::emit(const std::vector* callbacks, std::any data) { +void CHookSystemManager::emit(const std::vector* callbacks, SCallbackInfo& info, std::any data) { if (callbacks->empty()) return; @@ -38,7 +38,7 @@ void CHookSystemManager::emit(const std::vector* callbacks, std: if (!cb.handle) { // we don't guard hl hooks - (*cb.fn)(cb.fn, data); + (*cb.fn)(cb.fn, info, data); continue; } @@ -49,7 +49,7 @@ void CHookSystemManager::emit(const std::vector* callbacks, std: try { if (!setjmp(m_jbHookFaultJumpBuf)) - (*cb.fn)(cb.fn, data); + (*cb.fn)(cb.fn, info, data); else { // this module crashed. throw std::exception(); diff --git a/src/managers/HookSystemManager.hpp b/src/managers/HookSystemManager.hpp index 66edd1c0..ca8e9006 100644 --- a/src/managers/HookSystemManager.hpp +++ b/src/managers/HookSystemManager.hpp @@ -12,7 +12,8 @@ #include "../plugins/PluginAPI.hpp" // global typedef for hooked functions. Passes itself as a ptr when called, and `data` additionally. -typedef std::function HOOK_CALLBACK_FN; + +typedef std::function HOOK_CALLBACK_FN; struct SCallbackFNPtr { HOOK_CALLBACK_FN* fn = nullptr; @@ -22,7 +23,17 @@ struct SCallbackFNPtr { #define EMIT_HOOK_EVENT(name, param) \ { \ static auto* const PEVENTVEC = g_pHookSystem->getVecForEvent(name); \ - g_pHookSystem->emit(PEVENTVEC, param); \ + SCallbackInfo info; \ + g_pHookSystem->emit(PEVENTVEC, info, param); \ + } + +#define EMIT_HOOK_EVENT_CANCELLABLE(name, param) \ + { \ + static auto* const PEVENTVEC = g_pHookSystem->getVecForEvent(name); \ + SCallbackInfo info; \ + g_pHookSystem->emit(PEVENTVEC, info, param); \ + if (info.cancelled) \ + return; \ } class CHookSystemManager { @@ -34,7 +45,7 @@ class CHookSystemManager { void hookStatic(const std::string& event, HOOK_CALLBACK_FN* fn, HANDLE handle = nullptr); void unhook(HOOK_CALLBACK_FN* fn); - void emit(const std::vector* callbacks, std::any data = 0); + void emit(const std::vector* callbacks, SCallbackInfo& info, std::any data = 0); std::vector* getVecForEvent(const std::string& event); bool m_bCurrentEventPlugin = false; diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index 7e0b7620..321409cc 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -77,7 +77,7 @@ CKeybindManager::CKeybindManager() { m_tScrollTimer.reset(); - g_pHookSystem->hookDynamic("configReloaded", [this](void* hk, std::any param) { + g_pHookSystem->hookDynamic("configReloaded", [this](void* hk, SCallbackInfo& info, std::any param) { // clear cuz realloc'd m_pActiveKeybind = nullptr; m_vPressedSpecialBinds.clear(); diff --git a/src/managers/input/InputManager.cpp b/src/managers/input/InputManager.cpp index ab5be4bc..1d8b5135 100644 --- a/src/managers/input/InputManager.cpp +++ b/src/managers/input/InputManager.cpp @@ -97,11 +97,11 @@ void CInputManager::mouseMoveUnified(uint32_t time, bool refocus) { if (MOUSECOORDSFLOORED == m_vLastCursorPosFloored && !refocus) return; + EMIT_HOOK_EVENT_CANCELLABLE("mouseMove", MOUSECOORDSFLOORED); + if (time) g_pCompositor->notifyIdleActivity(); - EMIT_HOOK_EVENT("mouseMove", MOUSECOORDSFLOORED); - m_vLastCursorPosFloored = MOUSECOORDSFLOORED; const auto PMONITOR = g_pCompositor->getMonitorFromCursor(); @@ -422,9 +422,9 @@ void CInputManager::mouseMoveUnified(uint32_t time, bool refocus) { } void CInputManager::onMouseButton(wlr_pointer_button_event* e) { - g_pCompositor->notifyIdleActivity(); + EMIT_HOOK_EVENT_CANCELLABLE("mouseButton", e); - EMIT_HOOK_EVENT("mouseButton", e); + g_pCompositor->notifyIdleActivity(); m_tmrLastCursorMovement.reset(); @@ -1580,7 +1580,7 @@ void CInputManager::setCursorIconOnBorder(CWindow* w) { wlr_box box = {w->m_vRealPosition.vec().x, w->m_vRealPosition.vec().y, w->m_vRealSize.vec().x, w->m_vRealSize.vec().y}; eBorderIconDirection direction = BORDERICON_NONE; wlr_box boxFullGrabInput = {box.x - *PEXTENDBORDERGRAB - BORDERSIZE, box.y - *PEXTENDBORDERGRAB - BORDERSIZE, box.width + 2 * (*PEXTENDBORDERGRAB + BORDERSIZE), - box.height + 2 * (*PEXTENDBORDERGRAB + BORDERSIZE)}; + box.height + 2 * (*PEXTENDBORDERGRAB + BORDERSIZE)}; if (!wlr_box_contains_point(&boxFullGrabInput, mouseCoords.x, mouseCoords.y) || (!m_lCurrentlyHeldButtons.empty() && !currentlyDraggedWindow)) { direction = BORDERICON_NONE; diff --git a/src/managers/input/InputMethodRelay.cpp b/src/managers/input/InputMethodRelay.cpp index 58ad2a2d..4af17096 100644 --- a/src/managers/input/InputMethodRelay.cpp +++ b/src/managers/input/InputMethodRelay.cpp @@ -3,7 +3,7 @@ #include "../../Compositor.hpp" CInputMethodRelay::CInputMethodRelay() { - g_pHookSystem->hookDynamic("keyboardFocus", [&](void* self, std::any param) { onKeyboardFocus(std::any_cast(param)); }); + g_pHookSystem->hookDynamic("keyboardFocus", [&](void* self, SCallbackInfo& info, std::any param) { onKeyboardFocus(std::any_cast(param)); }); } void CInputMethodRelay::onNewIME(wlr_input_method_v2* pIME) { -- cgit v1.2.3