HookSystem: improve callback safety

This commit is contained in:
Vaxry 2024-04-20 20:16:42 +01:00
parent 1055e6bee6
commit 4ad739ec63
18 changed files with 89 additions and 105 deletions

View file

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

View file

@ -132,28 +132,20 @@ 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.

View file

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