1
0
Fork 0
forked from wry/wry

implement wlr-gamma-control-unstable-v1

This commit is contained in:
khyperia 2026-01-24 20:26:51 +01:00 committed by Julian Orth
parent 11b3f08514
commit b1db715a90
26 changed files with 475 additions and 21 deletions

View file

@ -0,0 +1,115 @@
use {
crate::{
client::{CAP_GAMMA_CONTROL_MANAGER, Client, ClientCaps, ClientError},
globals::{Global, GlobalName},
ifs::zwlr_gamma_control_v1::*,
leaks::Tracker,
object::{Object, Version},
wire::{ZwlrGammaControlManagerV1Id, zwlr_gamma_control_manager_v1::*},
},
std::rc::Rc,
thiserror::Error,
};
pub struct ZwlrGammaControlManagerV1Global {
name: GlobalName,
}
impl ZwlrGammaControlManagerV1Global {
pub fn new(name: GlobalName) -> Self {
Self { name }
}
fn bind_(
self: Rc<Self>,
id: ZwlrGammaControlManagerV1Id,
client: &Rc<Client>,
version: Version,
) -> Result<(), ZwlrGammaControlManagerV1Error> {
let obj = Rc::new(ZwlrGammaControlManagerV1 {
id,
client: client.clone(),
tracker: Default::default(),
version,
});
track!(client, obj);
client.add_client_obj(&obj)?;
Ok(())
}
}
global_base!(
ZwlrGammaControlManagerV1Global,
ZwlrGammaControlManagerV1,
ZwlrGammaControlManagerV1Error
);
simple_add_global!(ZwlrGammaControlManagerV1Global);
impl Global for ZwlrGammaControlManagerV1Global {
fn singleton(&self) -> bool {
true
}
fn version(&self) -> u32 {
1
}
fn required_caps(&self) -> ClientCaps {
CAP_GAMMA_CONTROL_MANAGER
}
}
pub struct ZwlrGammaControlManagerV1 {
pub id: ZwlrGammaControlManagerV1Id,
pub client: Rc<Client>,
pub tracker: Tracker<Self>,
pub version: Version,
}
impl ZwlrGammaControlManagerV1RequestHandler for ZwlrGammaControlManagerV1 {
type Error = ZwlrGammaControlManagerV1Error;
fn get_gamma_control(&self, req: GetGammaControl, slf: &Rc<Self>) -> Result<(), Self::Error> {
let output = self.client.lookup(req.output)?.global.clone();
let p = Rc::new(ZwlrGammaControlV1::new(req.id, slf, output.clone()));
track!(self.client, p);
self.client.add_client_obj(&p)?;
let Some(size) = p.gamma_lut_size() else {
p.send_failed();
return Ok(());
};
let Some(node) = output.node() else {
p.send_failed();
return Ok(());
};
if node.active_zwlr_gamma_control.is_some() {
p.send_failed();
return Ok(());
}
p.send_gamma_size(size);
node.active_zwlr_gamma_control.set(Some(p));
Ok(())
}
fn destroy(&self, _req: Destroy, _slf: &Rc<Self>) -> Result<(), Self::Error> {
self.client.remove_obj(self)?;
Ok(())
}
}
object_base! {
self = ZwlrGammaControlManagerV1;
version = self.version;
}
impl Object for ZwlrGammaControlManagerV1 {}
simple_add_obj!(ZwlrGammaControlManagerV1);
#[derive(Debug, Error)]
pub enum ZwlrGammaControlManagerV1Error {
#[error(transparent)]
ClientError(Box<ClientError>),
}
efrom!(ZwlrGammaControlManagerV1Error, ClientError);

View file

@ -0,0 +1,166 @@
use {
crate::{
backend::{BackendGammaLut, BackendGammaLutElement},
client::{Client, ClientError, ClientId},
clientmem::{ClientMem, ClientMemError},
ifs::{
wl_output::OutputGlobalOpt, zwlr_gamma_control_manager_v1::ZwlrGammaControlManagerV1,
},
leaks::Tracker,
object::{Object, Version},
wire::{ZwlrGammaControlV1Id, zwlr_gamma_control_v1::*},
},
std::rc::Rc,
thiserror::Error,
};
pub struct ZwlrGammaControlV1 {
id: ZwlrGammaControlV1Id,
client: Rc<Client>,
version: Version,
output: Rc<OutputGlobalOpt>,
pub tracker: Tracker<Self>,
}
impl ZwlrGammaControlV1 {
pub fn new(
id: ZwlrGammaControlV1Id,
manager: &Rc<ZwlrGammaControlManagerV1>,
output: Rc<OutputGlobalOpt>,
) -> Self {
Self {
id,
client: manager.client.clone(),
version: manager.version,
output,
tracker: Default::default(),
}
}
pub fn id(&self) -> (ClientId, ZwlrGammaControlV1Id) {
(self.client.id, self.id)
}
pub fn send_gamma_size(&self, size: u32) {
self.client.event(GammaSize {
self_id: self.id,
size,
});
}
pub fn send_failed(&self) {
self.client.event(Failed { self_id: self.id });
}
pub fn gamma_lut_size(&self) -> Option<u32> {
self.output
.node()
.and_then(|node| node.global.connector.connector.gamma_lut_size())
}
fn detach(&self) {
if let Some(node) = self.output.node()
&& let Some(active_zwlr_gamma_control) = node.active_zwlr_gamma_control.get()
&& active_zwlr_gamma_control.id() == self.id()
{
node.active_zwlr_gamma_control.set(None);
let _ = node.set_gamma_lut(None);
}
}
}
// Wayland's LUT is ([red], [green], [blue]). DRM's LUT is [(red, green, blue, _)]. Both are u16.
fn wayland_gamma_lut_to_drm_gamma_lut(data: &[u16]) -> Vec<BackendGammaLutElement> {
let elem_count = data.len() / 3;
let (red, rest) = data.split_at(elem_count);
let (green, blue) = rest.split_at(elem_count);
red.iter()
.copied()
.zip(green.iter().copied())
.zip(blue.iter().copied())
.map(|((red, green), blue)| BackendGammaLutElement {
red,
green,
blue,
reserved: 0,
})
.collect()
}
impl ZwlrGammaControlV1RequestHandler for ZwlrGammaControlV1 {
type Error = ZwlrGammaControlV1Error;
fn set_gamma(&self, req: SetGamma, _slf: &Rc<Self>) -> Result<(), Self::Error> {
let fail = || {
self.detach();
self.send_failed();
};
let Some(node) = self.output.node() else {
return Ok(());
};
// if the active gamma control isn't us, that implies we are not valid, and have already
// sent a failed event
if node.active_zwlr_gamma_control.get().map(|v| v.id()) != Some(self.id()) {
return Ok(());
}
let Some(gamma_lut_size) = self.gamma_lut_size() else {
fail();
return Ok(());
};
// 3 color channels
let data_size = gamma_lut_size * 3;
let mut gamma_lut = vec![];
Rc::new(ClientMem::new_private(
&req.fd,
(2 * data_size) as _,
true,
Some(&self.client),
None,
)?)
.offset(0)
.read(&mut gamma_lut)?;
let gamma_lut = &gamma_lut[..data_size as _];
let gamma_lut = wayland_gamma_lut_to_drm_gamma_lut(gamma_lut);
let gamma_lut = Rc::new(BackendGammaLut::new(gamma_lut));
if node.set_gamma_lut(Some(gamma_lut)).is_err() {
fail();
return Ok(());
}
Ok(())
}
fn destroy(&self, _req: Destroy, _slf: &Rc<Self>) -> Result<(), Self::Error> {
self.detach();
self.client.remove_obj(self)?;
Ok(())
}
}
object_base! {
self = ZwlrGammaControlV1;
version = self.version;
}
impl Object for ZwlrGammaControlV1 {
fn break_loops(&self) {
self.detach();
}
}
simple_add_obj!(ZwlrGammaControlV1);
#[derive(Debug, Error)]
pub enum ZwlrGammaControlV1Error {
#[error(transparent)]
ClientError(Box<ClientError>),
#[error(transparent)]
CLientMemError(#[from] ClientMemError),
}
efrom!(ZwlrGammaControlV1Error, ClientError);