Dynamically map connection.

This commit is contained in:
Bill Thiede 2020-11-05 18:02:19 -08:00
parent ab33174956
commit a39c6c78da
2 changed files with 26 additions and 25 deletions

View File

@ -80,15 +80,14 @@ impl fmt::Debug for Orientation {
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Screen { pub struct Screen {
pub name: Option<String>, pub name: String,
pub connection: String,
pub resolution: Resolution, pub resolution: Resolution,
pub offset: Offset, pub offset: Offset,
pub orientation: Orientation, pub orientation: Orientation,
} }
impl fmt::Display for Screen { impl Screen {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn metamode(&self, map: &ScreenMapping) -> Result<String, CommandError> {
let Resolution { width, height } = self.resolution; let Resolution { width, height } = self.resolution;
let Offset { x, y } = self.offset; let Offset { x, y } = self.offset;
let (in_w, in_h) = match self.orientation { let (in_w, in_h) = match self.orientation {
@ -102,8 +101,13 @@ impl fmt::Display for Screen {
Orientation::Left => "90", Orientation::Left => "90",
}; };
write!(f, "{connection}: {w}x{h} @{in_w}x{in_h} +{x}+{y} {{ViewPortIn={in_w}x{in_h}, ViewPortOut={w}x{h}+0+0, Rotation={rotation}}}", let connection = match map.get(&self.name) {
connection=self.connection, Some(connection) => connection,
None => return Err(CommandError::MissingMonitor(self.name.to_string())),
};
Ok(format!("{connection}: {w}x{h} @{in_w}x{in_h} +{x}+{y} {{ViewPortIn={in_w}x{in_h}, ViewPortOut={w}x{h}+0+0, Rotation={rotation}}}",
connection=connection,
w=width, w=width,
h=height, h=height,
in_w=in_w, in_w=in_w,
@ -111,7 +115,7 @@ impl fmt::Display for Screen {
x=x, x=x,
y=y, y=y,
rotation=rotation, rotation=rotation,
) ))
} }
} }
@ -120,7 +124,7 @@ pub struct Config {
pub screens: Vec<Screen>, pub screens: Vec<Screen>,
} }
pub fn run_cmd(screen_mapping: ScreenMapping, cfg: Config) -> Result<Output, CommandError> { pub fn run_cmd(screen_mapping: &ScreenMapping, cfg: Config) -> Result<Output, CommandError> {
let args = build_cmd_args(screen_mapping, cfg)?; let args = build_cmd_args(screen_mapping, cfg)?;
if cfg!(debug_assertions) { if cfg!(debug_assertions) {
Ok(Command::new("echo").args(args).output()?) Ok(Command::new("echo").args(args).output()?)
@ -129,12 +133,15 @@ pub fn run_cmd(screen_mapping: ScreenMapping, cfg: Config) -> Result<Output, Com
} }
} }
fn build_cmd_args(screen_mapping: ScreenMapping, cfg: Config) -> Result<Vec<String>, CommandError> { fn build_cmd_args(
screen_mapping: &ScreenMapping,
cfg: Config,
) -> Result<Vec<String>, CommandError> {
let metamode = cfg let metamode = cfg
.screens .screens
.iter() .iter()
.map(|c| c.to_string()) .map(|c| c.metamode(screen_mapping))
.collect::<Vec<String>>() .collect::<Result<Vec<String>, CommandError>>()?
.join(", "); .join(", ");
Ok(vec![ Ok(vec![
"nvidia-settings", "nvidia-settings",
@ -155,8 +162,7 @@ mod tests {
let cfg = Config { let cfg = Config {
screens: vec![ screens: vec![
Screen { Screen {
name: Some("DELL U2415".to_string()), name: "DELL U2415".to_string(),
connection: "DFP-5.8".to_string(),
resolution: Resolution { resolution: Resolution {
width: 1920, width: 1920,
height: 1200, height: 1200,
@ -165,8 +171,7 @@ mod tests {
..Default::default() ..Default::default()
}, },
Screen { Screen {
name: Some("LG Electronics 34UM95".to_string()), name: "LG Electronics 34UM95".to_string(),
connection: "DFP-0.1".to_string(),
resolution: Resolution { resolution: Resolution {
width: 3440, width: 3440,
height: 1440, height: 1440,
@ -175,8 +180,7 @@ mod tests {
..Default::default() ..Default::default()
}, },
Screen { Screen {
name: Some("Lenovo Group Limited P27h-20".to_string()), name: "Lenovo Group Limited P27h-20".to_string(),
connection: "DFP-0.8".to_string(),
resolution: Resolution { resolution: Resolution {
width: 2560, width: 2560,
height: 1440, height: 1440,
@ -195,7 +199,7 @@ mod tests {
.map(|(k, v)| (k.to_string(), v.to_string())) .map(|(k, v)| (k.to_string(), v.to_string()))
.collect(); .collect();
assert_eq!( assert_eq!(
build_cmd_args(map, cfg).expect("failed build_cmd_args"), build_cmd_args(&map, cfg).expect("failed build_cmd_args"),
vec![ vec![
"nvidia-settings", "nvidia-settings",
"--assign", "--assign",

View File

@ -6,8 +6,7 @@ fn main() -> Result<()> {
let cfg = Config { let cfg = Config {
screens: vec![ screens: vec![
Screen { Screen {
name: Some("DELL U2415".to_string()), name: ("DELL U2415".to_string()),
connection: "DFP-5.8".to_string(),
resolution: Resolution { resolution: Resolution {
width: 1920, width: 1920,
height: 1200, height: 1200,
@ -16,8 +15,7 @@ fn main() -> Result<()> {
..Default::default() ..Default::default()
}, },
Screen { Screen {
name: Some("LG Electronics 34UM95".to_string()), name: ("LG Electronics 34UM95".to_string()),
connection: "DFP-0.1".to_string(),
resolution: Resolution { resolution: Resolution {
width: 3440, width: 3440,
height: 1440, height: 1440,
@ -26,8 +24,7 @@ fn main() -> Result<()> {
..Default::default() ..Default::default()
}, },
Screen { Screen {
name: Some("Lenovo Group Limited P27h-20".to_string()), name: ("Lenovo Group Limited P27h-20".to_string()),
connection: "DFP-0.8".to_string(),
resolution: Resolution { resolution: Resolution {
width: 2560, width: 2560,
height: 1440, height: 1440,
@ -39,7 +36,7 @@ fn main() -> Result<()> {
}; };
let map = screen_mapping_from_xorg_log("/var/log/Xorg.0.log")?; let map = screen_mapping_from_xorg_log("/var/log/Xorg.0.log")?;
let cmd = run_cmd(map, cfg)?; let cmd = run_cmd(&map, cfg)?;
println!("cmd {:#?}", cmd); println!("cmd {:#?}", cmd);
Ok(()) Ok(())