hyprland-plugins/hyprwinwrap/main.cpp

181 lines
6.7 KiB
C++
Raw Permalink Normal View History

2023-11-24 22:13:34 +01:00
#define WLR_USE_UNSTABLE
#include <unistd.h>
#include <vector>
#include <hyprland/src/includes.hpp>
#include <any>
2024-07-21 13:40:40 +02:00
#include <sstream>
2023-11-24 22:13:34 +01:00
#define private public
#include <hyprland/src/Compositor.hpp>
2024-03-20 04:02:10 +01:00
#include <hyprland/src/desktop/Window.hpp>
2023-11-24 22:13:34 +01:00
#include <hyprland/src/config/ConfigManager.hpp>
#include <hyprland/src/render/Renderer.hpp>
#undef private
#include "globals.hpp"
// Do NOT change this function.
APICALL EXPORT std::string PLUGIN_API_VERSION() {
return HYPRLAND_API_VERSION;
}
// hooks
2024-03-01 14:44:29 +01:00
inline CFunctionHook* subsurfaceHook = nullptr;
inline CFunctionHook* commitHook = nullptr;
typedef void (*origCommitSubsurface)(CSubsurface* thisptr);
2023-11-24 22:13:34 +01:00
typedef void (*origCommit)(void* owner, void* data);
2024-04-27 14:03:46 +02:00
std::vector<PHLWINDOWREF> bgWindows;
2023-11-24 22:13:34 +01:00
//
2024-04-27 14:03:46 +02:00
void onNewWindow(PHLWINDOW pWindow) {
2024-02-18 16:30:21 +01:00
static auto* const PCLASS = (Hyprlang::STRING const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprwinwrap:class")->getDataStaticPtr();
2023-11-24 22:13:34 +01:00
if (pWindow->m_szInitialClass != *PCLASS)
return;
const auto PMONITOR = g_pCompositor->getMonitorFromID(pWindow->m_iMonitorID);
if (!PMONITOR)
return;
if (!pWindow->m_bIsFloating)
g_pLayoutManager->getCurrentLayout()->changeWindowFloatingMode(pWindow);
pWindow->m_vRealSize.setValueAndWarp(PMONITOR->vecSize);
pWindow->m_vRealPosition.setValueAndWarp(PMONITOR->vecPosition);
pWindow->m_vSize = PMONITOR->vecSize;
pWindow->m_vPosition = PMONITOR->vecPosition;
pWindow->m_bPinned = true;
2024-03-02 20:38:48 +01:00
g_pXWaylandManager->setWindowSize(pWindow, pWindow->m_vRealSize.goal(), true);
2023-11-24 22:13:34 +01:00
bgWindows.push_back(pWindow);
2024-02-06 17:11:46 +01:00
pWindow->m_bHidden = true; // no renderino hyprland pls
2023-11-24 22:13:34 +01:00
g_pInputManager->refocus();
Debug::log(LOG, "[hyprwinwrap] new window moved to bg {}", pWindow);
}
2024-04-27 14:03:46 +02:00
void onCloseWindow(PHLWINDOW pWindow) {
std::erase_if(bgWindows, [pWindow](const auto& ref) { return ref.expired() || ref.lock() == pWindow; });
2023-11-24 22:13:34 +01:00
Debug::log(LOG, "[hyprwinwrap] closed window {}", pWindow);
}
void onRenderStage(eRenderStage stage) {
if (stage != RENDER_PRE_WINDOWS)
return;
2024-04-27 14:03:46 +02:00
for (auto& bg : bgWindows) {
const auto bgw = bg.lock();
2023-11-24 22:13:34 +01:00
if (bgw->m_iMonitorID != g_pHyprOpenGL->m_RenderData.pMonitor->ID)
continue;
timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
2024-02-06 17:11:46 +01:00
// cant use setHidden cuz that sends suspended and shit too that would be laggy
bgw->m_bHidden = false;
2023-11-24 22:13:34 +01:00
g_pHyprRenderer->renderWindow(bgw, g_pHyprOpenGL->m_RenderData.pMonitor, &now, false, RENDER_PASS_ALL, false, true);
2024-02-06 17:11:46 +01:00
bgw->m_bHidden = true;
2023-11-24 22:13:34 +01:00
}
}
2024-03-01 14:44:29 +01:00
void onCommitSubsurface(CSubsurface* thisptr) {
2024-06-08 11:12:34 +02:00
const auto PWINDOW = thisptr->m_pWLSurface->getWindow();
2023-11-24 22:13:34 +01:00
2024-04-27 14:03:46 +02:00
if (!PWINDOW || std::find_if(bgWindows.begin(), bgWindows.end(), [PWINDOW](const auto& ref) { return ref.lock() == PWINDOW; }) == bgWindows.end()) {
2024-03-01 14:44:29 +01:00
((origCommitSubsurface)subsurfaceHook->m_pOriginal)(thisptr);
2023-11-24 22:13:34 +01:00
return;
}
2024-02-06 17:11:46 +01:00
// cant use setHidden cuz that sends suspended and shit too that would be laggy
PWINDOW->m_bHidden = false;
2023-11-24 22:13:34 +01:00
2024-03-01 14:44:29 +01:00
((origCommitSubsurface)subsurfaceHook->m_pOriginal)(thisptr);
if (const auto MON = g_pCompositor->getMonitorFromID(PWINDOW->m_iMonitorID); MON)
g_pHyprOpenGL->markBlurDirtyForMonitor(MON);
2023-11-24 22:13:34 +01:00
2024-02-06 17:11:46 +01:00
PWINDOW->m_bHidden = true;
2023-11-24 22:13:34 +01:00
}
void onCommit(void* owner, void* data) {
2024-04-27 14:03:46 +02:00
const auto PWINDOW = ((CWindow*)owner)->m_pSelf.lock();
2023-11-24 22:13:34 +01:00
2024-04-27 14:03:46 +02:00
if (std::find_if(bgWindows.begin(), bgWindows.end(), [PWINDOW](const auto& ref) { return ref.lock() == PWINDOW; }) == bgWindows.end()) {
2023-11-24 22:13:34 +01:00
((origCommit)commitHook->m_pOriginal)(owner, data);
return;
}
2024-02-06 17:11:46 +01:00
// cant use setHidden cuz that sends suspended and shit too that would be laggy
PWINDOW->m_bHidden = false;
2023-11-24 22:13:34 +01:00
((origCommit)commitHook->m_pOriginal)(owner, data);
if (const auto MON = g_pCompositor->getMonitorFromID(PWINDOW->m_iMonitorID); MON)
g_pHyprOpenGL->markBlurDirtyForMonitor(MON);
2023-11-24 22:13:34 +01:00
2024-02-06 17:11:46 +01:00
PWINDOW->m_bHidden = true;
2023-11-24 22:13:34 +01:00
}
void onConfigReloaded() {
2024-02-18 16:30:21 +01:00
static auto* const PCLASS = (Hyprlang::STRING const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprwinwrap:class")->getDataStaticPtr();
g_pConfigManager->parseKeyword("windowrulev2", std::string{"float, class:^("} + *PCLASS + ")$");
g_pConfigManager->parseKeyword("windowrulev2", std::string{"size 100\% 100\%, class:^("} + *PCLASS + ")$");
2023-11-24 22:13:34 +01:00
}
APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) {
PHANDLE = handle;
const std::string HASH = __hyprland_api_get_hash();
if (HASH != GIT_COMMIT_HASH) {
HyprlandAPI::addNotification(PHANDLE, "[hyprwinwrap] Failure in initialization: Version mismatch (headers ver is not equal to running hyprland ver)",
CColor{1.0, 0.2, 0.2, 1.0}, 5000);
throw std::runtime_error("[hww] Version mismatch");
}
2024-04-22 16:57:18 +02:00
// clang-format off
2024-04-27 14:03:46 +02:00
static auto P = HyprlandAPI::registerCallbackDynamic(PHANDLE, "openWindow", [&](void* self, SCallbackInfo& info, std::any data) { onNewWindow(std::any_cast<PHLWINDOW>(data)); });
static auto P2 = HyprlandAPI::registerCallbackDynamic(PHANDLE, "closeWindow", [&](void* self, SCallbackInfo& info, std::any data) { onCloseWindow(std::any_cast<PHLWINDOW>(data)); });
2024-04-22 16:57:18 +02:00
static auto P3 = HyprlandAPI::registerCallbackDynamic(PHANDLE, "render", [&](void* self, SCallbackInfo& info, std::any data) { onRenderStage(std::any_cast<eRenderStage>(data)); });
static auto P4 = HyprlandAPI::registerCallbackDynamic(PHANDLE, "configReloaded", [&](void* self, SCallbackInfo& info, std::any data) { onConfigReloaded(); });
// clang-format on
2023-11-24 22:13:34 +01:00
2024-03-01 14:44:29 +01:00
auto fns = HyprlandAPI::findFunctionsByName(PHANDLE, "onCommit");
2023-11-24 22:13:34 +01:00
if (fns.size() < 1)
2024-03-01 14:44:29 +01:00
throw std::runtime_error("hyprwinwrap: onCommit not found");
for (auto& fn : fns) {
if (!fn.demangled.contains("CSubsurface"))
continue;
subsurfaceHook = HyprlandAPI::createFunctionHook(PHANDLE, fn.address, (void*)&onCommitSubsurface);
2024-03-01 14:44:29 +01:00
}
2023-11-24 22:13:34 +01:00
fns = HyprlandAPI::findFunctionsByName(PHANDLE, "listener_commitWindow");
if (fns.size() < 1)
throw std::runtime_error("hyprwinwrap: listener_commitWindow not found");
commitHook = HyprlandAPI::createFunctionHook(PHANDLE, fns[0].address, (void*)&onCommit);
2024-03-01 14:44:29 +01:00
bool hkResult = subsurfaceHook->hook();
2023-11-24 22:13:34 +01:00
hkResult = hkResult && commitHook->hook();
if (!hkResult)
throw std::runtime_error("hyprwinwrap: hooks failed");
2024-02-18 16:30:21 +01:00
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprwinwrap:class", Hyprlang::STRING{"kitty-bg"});
2023-11-24 22:13:34 +01:00
HyprlandAPI::addNotification(PHANDLE, "[hyprwinwrap] Initialized successfully!", CColor{0.2, 1.0, 0.2, 1.0}, 5000);
return {"hyprwinwrap", "A clone of xwinwrap for Hyprland", "Vaxry", "1.0"};
}
APICALL EXPORT void PLUGIN_EXIT() {
;
}