mirror of
https://github.com/hyprwm/Hyprland
synced 2025-02-17 02:02:06 +01:00
pluginapi: unregister callbacks on lost ptrs
This commit is contained in:
parent
450343b7b8
commit
e91513a5e8
4 changed files with 21 additions and 17 deletions
|
@ -20,7 +20,7 @@ APICALL std::shared_ptr<HOOK_CALLBACK_FN> HyprlandAPI::registerCallbackDynamic(H
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
auto PFN = g_pHookSystem->hookDynamic(event, fn, handle);
|
auto PFN = g_pHookSystem->hookDynamic(event, fn, handle);
|
||||||
PLUGIN->registeredCallbacks.emplace_back(std::make_pair<>(event, PFN));
|
PLUGIN->registeredCallbacks.emplace_back(std::make_pair<>(event, std::weak_ptr<HOOK_CALLBACK_FN>(PFN)));
|
||||||
return PFN;
|
return PFN;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ APICALL bool HyprlandAPI::unregisterCallback(HANDLE handle, std::shared_ptr<HOOK
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
g_pHookSystem->unhook(fn);
|
g_pHookSystem->unhook(fn);
|
||||||
std::erase_if(PLUGIN->registeredCallbacks, [&](const auto& other) { return other.second == fn; });
|
std::erase_if(PLUGIN->registeredCallbacks, [&](const auto& other) { return other.second.lock() == fn; });
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -137,15 +137,19 @@ namespace HyprlandAPI {
|
||||||
Pointer will be free'd by Hyprland on unregisterCallback().
|
Pointer will be free'd by Hyprland on unregisterCallback().
|
||||||
|
|
||||||
returns: a pointer to the newly allocated function. nullptr on fail.
|
returns: a pointer to the newly allocated function. nullptr on fail.
|
||||||
|
|
||||||
|
WARNING: Losing this pointer will unregister the callback!
|
||||||
*/
|
*/
|
||||||
APICALL std::shared_ptr<HOOK_CALLBACK_FN> registerCallbackDynamic(HANDLE handle, const std::string& event, HOOK_CALLBACK_FN fn);
|
APICALL [[nodiscard]] 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.
|
Unregisters a callback. If the callback was dynamic, frees the memory.
|
||||||
|
|
||||||
returns: true on success, false on fail
|
returns: true on success, false on fail
|
||||||
|
|
||||||
|
Deprecated: just reset the pointer you received with registerCallbackDynamic
|
||||||
*/
|
*/
|
||||||
APICALL bool unregisterCallback(HANDLE handle, std::shared_ptr<HOOK_CALLBACK_FN> fn);
|
APICALL [[deprecated]] bool unregisterCallback(HANDLE handle, std::shared_ptr<HOOK_CALLBACK_FN> fn);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Calls a hyprctl command.
|
Calls a hyprctl command.
|
||||||
|
|
|
@ -121,7 +121,7 @@ void CPluginSystem::unloadPlugin(const CPlugin* plugin, bool eject) {
|
||||||
|
|
||||||
// save these two for dlclose and a log,
|
// save these two for dlclose and a log,
|
||||||
// as erase_if will kill the pointer
|
// as erase_if will kill the pointer
|
||||||
const auto PLNAME = plugin->name;
|
const auto PLNAME = plugin->name;
|
||||||
const auto PLHANDLE = plugin->m_pHandle;
|
const auto PLHANDLE = plugin->m_pHandle;
|
||||||
|
|
||||||
std::erase_if(m_vLoadedPlugins, [&](const auto& other) { return other->m_pHandle == PLHANDLE; });
|
std::erase_if(m_vLoadedPlugins, [&](const auto& other) { return other->m_pHandle == PLHANDLE; });
|
||||||
|
|
|
@ -8,22 +8,22 @@ class IHyprWindowDecoration;
|
||||||
|
|
||||||
class CPlugin {
|
class CPlugin {
|
||||||
public:
|
public:
|
||||||
std::string name = "";
|
std::string name = "";
|
||||||
std::string description = "";
|
std::string description = "";
|
||||||
std::string author = "";
|
std::string author = "";
|
||||||
std::string version = "";
|
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<IHyprLayout*> registeredLayouts;
|
||||||
std::vector<IHyprWindowDecoration*> registeredDecorations;
|
std::vector<IHyprWindowDecoration*> registeredDecorations;
|
||||||
std::vector<std::pair<std::string, std::shared_ptr<HOOK_CALLBACK_FN>>> registeredCallbacks;
|
std::vector<std::pair<std::string, std::weak_ptr<HOOK_CALLBACK_FN>>> registeredCallbacks;
|
||||||
std::vector<std::string> registeredDispatchers;
|
std::vector<std::string> registeredDispatchers;
|
||||||
std::vector<std::shared_ptr<SHyprCtlCommand>> registeredHyprctlCommands;
|
std::vector<std::shared_ptr<SHyprCtlCommand>> registeredHyprctlCommands;
|
||||||
};
|
};
|
||||||
|
|
||||||
class CPluginSystem {
|
class CPluginSystem {
|
||||||
|
|
Loading…
Add table
Reference in a new issue