Added an Event Hook System (#1578)

* added an eventHookSystem

* Add all socket2 events to hooks
This commit is contained in:
Vaxry 2023-02-19 20:54:53 +00:00 committed by GitHub
parent 6e16627cbc
commit 1b56cc4e99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 142 additions and 14 deletions

View file

@ -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;
}

View file

@ -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;

View file

@ -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;
}
}

View file

@ -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}));
}
}

View file

@ -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) {