From 3fd30dc570f9571b48bcc171c0875a1afb90ad05 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sun, 12 Nov 2023 01:02:03 +0100 Subject: [PATCH] use event hooks --- src/main.cpp | 61 +++++++++++++++++----------------------------------- 1 file changed, 20 insertions(+), 41 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index e0fd030..8b92efc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,48 +5,35 @@ #include #include #include +#include #include #include const CColor s_pluginColor = {0x61 / 255.0f, 0xAF / 255.0f, 0xEF / 255.0f, 1.0f}; -inline CFunctionHook* g_pTouchDownHook = nullptr; -inline CFunctionHook* g_pTouchUpHook = nullptr; -inline CFunctionHook* g_pTouchMoveHook = nullptr; -typedef void (*origTouchDown)(void*, wlr_touch_down_event*); -typedef void (*origTouchUp)(void*, wlr_touch_up_event*); -typedef void (*origTouchMove)(void*, wlr_touch_motion_event*); - -void hkOnTouchDown(void* thisptr, wlr_touch_down_event* e) { - const auto BLOCK = g_pGestureManager->onTouchDown(e); - - if (BLOCK) { - return; - } - (*(origTouchDown)g_pTouchDownHook->m_pOriginal)(thisptr, e); +void hkOnTouchDown(void* _, SCallbackInfo& cbinfo, std::any e) { + auto ev = std::any_cast(e); + + cbinfo.cancelled = g_pGestureManager->onTouchDown(ev); } -void hkOnTouchUp(void* thisptr, wlr_touch_up_event* e) { - const auto BLOCK = g_pGestureManager->onTouchUp(e); +void hkOnTouchUp(void* _, SCallbackInfo& cbinfo, std::any e) { + auto ev = std::any_cast(e); - if (BLOCK) { - return; - } - - (*(origTouchUp)g_pTouchUpHook->m_pOriginal)(thisptr, e); + cbinfo.cancelled = g_pGestureManager->onTouchUp(ev); } -void hkOnTouchMove(void* thisptr, wlr_touch_motion_event* e) { - const auto BLOCK = g_pGestureManager->onTouchMove(e); - - if (BLOCK) { - return; - } +void hkOnTouchMove(void* _, SCallbackInfo& cbinfo, std::any e) { + auto ev = std::any_cast(e); - (*(origTouchMove)g_pTouchMoveHook->m_pOriginal)(thisptr, e); + cbinfo.cancelled = g_pGestureManager->onTouchMove(ev); } +HOOK_CALLBACK_FN gTouchDownCallback = hkOnTouchDown; +HOOK_CALLBACK_FN gTouchUpCallback = hkOnTouchUp; +HOOK_CALLBACK_FN gTouchMoveCallback = hkOnTouchMove; + // Do NOT change this function. APICALL EXPORT std::string PLUGIN_API_VERSION() { return HYPRLAND_API_VERSION; @@ -74,16 +61,6 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) { } #pragma GCC diagnostic pop -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpmf-conversions" - g_pTouchDownHook = HyprlandAPI::createFunctionHook( - PHANDLE, (void*)&CInputManager::onTouchDown, (void*)&hkOnTouchDown); - g_pTouchUpHook = HyprlandAPI::createFunctionHook( - PHANDLE, (void*)&CInputManager::onTouchUp, (void*)&hkOnTouchUp); - g_pTouchMoveHook = HyprlandAPI::createFunctionHook( - PHANDLE, (void*)&CInputManager::onTouchMove, (void*)&hkOnTouchMove); -#pragma GCC diagnostic pop - const auto hlTargetVersion = GIT_COMMIT_HASH; const auto hlVersion = HyprlandAPI::getHyprlandVersion(PHANDLE); @@ -98,9 +75,11 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) { hlVersion.hash); } - g_pTouchDownHook->hook(); - g_pTouchUpHook->hook(); - g_pTouchMoveHook->hook(); + HyprlandAPI::registerCallbackStatic(PHANDLE, "touchDown", + &gTouchDownCallback); + HyprlandAPI::registerCallbackStatic(PHANDLE, "touchUp", &gTouchUpCallback); + HyprlandAPI::registerCallbackStatic(PHANDLE, "touchMove", + &gTouchMoveCallback); HyprlandAPI::reloadConfig();