Hyprland/src/protocols/GammaControl.cpp

178 lines
5.7 KiB
C++
Raw Normal View History

2024-04-22 19:21:03 +02:00
#include "GammaControl.hpp"
#include <fcntl.h>
#include <unistd.h>
#include "../helpers/Monitor.hpp"
#include "../Compositor.hpp"
#include "../protocols/core/Output.hpp"
2024-04-22 19:21:03 +02:00
2024-04-22 19:44:25 +02:00
#define LOGM PROTO::gamma->protoLog
2024-04-22 19:21:03 +02:00
CGammaControl::CGammaControl(SP<CZwlrGammaControlV1> resource_, wl_resource* output) : resource(resource_) {
if (!resource_->resource())
return;
auto OUTPUTRES = CWLOutputResource::fromResource(output);
2024-04-22 19:21:03 +02:00
if (!OUTPUTRES) {
LOGM(ERR, "No output in CGammaControl");
2024-04-22 19:21:03 +02:00
resource->sendFailed();
return;
}
pMonitor = OUTPUTRES->monitor.get();
2024-04-22 19:21:03 +02:00
if (!pMonitor) {
2024-04-22 19:44:25 +02:00
LOGM(ERR, "No CMonitor");
2024-04-22 19:21:03 +02:00
resource->sendFailed();
return;
}
for (auto& g : PROTO::gamma->m_vGammaControllers) {
if (g->pMonitor == pMonitor) {
resource->sendFailed();
return;
}
}
gammaSize = wlr_output_get_gamma_size(pMonitor->output);
2024-04-22 19:21:03 +02:00
if (gammaSize <= 0) {
2024-04-22 19:44:25 +02:00
LOGM(ERR, "Output {} doesn't support gamma", pMonitor->szName);
2024-04-22 19:21:03 +02:00
resource->sendFailed();
return;
}
gammaTable.resize(gammaSize * 3);
resource->setDestroy([this](CZwlrGammaControlV1* gamma) { PROTO::gamma->destroyGammaControl(this); });
resource->setOnDestroy([this](CZwlrGammaControlV1* gamma) { PROTO::gamma->destroyGammaControl(this); });
resource->setSetGamma([this](CZwlrGammaControlV1* gamma, int32_t fd) {
2024-04-22 19:44:25 +02:00
LOGM(LOG, "setGamma for {}", pMonitor->szName);
2024-04-22 19:21:03 +02:00
int fdFlags = fcntl(fd, F_GETFL, 0);
if (fdFlags < 0) {
2024-04-22 19:44:25 +02:00
LOGM(ERR, "Failed to get fd flags");
2024-04-22 19:21:03 +02:00
resource->sendFailed();
close(fd);
return;
}
if (fcntl(fd, F_SETFL, fdFlags | O_NONBLOCK) < 0) {
2024-04-22 19:44:25 +02:00
LOGM(ERR, "Failed to set fd flags");
2024-04-22 19:21:03 +02:00
resource->sendFailed();
close(fd);
return;
}
ssize_t readBytes = pread(fd, gammaTable.data(), gammaTable.size() * sizeof(uint16_t), 0);
if (readBytes < 0 || (size_t)readBytes != gammaTable.size() * sizeof(uint16_t)) {
2024-04-22 19:44:25 +02:00
LOGM(ERR, "Failed to read bytes");
2024-04-22 19:21:03 +02:00
close(fd);
if ((size_t)readBytes != gammaTable.size() * sizeof(uint16_t)) {
gamma->error(ZWLR_GAMMA_CONTROL_V1_ERROR_INVALID_GAMMA, "Gamma ramps size mismatch");
2024-04-22 19:21:03 +02:00
return;
}
resource->sendFailed();
return;
}
gammaTableSet = true;
close(fd);
applyToMonitor();
});
resource->sendGammaSize(gammaSize);
listeners.monitorDestroy = pMonitor->events.destroy.registerListener([this](std::any) { this->onMonitorDestroy(); });
listeners.monitorDisconnect = pMonitor->events.destroy.registerListener([this](std::any) { this->onMonitorDestroy(); });
}
CGammaControl::~CGammaControl() {
if (!gammaTableSet || !pMonitor)
return;
// reset the LUT if the client dies for whatever reason and doesn't unset the gamma
wlr_output_state_set_gamma_lut(pMonitor->state.wlr(), 0, nullptr, nullptr, nullptr);
}
bool CGammaControl::good() {
return resource->resource();
}
void CGammaControl::applyToMonitor() {
if (!pMonitor)
return; // ??
2024-04-22 19:44:25 +02:00
LOGM(LOG, "setting to monitor {}", pMonitor->szName);
2024-04-22 19:21:03 +02:00
if (!gammaTableSet) {
wlr_output_state_set_gamma_lut(pMonitor->state.wlr(), 0, nullptr, nullptr, nullptr);
return;
}
uint16_t* red = &gammaTable.at(0);
uint16_t* green = &gammaTable.at(gammaSize);
uint16_t* blue = &gammaTable.at(gammaSize * 2);
wlr_output_state_set_gamma_lut(pMonitor->state.wlr(), gammaSize, red, green, blue);
if (!pMonitor->state.test()) {
2024-04-22 19:44:25 +02:00
LOGM(LOG, "setting to monitor {} failed", pMonitor->szName);
2024-04-22 19:21:03 +02:00
wlr_output_state_set_gamma_lut(pMonitor->state.wlr(), 0, nullptr, nullptr, nullptr);
}
g_pHyprRenderer->damageMonitor(pMonitor);
}
CMonitor* CGammaControl::getMonitor() {
return pMonitor;
}
void CGammaControl::onMonitorDestroy() {
resource->sendFailed();
pMonitor = nullptr;
}
CGammaControlProtocol::CGammaControlProtocol(const wl_interface* iface, const int& ver, const std::string& name) : IWaylandProtocol(iface, ver, name) {
;
}
void CGammaControlProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) {
const auto RESOURCE = m_vManagers.emplace_back(std::make_unique<CZwlrGammaControlManagerV1>(client, ver, id)).get();
RESOURCE->setOnDestroy([this](CZwlrGammaControlManagerV1* p) { this->onManagerResourceDestroy(p->resource()); });
RESOURCE->setDestroy([this](CZwlrGammaControlManagerV1* pMgr) { this->onManagerResourceDestroy(pMgr->resource()); });
RESOURCE->setGetGammaControl([this](CZwlrGammaControlManagerV1* pMgr, uint32_t id, wl_resource* output) { this->onGetGammaControl(pMgr, id, output); });
}
void CGammaControlProtocol::onManagerResourceDestroy(wl_resource* res) {
std::erase_if(m_vManagers, [&](const auto& other) { return other->resource() == res; });
}
void CGammaControlProtocol::destroyGammaControl(CGammaControl* gamma) {
std::erase_if(m_vGammaControllers, [&](const auto& other) { return other.get() == gamma; });
}
void CGammaControlProtocol::onGetGammaControl(CZwlrGammaControlManagerV1* pMgr, uint32_t id, wl_resource* output) {
const auto CLIENT = pMgr->client();
const auto RESOURCE = m_vGammaControllers.emplace_back(std::make_unique<CGammaControl>(makeShared<CZwlrGammaControlV1>(CLIENT, pMgr->version(), id), output)).get();
2024-04-22 19:21:03 +02:00
if (!RESOURCE->good()) {
pMgr->noMemory();
2024-04-22 19:21:03 +02:00
m_vGammaControllers.pop_back();
return;
}
}
void CGammaControlProtocol::applyGammaToState(CMonitor* pMonitor) {
for (auto& g : m_vGammaControllers) {
if (g->getMonitor() != pMonitor)
continue;
g->applyToMonitor();
break;
}
}