diff options
| author | Vaxry <43317083+vaxerski@users.noreply.github.com> | 2023-02-19 20:54:53 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-19 20:54:53 +0000 |
| commit | 1b56cc4e99c391a94fc05a24e1f71c8e425168f1 (patch) | |
| tree | 110cf0d60d9f01bcbcf3624458afa5ededb5fe8a /src/managers | |
| parent | 6e16627cbcb5d4712195c97e106f2198076c8aff (diff) | |
Added an Event Hook System (#1578)
* added an eventHookSystem
* Add all socket2 events to hooks
Diffstat (limited to 'src/managers')
| -rw-r--r-- | src/managers/HookSystemManager.cpp | 44 | ||||
| -rw-r--r-- | src/managers/HookSystemManager.hpp | 36 | ||||
| -rw-r--r-- | src/managers/KeybindManager.cpp | 5 | ||||
| -rw-r--r-- | src/managers/input/InputManager.cpp | 18 | ||||
| -rw-r--r-- | src/managers/input/InputMethodRelay.cpp | 4 |
5 files changed, 102 insertions, 5 deletions
diff --git a/src/managers/HookSystemManager.cpp b/src/managers/HookSystemManager.cpp new file mode 100644 index 00000000..02775a24 --- /dev/null +++ b/src/managers/HookSystemManager.cpp @@ -0,0 +1,44 @@ +#include "HookSystemManager.hpp" + +CHookSystemManager::CHookSystemManager() { + ; // +} + +// returns the pointer to the function +HOOK_CALLBACK_FN* CHookSystemManager::hookDynamic(const std::string& event, HOOK_CALLBACK_FN fn) { + const auto PVEC = getVecForEvent(event); + const auto PFN = &m_lCallbackFunctions.emplace_back(fn); + PVEC->emplace_back(PFN); + return PFN; +} + +void CHookSystemManager::hookStatic(const std::string& event, HOOK_CALLBACK_FN* fn) { + const auto PVEC = getVecForEvent(event); + PVEC->emplace_back(fn); +} + +void CHookSystemManager::unhook(HOOK_CALLBACK_FN* fn) { + std::erase_if(m_lCallbackFunctions, [&](const auto& other) { return &other == fn; }); + for (auto& [k, v] : m_lpRegisteredHooks) { + std::erase_if(v, [&](const auto& other) { return other == fn; }); + } +} + +void CHookSystemManager::emit(const std::vector<HOOK_CALLBACK_FN*>* callbacks, std::any data) { + if (callbacks->empty()) + return; + + for (auto& cb : *callbacks) + (*cb)(cb, data); +} + +std::vector<HOOK_CALLBACK_FN*>* CHookSystemManager::getVecForEvent(const std::string& event) { + auto IT = std::find_if(m_lpRegisteredHooks.begin(), m_lpRegisteredHooks.end(), [&](const auto& other) { return other.first == event; }); + + if (IT != m_lpRegisteredHooks.end()) + return &IT->second; + + Debug::log(LOG, "[hookSystem] New hook event registered: %s", event.c_str()); + + return &m_lpRegisteredHooks.emplace_back(std::make_pair<>(event, std::vector<HOOK_CALLBACK_FN*>{})).second; +}
\ No newline at end of file diff --git a/src/managers/HookSystemManager.hpp b/src/managers/HookSystemManager.hpp new file mode 100644 index 00000000..57f1e58b --- /dev/null +++ b/src/managers/HookSystemManager.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include "../defines.hpp" + +#include <unordered_map> +#include <any> +#include <list> + +// global typedef for hooked functions. Passes itself as a ptr when called, and `data` additionally. +typedef std::function<void(void*, std::any)> HOOK_CALLBACK_FN; + +#define EMIT_HOOK_EVENT(name, param) \ + { \ + static auto* const PEVENTVEC = g_pHookSystem->getVecForEvent(name); \ + g_pHookSystem->emit(PEVENTVEC, param); \ + } + +class CHookSystemManager { + public: + CHookSystemManager(); + + // returns the pointer to the function + HOOK_CALLBACK_FN* hookDynamic(const std::string& event, HOOK_CALLBACK_FN fn); + void hookStatic(const std::string& event, HOOK_CALLBACK_FN* fn); + void unhook(HOOK_CALLBACK_FN* fn); + + void emit(const std::vector<HOOK_CALLBACK_FN*>* callbacks, std::any data = 0); + std::vector<HOOK_CALLBACK_FN*>* getVecForEvent(const std::string& event); + + private: + // todo: this is slow. Maybe static ptrs should be somehow allowed. unique ptr for vec? + std::list<std::pair<std::string, std::vector<HOOK_CALLBACK_FN*>>> m_lpRegisteredHooks; + std::list<HOOK_CALLBACK_FN> m_lCallbackFunctions; +}; + +inline std::unique_ptr<CHookSystemManager> g_pHookSystem;
\ No newline at end of file diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index ef988c32..76c08611 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -792,6 +792,7 @@ void CKeybindManager::changeworkspace(std::string args) { PWORKSPACETOCHANGETO->startAnim(true, ANIMTOLEFT); g_pEventManager->postEvent(SHyprIPCEvent{"workspace", PWORKSPACETOCHANGETO->m_szName}); + EMIT_HOOK_EVENT("workspace", PWORKSPACETOCHANGETO); } // If the monitor is not the one our cursor's at, warp to it. @@ -885,6 +886,7 @@ void CKeybindManager::changeworkspace(std::string args) { g_pCompositor->warpCursorTo(PMONITOR->vecPosition + PMONITOR->vecSize / 2.f); g_pEventManager->postEvent(SHyprIPCEvent{"workspace", PWORKSPACE->m_szName}); + EMIT_HOOK_EVENT("workspace", PWORKSPACE); g_pCompositor->setActiveMonitor(PMONITOR); @@ -1066,6 +1068,7 @@ void CKeybindManager::moveActiveToWorkspaceSilent(std::string args) { // manually post event cuz it got ignored above g_pEventManager->postEvent(SHyprIPCEvent{"movewindow", getFormat("%x,%s", PWINDOW, PWORKSPACE->m_szName.c_str())}); + EMIT_HOOK_EVENT("moveWindow", (std::vector<void*>{PWINDOW, PWORKSPACE})); PWINDOW->m_iWorkspaceID = OLDWORKSPACEIDRETURN; const auto PNEXTCANDIDATE = g_pLayoutManager->getCurrentLayout()->getNextWindowCandidate(PWINDOW); @@ -1702,6 +1705,7 @@ void CKeybindManager::setSubmap(std::string submap) { m_szCurrentSelectedSubmap = ""; Debug::log(LOG, "Reset active submap to the default one."); g_pEventManager->postEvent(SHyprIPCEvent{"submap", ""}); + EMIT_HOOK_EVENT("submap", m_szCurrentSelectedSubmap); return; } @@ -1710,6 +1714,7 @@ void CKeybindManager::setSubmap(std::string submap) { m_szCurrentSelectedSubmap = submap; Debug::log(LOG, "Changed keybind submap to %s", submap.c_str()); g_pEventManager->postEvent(SHyprIPCEvent{"submap", submap}); + EMIT_HOOK_EVENT("submap", m_szCurrentSelectedSubmap); return; } } diff --git a/src/managers/input/InputManager.cpp b/src/managers/input/InputManager.cpp index 6aa3b73b..0c6a2420 100644 --- a/src/managers/input/InputManager.cpp +++ b/src/managers/input/InputManager.cpp @@ -531,8 +531,10 @@ void CInputManager::newKeyboard(wlr_input_device* keyboard) { &wlr_keyboard_from_input_device(keyboard)->events.keymap, [&](void* owner, void* data) { const auto PKEYBOARD = (SKeyboard*)owner; + const auto LAYOUT = getActiveLayoutForKeyboard(PKEYBOARD); - g_pEventManager->postEvent(SHyprIPCEvent{"activelayout", PKEYBOARD->name + "," + getActiveLayoutForKeyboard(PKEYBOARD)}, true); // force as this should ALWAYS be sent + g_pEventManager->postEvent(SHyprIPCEvent{"activelayout", PKEYBOARD->name + "," + LAYOUT}, true); // force as this should ALWAYS be sent + EMIT_HOOK_EVENT("activeLayout", (std::vector<void*>{PKEYBOARD, (void*)&LAYOUT})); }, PNEWKEYBOARD, "Keyboard"); @@ -568,8 +570,10 @@ void CInputManager::newVirtualKeyboard(wlr_input_device* keyboard) { &wlr_keyboard_from_input_device(keyboard)->events.keymap, [&](void* owner, void* data) { const auto PKEYBOARD = (SKeyboard*)owner; + const auto LAYOUT = getActiveLayoutForKeyboard(PKEYBOARD); - g_pEventManager->postEvent(SHyprIPCEvent{"activelayout", PKEYBOARD->name + "," + getActiveLayoutForKeyboard(PKEYBOARD)}, true); // force as this should ALWAYS be sent + g_pEventManager->postEvent(SHyprIPCEvent{"activelayout", PKEYBOARD->name + "," + LAYOUT}, true); // force as this should ALWAYS be sent + EMIT_HOOK_EVENT("activeLayout", (std::vector<void*>{PKEYBOARD, (void*)&LAYOUT})); }, PNEWKEYBOARD, "Keyboard"); @@ -710,7 +714,10 @@ void CInputManager::applyConfigToKeyboard(SKeyboard* pKeyboard) { xkb_keymap_unref(KEYMAP); xkb_context_unref(CONTEXT); - g_pEventManager->postEvent(SHyprIPCEvent{"activelayout", pKeyboard->name + "," + getActiveLayoutForKeyboard(pKeyboard)}, true); // force as this should ALWAYS be sent + const auto LAYOUTSTR = getActiveLayoutForKeyboard(pKeyboard); + + g_pEventManager->postEvent(SHyprIPCEvent{"activelayout", pKeyboard->name + "," + LAYOUTSTR}, true); // force as this should ALWAYS be sent + EMIT_HOOK_EVENT("activeLayout", (std::vector<void*>{pKeyboard, (void*)&LAYOUTSTR})); Debug::log(LOG, "Set the keyboard layout to %s and variant to %s for keyboard \"%s\"", rules.layout, rules.variant, pKeyboard->keyboard->name); } @@ -974,7 +981,10 @@ void CInputManager::onKeyboardMod(void* data, SKeyboard* pKeyboard) { if (PWLRKB->modifiers.group != pKeyboard->activeLayout) { pKeyboard->activeLayout = PWLRKB->modifiers.group; - g_pEventManager->postEvent(SHyprIPCEvent{"activelayout", pKeyboard->name + "," + getActiveLayoutForKeyboard(pKeyboard)}, true); // force as this should ALWAYS be sent + const auto LAYOUT = getActiveLayoutForKeyboard(pKeyboard); + + g_pEventManager->postEvent(SHyprIPCEvent{"activelayout", pKeyboard->name + "," + LAYOUT}, true); // force as this should ALWAYS be sent + EMIT_HOOK_EVENT("activeLayout", (std::vector<void*>{pKeyboard, (void*)&LAYOUT})); } } diff --git a/src/managers/input/InputMethodRelay.cpp b/src/managers/input/InputMethodRelay.cpp index 20057a6a..95f0b6fb 100644 --- a/src/managers/input/InputMethodRelay.cpp +++ b/src/managers/input/InputMethodRelay.cpp @@ -2,7 +2,9 @@ #include "InputManager.hpp" #include "../../Compositor.hpp" -CInputMethodRelay::CInputMethodRelay() {} +CInputMethodRelay::CInputMethodRelay() { + g_pHookSystem->hookDynamic("keyboardFocus", [&](void* self, std::any param) { onKeyboardFocus(std::any_cast<wlr_surface*>(param)); }); +} void CInputMethodRelay::onNewIME(wlr_input_method_v2* pIME) { if (m_pWLRIME) { |
