Fix selection hook segfaulting on reload

This commit is contained in:
outfoxxed 2023-04-30 00:47:01 -07:00
parent e41c3cb256
commit 136c910c5a
No known key found for this signature in database
GPG key ID: 4C88A185FB89301E
4 changed files with 37 additions and 27 deletions

View file

@ -1017,11 +1017,11 @@ void Hy3Layout::onEnable() {
this->onWindowCreatedTiling(window.get()); this->onWindowCreatedTiling(window.get());
} }
setup_selection_hook(); selection_hook::enable();
} }
void Hy3Layout::onDisable() { void Hy3Layout::onDisable() {
disable_selection_hook(); selection_hook::disable();
this->nodes.clear(); this->nodes.clear();
} }

View file

@ -2,29 +2,30 @@
#include <hyprland/src/plugins/PluginAPI.hpp> #include <hyprland/src/plugins/PluginAPI.hpp>
#include <hyprland/src/Compositor.hpp> #include <hyprland/src/Compositor.hpp>
inline CFunctionHook* g_LastSelectionHook = nullptr; namespace selection_hook {
inline CFunctionHook* g_LastSelectionHook = nullptr;
void hook_update_decos(void* thisptr, CWindow* window) { void hook_updateDecos(void* thisptr, CWindow* window) {
bool explicitly_selected = g_Hy3Layout->shouldRenderSelected(window); bool explicitly_selected = g_Hy3Layout->shouldRenderSelected(window);
Debug::log(LOG, "update decos for %p - selected: %d", window, explicitly_selected); Debug::log(LOG, "update decos for %p - selected: %d", window, explicitly_selected);
auto* lastWindow = g_pCompositor->m_pLastWindow; auto* lastWindow = g_pCompositor->m_pLastWindow;
if (explicitly_selected) { if (explicitly_selected) {
g_pCompositor->m_pLastWindow = window; g_pCompositor->m_pLastWindow = window;
}
((void (*)(void*, CWindow*)) g_LastSelectionHook->m_pOriginal)(thisptr, window);
if (explicitly_selected) {
g_pCompositor->m_pLastWindow = lastWindow;
}
} }
((void (*)(void*, CWindow*)) g_LastSelectionHook->m_pOriginal)(thisptr, window); void init() {
if (explicitly_selected) {
g_pCompositor->m_pLastWindow = lastWindow;
}
}
void setup_selection_hook() {
if (g_LastSelectionHook == nullptr) {
static const auto decoUpdateCandidates = HyprlandAPI::findFunctionsByName(PHANDLE, "updateWindowAnimatedDecorationValues"); static const auto decoUpdateCandidates = HyprlandAPI::findFunctionsByName(PHANDLE, "updateWindowAnimatedDecorationValues");
if (decoUpdateCandidates.size() != 1) { if (decoUpdateCandidates.size() != 1) {
g_LastSelectionHook = nullptr;
Debug::log(ERR, "Expected one matching function to hook for \"updateWindowAnimatedDecorationValues\", found %d", decoUpdateCandidates.size()); Debug::log(ERR, "Expected one matching function to hook for \"updateWindowAnimatedDecorationValues\", found %d", decoUpdateCandidates.size());
HyprlandAPI::addNotificationV2(PHANDLE, { HyprlandAPI::addNotificationV2(PHANDLE, {
{"text", "Failed to load function hooks: \"updateWindowAnimatedDecorationValues\""}, {"text", "Failed to load function hooks: \"updateWindowAnimatedDecorationValues\""},
@ -35,14 +36,18 @@ void setup_selection_hook() {
return; return;
} }
g_LastSelectionHook = HyprlandAPI::createFunctionHook(PHANDLE, decoUpdateCandidates[0].address, (void*)&hook_update_decos); g_LastSelectionHook = HyprlandAPI::createFunctionHook(PHANDLE, decoUpdateCandidates[0].address, (void*)&hook_updateDecos);
} }
g_LastSelectionHook->hook(); void enable() {
} if (g_LastSelectionHook != nullptr) {
g_LastSelectionHook->hook();
}
}
void disable_selection_hook() { void disable() {
if (g_LastSelectionHook != nullptr) { if (g_LastSelectionHook != nullptr) {
g_LastSelectionHook->unhook(); g_LastSelectionHook->unhook();
}
} }
} }

View file

@ -1,4 +1,7 @@
#pragma once #pragma once
void setup_selection_hook(); namespace selection_hook {
void disable_selection_hook(); void init();
void enable();
void disable();
}

View file

@ -2,7 +2,7 @@
#include <hyprland/src/Compositor.hpp> #include <hyprland/src/Compositor.hpp>
#include "globals.hpp" #include "globals.hpp"
#include "SelectionHook.hpp"
APICALL EXPORT std::string PLUGIN_API_VERSION() { APICALL EXPORT std::string PLUGIN_API_VERSION() {
return HYPRLAND_API_VERSION; return HYPRLAND_API_VERSION;
@ -98,6 +98,8 @@ void dispatch_debug(std::string arg) {
APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) { APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) {
PHANDLE = handle; PHANDLE = handle;
selection_hook::init();
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hy3:no_gaps_when_only", SConfigValue{.intValue = 0}); HyprlandAPI::addConfigValue(PHANDLE, "plugin:hy3:no_gaps_when_only", SConfigValue{.intValue = 0});
g_Hy3Layout = std::make_unique<Hy3Layout>(); g_Hy3Layout = std::make_unique<Hy3Layout>();