diff options
| author | Vaxry <vaxry@vaxry.net> | 2024-04-20 20:16:42 +0100 |
|---|---|---|
| committer | Vaxry <vaxry@vaxry.net> | 2024-04-20 20:16:42 +0100 |
| commit | 4ad739ec63c9a11f0537a884ae2a4c56d6bab10b (patch) | |
| tree | 0842db7489691fc6e7cbb3cd6d3321dd0d4790b6 /src/plugins | |
| parent | 1055e6bee677ef9c4ab4d09e43155e00a98a3b49 (diff) | |
HookSystem: improve callback safety
Diffstat (limited to 'src/plugins')
| -rw-r--r-- | src/plugins/PluginAPI.cpp | 18 | ||||
| -rw-r--r-- | src/plugins/PluginAPI.hpp | 12 | ||||
| -rw-r--r-- | src/plugins/PluginSystem.hpp | 24 |
3 files changed, 17 insertions, 37 deletions
diff --git a/src/plugins/PluginAPI.cpp b/src/plugins/PluginAPI.cpp index 78870666..c89918f5 100644 --- a/src/plugins/PluginAPI.cpp +++ b/src/plugins/PluginAPI.cpp @@ -13,30 +13,18 @@ APICALL const char* __hyprland_api_get_hash() { return GIT_COMMIT_HASH; } -APICALL bool HyprlandAPI::registerCallbackStatic(HANDLE handle, const std::string& event, HOOK_CALLBACK_FN* fn) { - auto* const PLUGIN = g_pPluginSystem->getPluginByHandle(handle); - - if (!PLUGIN) - return false; - - g_pHookSystem->hookStatic(event, fn, handle); - PLUGIN->registeredCallbacks.emplace_back(std::make_pair<>(event, fn)); - - return true; -} - -APICALL HOOK_CALLBACK_FN* HyprlandAPI::registerCallbackDynamic(HANDLE handle, const std::string& event, HOOK_CALLBACK_FN fn) { +APICALL std::shared_ptr<HOOK_CALLBACK_FN> HyprlandAPI::registerCallbackDynamic(HANDLE handle, const std::string& event, HOOK_CALLBACK_FN fn) { auto* const PLUGIN = g_pPluginSystem->getPluginByHandle(handle); if (!PLUGIN) return nullptr; - auto* const PFN = g_pHookSystem->hookDynamic(event, fn, handle); + auto PFN = g_pHookSystem->hookDynamic(event, fn, handle); PLUGIN->registeredCallbacks.emplace_back(std::make_pair<>(event, PFN)); return PFN; } -APICALL bool HyprlandAPI::unregisterCallback(HANDLE handle, HOOK_CALLBACK_FN* fn) { +APICALL bool HyprlandAPI::unregisterCallback(HANDLE handle, std::shared_ptr<HOOK_CALLBACK_FN> fn) { auto* const PLUGIN = g_pPluginSystem->getPluginByHandle(handle); if (!PLUGIN) diff --git a/src/plugins/PluginAPI.hpp b/src/plugins/PluginAPI.hpp index 75dca24e..62db8e3b 100644 --- a/src/plugins/PluginAPI.hpp +++ b/src/plugins/PluginAPI.hpp @@ -133,27 +133,19 @@ namespace HyprlandAPI { APICALL Hyprlang::CConfigValue* getConfigValue(HANDLE handle, const std::string& name); /* - Register a static (pointer) callback to a selected event. - Pointer must be kept valid until unregisterCallback() is called. - - returns: true on success, false on fail - */ - APICALL bool registerCallbackStatic(HANDLE handle, const std::string& event, HOOK_CALLBACK_FN* fn); - - /* Register a dynamic (function) callback to a selected event. Pointer will be free'd by Hyprland on unregisterCallback(). returns: a pointer to the newly allocated function. nullptr on fail. */ - APICALL HOOK_CALLBACK_FN* registerCallbackDynamic(HANDLE handle, const std::string& event, HOOK_CALLBACK_FN fn); + APICALL std::shared_ptr<HOOK_CALLBACK_FN> registerCallbackDynamic(HANDLE handle, const std::string& event, HOOK_CALLBACK_FN fn); /* Unregisters a callback. If the callback was dynamic, frees the memory. returns: true on success, false on fail */ - APICALL bool unregisterCallback(HANDLE handle, HOOK_CALLBACK_FN* fn); + APICALL bool unregisterCallback(HANDLE handle, std::shared_ptr<HOOK_CALLBACK_FN> fn); /* Calls a hyprctl command. diff --git a/src/plugins/PluginSystem.hpp b/src/plugins/PluginSystem.hpp index ce2c2bb2..c618489a 100644 --- a/src/plugins/PluginSystem.hpp +++ b/src/plugins/PluginSystem.hpp @@ -8,22 +8,22 @@ class IHyprWindowDecoration; class CPlugin { public: - std::string name = ""; - std::string description = ""; - std::string author = ""; - std::string version = ""; + std::string name = ""; + std::string description = ""; + std::string author = ""; + std::string version = ""; - std::string path = ""; + std::string path = ""; - bool m_bLoadedWithConfig = false; + bool m_bLoadedWithConfig = false; - HANDLE m_pHandle = nullptr; + HANDLE m_pHandle = nullptr; - std::vector<IHyprLayout*> registeredLayouts; - std::vector<IHyprWindowDecoration*> registeredDecorations; - std::vector<std::pair<std::string, HOOK_CALLBACK_FN*>> registeredCallbacks; - std::vector<std::string> registeredDispatchers; - std::vector<std::shared_ptr<SHyprCtlCommand>> registeredHyprctlCommands; + std::vector<IHyprLayout*> registeredLayouts; + std::vector<IHyprWindowDecoration*> registeredDecorations; + std::vector<std::pair<std::string, std::shared_ptr<HOOK_CALLBACK_FN>>> registeredCallbacks; + std::vector<std::string> registeredDispatchers; + std::vector<std::shared_ptr<SHyprCtlCommand>> registeredHyprctlCommands; }; class CPluginSystem { |
