niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·055a94d·Ivan Molodetskikh·1 year ago·2247 commits
| 1 | use std::collections::HashMap; |
| 2 | use std::fs::File; |
| 3 | use std::io::Read; |
| 4 | |
| 5 | use smithay::output::Output; |
| 6 | use smithay::reexports::wayland_protocols_wlr; |
| 7 | use smithay::reexports::wayland_server::backend::ClientId; |
| 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 | // Active gamma controls only. Failed ones are removed. |
| 21 | gamma_controls: HashMap<Output, ZwlrGammaControlV1>, |
| 22 | } |
| 23 | |
| 24 | pub struct GammaControlManagerGlobalData { |
| 25 | filter: Box<dyn for<'c> Fn(&'c Client) -> bool + Send + Sync>, |
| 26 | } |
| 27 | |
| 28 | pub trait GammaControlHandler { |
| 29 | fn gamma_control_manager_state(&mut self) -> &mut GammaControlManagerState; |
| 30 | fn get_gamma_size(&mut self, output: &Output) -> Option<u32>; |
| 31 | fn set_gamma(&mut self, output: &Output, ramp: Option<Vec<u16>>) -> Option<()>; |
| 32 | } |
| 33 | |
| 34 | pub struct GammaControlState { |
| 35 | gamma_size: u32, |
| 36 | } |
| 37 | |
| 38 | impl GammaControlManagerState { |
| 39 | pub fn new<D, F>(display: &DisplayHandle, filter: F) -> Self |
| 40 | where |
| 41 | D: GlobalDispatch<ZwlrGammaControlManagerV1, GammaControlManagerGlobalData>, |
| 42 | D: Dispatch<ZwlrGammaControlManagerV1, ()>, |
| 43 | D: Dispatch<ZwlrGammaControlV1, GammaControlState>, |
| 44 | D: GammaControlHandler, |
| 45 | D: 'static, |
| 46 | F: for<'c> Fn(&'c Client) -> bool + Send + Sync + 'static, |
| 47 | { |
| 48 | let global_data = GammaControlManagerGlobalData { |
| 49 | filter: Box::new(filter), |
| 50 | }; |
| 51 | display.create_global::<D, ZwlrGammaControlManagerV1, _>(VERSION, global_data); |
| 52 | |
| 53 | Self { |
| 54 | gamma_controls: HashMap::new(), |
| 55 | } |
| 56 | } |
| 57 | |
| 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 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | impl<D> GlobalDispatch<ZwlrGammaControlManagerV1, GammaControlManagerGlobalData, D> |
| 66 | for GammaControlManagerState |
| 67 | where |
| 68 | D: GlobalDispatch<ZwlrGammaControlManagerV1, GammaControlManagerGlobalData>, |
| 69 | D: Dispatch<ZwlrGammaControlManagerV1, ()>, |
| 70 | D: Dispatch<ZwlrGammaControlV1, GammaControlState>, |
| 71 | D: GammaControlHandler, |
| 72 | D: 'static, |
| 73 | { |
| 74 | fn bind( |
| 75 | _state: &mut D, |
| 76 | _handle: &DisplayHandle, |
| 77 | _client: &Client, |
| 78 | manager: New<ZwlrGammaControlManagerV1>, |
| 79 | _manager_state: &GammaControlManagerGlobalData, |
| 80 | data_init: &mut DataInit<'_, D>, |
| 81 | ) { |
| 82 | data_init.init(manager, ()); |
| 83 | } |
| 84 | |
| 85 | fn can_view(client: Client, global_data: &GammaControlManagerGlobalData) -> bool { |
| 86 | (global_data.filter)(&client) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | impl<D> Dispatch<ZwlrGammaControlManagerV1, (), D> for GammaControlManagerState |
| 91 | where |
| 92 | D: Dispatch<ZwlrGammaControlManagerV1, ()>, |
| 93 | D: Dispatch<ZwlrGammaControlV1, GammaControlState>, |
| 94 | D: GammaControlHandler, |
| 95 | D: 'static, |
| 96 | { |
| 97 | fn request( |
| 98 | state: &mut D, |
| 99 | _client: &Client, |
| 100 | _resource: &ZwlrGammaControlManagerV1, |
| 101 | request: <ZwlrGammaControlManagerV1 as Resource>::Request, |
| 102 | _data: &(), |
| 103 | _dhandle: &DisplayHandle, |
| 104 | data_init: &mut DataInit<'_, D>, |
| 105 | ) { |
| 106 | match request { |
| 107 | zwlr_gamma_control_manager_v1::Request::GetGammaControl { id, output } => { |
| 108 | if let Some(output) = Output::from_resource(&output) { |
| 109 | // We borrow state in the middle. |
| 110 | #[allow(clippy::map_entry)] |
| 111 | if !state |
| 112 | .gamma_control_manager_state() |
| 113 | .gamma_controls |
| 114 | .contains_key(&output) |
| 115 | { |
| 116 | if let Some(gamma_size) = state.get_gamma_size(&output) { |
| 117 | let zwlr_gamma_control = |
| 118 | data_init.init(id, GammaControlState { gamma_size }); |
| 119 | zwlr_gamma_control.gamma_size(gamma_size); |
| 120 | state |
| 121 | .gamma_control_manager_state() |
| 122 | .gamma_controls |
| 123 | .insert(output, zwlr_gamma_control); |
| 124 | return; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | data_init |
| 130 | .init(id, GammaControlState { gamma_size: 0 }) |
| 131 | .failed(); |
| 132 | } |
| 133 | zwlr_gamma_control_manager_v1::Request::Destroy => (), |
| 134 | _ => unreachable!(), |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | impl<D> Dispatch<ZwlrGammaControlV1, GammaControlState, D> for GammaControlManagerState |
| 140 | where |
| 141 | D: Dispatch<ZwlrGammaControlV1, GammaControlState>, |
| 142 | D: GammaControlHandler, |
| 143 | D: 'static, |
| 144 | { |
| 145 | fn request( |
| 146 | state: &mut D, |
| 147 | _client: &Client, |
| 148 | resource: &ZwlrGammaControlV1, |
| 149 | request: <ZwlrGammaControlV1 as Resource>::Request, |
| 150 | data: &GammaControlState, |
| 151 | _dhandle: &DisplayHandle, |
| 152 | _data_init: &mut DataInit<'_, D>, |
| 153 | ) { |
| 154 | match request { |
| 155 | zwlr_gamma_control_v1::Request::SetGamma { fd } => { |
| 156 | let gamma_controls = &mut state.gamma_control_manager_state().gamma_controls; |
| 157 | let Some((output, _)) = gamma_controls.iter().find(|(_, x)| *x == resource) else { |
| 158 | return; |
| 159 | }; |
| 160 | let output = output.clone(); |
| 161 | |
| 162 | trace!("setting gamma for output {}", output.name()); |
| 163 | |
| 164 | // Start with a u16 slice so it's aligned correctly. |
| 165 | let mut gamma = vec![0u16; data.gamma_size as usize * 3]; |
| 166 | let buf = bytemuck::cast_slice_mut(&mut gamma); |
| 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 | { |
| 181 | match file.read(&mut [0]) { |
| 182 | Ok(0) => (), |
| 183 | Ok(_) => { |
| 184 | warn!("gamma data is too large"); |
| 185 | resource.failed(); |
| 186 | gamma_controls.remove(&output); |
| 187 | let _ = state.set_gamma(&output, None); |
| 188 | return; |
| 189 | } |
| 190 | Err(err) => { |
| 191 | warn!("error reading gamma data: {err:?}"); |
| 192 | resource.failed(); |
| 193 | gamma_controls.remove(&output); |
| 194 | let _ = state.set_gamma(&output, None); |
| 195 | return; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if state.set_gamma(&output, Some(gamma)).is_none() { |
| 202 | resource.failed(); |
| 203 | let gamma_controls = &mut state.gamma_control_manager_state().gamma_controls; |
| 204 | gamma_controls.remove(&output); |
| 205 | let _ = state.set_gamma(&output, None); |
| 206 | } |
| 207 | } |
| 208 | zwlr_gamma_control_v1::Request::Destroy => (), |
| 209 | _ => unreachable!(), |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | fn destroyed( |
| 214 | state: &mut D, |
| 215 | _client: ClientId, |
| 216 | resource: &ZwlrGammaControlV1, |
| 217 | _data: &GammaControlState, |
| 218 | ) { |
| 219 | let gamma_controls = &mut state.gamma_control_manager_state().gamma_controls; |
| 220 | let Some((output, _)) = gamma_controls.iter().find(|(_, x)| *x == resource) else { |
| 221 | return; |
| 222 | }; |
| 223 | let output = output.clone(); |
| 224 | gamma_controls.remove(&output); |
| 225 | |
| 226 | let _ = state.set_gamma(&output, None); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | #[macro_export] |
| 231 | macro_rules! delegate_gamma_control { |
| 232 | ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => { |
| 233 | smithay::reexports::wayland_server::delegate_global_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 234 | smithay::reexports::wayland_protocols_wlr::gamma_control::v1::server::zwlr_gamma_control_manager_v1::ZwlrGammaControlManagerV1: $crate::protocols::gamma_control::GammaControlManagerGlobalData |
| 235 | ] => $crate::protocols::gamma_control::GammaControlManagerState); |
| 236 | |
| 237 | smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 238 | smithay::reexports::wayland_protocols_wlr::gamma_control::v1::server::zwlr_gamma_control_manager_v1::ZwlrGammaControlManagerV1: () |
| 239 | ] => $crate::protocols::gamma_control::GammaControlManagerState); |
| 240 | |
| 241 | smithay::reexports::wayland_server::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [ |
| 242 | smithay::reexports::wayland_protocols_wlr::gamma_control::v1::server::zwlr_gamma_control_v1::ZwlrGammaControlV1: $crate::protocols::gamma_control::GammaControlState |
| 243 | ] => $crate::protocols::gamma_control::GammaControlManagerState); |
| 244 | }; |
| 245 | } |