2024-04-19 23:16:35 +02:00
|
|
|
#include "TearingControl.hpp"
|
|
|
|
#include "../managers/ProtocolManager.hpp"
|
|
|
|
#include "../desktop/Window.hpp"
|
|
|
|
#include "../Compositor.hpp"
|
|
|
|
|
|
|
|
CTearingControlProtocol::CTearingControlProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) {
|
2024-04-20 21:16:42 +02:00
|
|
|
static auto P = g_pHookSystem->hookDynamic("destroyWindow", [this](void* self, SCallbackInfo& info, std::any param) { this->onWindowDestroy(std::any_cast<CWindow*>(param)); });
|
2024-04-19 23:16:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTearingControlProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) {
|
2024-04-20 14:25:29 +02:00
|
|
|
const auto RESOURCE = m_vManagers.emplace_back(std::make_unique<CWpTearingControlManagerV1>(client, ver, id)).get();
|
|
|
|
RESOURCE->setOnDestroy([this](CWpTearingControlManagerV1* p) { this->onManagerResourceDestroy(p->resource()); });
|
2024-04-19 23:16:35 +02:00
|
|
|
|
2024-04-20 14:25:29 +02:00
|
|
|
RESOURCE->setDestroy([this](CWpTearingControlManagerV1* pMgr) { this->onManagerResourceDestroy(pMgr->resource()); });
|
|
|
|
RESOURCE->setGetTearingControl([this](CWpTearingControlManagerV1* pMgr, uint32_t id, wl_resource* surface) {
|
|
|
|
this->onGetController(wl_resource_get_client(pMgr->resource()), pMgr->resource(), id, wlr_surface_from_resource(surface));
|
|
|
|
});
|
2024-04-19 23:16:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTearingControlProtocol::onManagerResourceDestroy(wl_resource* res) {
|
|
|
|
std::erase_if(m_vManagers, [&](const auto& other) { return other->resource() == res; });
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTearingControlProtocol::onGetController(wl_client* client, wl_resource* resource, uint32_t id, wlr_surface* surf) {
|
2024-04-20 14:25:29 +02:00
|
|
|
const auto CONTROLLER =
|
|
|
|
m_vTearingControllers.emplace_back(std::make_unique<CTearingControl>(std::make_shared<CWpTearingControlV1>(client, wl_resource_get_version(resource), id), surf)).get();
|
2024-04-19 23:16:35 +02:00
|
|
|
|
|
|
|
if (!CONTROLLER->good()) {
|
2024-04-21 20:29:45 +02:00
|
|
|
wl_resource_post_no_memory(resource);
|
2024-04-19 23:16:35 +02:00
|
|
|
m_vTearingControllers.pop_back();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTearingControlProtocol::onControllerDestroy(CTearingControl* control) {
|
|
|
|
std::erase_if(m_vTearingControllers, [control](const auto& other) { return other.get() == control; });
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTearingControlProtocol::onWindowDestroy(CWindow* pWindow) {
|
|
|
|
for (auto& c : m_vTearingControllers) {
|
|
|
|
if (c->pWindow == pWindow)
|
|
|
|
c->pWindow = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2024-04-20 14:25:29 +02:00
|
|
|
CTearingControl::CTearingControl(SP<CWpTearingControlV1> resource_, wlr_surface* surf_) : resource(resource_) {
|
2024-04-19 23:16:35 +02:00
|
|
|
resource->setData(this);
|
2024-04-20 14:25:29 +02:00
|
|
|
resource->setOnDestroy([this](CWpTearingControlV1* res) { PROTO::tearing->onControllerDestroy(this); });
|
|
|
|
resource->setDestroy([this](CWpTearingControlV1* res) { PROTO::tearing->onControllerDestroy(this); });
|
|
|
|
resource->setSetPresentationHint([this](CWpTearingControlV1* res, wpTearingControlV1PresentationHint hint) { this->onHint(hint); });
|
2024-04-19 23:16:35 +02:00
|
|
|
|
2024-04-20 01:08:49 +02:00
|
|
|
for (auto& w : g_pCompositor->m_vWindows) {
|
|
|
|
if (w->m_pWLSurface.wlr() == surf_) {
|
|
|
|
pWindow = w.get();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-04-19 23:16:35 +02:00
|
|
|
}
|
|
|
|
|
2024-04-20 14:25:29 +02:00
|
|
|
void CTearingControl::onHint(wpTearingControlV1PresentationHint hint_) {
|
|
|
|
hint = hint_;
|
2024-04-19 23:16:35 +02:00
|
|
|
updateWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTearingControl::updateWindow() {
|
|
|
|
if (!pWindow)
|
|
|
|
return;
|
|
|
|
|
2024-04-21 22:20:48 +02:00
|
|
|
pWindow->m_bTearingHint = hint == WP_TEARING_CONTROL_V1_PRESENTATION_HINT_ASYNC;
|
2024-04-19 23:16:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CTearingControl::good() {
|
2024-04-20 14:25:29 +02:00
|
|
|
return resource->resource();
|
2024-04-19 23:16:35 +02:00
|
|
|
}
|