config: Extract output
Browse files at this commit19c576cfd88fa58c8f3e8123933e8a13ba6da3faParent: ab52bb92 changed files +361 -350
- niri-config/src/lib.rs +3 -350
- niri-config/src/output.rs +358 -0
niri-config/src/lib.rsmodified+3 -350
| @@ -14,8 +14,7 @@ use bitflags::bitflags; | ||
| 14 | 14 | use knuffel::errors::DecodeError; |
| 15 | 15 | use miette::{miette, Context, IntoDiagnostic}; |
| 16 | 16 | use niri_ipc::{ |
| 17 | - ColumnDisplay, ConfiguredMode, LayoutSwitchTarget, PositionChange, SizeChange, Transform, | |
| 18 | - WorkspaceReferenceArg, | |
| 17 | + ColumnDisplay, LayoutSwitchTarget, PositionChange, SizeChange, WorkspaceReferenceArg, | |
| 19 | 18 | }; |
| 20 | 19 | use smithay::backend::renderer::Color32F; |
| 21 | 20 | use smithay::input::keyboard::keysyms::KEY_NoSymbol; |
| @@ -28,6 +27,7 @@ pub const DEFAULT_BACKDROP_COLOR: Color = Color::from_array_unpremul([0.15, 0.15 | ||
| 28 | 27 | pub mod animations; |
| 29 | 28 | pub mod input; |
| 30 | 29 | pub mod layer_rule; |
| 30 | +pub mod output; | |
| 31 | 31 | pub mod utils; |
| 32 | 32 | pub mod window_rule; |
| 33 | 33 | |
| @@ -36,6 +36,7 @@ pub use crate::animations::{ | ||
| 36 | 36 | }; |
| 37 | 37 | pub use crate::input::{Input, ModKey, ScrollMethod, TrackLayout, WarpMouseToFocusMode, Xkb}; |
| 38 | 38 | pub use crate::layer_rule::LayerRule; |
| 39 | +pub use crate::output::{Output, OutputName, Outputs, Position, Vrr}; | |
| 39 | 40 | pub use crate::window_rule::{FloatingPosition, RelativeTo, WindowRule}; |
| 40 | 41 | |
| 41 | 42 | #[derive(knuffel::Decode, Debug, PartialEq)] |
| @@ -107,86 +108,6 @@ pub enum CenterFocusedColumn { | ||
| 107 | 108 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 108 | 109 | pub struct Percent(pub f64); |
| 109 | 110 | |
| 110 | -#[derive(Debug, Default, Clone, PartialEq)] | |
| 111 | -pub struct Outputs(pub Vec<Output>); | |
| 112 | - | |
| 113 | -#[derive(knuffel::Decode, Debug, Clone, PartialEq)] | |
| 114 | -pub struct Output { | |
| 115 | - #[knuffel(child)] | |
| 116 | - pub off: bool, | |
| 117 | - #[knuffel(argument)] | |
| 118 | - pub name: String, | |
| 119 | - #[knuffel(child, unwrap(argument))] | |
| 120 | - pub scale: Option<FloatOrInt<0, 10>>, | |
| 121 | - #[knuffel(child, unwrap(argument, str), default = Transform::Normal)] | |
| 122 | - pub transform: Transform, | |
| 123 | - #[knuffel(child)] | |
| 124 | - pub position: Option<Position>, | |
| 125 | - #[knuffel(child, unwrap(argument, str))] | |
| 126 | - pub mode: Option<ConfiguredMode>, | |
| 127 | - #[knuffel(child)] | |
| 128 | - pub variable_refresh_rate: Option<Vrr>, | |
| 129 | - #[knuffel(child)] | |
| 130 | - pub focus_at_startup: bool, | |
| 131 | - #[knuffel(child)] | |
| 132 | - pub background_color: Option<Color>, | |
| 133 | - #[knuffel(child)] | |
| 134 | - pub backdrop_color: Option<Color>, | |
| 135 | -} | |
| 136 | - | |
| 137 | -impl Output { | |
| 138 | - pub fn is_vrr_always_on(&self) -> bool { | |
| 139 | - self.variable_refresh_rate == Some(Vrr { on_demand: false }) | |
| 140 | - } | |
| 141 | - | |
| 142 | - pub fn is_vrr_on_demand(&self) -> bool { | |
| 143 | - self.variable_refresh_rate == Some(Vrr { on_demand: true }) | |
| 144 | - } | |
| 145 | - | |
| 146 | - pub fn is_vrr_always_off(&self) -> bool { | |
| 147 | - self.variable_refresh_rate.is_none() | |
| 148 | - } | |
| 149 | -} | |
| 150 | - | |
| 151 | -impl Default for Output { | |
| 152 | - fn default() -> Self { | |
| 153 | - Self { | |
| 154 | - off: false, | |
| 155 | - focus_at_startup: false, | |
| 156 | - name: String::new(), | |
| 157 | - scale: None, | |
| 158 | - transform: Transform::Normal, | |
| 159 | - position: None, | |
| 160 | - mode: None, | |
| 161 | - variable_refresh_rate: None, | |
| 162 | - background_color: None, | |
| 163 | - backdrop_color: None, | |
| 164 | - } | |
| 165 | - } | |
| 166 | -} | |
| 167 | - | |
| 168 | -#[derive(Debug, Clone)] | |
| 169 | -pub struct OutputName { | |
| 170 | - pub connector: String, | |
| 171 | - pub make: Option<String>, | |
| 172 | - pub model: Option<String>, | |
| 173 | - pub serial: Option<String>, | |
| 174 | -} | |
| 175 | - | |
| 176 | -#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq, Eq)] | |
| 177 | -pub struct Position { | |
| 178 | - #[knuffel(property)] | |
| 179 | - pub x: i32, | |
| 180 | - #[knuffel(property)] | |
| 181 | - pub y: i32, | |
| 182 | -} | |
| 183 | - | |
| 184 | -#[derive(knuffel::Decode, Debug, Clone, PartialEq, Default)] | |
| 185 | -pub struct Vrr { | |
| 186 | - #[knuffel(property, default = false)] | |
| 187 | - pub on_demand: bool, | |
| 188 | -} | |
| 189 | - | |
| 190 | 111 | // MIN and MAX generics are only used during parsing to check the value. |
| 191 | 112 | #[derive(Debug, Default, Clone, Copy, PartialEq)] |
| 192 | 113 | pub struct FloatOrInt<const MIN: i32, const MAX: i32>(pub f64); |
| @@ -2263,129 +2184,6 @@ fn expect_only_children<S>( | ||
| 2263 | 2184 | } |
| 2264 | 2185 | } |
| 2265 | 2186 | |
| 2266 | -impl FromIterator<Output> for Outputs { | |
| 2267 | - fn from_iter<T: IntoIterator<Item = Output>>(iter: T) -> Self { | |
| 2268 | - Self(Vec::from_iter(iter)) | |
| 2269 | - } | |
| 2270 | -} | |
| 2271 | - | |
| 2272 | -impl Outputs { | |
| 2273 | - pub fn find(&self, name: &OutputName) -> Option<&Output> { | |
| 2274 | - self.0.iter().find(|o| name.matches(&o.name)) | |
| 2275 | - } | |
| 2276 | - | |
| 2277 | - pub fn find_mut(&mut self, name: &OutputName) -> Option<&mut Output> { | |
| 2278 | - self.0.iter_mut().find(|o| name.matches(&o.name)) | |
| 2279 | - } | |
| 2280 | -} | |
| 2281 | - | |
| 2282 | -impl OutputName { | |
| 2283 | - pub fn from_ipc_output(output: &niri_ipc::Output) -> Self { | |
| 2284 | - Self { | |
| 2285 | - connector: output.name.clone(), | |
| 2286 | - make: (output.make != "Unknown").then(|| output.make.clone()), | |
| 2287 | - model: (output.model != "Unknown").then(|| output.model.clone()), | |
| 2288 | - serial: output.serial.clone(), | |
| 2289 | - } | |
| 2290 | - } | |
| 2291 | - | |
| 2292 | - /// Returns an output description matching what Smithay's `Output::new()` does. | |
| 2293 | - pub fn format_description(&self) -> String { | |
| 2294 | - format!( | |
| 2295 | - "{} - {} - {}", | |
| 2296 | - self.make.as_deref().unwrap_or("Unknown"), | |
| 2297 | - self.model.as_deref().unwrap_or("Unknown"), | |
| 2298 | - self.connector, | |
| 2299 | - ) | |
| 2300 | - } | |
| 2301 | - | |
| 2302 | - /// Returns an output name that will match by make/model/serial or, if they are missing, by | |
| 2303 | - /// connector. | |
| 2304 | - pub fn format_make_model_serial_or_connector(&self) -> String { | |
| 2305 | - if self.make.is_none() && self.model.is_none() && self.serial.is_none() { | |
| 2306 | - self.connector.to_string() | |
| 2307 | - } else { | |
| 2308 | - self.format_make_model_serial() | |
| 2309 | - } | |
| 2310 | - } | |
| 2311 | - | |
| 2312 | - pub fn format_make_model_serial(&self) -> String { | |
| 2313 | - let make = self.make.as_deref().unwrap_or("Unknown"); | |
| 2314 | - let model = self.model.as_deref().unwrap_or("Unknown"); | |
| 2315 | - let serial = self.serial.as_deref().unwrap_or("Unknown"); | |
| 2316 | - format!("{make} {model} {serial}") | |
| 2317 | - } | |
| 2318 | - | |
| 2319 | - pub fn matches(&self, target: &str) -> bool { | |
| 2320 | - // Match by connector. | |
| 2321 | - if target.eq_ignore_ascii_case(&self.connector) { | |
| 2322 | - return true; | |
| 2323 | - } | |
| 2324 | - | |
| 2325 | - // If no other fields are available, don't try to match by them. | |
| 2326 | - // | |
| 2327 | - // This is used by niri msg output. | |
| 2328 | - if self.make.is_none() && self.model.is_none() && self.serial.is_none() { | |
| 2329 | - return false; | |
| 2330 | - } | |
| 2331 | - | |
| 2332 | - // Match by "make model serial" with Unknown if something is missing. | |
| 2333 | - let make = self.make.as_deref().unwrap_or("Unknown"); | |
| 2334 | - let model = self.model.as_deref().unwrap_or("Unknown"); | |
| 2335 | - let serial = self.serial.as_deref().unwrap_or("Unknown"); | |
| 2336 | - | |
| 2337 | - let Some(target_make) = target.get(..make.len()) else { | |
| 2338 | - return false; | |
| 2339 | - }; | |
| 2340 | - let rest = &target[make.len()..]; | |
| 2341 | - if !target_make.eq_ignore_ascii_case(make) { | |
| 2342 | - return false; | |
| 2343 | - } | |
| 2344 | - if !rest.starts_with(' ') { | |
| 2345 | - return false; | |
| 2346 | - } | |
| 2347 | - let rest = &rest[1..]; | |
| 2348 | - | |
| 2349 | - let Some(target_model) = rest.get(..model.len()) else { | |
| 2350 | - return false; | |
| 2351 | - }; | |
| 2352 | - let rest = &rest[model.len()..]; | |
| 2353 | - if !target_model.eq_ignore_ascii_case(model) { | |
| 2354 | - return false; | |
| 2355 | - } | |
| 2356 | - if !rest.starts_with(' ') { | |
| 2357 | - return false; | |
| 2358 | - } | |
| 2359 | - | |
| 2360 | - let rest = &rest[1..]; | |
| 2361 | - if !rest.eq_ignore_ascii_case(serial) { | |
| 2362 | - return false; | |
| 2363 | - } | |
| 2364 | - | |
| 2365 | - true | |
| 2366 | - } | |
| 2367 | - | |
| 2368 | - // Similar in spirit to Ord, but I don't want to derive Eq to avoid mistakes (you should use | |
| 2369 | - // `Self::match`, not Eq). | |
| 2370 | - pub fn compare(&self, other: &Self) -> std::cmp::Ordering { | |
| 2371 | - let self_missing_mms = self.make.is_none() && self.model.is_none() && self.serial.is_none(); | |
| 2372 | - let other_missing_mms = | |
| 2373 | - other.make.is_none() && other.model.is_none() && other.serial.is_none(); | |
| 2374 | - | |
| 2375 | - match (self_missing_mms, other_missing_mms) { | |
| 2376 | - (true, true) => self.connector.cmp(&other.connector), | |
| 2377 | - (true, false) => std::cmp::Ordering::Greater, | |
| 2378 | - (false, true) => std::cmp::Ordering::Less, | |
| 2379 | - (false, false) => self | |
| 2380 | - .make | |
| 2381 | - .cmp(&other.make) | |
| 2382 | - .then_with(|| self.model.cmp(&other.model)) | |
| 2383 | - .then_with(|| self.serial.cmp(&other.serial)) | |
| 2384 | - .then_with(|| self.connector.cmp(&other.connector)), | |
| 2385 | - } | |
| 2386 | - } | |
| 2387 | -} | |
| 2388 | - | |
| 2389 | 2187 | impl<S> knuffel::Decode<S> for DefaultPresetSize |
| 2390 | 2188 | where |
| 2391 | 2189 | S: knuffel::traits::ErrorSpan, |
| @@ -4601,32 +4399,6 @@ mod tests { | ||
| 4601 | 4399 | let _ = Config::default(); |
| 4602 | 4400 | } |
| 4603 | 4401 | |
| 4604 | - #[test] | |
| 4605 | - fn parse_mode() { | |
| 4606 | - assert_eq!( | |
| 4607 | - "2560x1600@165.004".parse::<ConfiguredMode>().unwrap(), | |
| 4608 | - ConfiguredMode { | |
| 4609 | - width: 2560, | |
| 4610 | - height: 1600, | |
| 4611 | - refresh: Some(165.004), | |
| 4612 | - }, | |
| 4613 | - ); | |
| 4614 | - | |
| 4615 | - assert_eq!( | |
| 4616 | - "1920x1080".parse::<ConfiguredMode>().unwrap(), | |
| 4617 | - ConfiguredMode { | |
| 4618 | - width: 1920, | |
| 4619 | - height: 1080, | |
| 4620 | - refresh: None, | |
| 4621 | - }, | |
| 4622 | - ); | |
| 4623 | - | |
| 4624 | - assert!("1920".parse::<ConfiguredMode>().is_err()); | |
| 4625 | - assert!("1920x".parse::<ConfiguredMode>().is_err()); | |
| 4626 | - assert!("1920x1080@".parse::<ConfiguredMode>().is_err()); | |
| 4627 | - assert!("1920x1080@60Hz".parse::<ConfiguredMode>().is_err()); | |
| 4628 | - } | |
| 4629 | - | |
| 4630 | 4402 | #[test] |
| 4631 | 4403 | fn parse_size_change() { |
| 4632 | 4404 | assert_eq!( |
| @@ -4795,125 +4567,6 @@ mod tests { | ||
| 4795 | 4567 | assert_eq!(config.input.keyboard.repeat_rate, 25); |
| 4796 | 4568 | } |
| 4797 | 4569 | |
| 4798 | - fn make_output_name( | |
| 4799 | - connector: &str, | |
| 4800 | - make: Option<&str>, | |
| 4801 | - model: Option<&str>, | |
| 4802 | - serial: Option<&str>, | |
| 4803 | - ) -> OutputName { | |
| 4804 | - OutputName { | |
| 4805 | - connector: connector.to_string(), | |
| 4806 | - make: make.map(|x| x.to_string()), | |
| 4807 | - model: model.map(|x| x.to_string()), | |
| 4808 | - serial: serial.map(|x| x.to_string()), | |
| 4809 | - } | |
| 4810 | - } | |
| 4811 | - | |
| 4812 | - #[test] | |
| 4813 | - fn test_output_name_match() { | |
| 4814 | - fn check( | |
| 4815 | - target: &str, | |
| 4816 | - connector: &str, | |
| 4817 | - make: Option<&str>, | |
| 4818 | - model: Option<&str>, | |
| 4819 | - serial: Option<&str>, | |
| 4820 | - ) -> bool { | |
| 4821 | - let name = make_output_name(connector, make, model, serial); | |
| 4822 | - name.matches(target) | |
| 4823 | - } | |
| 4824 | - | |
| 4825 | - assert!(check("dp-2", "DP-2", None, None, None)); | |
| 4826 | - assert!(!check("dp-1", "DP-2", None, None, None)); | |
| 4827 | - assert!(check("dp-2", "DP-2", Some("a"), Some("b"), Some("c"))); | |
| 4828 | - assert!(check( | |
| 4829 | - "some company some monitor 1234", | |
| 4830 | - "DP-2", | |
| 4831 | - Some("Some Company"), | |
| 4832 | - Some("Some Monitor"), | |
| 4833 | - Some("1234") | |
| 4834 | - )); | |
| 4835 | - assert!(!check( | |
| 4836 | - "some other company some monitor 1234", | |
| 4837 | - "DP-2", | |
| 4838 | - Some("Some Company"), | |
| 4839 | - Some("Some Monitor"), | |
| 4840 | - Some("1234") | |
| 4841 | - )); | |
| 4842 | - assert!(!check( | |
| 4843 | - "make model serial ", | |
| 4844 | - "DP-2", | |
| 4845 | - Some("make"), | |
| 4846 | - Some("model"), | |
| 4847 | - Some("serial") | |
| 4848 | - )); | |
| 4849 | - assert!(check( | |
| 4850 | - "make serial", | |
| 4851 | - "DP-2", | |
| 4852 | - Some("make"), | |
| 4853 | - Some(""), | |
| 4854 | - Some("serial") | |
| 4855 | - )); | |
| 4856 | - assert!(check( | |
| 4857 | - "make model unknown", | |
| 4858 | - "DP-2", | |
| 4859 | - Some("Make"), | |
| 4860 | - Some("Model"), | |
| 4861 | - None | |
| 4862 | - )); | |
| 4863 | - assert!(check( | |
| 4864 | - "unknown unknown serial", | |
| 4865 | - "DP-2", | |
| 4866 | - None, | |
| 4867 | - None, | |
| 4868 | - Some("Serial") | |
| 4869 | - )); | |
| 4870 | - assert!(!check("unknown unknown unknown", "DP-2", None, None, None)); | |
| 4871 | - } | |
| 4872 | - | |
| 4873 | - #[test] | |
| 4874 | - fn test_output_name_sorting() { | |
| 4875 | - let mut names = vec![ | |
| 4876 | - make_output_name("DP-2", None, None, None), | |
| 4877 | - make_output_name("DP-1", None, None, None), | |
| 4878 | - make_output_name("DP-3", Some("B"), Some("A"), Some("A")), | |
| 4879 | - make_output_name("DP-3", Some("A"), Some("B"), Some("A")), | |
| 4880 | - make_output_name("DP-3", Some("A"), Some("A"), Some("B")), | |
| 4881 | - make_output_name("DP-3", None, Some("A"), Some("A")), | |
| 4882 | - make_output_name("DP-3", Some("A"), None, Some("A")), | |
| 4883 | - make_output_name("DP-3", Some("A"), Some("A"), None), | |
| 4884 | - make_output_name("DP-5", Some("A"), Some("A"), Some("A")), | |
| 4885 | - make_output_name("DP-4", Some("A"), Some("A"), Some("A")), | |
| 4886 | - ]; | |
| 4887 | - names.sort_by(|a, b| a.compare(b)); | |
| 4888 | - let names = names | |
| 4889 | - .into_iter() | |
| 4890 | - .map(|name| { | |
| 4891 | - format!( | |
| 4892 | - "{} | {}", | |
| 4893 | - name.format_make_model_serial_or_connector(), | |
| 4894 | - name.connector, | |
| 4895 | - ) | |
| 4896 | - }) | |
| 4897 | - .collect::<Vec<_>>(); | |
| 4898 | - assert_debug_snapshot!( | |
| 4899 | - names, | |
| 4900 | - @r#" | |
| 4901 | - [ | |
| 4902 | - "Unknown A A | DP-3", | |
| 4903 | - "A Unknown A | DP-3", | |
| 4904 | - "A A Unknown | DP-3", | |
| 4905 | - "A A A | DP-4", | |
| 4906 | - "A A A | DP-5", | |
| 4907 | - "A A B | DP-3", | |
| 4908 | - "A B A | DP-3", | |
| 4909 | - "B A A | DP-3", | |
| 4910 | - "DP-1 | DP-1", | |
| 4911 | - "DP-2 | DP-2", | |
| 4912 | - ] | |
| 4913 | - "# | |
| 4914 | - ); | |
| 4915 | - } | |
| 4916 | - | |
| 4917 | 4570 | #[test] |
| 4918 | 4571 | fn test_border_rule_on_off_merging() { |
| 4919 | 4572 | fn is_on(config: &str, rules: &[&str]) -> String { |
niri-config/src/output.rsadded+358 -0
| @@ -0,0 +1,358 @@ | ||
| 1 | +use niri_ipc::{ConfiguredMode, Transform}; | |
| 2 | + | |
| 3 | +use crate::{Color, FloatOrInt}; | |
| 4 | + | |
| 5 | +#[derive(Debug, Default, Clone, PartialEq)] | |
| 6 | +pub struct Outputs(pub Vec<Output>); | |
| 7 | + | |
| 8 | +#[derive(knuffel::Decode, Debug, Clone, PartialEq)] | |
| 9 | +pub struct Output { | |
| 10 | + #[knuffel(child)] | |
| 11 | + pub off: bool, | |
| 12 | + #[knuffel(argument)] | |
| 13 | + pub name: String, | |
| 14 | + #[knuffel(child, unwrap(argument))] | |
| 15 | + pub scale: Option<FloatOrInt<0, 10>>, | |
| 16 | + #[knuffel(child, unwrap(argument, str), default = Transform::Normal)] | |
| 17 | + pub transform: Transform, | |
| 18 | + #[knuffel(child)] | |
| 19 | + pub position: Option<Position>, | |
| 20 | + #[knuffel(child, unwrap(argument, str))] | |
| 21 | + pub mode: Option<ConfiguredMode>, | |
| 22 | + #[knuffel(child)] | |
| 23 | + pub variable_refresh_rate: Option<Vrr>, | |
| 24 | + #[knuffel(child)] | |
| 25 | + pub focus_at_startup: bool, | |
| 26 | + #[knuffel(child)] | |
| 27 | + pub background_color: Option<Color>, | |
| 28 | + #[knuffel(child)] | |
| 29 | + pub backdrop_color: Option<Color>, | |
| 30 | +} | |
| 31 | + | |
| 32 | +impl Output { | |
| 33 | + pub fn is_vrr_always_on(&self) -> bool { | |
| 34 | + self.variable_refresh_rate == Some(Vrr { on_demand: false }) | |
| 35 | + } | |
| 36 | + | |
| 37 | + pub fn is_vrr_on_demand(&self) -> bool { | |
| 38 | + self.variable_refresh_rate == Some(Vrr { on_demand: true }) | |
| 39 | + } | |
| 40 | + | |
| 41 | + pub fn is_vrr_always_off(&self) -> bool { | |
| 42 | + self.variable_refresh_rate.is_none() | |
| 43 | + } | |
| 44 | +} | |
| 45 | + | |
| 46 | +impl Default for Output { | |
| 47 | + fn default() -> Self { | |
| 48 | + Self { | |
| 49 | + off: false, | |
| 50 | + focus_at_startup: false, | |
| 51 | + name: String::new(), | |
| 52 | + scale: None, | |
| 53 | + transform: Transform::Normal, | |
| 54 | + position: None, | |
| 55 | + mode: None, | |
| 56 | + variable_refresh_rate: None, | |
| 57 | + background_color: None, | |
| 58 | + backdrop_color: None, | |
| 59 | + } | |
| 60 | + } | |
| 61 | +} | |
| 62 | + | |
| 63 | +#[derive(Debug, Clone)] | |
| 64 | +pub struct OutputName { | |
| 65 | + pub connector: String, | |
| 66 | + pub make: Option<String>, | |
| 67 | + pub model: Option<String>, | |
| 68 | + pub serial: Option<String>, | |
| 69 | +} | |
| 70 | + | |
| 71 | +#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq, Eq)] | |
| 72 | +pub struct Position { | |
| 73 | + #[knuffel(property)] | |
| 74 | + pub x: i32, | |
| 75 | + #[knuffel(property)] | |
| 76 | + pub y: i32, | |
| 77 | +} | |
| 78 | + | |
| 79 | +#[derive(knuffel::Decode, Debug, Clone, PartialEq, Default)] | |
| 80 | +pub struct Vrr { | |
| 81 | + #[knuffel(property, default = false)] | |
| 82 | + pub on_demand: bool, | |
| 83 | +} | |
| 84 | + | |
| 85 | +impl FromIterator<Output> for Outputs { | |
| 86 | + fn from_iter<T: IntoIterator<Item = Output>>(iter: T) -> Self { | |
| 87 | + Self(Vec::from_iter(iter)) | |
| 88 | + } | |
| 89 | +} | |
| 90 | + | |
| 91 | +impl Outputs { | |
| 92 | + pub fn find(&self, name: &OutputName) -> Option<&Output> { | |
| 93 | + self.0.iter().find(|o| name.matches(&o.name)) | |
| 94 | + } | |
| 95 | + | |
| 96 | + pub fn find_mut(&mut self, name: &OutputName) -> Option<&mut Output> { | |
| 97 | + self.0.iter_mut().find(|o| name.matches(&o.name)) | |
| 98 | + } | |
| 99 | +} | |
| 100 | + | |
| 101 | +impl OutputName { | |
| 102 | + pub fn from_ipc_output(output: &niri_ipc::Output) -> Self { | |
| 103 | + Self { | |
| 104 | + connector: output.name.clone(), | |
| 105 | + make: (output.make != "Unknown").then(|| output.make.clone()), | |
| 106 | + model: (output.model != "Unknown").then(|| output.model.clone()), | |
| 107 | + serial: output.serial.clone(), | |
| 108 | + } | |
| 109 | + } | |
| 110 | + | |
| 111 | + /// Returns an output description matching what Smithay's `Output::new()` does. | |
| 112 | + pub fn format_description(&self) -> String { | |
| 113 | + format!( | |
| 114 | + "{} - {} - {}", | |
| 115 | + self.make.as_deref().unwrap_or("Unknown"), | |
| 116 | + self.model.as_deref().unwrap_or("Unknown"), | |
| 117 | + self.connector, | |
| 118 | + ) | |
| 119 | + } | |
| 120 | + | |
| 121 | + /// Returns an output name that will match by make/model/serial or, if they are missing, by | |
| 122 | + /// connector. | |
| 123 | + pub fn format_make_model_serial_or_connector(&self) -> String { | |
| 124 | + if self.make.is_none() && self.model.is_none() && self.serial.is_none() { | |
| 125 | + self.connector.to_string() | |
| 126 | + } else { | |
| 127 | + self.format_make_model_serial() | |
| 128 | + } | |
| 129 | + } | |
| 130 | + | |
| 131 | + pub fn format_make_model_serial(&self) -> String { | |
| 132 | + let make = self.make.as_deref().unwrap_or("Unknown"); | |
| 133 | + let model = self.model.as_deref().unwrap_or("Unknown"); | |
| 134 | + let serial = self.serial.as_deref().unwrap_or("Unknown"); | |
| 135 | + format!("{make} {model} {serial}") | |
| 136 | + } | |
| 137 | + | |
| 138 | + pub fn matches(&self, target: &str) -> bool { | |
| 139 | + // Match by connector. | |
| 140 | + if target.eq_ignore_ascii_case(&self.connector) { | |
| 141 | + return true; | |
| 142 | + } | |
| 143 | + | |
| 144 | + // If no other fields are available, don't try to match by them. | |
| 145 | + // | |
| 146 | + // This is used by niri msg output. | |
| 147 | + if self.make.is_none() && self.model.is_none() && self.serial.is_none() { | |
| 148 | + return false; | |
| 149 | + } | |
| 150 | + | |
| 151 | + // Match by "make model serial" with Unknown if something is missing. | |
| 152 | + let make = self.make.as_deref().unwrap_or("Unknown"); | |
| 153 | + let model = self.model.as_deref().unwrap_or("Unknown"); | |
| 154 | + let serial = self.serial.as_deref().unwrap_or("Unknown"); | |
| 155 | + | |
| 156 | + let Some(target_make) = target.get(..make.len()) else { | |
| 157 | + return false; | |
| 158 | + }; | |
| 159 | + let rest = &target[make.len()..]; | |
| 160 | + if !target_make.eq_ignore_ascii_case(make) { | |
| 161 | + return false; | |
| 162 | + } | |
| 163 | + if !rest.starts_with(' ') { | |
| 164 | + return false; | |
| 165 | + } | |
| 166 | + let rest = &rest[1..]; | |
| 167 | + | |
| 168 | + let Some(target_model) = rest.get(..model.len()) else { | |
| 169 | + return false; | |
| 170 | + }; | |
| 171 | + let rest = &rest[model.len()..]; | |
| 172 | + if !target_model.eq_ignore_ascii_case(model) { | |
| 173 | + return false; | |
| 174 | + } | |
| 175 | + if !rest.starts_with(' ') { | |
| 176 | + return false; | |
| 177 | + } | |
| 178 | + | |
| 179 | + let rest = &rest[1..]; | |
| 180 | + if !rest.eq_ignore_ascii_case(serial) { | |
| 181 | + return false; | |
| 182 | + } | |
| 183 | + | |
| 184 | + true | |
| 185 | + } | |
| 186 | + | |
| 187 | + // Similar in spirit to Ord, but I don't want to derive Eq to avoid mistakes (you should use | |
| 188 | + // `Self::match`, not Eq). | |
| 189 | + pub fn compare(&self, other: &Self) -> std::cmp::Ordering { | |
| 190 | + let self_missing_mms = self.make.is_none() && self.model.is_none() && self.serial.is_none(); | |
| 191 | + let other_missing_mms = | |
| 192 | + other.make.is_none() && other.model.is_none() && other.serial.is_none(); | |
| 193 | + | |
| 194 | + match (self_missing_mms, other_missing_mms) { | |
| 195 | + (true, true) => self.connector.cmp(&other.connector), | |
| 196 | + (true, false) => std::cmp::Ordering::Greater, | |
| 197 | + (false, true) => std::cmp::Ordering::Less, | |
| 198 | + (false, false) => self | |
| 199 | + .make | |
| 200 | + .cmp(&other.make) | |
| 201 | + .then_with(|| self.model.cmp(&other.model)) | |
| 202 | + .then_with(|| self.serial.cmp(&other.serial)) | |
| 203 | + .then_with(|| self.connector.cmp(&other.connector)), | |
| 204 | + } | |
| 205 | + } | |
| 206 | +} | |
| 207 | + | |
| 208 | +#[cfg(test)] | |
| 209 | +mod tests { | |
| 210 | + use insta::assert_debug_snapshot; | |
| 211 | + | |
| 212 | + use super::*; | |
| 213 | + | |
| 214 | + #[test] | |
| 215 | + fn parse_mode() { | |
| 216 | + assert_eq!( | |
| 217 | + "2560x1600@165.004".parse::<ConfiguredMode>().unwrap(), | |
| 218 | + ConfiguredMode { | |
| 219 | + width: 2560, | |
| 220 | + height: 1600, | |
| 221 | + refresh: Some(165.004), | |
| 222 | + }, | |
| 223 | + ); | |
| 224 | + | |
| 225 | + assert_eq!( | |
| 226 | + "1920x1080".parse::<ConfiguredMode>().unwrap(), | |
| 227 | + ConfiguredMode { | |
| 228 | + width: 1920, | |
| 229 | + height: 1080, | |
| 230 | + refresh: None, | |
| 231 | + }, | |
| 232 | + ); | |
| 233 | + | |
| 234 | + assert!("1920".parse::<ConfiguredMode>().is_err()); | |
| 235 | + assert!("1920x".parse::<ConfiguredMode>().is_err()); | |
| 236 | + assert!("1920x1080@".parse::<ConfiguredMode>().is_err()); | |
| 237 | + assert!("1920x1080@60Hz".parse::<ConfiguredMode>().is_err()); | |
| 238 | + } | |
| 239 | + | |
| 240 | + fn make_output_name( | |
| 241 | + connector: &str, | |
| 242 | + make: Option<&str>, | |
| 243 | + model: Option<&str>, | |
| 244 | + serial: Option<&str>, | |
| 245 | + ) -> OutputName { | |
| 246 | + OutputName { | |
| 247 | + connector: connector.to_string(), | |
| 248 | + make: make.map(|x| x.to_string()), | |
| 249 | + model: model.map(|x| x.to_string()), | |
| 250 | + serial: serial.map(|x| x.to_string()), | |
| 251 | + } | |
| 252 | + } | |
| 253 | + | |
| 254 | + #[test] | |
| 255 | + fn test_output_name_match() { | |
| 256 | + fn check( | |
| 257 | + target: &str, | |
| 258 | + connector: &str, | |
| 259 | + make: Option<&str>, | |
| 260 | + model: Option<&str>, | |
| 261 | + serial: Option<&str>, | |
| 262 | + ) -> bool { | |
| 263 | + let name = make_output_name(connector, make, model, serial); | |
| 264 | + name.matches(target) | |
| 265 | + } | |
| 266 | + | |
| 267 | + assert!(check("dp-2", "DP-2", None, None, None)); | |
| 268 | + assert!(!check("dp-1", "DP-2", None, None, None)); | |
| 269 | + assert!(check("dp-2", "DP-2", Some("a"), Some("b"), Some("c"))); | |
| 270 | + assert!(check( | |
| 271 | + "some company some monitor 1234", | |
| 272 | + "DP-2", | |
| 273 | + Some("Some Company"), | |
| 274 | + Some("Some Monitor"), | |
| 275 | + Some("1234") | |
| 276 | + )); | |
| 277 | + assert!(!check( | |
| 278 | + "some other company some monitor 1234", | |
| 279 | + "DP-2", | |
| 280 | + Some("Some Company"), | |
| 281 | + Some("Some Monitor"), | |
| 282 | + Some("1234") | |
| 283 | + )); | |
| 284 | + assert!(!check( | |
| 285 | + "make model serial ", | |
| 286 | + "DP-2", | |
| 287 | + Some("make"), | |
| 288 | + Some("model"), | |
| 289 | + Some("serial") | |
| 290 | + )); | |
| 291 | + assert!(check( | |
| 292 | + "make serial", | |
| 293 | + "DP-2", | |
| 294 | + Some("make"), | |
| 295 | + Some(""), | |
| 296 | + Some("serial") | |
| 297 | + )); | |
| 298 | + assert!(check( | |
| 299 | + "make model unknown", | |
| 300 | + "DP-2", | |
| 301 | + Some("Make"), | |
| 302 | + Some("Model"), | |
| 303 | + None | |
| 304 | + )); | |
| 305 | + assert!(check( | |
| 306 | + "unknown unknown serial", | |
| 307 | + "DP-2", | |
| 308 | + None, | |
| 309 | + None, | |
| 310 | + Some("Serial") | |
| 311 | + )); | |
| 312 | + assert!(!check("unknown unknown unknown", "DP-2", None, None, None)); | |
| 313 | + } | |
| 314 | + | |
| 315 | + #[test] | |
| 316 | + fn test_output_name_sorting() { | |
| 317 | + let mut names = vec![ | |
| 318 | + make_output_name("DP-2", None, None, None), | |
| 319 | + make_output_name("DP-1", None, None, None), | |
| 320 | + make_output_name("DP-3", Some("B"), Some("A"), Some("A")), | |
| 321 | + make_output_name("DP-3", Some("A"), Some("B"), Some("A")), | |
| 322 | + make_output_name("DP-3", Some("A"), Some("A"), Some("B")), | |
| 323 | + make_output_name("DP-3", None, Some("A"), Some("A")), | |
| 324 | + make_output_name("DP-3", Some("A"), None, Some("A")), | |
| 325 | + make_output_name("DP-3", Some("A"), Some("A"), None), | |
| 326 | + make_output_name("DP-5", Some("A"), Some("A"), Some("A")), | |
| 327 | + make_output_name("DP-4", Some("A"), Some("A"), Some("A")), | |
| 328 | + ]; | |
| 329 | + names.sort_by(|a, b| a.compare(b)); | |
| 330 | + let names = names | |
| 331 | + .into_iter() | |
| 332 | + .map(|name| { | |
| 333 | + format!( | |
| 334 | + "{} | {}", | |
| 335 | + name.format_make_model_serial_or_connector(), | |
| 336 | + name.connector, | |
| 337 | + ) | |
| 338 | + }) | |
| 339 | + .collect::<Vec<_>>(); | |
| 340 | + assert_debug_snapshot!( | |
| 341 | + names, | |
| 342 | + @r#" | |
| 343 | + [ | |
| 344 | + "Unknown A A | DP-3", | |
| 345 | + "A Unknown A | DP-3", | |
| 346 | + "A A Unknown | DP-3", | |
| 347 | + "A A A | DP-4", | |
| 348 | + "A A A | DP-5", | |
| 349 | + "A A B | DP-3", | |
| 350 | + "A B A | DP-3", | |
| 351 | + "B A A | DP-3", | |
| 352 | + "DP-1 | DP-1", | |
| 353 | + "DP-2 | DP-2", | |
| 354 | + ] | |
| 355 | + "# | |
| 356 | + ); | |
| 357 | + } | |
| 358 | +} |