niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·b0eb8ad·Ivan Molodetskikh·3 months ago·2857 commits
| 1 | use std::collections::HashMap; |
| 2 | use std::str::FromStr; |
| 3 | use std::sync::{Arc, Mutex}; |
| 4 | |
| 5 | use serde::{Deserialize, Serialize}; |
| 6 | use smithay::utils::Size; |
| 7 | use zbus::fdo::RequestNameFlags; |
| 8 | use zbus::object_server::SignalEmitter; |
| 9 | use zbus::zvariant::{self, OwnedValue, Type}; |
| 10 | use zbus::{fdo, interface}; |
| 11 | |
| 12 | use super::Start; |
| 13 | use crate::backend::IpcOutputMap; |
| 14 | use crate::utils::is_laptop_panel; |
| 15 | use crate::utils::scale::supported_scales; |
| 16 | |
| 17 | pub struct DisplayConfig { |
| 18 | to_niri: calloop::channel::Sender<HashMap<String, Option<niri_config::Output>>>, |
| 19 | ipc_outputs: Arc<Mutex<IpcOutputMap>>, |
| 20 | } |
| 21 | |
| 22 | #[derive(Serialize, Type)] |
| 23 | pub struct Monitor { |
| 24 | names: (String, String, String, String), |
| 25 | modes: Vec<Mode>, |
| 26 | properties: HashMap<String, OwnedValue>, |
| 27 | } |
| 28 | |
| 29 | #[derive(Serialize, Type)] |
| 30 | pub struct Mode { |
| 31 | id: String, |
| 32 | width: i32, |
| 33 | height: i32, |
| 34 | refresh_rate: f64, |
| 35 | preferred_scale: f64, |
| 36 | supported_scales: Vec<f64>, |
| 37 | properties: HashMap<String, OwnedValue>, |
| 38 | } |
| 39 | |
| 40 | #[derive(Serialize, Type)] |
| 41 | pub struct LogicalMonitor { |
| 42 | x: i32, |
| 43 | y: i32, |
| 44 | scale: f64, |
| 45 | transform: u32, |
| 46 | is_primary: bool, |
| 47 | monitors: Vec<(String, String, String, String)>, |
| 48 | properties: HashMap<String, OwnedValue>, |
| 49 | } |
| 50 | |
| 51 | // ApplyMonitorsConfig |
| 52 | #[derive(Deserialize, Type)] |
| 53 | pub struct LogicalMonitorConfiguration { |
| 54 | x: i32, |
| 55 | y: i32, |
| 56 | scale: f64, |
| 57 | transform: u32, |
| 58 | _is_primary: bool, |
| 59 | monitors: Vec<(String, String, HashMap<String, OwnedValue>)>, |
| 60 | } |
| 61 | |
| 62 | #[interface(name = "org.gnome.Mutter.DisplayConfig")] |
| 63 | impl DisplayConfig { |
| 64 | async fn get_current_state( |
| 65 | &self, |
| 66 | ) -> fdo::Result<( |
| 67 | u32, |
| 68 | Vec<Monitor>, |
| 69 | Vec<LogicalMonitor>, |
| 70 | HashMap<String, OwnedValue>, |
| 71 | )> { |
| 72 | // Construct the DBus response. |
| 73 | let mut monitors = Vec::new(); |
| 74 | let mut logical_monitors = Vec::new(); |
| 75 | |
| 76 | for output in self.ipc_outputs.lock().unwrap().values() { |
| 77 | // Loosely matches the check in Mutter. |
| 78 | let c = &output.name; |
| 79 | let is_laptop_panel = is_laptop_panel(c); |
| 80 | let display_name = make_display_name(output, is_laptop_panel); |
| 81 | |
| 82 | let mut properties = HashMap::new(); |
| 83 | properties.insert( |
| 84 | String::from("display-name"), |
| 85 | OwnedValue::from(zvariant::Str::from(display_name)), |
| 86 | ); |
| 87 | properties.insert( |
| 88 | String::from("is-builtin"), |
| 89 | OwnedValue::from(is_laptop_panel), |
| 90 | ); |
| 91 | |
| 92 | let mut modes: Vec<Mode> = output |
| 93 | .modes |
| 94 | .iter() |
| 95 | .map(|m| { |
| 96 | let niri_ipc::Mode { |
| 97 | width, |
| 98 | height, |
| 99 | refresh_rate, |
| 100 | is_preferred, |
| 101 | } = *m; |
| 102 | let width = i32::from(width); |
| 103 | let height = i32::from(height); |
| 104 | let refresh_rate = refresh_rate as f64 / 1000.; |
| 105 | |
| 106 | Mode { |
| 107 | id: format!("{width}x{height}@{refresh_rate:.3}"), |
| 108 | width, |
| 109 | height, |
| 110 | refresh_rate, |
| 111 | preferred_scale: 1., |
| 112 | supported_scales: supported_scales(Size::from((width, height))).collect(), |
| 113 | properties: HashMap::from([( |
| 114 | String::from("is-preferred"), |
| 115 | OwnedValue::from(is_preferred), |
| 116 | )]), |
| 117 | } |
| 118 | }) |
| 119 | .collect(); |
| 120 | if let Some(mode) = output.current_mode { |
| 121 | modes[mode] |
| 122 | .properties |
| 123 | .insert(String::from("is-current"), OwnedValue::from(true)); |
| 124 | } |
| 125 | |
| 126 | let connector = c.clone(); |
| 127 | let model = output.model.clone(); |
| 128 | let make = output.make.clone(); |
| 129 | |
| 130 | // Serial is used for session restore, so fall back to the connector name if it's |
| 131 | // not available. |
| 132 | let serial = output.serial.as_ref().unwrap_or(&connector).clone(); |
| 133 | |
| 134 | let names = (connector, make, model, serial); |
| 135 | |
| 136 | if let Some(logical) = output.logical.as_ref() { |
| 137 | let transform = match logical.transform { |
| 138 | niri_ipc::Transform::Normal => 0, |
| 139 | niri_ipc::Transform::_90 => 1, |
| 140 | niri_ipc::Transform::_180 => 2, |
| 141 | niri_ipc::Transform::_270 => 3, |
| 142 | niri_ipc::Transform::Flipped => 4, |
| 143 | niri_ipc::Transform::Flipped90 => 5, |
| 144 | niri_ipc::Transform::Flipped180 => 6, |
| 145 | niri_ipc::Transform::Flipped270 => 7, |
| 146 | }; |
| 147 | |
| 148 | logical_monitors.push(LogicalMonitor { |
| 149 | x: logical.x, |
| 150 | y: logical.y, |
| 151 | scale: logical.scale, |
| 152 | transform, |
| 153 | is_primary: false, |
| 154 | monitors: vec![names.clone()], |
| 155 | properties: HashMap::new(), |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | monitors.push(Monitor { |
| 160 | names, |
| 161 | modes, |
| 162 | properties, |
| 163 | }); |
| 164 | } |
| 165 | |
| 166 | // Sort by connector. |
| 167 | monitors.sort_unstable_by(|a, b| a.names.0.cmp(&b.names.0)); |
| 168 | logical_monitors.sort_unstable_by(|a, b| a.monitors[0].0.cmp(&b.monitors[0].0)); |
| 169 | |
| 170 | let properties = HashMap::from([(String::from("layout-mode"), OwnedValue::from(1u32))]); |
| 171 | Ok((0, monitors, logical_monitors, properties)) |
| 172 | } |
| 173 | |
| 174 | async fn apply_monitors_config( |
| 175 | &self, |
| 176 | _serial: u32, |
| 177 | method: u32, |
| 178 | logical_monitor_configs: Vec<LogicalMonitorConfiguration>, |
| 179 | _properties: HashMap<String, OwnedValue>, |
| 180 | ) -> fdo::Result<()> { |
| 181 | let current_conf = self.ipc_outputs.lock().unwrap(); |
| 182 | let mut new_conf = HashMap::new(); |
| 183 | for requested_config in logical_monitor_configs { |
| 184 | if requested_config.monitors.len() > 1 { |
| 185 | return Err(zbus::fdo::Error::Failed( |
| 186 | "Mirroring is not yet supported".to_owned(), |
| 187 | )); |
| 188 | } |
| 189 | for (connector, mode, _props) in requested_config.monitors { |
| 190 | if !current_conf.values().any(|o| o.name == connector) { |
| 191 | return Err(zbus::fdo::Error::Failed(format!( |
| 192 | "Connector '{connector}' not found", |
| 193 | ))); |
| 194 | } |
| 195 | new_conf.insert( |
| 196 | connector.clone(), |
| 197 | Some(niri_config::Output { |
| 198 | off: false, |
| 199 | name: connector, |
| 200 | scale: Some(niri_config::FloatOrInt(requested_config.scale)), |
| 201 | transform: match requested_config.transform { |
| 202 | 0 => niri_ipc::Transform::Normal, |
| 203 | 1 => niri_ipc::Transform::_90, |
| 204 | 2 => niri_ipc::Transform::_180, |
| 205 | 3 => niri_ipc::Transform::_270, |
| 206 | 4 => niri_ipc::Transform::Flipped, |
| 207 | 5 => niri_ipc::Transform::Flipped90, |
| 208 | 6 => niri_ipc::Transform::Flipped180, |
| 209 | 7 => niri_ipc::Transform::Flipped270, |
| 210 | x => { |
| 211 | return Err(zbus::fdo::Error::Failed(format!( |
| 212 | "Unknown transform {x}", |
| 213 | ))) |
| 214 | } |
| 215 | }, |
| 216 | position: Some(niri_config::Position { |
| 217 | x: requested_config.x, |
| 218 | y: requested_config.y, |
| 219 | }), |
| 220 | mode: Some(niri_config::output::Mode { |
| 221 | custom: false, |
| 222 | mode: niri_ipc::ConfiguredMode::from_str(&mode).map_err(|e| { |
| 223 | zbus::fdo::Error::Failed(format!( |
| 224 | "Could not parse mode '{mode}': {e}" |
| 225 | )) |
| 226 | })?, |
| 227 | }), |
| 228 | // FIXME: VRR |
| 229 | ..Default::default() |
| 230 | }), |
| 231 | ); |
| 232 | } |
| 233 | } |
| 234 | if new_conf.is_empty() { |
| 235 | return Err(zbus::fdo::Error::Failed( |
| 236 | "At least one output must be enabled".to_owned(), |
| 237 | )); |
| 238 | } |
| 239 | for output in current_conf.values() { |
| 240 | if !new_conf.contains_key(&output.name) { |
| 241 | new_conf.insert(output.name.clone(), None); |
| 242 | } |
| 243 | } |
| 244 | if method == 0 { |
| 245 | // 0 means "verify", so don't actually apply here |
| 246 | return Ok(()); |
| 247 | } |
| 248 | if let Err(err) = self.to_niri.send(new_conf) { |
| 249 | warn!("error sending message to niri: {err:?}"); |
| 250 | return Err(fdo::Error::Failed("internal error".to_owned())); |
| 251 | } |
| 252 | Ok(()) |
| 253 | } |
| 254 | |
| 255 | #[zbus(signal)] |
| 256 | pub async fn monitors_changed(ctxt: &SignalEmitter<'_>) -> zbus::Result<()>; |
| 257 | |
| 258 | #[zbus(property)] |
| 259 | fn power_save_mode(&self) -> i32 { |
| 260 | -1 |
| 261 | } |
| 262 | |
| 263 | #[zbus(property)] |
| 264 | fn set_power_save_mode(&self, _mode: i32) -> zbus::Result<()> { |
| 265 | Err(zbus::Error::Unsupported) |
| 266 | } |
| 267 | |
| 268 | #[zbus(property)] |
| 269 | fn panel_orientation_managed(&self) -> bool { |
| 270 | false |
| 271 | } |
| 272 | |
| 273 | #[zbus(property)] |
| 274 | fn apply_monitors_config_allowed(&self) -> bool { |
| 275 | true |
| 276 | } |
| 277 | |
| 278 | #[zbus(property)] |
| 279 | fn night_light_supported(&self) -> bool { |
| 280 | false |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | impl DisplayConfig { |
| 285 | pub fn new( |
| 286 | to_niri: calloop::channel::Sender<HashMap<String, Option<niri_config::Output>>>, |
| 287 | ipc_outputs: Arc<Mutex<IpcOutputMap>>, |
| 288 | ) -> Self { |
| 289 | Self { |
| 290 | to_niri, |
| 291 | ipc_outputs, |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | impl Start for DisplayConfig { |
| 297 | fn start(self) -> anyhow::Result<zbus::blocking::Connection> { |
| 298 | let conn = zbus::blocking::Connection::session()?; |
| 299 | let flags = RequestNameFlags::AllowReplacement |
| 300 | | RequestNameFlags::ReplaceExisting |
| 301 | | RequestNameFlags::DoNotQueue; |
| 302 | |
| 303 | conn.object_server() |
| 304 | .at("/org/gnome/Mutter/DisplayConfig", self)?; |
| 305 | conn.request_name_with_flags("org.gnome.Mutter.DisplayConfig", flags)?; |
| 306 | |
| 307 | Ok(conn) |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // Adapted from Mutter. |
| 312 | fn make_display_name(output: &niri_ipc::Output, is_laptop_panel: bool) -> String { |
| 313 | if is_laptop_panel { |
| 314 | return String::from("Built-in display"); |
| 315 | } |
| 316 | |
| 317 | let make = &output.make; |
| 318 | let model = &output.model; |
| 319 | if let Some(diagonal) = output.physical_size.map(|(width_mm, height_mm)| { |
| 320 | let diagonal = f64::hypot(f64::from(width_mm), f64::from(height_mm)) / 25.4; |
| 321 | format_diagonal(diagonal) |
| 322 | }) { |
| 323 | format!("{make} {diagonal}") |
| 324 | } else if model != "Unknown" { |
| 325 | format!("{make} {model}") |
| 326 | } else { |
| 327 | make.clone() |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | fn format_diagonal(diagonal_inches: f64) -> String { |
| 332 | let known = [12.1, 13.3, 15.6]; |
| 333 | if let Some(d) = known.iter().find(|d| (*d - diagonal_inches).abs() < 0.1) { |
| 334 | format!("{d:.1}″") |
| 335 | } else { |
| 336 | format!("{}″", diagonal_inches.round() as u32) |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | #[cfg(test)] |
| 341 | mod tests { |
| 342 | use insta::assert_snapshot; |
| 343 | |
| 344 | use super::*; |
| 345 | |
| 346 | #[test] |
| 347 | fn test_format_diagonal() { |
| 348 | assert_snapshot!(format_diagonal(12.11), @"12.1″"); |
| 349 | assert_snapshot!(format_diagonal(13.28), @"13.3″"); |
| 350 | assert_snapshot!(format_diagonal(15.6), @"15.6″"); |
| 351 | assert_snapshot!(format_diagonal(23.2), @"23″"); |
| 352 | assert_snapshot!(format_diagonal(24.8), @"25″"); |
| 353 | } |
| 354 | } |