Adds support for wlr_gamma_control_unstable_v1 protocol
Browse files at this commit89dfaa6cac128887083c4e2fc195864c8de7d5faParent: f6ffe8b7 changed files +372 -2
- Cargo.lock +1 -0
- Cargo.toml +1 -0
- src/backend/tty.rs +60 -1
- src/handlers/mod.rs +59 -1
- src/niri.rs +12 -0
- src/protocols/gamma_control.rs +238 -0
- src/protocols/mod.rs +1 -0
Cargo.lockmodified+1 -0
| @@ -2119,6 +2119,7 @@ dependencies = [ | |||
| 2119 | "async-channel", | 2119 | "async-channel", |
| 2120 | "async-io 1.13.0", | 2120 | "async-io 1.13.0", |
| 2121 | "bitflags 2.4.2", | 2121 | "bitflags 2.4.2", |
| 2122 | + "bytemuck", | ||
| 2122 | "calloop 0.13.0", | 2123 | "calloop 0.13.0", |
| 2123 | "clap", | 2124 | "clap", |
| 2124 | "directories", | 2125 | "directories", |
Cargo.tomlmodified+1 -0
| @@ -73,6 +73,7 @@ tracy-client.workspace = true | |||
| 73 | url = { version = "2.5.0", optional = true } | 73 | url = { version = "2.5.0", optional = true } |
| 74 | xcursor = "0.3.5" | 74 | xcursor = "0.3.5" |
| 75 | zbus = { version = "~3.15.2", optional = true } | 75 | zbus = { version = "~3.15.2", optional = true } |
| 76 | +bytemuck = "1.14.3" | ||
| 76 | 77 | ||
| 77 | [dependencies.smithay] | 78 | [dependencies.smithay] |
| 78 | workspace = true | 79 | workspace = true |
src/backend/tty.rsmodified+60 -1
| @@ -8,7 +8,7 @@ use std::sync::{Arc, Mutex}; | |||
| 8 | use std::time::Duration; | 8 | use std::time::Duration; |
| 9 | use std::{io, mem}; | 9 | use std::{io, mem}; |
| 10 | 10 | ||
| 11 | -use anyhow::{anyhow, bail, Context}; | 11 | +use anyhow::{anyhow, bail, ensure, Context}; |
| 12 | use libc::dev_t; | 12 | use libc::dev_t; |
| 13 | use niri_config::Config; | 13 | use niri_config::Config; |
| 14 | use smithay::backend::allocator::dmabuf::Dmabuf; | 14 | use smithay::backend::allocator::dmabuf::Dmabuf; |
| @@ -1244,6 +1244,65 @@ impl Tty { | |||
| 1244 | } | 1244 | } |
| 1245 | } | 1245 | } |
| 1246 | 1246 | ||
| 1247 | + pub fn get_gamma_length(&self, output: &Output) -> Option<u32> { | ||
| 1248 | + let tty_state = output.user_data().get::<TtyOutputState>().unwrap(); | ||
| 1249 | + let device = self.devices.get(&tty_state.node)?; | ||
| 1250 | + let crtc_info = device.drm.get_crtc(tty_state.crtc.clone()).ok()?; | ||
| 1251 | + return Some(crtc_info.gamma_length()); | ||
| 1252 | + } | ||
| 1253 | + | ||
| 1254 | + pub fn get_gamma(&self, output: &Output) -> Option<Vec<u16>> { | ||
| 1255 | + let tty_state = output.user_data().get::<TtyOutputState>().unwrap(); | ||
| 1256 | + let device = self.devices.get(&tty_state.node)?; | ||
| 1257 | + let crtc_info = device.drm.get_crtc(tty_state.crtc.clone()).ok()?; | ||
| 1258 | + let crtc = tty_state.crtc.clone(); | ||
| 1259 | + let gamma_length = crtc_info.gamma_length() as usize; | ||
| 1260 | + let mut red = vec![0; gamma_length]; | ||
| 1261 | + let mut green = vec![0; gamma_length]; | ||
| 1262 | + let mut blue = vec![0; 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 | + } | ||
| 1271 | + | ||
| 1272 | + pub fn set_gamma( | ||
| 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(); | ||
| 1279 | + let device = self | ||
| 1280 | + .devices | ||
| 1281 | + .get(&tty_state.node) | ||
| 1282 | + .context("missing device")?; | ||
| 1283 | + | ||
| 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 | + } | ||
| 1305 | + | ||
| 1247 | fn refresh_ipc_outputs(&self) { | 1306 | fn refresh_ipc_outputs(&self) { |
| 1248 | let _span = tracy_client::span!("Tty::refresh_ipc_outputs"); | 1307 | let _span = tracy_client::span!("Tty::refresh_ipc_outputs"); |
| 1249 | 1308 | ||
src/handlers/mod.rsmodified+59 -1
| @@ -8,6 +8,7 @@ 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; | ||
| 11 | use smithay::backend::allocator::dmabuf::Dmabuf; | 12 | use smithay::backend::allocator::dmabuf::Dmabuf; |
| 12 | use smithay::backend::drm::DrmNode; | 13 | use smithay::backend::drm::DrmNode; |
| 13 | use smithay::desktop::{PopupKind, PopupManager}; | 14 | use smithay::desktop::{PopupKind, PopupManager}; |
| @@ -15,6 +16,7 @@ use smithay::input::pointer::{CursorIcon, CursorImageStatus, PointerHandle}; | |||
| 15 | use smithay::input::{keyboard, Seat, SeatHandler, SeatState}; | 16 | use smithay::input::{keyboard, Seat, SeatHandler, SeatState}; |
| 16 | use smithay::output::Output; | 17 | use smithay::output::Output; |
| 17 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; | 18 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; |
| 19 | +use smithay::reexports::wayland_server::backend::ObjectId; | ||
| 18 | use smithay::reexports::wayland_server::protocol::wl_data_source::WlDataSource; | 20 | use smithay::reexports::wayland_server::protocol::wl_data_source::WlDataSource; |
| 19 | use smithay::reexports::wayland_server::protocol::wl_output::WlOutput; | 21 | use smithay::reexports::wayland_server::protocol::wl_output::WlOutput; |
| 20 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; | 22 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| @@ -58,9 +60,10 @@ use crate::niri::{ClientState, State}; | |||
| 58 | use crate::protocols::foreign_toplevel::{ | 60 | use crate::protocols::foreign_toplevel::{ |
| 59 | self, ForeignToplevelHandler, ForeignToplevelManagerState, | 61 | self, ForeignToplevelHandler, ForeignToplevelManagerState, |
| 60 | }; | 62 | }; |
| 63 | +use crate::protocols::gamma_control::{GammaControlHandler, GammaControlManagerState}; | ||
| 61 | use crate::protocols::screencopy::{Screencopy, ScreencopyHandler}; | 64 | use crate::protocols::screencopy::{Screencopy, ScreencopyHandler}; |
| 62 | use crate::utils::output_size; | 65 | use crate::utils::output_size; |
| 63 | -use crate::{delegate_foreign_toplevel, delegate_screencopy}; | 66 | +use crate::{delegate_foreign_toplevel, delegate_gamma_control, delegate_screencopy}; |
| 64 | 67 | ||
| 65 | impl SeatHandler for State { | 68 | impl SeatHandler for State { |
| 66 | type KeyboardFocus = WlSurface; | 69 | type KeyboardFocus = WlSurface; |
| @@ -440,3 +443,58 @@ impl DrmLeaseHandler for State { | |||
| 440 | delegate_drm_lease!(State); | 443 | delegate_drm_lease!(State); |
| 441 | 444 | ||
| 442 | delegate_viewporter!(State); | 445 | delegate_viewporter!(State); |
| 446 | + | ||
| 447 | +impl GammaControlHandler for State { | ||
| 448 | + fn gamma_control_manager_state(&mut self) -> &mut GammaControlManagerState { | ||
| 449 | + &mut self.niri.gamma_control_manager_state | ||
| 450 | + } | ||
| 451 | + | ||
| 452 | + fn set_gamma( | ||
| 453 | + &mut self, | ||
| 454 | + wl_output: &WlOutput, | ||
| 455 | + ramp: Vec<u16>, | ||
| 456 | + gamma_size: u32, | ||
| 457 | + ) -> anyhow::Result<()> { | ||
| 458 | + if ramp.len() != gamma_size as usize * 3 { | ||
| 459 | + error!( | ||
| 460 | + "gamma length wrong (expected {}, got {})", | ||
| 461 | + gamma_size, | ||
| 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 | + } | ||
| 481 | + gamma_ramp | ||
| 482 | + } | ||
| 483 | + | ||
| 484 | + fn destroy(&mut self, output_id: ObjectId) { | ||
| 485 | + self.niri | ||
| 486 | + .gamma_control_manager_state | ||
| 487 | + .destroy_gamma_control(output_id) | ||
| 488 | + } | ||
| 489 | + | ||
| 490 | + fn get_gamma_size(&mut self, wl_output: &WlOutput) -> Option<u32> { | ||
| 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 | + } | ||
| 496 | + gamma_size | ||
| 497 | + } | ||
| 498 | +} | ||
| 499 | + | ||
| 500 | +delegate_gamma_control!(State); | ||
src/niri.rsmodified+12 -0
| @@ -101,6 +101,7 @@ use crate::input::{apply_libinput_settings, TabletData}; | |||
| 101 | use crate::ipc::server::IpcServer; | 101 | use crate::ipc::server::IpcServer; |
| 102 | use crate::layout::{Layout, MonitorRenderElement}; | 102 | use crate::layout::{Layout, MonitorRenderElement}; |
| 103 | use crate::protocols::foreign_toplevel::{self, ForeignToplevelManagerState}; | 103 | use crate::protocols::foreign_toplevel::{self, ForeignToplevelManagerState}; |
| 104 | +use crate::protocols::gamma_control::GammaControlManagerState; | ||
| 104 | use crate::protocols::screencopy::{Screencopy, ScreencopyManagerState}; | 105 | use crate::protocols::screencopy::{Screencopy, ScreencopyManagerState}; |
| 105 | use crate::pw_utils::{Cast, PipeWire}; | 106 | use crate::pw_utils::{Cast, PipeWire}; |
| 106 | use crate::render_helpers::renderer::NiriRenderer; | 107 | use crate::render_helpers::renderer::NiriRenderer; |
| @@ -187,6 +188,7 @@ pub struct Niri { | |||
| 187 | pub popup_grab: Option<PopupGrabState>, | 188 | pub popup_grab: Option<PopupGrabState>, |
| 188 | pub presentation_state: PresentationState, | 189 | pub presentation_state: PresentationState, |
| 189 | pub security_context_state: SecurityContextState, | 190 | pub security_context_state: SecurityContextState, |
| 191 | + pub gamma_control_manager_state: GammaControlManagerState, | ||
| 190 | 192 | ||
| 191 | pub seat: Seat<State>, | 193 | pub seat: Seat<State>, |
| 192 | /// Scancodes of the keys to suppress. | 194 | /// Scancodes of the keys to suppress. |
| @@ -929,6 +931,15 @@ impl Niri { | |||
| 929 | let viewporter_state = ViewporterState::new::<State>(&display_handle); | 931 | let viewporter_state = ViewporterState::new::<State>(&display_handle); |
| 930 | let xdg_foreign_state = XdgForeignState::new::<State>(&display_handle); | 932 | let xdg_foreign_state = XdgForeignState::new::<State>(&display_handle); |
| 931 | 933 | ||
| 934 | + let gamma_control_manager_state = GammaControlManagerState::new::<State, _>( | ||
| 935 | + &display_handle, | ||
| 936 | + match backend { | ||
| 937 | + Backend::Tty(_) => true, | ||
| 938 | + _ => false, | ||
| 939 | + }, | ||
| 940 | + |client| !client.get_data::<ClientState>().unwrap().restricted, | ||
| 941 | + ); | ||
| 942 | + | ||
| 932 | let mut seat: Seat<State> = seat_state.new_wl_seat(&display_handle, backend.seat_name()); | 943 | let mut seat: Seat<State> = seat_state.new_wl_seat(&display_handle, backend.seat_name()); |
| 933 | seat.add_keyboard( | 944 | seat.add_keyboard( |
| 934 | config_.input.keyboard.xkb.to_xkb_config(), | 945 | config_.input.keyboard.xkb.to_xkb_config(), |
| @@ -1082,6 +1093,7 @@ impl Niri { | |||
| 1082 | suppressed_keys: HashSet::new(), | 1093 | suppressed_keys: HashSet::new(), |
| 1083 | presentation_state, | 1094 | presentation_state, |
| 1084 | security_context_state, | 1095 | security_context_state, |
| 1096 | + gamma_control_manager_state, | ||
| 1085 | 1097 | ||
| 1086 | seat, | 1098 | seat, |
| 1087 | keyboard_focus: None, | 1099 | keyboard_focus: None, |
src/protocols/gamma_control.rsadded+238 -0
| @@ -0,0 +1,238 @@ | |||
| 1 | +use std::collections::HashMap; | ||
| 2 | +use std::fs::File; | ||
| 3 | +use std::io::Read; | ||
| 4 | + | ||
| 5 | +use smithay::reexports::wayland_protocols_wlr; | ||
| 6 | +use smithay::reexports::wayland_server::backend::{ClientId, ObjectId}; | ||
| 7 | +use smithay::reexports::wayland_server::protocol::wl_output::WlOutput; | ||
| 8 | +use smithay::reexports::wayland_server::{ | ||
| 9 | + Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New, Resource, | ||
| 10 | +}; | ||
| 11 | +use wayland_protocols_wlr::gamma_control::v1::server::{ | ||
| 12 | + zwlr_gamma_control_manager_v1, zwlr_gamma_control_v1, | ||
| 13 | +}; | ||
| 14 | +use zwlr_gamma_control_manager_v1::ZwlrGammaControlManagerV1; | ||
| 15 | +use zwlr_gamma_control_v1::ZwlrGammaControlV1; | ||
| 16 | + | ||
| 17 | +const VERSION: u32 = 1; | ||
| 18 | + | ||
| 19 | +pub struct GammaControlManagerState { | ||
| 20 | + gamma_controls: HashMap<WlOutput, ZwlrGammaControlV1>, | ||
| 21 | +} | ||
| 22 | + | ||
| 23 | +pub struct GammaControlManagerGlobalData { | ||
| 24 | + filter: Box<dyn for<'c> Fn(&'c Client) -> bool + Send + Sync>, | ||
| 25 | + can_view: bool, | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +pub trait GammaControlHandler { | ||
| 29 | + fn gamma_control_manager_state(&mut self) -> &mut GammaControlManagerState; | ||
| 30 | + fn set_gamma( | ||
| 31 | + &mut self, | ||
| 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 | +} | ||
| 40 | + | ||
| 41 | +pub struct GammaControlState { | ||
| 42 | + gamma_size: Option<u32>, | ||
| 43 | + previous_gamma_ramp: Option<Vec<u16>>, | ||
| 44 | + output: WlOutput, | ||
| 45 | + failed: bool, | ||
| 46 | +} | ||
| 47 | + | ||
| 48 | +impl GammaControlManagerState { | ||
| 49 | + pub fn new<D, F>(display: &DisplayHandle, can_view: bool, filter: F) -> Self | ||
| 50 | + where | ||
| 51 | + D: GlobalDispatch<ZwlrGammaControlManagerV1, GammaControlManagerGlobalData>, | ||
| 52 | + D: Dispatch<ZwlrGammaControlManagerV1, ()>, | ||
| 53 | + D: Dispatch<ZwlrGammaControlV1, GammaControlState>, | ||
| 54 | + D: GammaControlHandler, | ||
| 55 | + D: 'static, | ||
| 56 | + F: for<'c> Fn(&'c Client) -> bool + Send + Sync + 'static, | ||
| 57 | + { | ||
| 58 | + let global_data = GammaControlManagerGlobalData { | ||
| 59 | + filter: Box::new(filter), | ||
| 60 | + can_view, | ||
| 61 | + }; | ||
| 62 | + display.create_global::<D, ZwlrGammaControlManagerV1, _>(VERSION, global_data); | ||
| 63 | + | ||
| 64 | + Self { | ||
| 65 | + gamma_controls: HashMap::new(), | ||
| 66 | + } | ||
| 67 | + } | ||
| 68 | + pub fn destroy_gamma_control(&mut self, output_id: ObjectId) { | ||
| 69 | + self.gamma_controls.remove(&output_id); | ||
| 70 | + } | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +impl<D> GlobalDispatch<ZwlrGammaControlManagerV1, GammaControlManagerGlobalData, D> | ||
| 74 | + for GammaControlManagerState | ||
| 75 | +where | ||
| 76 | + D: GlobalDispatch<ZwlrGammaControlManagerV1, GammaControlManagerGlobalData>, | ||
| 77 | + D: Dispatch<ZwlrGammaControlManagerV1, ()>, | ||
| 78 | + D: Dispatch<ZwlrGammaControlV1, GammaControlState>, | ||
| 79 | + D: GammaControlHandler, | ||
| 80 | + D: 'static, | ||
| 81 | +{ | ||
| 82 | + fn bind( | ||
| 83 | + _state: &mut D, | ||
| 84 | + _handle: &DisplayHandle, | ||
| 85 | + _client: &Client, | ||
| 86 | + manager: New<ZwlrGammaControlManagerV1>, | ||
| 87 | + _manager_state: &GammaControlManagerGlobalData, | ||
| 88 | + data_init: &mut DataInit<'_, D>, | ||
| 89 | + ) { | ||
| 90 | + data_init.init(manager, ()); | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + fn can_view(client: Client, global_data: &GammaControlManagerGlobalData) -> bool { | ||
| 94 | + global_data.can_view && (global_data.filter)(&client) | ||
| 95 | + } | ||
| 96 | +} | ||
| 97 | + | ||
| 98 | +impl<D> Dispatch<ZwlrGammaControlManagerV1, (), D> for GammaControlManagerState | ||
| 99 | +where | ||
| 100 | + D: Dispatch<ZwlrGammaControlManagerV1, ()>, | ||
| 101 | + D: Dispatch<ZwlrGammaControlV1, GammaControlState>, | ||
| 102 | + D: GammaControlHandler, | ||
| 103 | + D: 'static, | ||
| 104 | +{ | ||
| 105 | + fn request( | ||
| 106 | + state: &mut D, | ||
| 107 | + _client: &Client, | ||
| 108 | + _resource: &ZwlrGammaControlManagerV1, | ||
| 109 | + request: <ZwlrGammaControlManagerV1 as Resource>::Request, | ||
| 110 | + _data: &(), | ||
| 111 | + _dhandle: &DisplayHandle, | ||
| 112 | + data_init: &mut DataInit<'_, D>, | ||
| 113 | + ) { | ||
| 114 | + match request { | ||
| 115 | + zwlr_gamma_control_manager_v1::Request::GetGammaControl { id, output } => { | ||
| 116 | + let gamma_size = state.get_gamma_size(&output); | ||
| 117 | + let previous_gamma_ramp = state.get_gamma(&output); | ||
| 118 | + | ||
| 119 | + if state | ||
| 120 | + .gamma_control_manager_state() | ||
| 121 | + .gamma_controls | ||
| 122 | + .contains_key(&output) | ||
| 123 | + || gamma_size.is_none() | ||
| 124 | + || previous_gamma_ramp.is_none() | ||
| 125 | + { | ||
| 126 | + data_init | ||
| 127 | + .init( | ||
| 128 | + id, | ||
| 129 | + GammaControlState { | ||
| 130 | + gamma_size: gamma_size.clone(), | ||
| 131 | + previous_gamma_ramp: None, | ||
| 132 | + output: output.clone(), | ||
| 133 | + failed: true, | ||
| 134 | + }, | ||
| 135 | + ) | ||
| 136 | + .failed(); | ||
| 137 | + return; | ||
| 138 | + } | ||
| 139 | + | ||
| 140 | + let zwlr_gamma_control = data_init.init( | ||
| 141 | + id, | ||
| 142 | + GammaControlState { | ||
| 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 | + } | ||
| 156 | + zwlr_gamma_control_manager_v1::Request::Destroy => (), | ||
| 157 | + _ => unreachable!(), | ||
| 158 | + } | ||
| 159 | + } | ||
| 160 | +} | ||
| 161 | + | ||
| 162 | +impl<D> Dispatch<ZwlrGammaControlV1, GammaControlState, D> for GammaControlManagerState | ||
| 163 | +where | ||
| 164 | + D: Dispatch<ZwlrGammaControlV1, GammaControlState>, | ||
| 165 | + D: GammaControlHandler, | ||
| 166 | + D: 'static, | ||
| 167 | +{ | ||
| 168 | + fn request( | ||
| 169 | + state: &mut D, | ||
| 170 | + _client: &Client, | ||
| 171 | + resource: &ZwlrGammaControlV1, | ||
| 172 | + request: <ZwlrGammaControlV1 as Resource>::Request, | ||
| 173 | + data: &GammaControlState, | ||
| 174 | + _dhandle: &DisplayHandle, | ||
| 175 | + _data_init: &mut DataInit<'_, D>, | ||
| 176 | + ) { | ||
| 177 | + match request { | ||
| 178 | + zwlr_gamma_control_v1::Request::SetGamma { fd } => { | ||
| 179 | + if data.failed { | ||
| 180 | + return; | ||
| 181 | + } | ||
| 182 | + debug!("setting gamma for output {:?}", data.output); | ||
| 183 | + let buf = &mut Vec::new(); | ||
| 184 | + if File::from(fd).read_to_end(buf).is_err() { | ||
| 185 | + warn!("failed to read gamma data for output {:?}", data.output); | ||
| 186 | + resource.failed(); | ||
| 187 | + return; | ||
| 188 | + } | ||
| 189 | + | ||
| 190 | + let gamma = bytemuck::cast_slice(buf).to_vec(); | ||
| 191 | + let gamma_size = data.gamma_size.unwrap(); | ||
| 192 | + | ||
| 193 | + if let Err(err) = state.set_gamma(&data.output, gamma, gamma_size) { | ||
| 194 | + warn!("error setting gamma: {err:?}"); | ||
| 195 | + resource.failed(); | ||
| 196 | + return; | ||
| 197 | + } | ||
| 198 | + } | ||
| 199 | + zwlr_gamma_control_v1::Request::Destroy => (), | ||
| 200 | + _ => unreachable!(), | ||
| 201 | + } | ||
| 202 | + } | ||
| 203 | + | ||
| 204 | + fn destroyed( | ||
| 205 | + state: &mut D, | ||
| 206 | + _client: ClientId, | ||
| 207 | + _resource: &ZwlrGammaControlV1, | ||
| 208 | + data: &GammaControlState, | ||
| 209 | + ) { | ||
| 210 | + if data.failed { | ||
| 211 | + return; | ||
| 212 | + } | ||
| 213 | + let ramp = data.previous_gamma_ramp.as_ref().unwrap(); | ||
| 214 | + | ||
| 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 | + | ||
| 219 | + state.destroy(data.output.id()); | ||
| 220 | + } | ||
| 221 | +} | ||
| 222 | + | ||
| 223 | +#[macro_export] | ||
| 224 | +macro_rules! delegate_gamma_control { | ||
| 225 | + ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => { | ||
| 226 | + smithay::reexports::wayland_server::delegate_global_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ | ||
| 227 | + smithay::reexports::wayland_protocols_wlr::gamma_control::v1::server::zwlr_gamma_control_manager_v1::ZwlrGammaControlManagerV1: $crate::protocols::gamma_control::GammaControlManagerGlobalData | ||
| 228 | + ] => $crate::protocols::gamma_control::GammaControlManagerState); | ||
| 229 | + | ||
| 230 | + smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ | ||
| 231 | + smithay::reexports::wayland_protocols_wlr::gamma_control::v1::server::zwlr_gamma_control_manager_v1::ZwlrGammaControlManagerV1: () | ||
| 232 | + ] => $crate::protocols::gamma_control::GammaControlManagerState); | ||
| 233 | + | ||
| 234 | + smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ | ||
| 235 | + smithay::reexports::wayland_protocols_wlr::gamma_control::v1::server::zwlr_gamma_control_v1::ZwlrGammaControlV1: $crate::protocols::gamma_control::GammaControlState | ||
| 236 | + ] => $crate::protocols::gamma_control::GammaControlManagerState); | ||
| 237 | + }; | ||
| 238 | +} | ||
src/protocols/mod.rsmodified+1 -0
| @@ -1,2 +1,3 @@ | |||
| 1 | pub mod foreign_toplevel; | 1 | pub mod foreign_toplevel; |
| 2 | +pub mod gamma_control; | ||
| 2 | pub mod screencopy; | 3 | pub mod screencopy; |