gamma-control: Misc. clean ups and fixes
Browse files at this commit9ae3cad82b6dfe901b40510a3d6ff0eb655a11dfParent: 89dfaa65 changed files +175 -184
- Cargo.toml +1 -1
- src/backend/tty.rs +62 -48
- src/handlers/mod.rs +17 -44
- src/niri.rs +6 -8
- src/protocols/gamma_control.rs +89 -83
Cargo.tomlmodified+1 -1
| @@ -45,6 +45,7 @@ arrayvec = "0.7.4" | |||
| 45 | async-channel = { version = "2.2.0", optional = true } | 45 | async-channel = { version = "2.2.0", optional = true } |
| 46 | async-io = { version = "1.13.0", optional = true } | 46 | async-io = { version = "1.13.0", optional = true } |
| 47 | bitflags = "2.4.2" | 47 | bitflags = "2.4.2" |
| 48 | +bytemuck = "1.14.3" | ||
| 48 | calloop = { version = "0.13.0", features = ["executor", "futures-io"] } | 49 | calloop = { version = "0.13.0", features = ["executor", "futures-io"] } |
| 49 | clap = { workspace = true, features = ["string"] } | 50 | clap = { workspace = true, features = ["string"] } |
| 50 | directories = "5.0.1" | 51 | directories = "5.0.1" |
| @@ -73,7 +74,6 @@ tracy-client.workspace = true | |||
| 73 | url = { version = "2.5.0", optional = true } | 74 | url = { version = "2.5.0", optional = true } |
| 74 | xcursor = "0.3.5" | 75 | xcursor = "0.3.5" |
| 75 | zbus = { version = "~3.15.2", optional = true } | 76 | zbus = { version = "~3.15.2", optional = true } |
| 76 | -bytemuck = "1.14.3" | ||
| 77 | 77 | ||
| 78 | [dependencies.smithay] | 78 | [dependencies.smithay] |
| 79 | workspace = true | 79 | workspace = true |
src/backend/tty.rsmodified+62 -48
| @@ -1,6 +1,7 @@ | |||
| 1 | use std::cell::RefCell; | 1 | use std::cell::RefCell; |
| 2 | use std::collections::{HashMap, HashSet}; | 2 | use std::collections::{HashMap, HashSet}; |
| 3 | use std::fmt::Write; | 3 | use std::fmt::Write; |
| 4 | +use std::iter::zip; | ||
| 4 | use std::panic::{catch_unwind, AssertUnwindSafe}; | 5 | use std::panic::{catch_unwind, AssertUnwindSafe}; |
| 5 | use std::path::Path; | 6 | use std::path::Path; |
| 6 | use std::rc::Rc; | 7 | use std::rc::Rc; |
| @@ -702,6 +703,11 @@ impl Tty { | |||
| 702 | Err(err) => debug!("error setting max bpc: {err:?}"), | 703 | Err(err) => debug!("error setting max bpc: {err:?}"), |
| 703 | } | 704 | } |
| 704 | 705 | ||
| 706 | + // Reset gamma to linear in case it was set before. | ||
| 707 | + if let Err(err) = set_gamma_for_crtc(&device.drm, crtc, None) { | ||
| 708 | + debug!("error resetting gamma: {err:?}"); | ||
| 709 | + } | ||
| 710 | + | ||
| 705 | let surface = device | 711 | let surface = device |
| 706 | .drm | 712 | .drm |
| 707 | .create_surface(crtc, mode, &[connector.handle()])?; | 713 | .create_surface(crtc, mode, &[connector.handle()])?; |
| @@ -1244,63 +1250,28 @@ impl Tty { | |||
| 1244 | } | 1250 | } |
| 1245 | } | 1251 | } |
| 1246 | 1252 | ||
| 1247 | - pub fn get_gamma_length(&self, output: &Output) -> Option<u32> { | 1253 | + pub fn get_gamma_size(&self, output: &Output) -> anyhow::Result<u32> { |
| 1248 | let tty_state = output.user_data().get::<TtyOutputState>().unwrap(); | 1254 | let tty_state = output.user_data().get::<TtyOutputState>().unwrap(); |
| 1249 | - let device = self.devices.get(&tty_state.node)?; | 1255 | + let crtc = tty_state.crtc; |
| 1250 | - let crtc_info = device.drm.get_crtc(tty_state.crtc.clone()).ok()?; | ||
| 1251 | - return Some(crtc_info.gamma_length()); | ||
| 1252 | - } | ||
| 1253 | 1256 | ||
| 1254 | - pub fn get_gamma(&self, output: &Output) -> Option<Vec<u16>> { | 1257 | + let device = self |
| 1255 | - let tty_state = output.user_data().get::<TtyOutputState>().unwrap(); | 1258 | + .devices |
| 1256 | - let device = self.devices.get(&tty_state.node)?; | 1259 | + .get(&tty_state.node) |
| 1257 | - let crtc_info = device.drm.get_crtc(tty_state.crtc.clone()).ok()?; | 1260 | + .context("missing device")?; |
| 1258 | - let crtc = tty_state.crtc.clone(); | 1261 | + let info = device |
| 1259 | - let gamma_length = crtc_info.gamma_length() as usize; | 1262 | + .drm |
| 1260 | - let mut red = vec![0; gamma_length]; | 1263 | + .get_crtc(crtc) |
| 1261 | - let mut green = vec![0; gamma_length]; | 1264 | + .context("error getting crtc info")?; |
| 1262 | - let mut blue = vec![0; gamma_length]; | 1265 | + Ok(info.gamma_length()) |
| 1263 | - if let Err(err) = device.drm.get_gamma(crtc, &mut red, &mut green, &mut blue) { | ||
| 1264 | - warn!("error getting gamma for crtc {crtc:?}: {err:?}"); | ||
| 1265 | - return None; | ||
| 1266 | - } | ||
| 1267 | - red.extend(green); | ||
| 1268 | - red.extend(blue); | ||
| 1269 | - return Some(red); | ||
| 1270 | } | 1266 | } |
| 1271 | 1267 | ||
| 1272 | - pub fn set_gamma( | 1268 | + pub fn set_gamma(&self, output: &Output, ramp: Option<&[u16]>) -> anyhow::Result<()> { |
| 1273 | - &self, | ||
| 1274 | - niri: &mut Niri, | ||
| 1275 | - output: &Output, | ||
| 1276 | - ramp: Vec<u16>, | ||
| 1277 | - ) -> anyhow::Result<()> { | ||
| 1278 | let tty_state = output.user_data().get::<TtyOutputState>().unwrap(); | 1269 | let tty_state = output.user_data().get::<TtyOutputState>().unwrap(); |
| 1279 | let device = self | 1270 | let device = self |
| 1280 | .devices | 1271 | .devices |
| 1281 | .get(&tty_state.node) | 1272 | .get(&tty_state.node) |
| 1282 | .context("missing device")?; | 1273 | .context("missing device")?; |
| 1283 | - | 1274 | + set_gamma_for_crtc(&device.drm, tty_state.crtc, ramp) |
| 1284 | - let crtc_info = device.drm.get_crtc(tty_state.crtc.clone())?; | ||
| 1285 | - let crtc = tty_state.crtc.clone(); | ||
| 1286 | - let gamma_length = crtc_info.gamma_length() as usize; | ||
| 1287 | - | ||
| 1288 | - ensure!(ramp.len() == gamma_length * 3, "wrong gamma length"); | ||
| 1289 | - let mut red = ramp.clone(); | ||
| 1290 | - red.truncate(gamma_length); | ||
| 1291 | - let mut green = ramp.clone(); | ||
| 1292 | - green.drain(0..gamma_length); | ||
| 1293 | - green.truncate(gamma_length); | ||
| 1294 | - let mut blue = ramp; | ||
| 1295 | - blue.drain(0..gamma_length * 2); | ||
| 1296 | - blue.truncate(gamma_length); | ||
| 1297 | - | ||
| 1298 | - device | ||
| 1299 | - .drm | ||
| 1300 | - .set_gamma(crtc, &mut red, &mut green, &mut blue) | ||
| 1301 | - .context("error setting gamma")?; | ||
| 1302 | - niri.queue_redraw(output.clone()); | ||
| 1303 | - Ok(()) | ||
| 1304 | } | 1275 | } |
| 1305 | 1276 | ||
| 1306 | fn refresh_ipc_outputs(&self) { | 1277 | fn refresh_ipc_outputs(&self) { |
| @@ -1870,6 +1841,49 @@ fn set_max_bpc(device: &DrmDevice, connector: connector::Handle, bpc: u64) -> an | |||
| 1870 | Err(anyhow!("couldn't find max bpc property")) | 1841 | Err(anyhow!("couldn't find max bpc property")) |
| 1871 | } | 1842 | } |
| 1872 | 1843 | ||
| 1844 | +pub fn set_gamma_for_crtc( | ||
| 1845 | + device: &DrmDevice, | ||
| 1846 | + crtc: crtc::Handle, | ||
| 1847 | + ramp: Option<&[u16]>, | ||
| 1848 | +) -> anyhow::Result<()> { | ||
| 1849 | + let _span = tracy_client::span!("set_gamma_for_crtc"); | ||
| 1850 | + | ||
| 1851 | + let info = device.get_crtc(crtc).context("error getting crtc info")?; | ||
| 1852 | + let gamma_length = info.gamma_length() as usize; | ||
| 1853 | + | ||
| 1854 | + let mut temp; | ||
| 1855 | + let ramp = if let Some(ramp) = ramp { | ||
| 1856 | + ensure!(ramp.len() == gamma_length * 3, "wrong gamma length"); | ||
| 1857 | + ramp | ||
| 1858 | + } else { | ||
| 1859 | + let _span = tracy_client::span!("generate linear gamma"); | ||
| 1860 | + | ||
| 1861 | + // The legacy API provides no way to reset the gamma, so set a linear one manually. | ||
| 1862 | + temp = vec![0u16; gamma_length * 3]; | ||
| 1863 | + | ||
| 1864 | + let (red, rest) = temp.split_at_mut(gamma_length); | ||
| 1865 | + let (green, blue) = rest.split_at_mut(gamma_length); | ||
| 1866 | + let denom = gamma_length as u64 - 1; | ||
| 1867 | + for (i, ((r, g), b)) in zip(zip(red, green), blue).enumerate() { | ||
| 1868 | + let value = (0xFFFFu64 * i as u64 / denom) as u16; | ||
| 1869 | + *r = value; | ||
| 1870 | + *g = value; | ||
| 1871 | + *b = value; | ||
| 1872 | + } | ||
| 1873 | + | ||
| 1874 | + &temp | ||
| 1875 | + }; | ||
| 1876 | + | ||
| 1877 | + let (red, ramp) = ramp.split_at(gamma_length); | ||
| 1878 | + let (green, blue) = ramp.split_at(gamma_length); | ||
| 1879 | + | ||
| 1880 | + device | ||
| 1881 | + .set_gamma(crtc, red, green, blue) | ||
| 1882 | + .context("error setting gamma")?; | ||
| 1883 | + | ||
| 1884 | + Ok(()) | ||
| 1885 | +} | ||
| 1886 | + | ||
| 1873 | #[cfg(test)] | 1887 | #[cfg(test)] |
| 1874 | mod tests { | 1888 | mod tests { |
| 1875 | use super::*; | 1889 | use super::*; |
src/handlers/mod.rsmodified+17 -44
| @@ -8,7 +8,6 @@ use std::os::fd::OwnedFd; | |||
| 8 | use std::sync::Arc; | 8 | use std::sync::Arc; |
| 9 | use std::thread; | 9 | use std::thread; |
| 10 | 10 | ||
| 11 | -use anyhow::anyhow; | ||
| 12 | use smithay::backend::allocator::dmabuf::Dmabuf; | 11 | use smithay::backend::allocator::dmabuf::Dmabuf; |
| 13 | use smithay::backend::drm::DrmNode; | 12 | use smithay::backend::drm::DrmNode; |
| 14 | use smithay::desktop::{PopupKind, PopupManager}; | 13 | use smithay::desktop::{PopupKind, PopupManager}; |
| @@ -16,7 +15,6 @@ use smithay::input::pointer::{CursorIcon, CursorImageStatus, PointerHandle}; | |||
| 16 | use smithay::input::{keyboard, Seat, SeatHandler, SeatState}; | 15 | use smithay::input::{keyboard, Seat, SeatHandler, SeatState}; |
| 17 | use smithay::output::Output; | 16 | use smithay::output::Output; |
| 18 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; | 17 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; |
| 19 | -use smithay::reexports::wayland_server::backend::ObjectId; | ||
| 20 | use smithay::reexports::wayland_server::protocol::wl_data_source::WlDataSource; | 18 | use smithay::reexports::wayland_server::protocol::wl_data_source::WlDataSource; |
| 21 | use smithay::reexports::wayland_server::protocol::wl_output::WlOutput; | 19 | use smithay::reexports::wayland_server::protocol::wl_output::WlOutput; |
| 22 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; | 20 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| @@ -449,52 +447,27 @@ impl GammaControlHandler for State { | |||
| 449 | &mut self.niri.gamma_control_manager_state | 447 | &mut self.niri.gamma_control_manager_state |
| 450 | } | 448 | } |
| 451 | 449 | ||
| 452 | - fn set_gamma( | 450 | + fn get_gamma_size(&mut self, output: &Output) -> Option<u32> { |
| 453 | - &mut self, | 451 | + match self.backend.tty().get_gamma_size(output) { |
| 454 | - wl_output: &WlOutput, | 452 | + Ok(size) => Some(size), |
| 455 | - ramp: Vec<u16>, | 453 | + Err(err) => { |
| 456 | - gamma_size: u32, | 454 | + warn!( |
| 457 | - ) -> anyhow::Result<()> { | 455 | + "error getting gamma size for output {}: {err:?}", |
| 458 | - if ramp.len() != gamma_size as usize * 3 { | 456 | + output.name() |
| 459 | - error!( | 457 | + ); |
| 460 | - "gamma length wrong (expected {}, got {})", | 458 | + None |
| 461 | - gamma_size, | 459 | + } |
| 462 | - ramp.len() | ||
| 463 | - ); | ||
| 464 | - return Err(anyhow!("Gamma length wrong")); | ||
| 465 | - } | ||
| 466 | - | ||
| 467 | - let Some(output) = Output::from_resource(wl_output) else { | ||
| 468 | - return Err(anyhow!("No Output matching WlOutput")); | ||
| 469 | - }; | ||
| 470 | - | ||
| 471 | - self.backend.tty().set_gamma(&mut self.niri, &output, ramp) | ||
| 472 | - } | ||
| 473 | - | ||
| 474 | - fn get_gamma(&mut self, wl_output: &WlOutput) -> Option<Vec<u16>> { | ||
| 475 | - let output = Output::from_resource(wl_output)?; | ||
| 476 | - | ||
| 477 | - let gamma_ramp = self.backend.tty().get_gamma(&output); | ||
| 478 | - if gamma_ramp.is_none() { | ||
| 479 | - warn!("Failed to get gamma ramp"); | ||
| 480 | } | 460 | } |
| 481 | - gamma_ramp | ||
| 482 | } | 461 | } |
| 483 | 462 | ||
| 484 | - fn destroy(&mut self, output_id: ObjectId) { | 463 | + fn set_gamma(&mut self, output: &Output, ramp: Option<&[u16]>) -> Option<()> { |
| 485 | - self.niri | 464 | + match self.backend.tty().set_gamma(output, ramp) { |
| 486 | - .gamma_control_manager_state | 465 | + Ok(()) => Some(()), |
| 487 | - .destroy_gamma_control(output_id) | 466 | + Err(err) => { |
| 488 | - } | 467 | + warn!("error setting gamma for output {}: {err:?}", output.name()); |
| 489 | - | 468 | + None |
| 490 | - fn get_gamma_size(&mut self, wl_output: &WlOutput) -> Option<u32> { | 469 | + } |
| 491 | - let output = Output::from_resource(wl_output)?; | ||
| 492 | - let gamma_size = self.backend.tty().get_gamma_length(&output); | ||
| 493 | - if gamma_size.is_none() { | ||
| 494 | - warn!("Failed to get gamma size"); | ||
| 495 | } | 470 | } |
| 496 | - gamma_size | ||
| 497 | } | 471 | } |
| 498 | } | 472 | } |
| 499 | - | ||
| 500 | delegate_gamma_control!(State); | 473 | delegate_gamma_control!(State); |
src/niri.rsmodified+6 -8
| @@ -931,14 +931,11 @@ impl Niri { | |||
| 931 | let viewporter_state = ViewporterState::new::<State>(&display_handle); | 931 | let viewporter_state = ViewporterState::new::<State>(&display_handle); |
| 932 | let xdg_foreign_state = XdgForeignState::new::<State>(&display_handle); | 932 | let xdg_foreign_state = XdgForeignState::new::<State>(&display_handle); |
| 933 | 933 | ||
| 934 | - let gamma_control_manager_state = GammaControlManagerState::new::<State, _>( | 934 | + let is_tty = matches!(backend, Backend::Tty(_)); |
| 935 | - &display_handle, | 935 | + let gamma_control_manager_state = |
| 936 | - match backend { | 936 | + GammaControlManagerState::new::<State, _>(&display_handle, move |client| { |
| 937 | - Backend::Tty(_) => true, | 937 | + is_tty && !client.get_data::<ClientState>().unwrap().restricted |
| 938 | - _ => false, | 938 | + }); |
| 939 | - }, | ||
| 940 | - |client| !client.get_data::<ClientState>().unwrap().restricted, | ||
| 941 | - ); | ||
| 942 | 939 | ||
| 943 | let mut seat: Seat<State> = seat_state.new_wl_seat(&display_handle, backend.seat_name()); | 940 | let mut seat: Seat<State> = seat_state.new_wl_seat(&display_handle, backend.seat_name()); |
| 944 | seat.add_keyboard( | 941 | seat.add_keyboard( |
| @@ -1326,6 +1323,7 @@ impl Niri { | |||
| 1326 | self.layout.remove_output(output); | 1323 | self.layout.remove_output(output); |
| 1327 | self.global_space.unmap_output(output); | 1324 | self.global_space.unmap_output(output); |
| 1328 | self.reposition_outputs(None); | 1325 | self.reposition_outputs(None); |
| 1326 | + self.gamma_control_manager_state.output_removed(output); | ||
| 1329 | 1327 | ||
| 1330 | let state = self.output_state.remove(output).unwrap(); | 1328 | let state = self.output_state.remove(output).unwrap(); |
| 1331 | self.output_by_name.remove(&output.name()).unwrap(); | 1329 | self.output_by_name.remove(&output.name()).unwrap(); |
src/protocols/gamma_control.rsmodified+89 -83
| @@ -2,9 +2,9 @@ use std::collections::HashMap; | |||
| 2 | use std::fs::File; | 2 | use std::fs::File; |
| 3 | use std::io::Read; | 3 | use std::io::Read; |
| 4 | 4 | ||
| 5 | +use smithay::output::Output; | ||
| 5 | use smithay::reexports::wayland_protocols_wlr; | 6 | use smithay::reexports::wayland_protocols_wlr; |
| 6 | -use smithay::reexports::wayland_server::backend::{ClientId, ObjectId}; | 7 | +use smithay::reexports::wayland_server::backend::ClientId; |
| 7 | -use smithay::reexports::wayland_server::protocol::wl_output::WlOutput; | ||
| 8 | use smithay::reexports::wayland_server::{ | 8 | use smithay::reexports::wayland_server::{ |
| 9 | Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New, Resource, | 9 | Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New, Resource, |
| 10 | }; | 10 | }; |
| @@ -17,36 +17,26 @@ use zwlr_gamma_control_v1::ZwlrGammaControlV1; | |||
| 17 | const VERSION: u32 = 1; | 17 | const VERSION: u32 = 1; |
| 18 | 18 | ||
| 19 | pub struct GammaControlManagerState { | 19 | pub struct GammaControlManagerState { |
| 20 | - gamma_controls: HashMap<WlOutput, ZwlrGammaControlV1>, | 20 | + // Active gamma controls only. Failed ones are removed. |
| 21 | + gamma_controls: HashMap<Output, ZwlrGammaControlV1>, | ||
| 21 | } | 22 | } |
| 22 | 23 | ||
| 23 | pub struct GammaControlManagerGlobalData { | 24 | pub struct GammaControlManagerGlobalData { |
| 24 | filter: Box<dyn for<'c> Fn(&'c Client) -> bool + Send + Sync>, | 25 | filter: Box<dyn for<'c> Fn(&'c Client) -> bool + Send + Sync>, |
| 25 | - can_view: bool, | ||
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | pub trait GammaControlHandler { | 28 | pub trait GammaControlHandler { |
| 29 | fn gamma_control_manager_state(&mut self) -> &mut GammaControlManagerState; | 29 | fn gamma_control_manager_state(&mut self) -> &mut GammaControlManagerState; |
| 30 | - fn set_gamma( | 30 | + fn get_gamma_size(&mut self, output: &Output) -> Option<u32>; |
| 31 | - &mut self, | 31 | + fn set_gamma(&mut self, output: &Output, ramp: Option<&[u16]>) -> Option<()>; |
| 32 | - output: &WlOutput, | ||
| 33 | - ramp: Vec<u16>, | ||
| 34 | - gamma_size: u32, | ||
| 35 | - ) -> anyhow::Result<()>; | ||
| 36 | - fn get_gamma(&mut self, output: &WlOutput) -> Option<Vec<u16>>; | ||
| 37 | - fn destroy(&mut self, output_id: ObjectId); | ||
| 38 | - fn get_gamma_size(&mut self, output: &WlOutput) -> Option<u32>; | ||
| 39 | } | 32 | } |
| 40 | 33 | ||
| 41 | pub struct GammaControlState { | 34 | pub struct GammaControlState { |
| 42 | - gamma_size: Option<u32>, | 35 | + gamma_size: u32, |
| 43 | - previous_gamma_ramp: Option<Vec<u16>>, | ||
| 44 | - output: WlOutput, | ||
| 45 | - failed: bool, | ||
| 46 | } | 36 | } |
| 47 | 37 | ||
| 48 | impl GammaControlManagerState { | 38 | impl GammaControlManagerState { |
| 49 | - pub fn new<D, F>(display: &DisplayHandle, can_view: bool, filter: F) -> Self | 39 | + pub fn new<D, F>(display: &DisplayHandle, filter: F) -> Self |
| 50 | where | 40 | where |
| 51 | D: GlobalDispatch<ZwlrGammaControlManagerV1, GammaControlManagerGlobalData>, | 41 | D: GlobalDispatch<ZwlrGammaControlManagerV1, GammaControlManagerGlobalData>, |
| 52 | D: Dispatch<ZwlrGammaControlManagerV1, ()>, | 42 | D: Dispatch<ZwlrGammaControlManagerV1, ()>, |
| @@ -57,7 +47,6 @@ impl GammaControlManagerState { | |||
| 57 | { | 47 | { |
| 58 | let global_data = GammaControlManagerGlobalData { | 48 | let global_data = GammaControlManagerGlobalData { |
| 59 | filter: Box::new(filter), | 49 | filter: Box::new(filter), |
| 60 | - can_view, | ||
| 61 | }; | 50 | }; |
| 62 | display.create_global::<D, ZwlrGammaControlManagerV1, _>(VERSION, global_data); | 51 | display.create_global::<D, ZwlrGammaControlManagerV1, _>(VERSION, global_data); |
| 63 | 52 | ||
| @@ -65,8 +54,11 @@ impl GammaControlManagerState { | |||
| 65 | gamma_controls: HashMap::new(), | 54 | gamma_controls: HashMap::new(), |
| 66 | } | 55 | } |
| 67 | } | 56 | } |
| 68 | - pub fn destroy_gamma_control(&mut self, output_id: ObjectId) { | 57 | + |
| 69 | - self.gamma_controls.remove(&output_id); | 58 | + pub fn output_removed(&mut self, output: &Output) { |
| 59 | + if let Some(gamma_control) = self.gamma_controls.remove(output) { | ||
| 60 | + gamma_control.failed(); | ||
| 61 | + } | ||
| 70 | } | 62 | } |
| 71 | } | 63 | } |
| 72 | 64 | ||
| @@ -91,7 +83,7 @@ where | |||
| 91 | } | 83 | } |
| 92 | 84 | ||
| 93 | fn can_view(client: Client, global_data: &GammaControlManagerGlobalData) -> bool { | 85 | fn can_view(client: Client, global_data: &GammaControlManagerGlobalData) -> bool { |
| 94 | - global_data.can_view && (global_data.filter)(&client) | 86 | + (global_data.filter)(&client) |
| 95 | } | 87 | } |
| 96 | } | 88 | } |
| 97 | 89 | ||
| @@ -113,45 +105,30 @@ where | |||
| 113 | ) { | 105 | ) { |
| 114 | match request { | 106 | match request { |
| 115 | zwlr_gamma_control_manager_v1::Request::GetGammaControl { id, output } => { | 107 | zwlr_gamma_control_manager_v1::Request::GetGammaControl { id, output } => { |
| 116 | - let gamma_size = state.get_gamma_size(&output); | 108 | + if let Some(output) = Output::from_resource(&output) { |
| 117 | - let previous_gamma_ramp = state.get_gamma(&output); | 109 | + // We borrow state in the middle. |
| 118 | - | 110 | + #[allow(clippy::map_entry)] |
| 119 | - if state | 111 | + if !state |
| 120 | - .gamma_control_manager_state() | 112 | + .gamma_control_manager_state() |
| 121 | - .gamma_controls | 113 | + .gamma_controls |
| 122 | - .contains_key(&output) | 114 | + .contains_key(&output) |
| 123 | - || gamma_size.is_none() | 115 | + { |
| 124 | - || previous_gamma_ramp.is_none() | 116 | + if let Some(gamma_size) = state.get_gamma_size(&output) { |
| 125 | - { | 117 | + let zwlr_gamma_control = |
| 126 | - data_init | 118 | + data_init.init(id, GammaControlState { gamma_size }); |
| 127 | - .init( | 119 | + zwlr_gamma_control.gamma_size(gamma_size); |
| 128 | - id, | 120 | + state |
| 129 | - GammaControlState { | 121 | + .gamma_control_manager_state() |
| 130 | - gamma_size: gamma_size.clone(), | 122 | + .gamma_controls |
| 131 | - previous_gamma_ramp: None, | 123 | + .insert(output, zwlr_gamma_control); |
| 132 | - output: output.clone(), | 124 | + return; |
| 133 | - failed: true, | 125 | + } |
| 134 | - }, | 126 | + } |
| 135 | - ) | ||
| 136 | - .failed(); | ||
| 137 | - return; | ||
| 138 | } | 127 | } |
| 139 | 128 | ||
| 140 | - let zwlr_gamma_control = data_init.init( | 129 | + data_init |
| 141 | - id, | 130 | + .init(id, GammaControlState { gamma_size: 0 }) |
| 142 | - GammaControlState { | 131 | + .failed(); |
| 143 | - gamma_size: gamma_size.clone(), | ||
| 144 | - previous_gamma_ramp, | ||
| 145 | - output: output.clone(), | ||
| 146 | - failed: false, | ||
| 147 | - }, | ||
| 148 | - ); | ||
| 149 | - | ||
| 150 | - zwlr_gamma_control.gamma_size(gamma_size.unwrap()); | ||
| 151 | - state | ||
| 152 | - .gamma_control_manager_state() | ||
| 153 | - .gamma_controls | ||
| 154 | - .insert(output, zwlr_gamma_control); | ||
| 155 | } | 132 | } |
| 156 | zwlr_gamma_control_manager_v1::Request::Destroy => (), | 133 | zwlr_gamma_control_manager_v1::Request::Destroy => (), |
| 157 | _ => unreachable!(), | 134 | _ => unreachable!(), |
| @@ -176,24 +153,55 @@ where | |||
| 176 | ) { | 153 | ) { |
| 177 | match request { | 154 | match request { |
| 178 | zwlr_gamma_control_v1::Request::SetGamma { fd } => { | 155 | zwlr_gamma_control_v1::Request::SetGamma { fd } => { |
| 179 | - if data.failed { | 156 | + let gamma_controls = &mut state.gamma_control_manager_state().gamma_controls; |
| 157 | + let Some((output, _)) = gamma_controls.iter().find(|(_, x)| *x == resource) else { | ||
| 180 | return; | 158 | return; |
| 181 | - } | 159 | + }; |
| 182 | - debug!("setting gamma for output {:?}", data.output); | 160 | + let output = output.clone(); |
| 183 | - let buf = &mut Vec::new(); | 161 | + |
| 184 | - if File::from(fd).read_to_end(buf).is_err() { | 162 | + trace!("setting gamma for output {}", output.name()); |
| 185 | - warn!("failed to read gamma data for output {:?}", data.output); | ||
| 186 | - resource.failed(); | ||
| 187 | - return; | ||
| 188 | - } | ||
| 189 | 163 | ||
| 190 | - let gamma = bytemuck::cast_slice(buf).to_vec(); | 164 | + // Start with a u16 slice so it's aligned correctly. |
| 191 | - let gamma_size = data.gamma_size.unwrap(); | 165 | + let mut buf = vec![0u16; data.gamma_size as usize * 3]; |
| 166 | + let buf = bytemuck::cast_slice_mut(&mut buf); | ||
| 167 | + let mut file = File::from(fd); | ||
| 168 | + { | ||
| 169 | + let _span = tracy_client::span!("read gamma from fd"); | ||
| 170 | + | ||
| 171 | + if let Err(err) = file.read_exact(buf) { | ||
| 172 | + warn!("failed to read gamma data: {err:?}"); | ||
| 173 | + resource.failed(); | ||
| 174 | + gamma_controls.remove(&output); | ||
| 175 | + let _ = state.set_gamma(&output, None); | ||
| 176 | + return; | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + // Verify that there's no more data. | ||
| 180 | + match file.read(&mut [0]) { | ||
| 181 | + Ok(0) => (), | ||
| 182 | + Ok(_) => { | ||
| 183 | + warn!("gamma data is too large"); | ||
| 184 | + resource.failed(); | ||
| 185 | + gamma_controls.remove(&output); | ||
| 186 | + let _ = state.set_gamma(&output, None); | ||
| 187 | + return; | ||
| 188 | + } | ||
| 189 | + Err(err) => { | ||
| 190 | + warn!("error reading gamma data: {err:?}"); | ||
| 191 | + resource.failed(); | ||
| 192 | + gamma_controls.remove(&output); | ||
| 193 | + let _ = state.set_gamma(&output, None); | ||
| 194 | + return; | ||
| 195 | + } | ||
| 196 | + } | ||
| 197 | + } | ||
| 198 | + let gamma = bytemuck::cast_slice(buf); | ||
| 192 | 199 | ||
| 193 | - if let Err(err) = state.set_gamma(&data.output, gamma, gamma_size) { | 200 | + if state.set_gamma(&output, Some(gamma)).is_none() { |
| 194 | - warn!("error setting gamma: {err:?}"); | ||
| 195 | resource.failed(); | 201 | resource.failed(); |
| 196 | - return; | 202 | + let gamma_controls = &mut state.gamma_control_manager_state().gamma_controls; |
| 203 | + gamma_controls.remove(&output); | ||
| 204 | + let _ = state.set_gamma(&output, None); | ||
| 197 | } | 205 | } |
| 198 | } | 206 | } |
| 199 | zwlr_gamma_control_v1::Request::Destroy => (), | 207 | zwlr_gamma_control_v1::Request::Destroy => (), |
| @@ -204,19 +212,17 @@ where | |||
| 204 | fn destroyed( | 212 | fn destroyed( |
| 205 | state: &mut D, | 213 | state: &mut D, |
| 206 | _client: ClientId, | 214 | _client: ClientId, |
| 207 | - _resource: &ZwlrGammaControlV1, | 215 | + resource: &ZwlrGammaControlV1, |
| 208 | - data: &GammaControlState, | 216 | + _data: &GammaControlState, |
| 209 | ) { | 217 | ) { |
| 210 | - if data.failed { | 218 | + let gamma_controls = &mut state.gamma_control_manager_state().gamma_controls; |
| 219 | + let Some((output, _)) = gamma_controls.iter().find(|(_, x)| *x == resource) else { | ||
| 211 | return; | 220 | return; |
| 212 | - } | 221 | + }; |
| 213 | - let ramp = data.previous_gamma_ramp.as_ref().unwrap(); | 222 | + let output = output.clone(); |
| 214 | - | 223 | + gamma_controls.remove(&output); |
| 215 | - if let Err(err) = state.set_gamma(&data.output, ramp.to_vec(), data.gamma_size.unwrap()) { | ||
| 216 | - warn!("error resetting gamma: {err:?}"); | ||
| 217 | - } | ||
| 218 | 224 | ||
| 219 | - state.destroy(data.output.id()); | 225 | + let _ = state.set_gamma(&output, None); |
| 220 | } | 226 | } |
| 221 | } | 227 | } |
| 222 | 228 | ||