use std::process::Command; use std::str; use json; use json::JsonValue; enum SwayOutput { Disabled { name: String, }, Enabled { name: String, width: i32, height: i32, x: i32, y: i32, } } fn main() { let output = Command::new("swaymsg") .arg("-t") .arg("get_outputs") .output() .expect("Failed to execute command"); let s = str::from_utf8(&output.stdout).unwrap(); let mut outputs: Vec = Vec::new(); let json = json::parse(s).unwrap(); // TODO I think there is a better alternative syntax to match here. let json_outputs = match json { JsonValue::Array(vec) => vec, _ => panic!("json was not an array") }; let size = json_outputs.len(); if size == 1 { // If there is 1 output, use it. let json_output = &json_outputs[0]; let name = &json_output["name"]; let mode = &json_output["modes"][0]; outputs.push(SwayOutput::Enabled { name: name.to_string(), width: mode["width"].as_i32().unwrap(), height: mode["height"].as_i32().unwrap(), x: 0, y: 0, }); } else { let dell_option:Option<&JsonValue> = json_outputs.iter() .find( |json_output| json_output["serial"].to_string() == "CFV9N99T0Y0U"); if dell_option.is_some() { // The Dell on my desk is attached. Use it, and disable others. let dell = dell_option.unwrap(); outputs.push(SwayOutput::Enabled { name: dell["name"].to_string(), width: 1920, height: 1200, x: 0, y: 0 }); json_outputs.iter() .filter( |json_output| { json_output["serial"].to_string() != "CFV9N99T0Y0U" }) .for_each( |json_output| { outputs.push(SwayOutput::Disabled { name: json_output["name"].to_string() }); }); } else { println!("unknown state, do nothing") } } // TODO do all enables then all disables. // TODO if there are only disables then do nothing. for output in &outputs { match output { SwayOutput::Disabled { name } => { println!("swaymsg output {} disabled", name); // TODO run }, SwayOutput::Enabled { name, width, height, x, y } => { println!( "swaymsg output {} enable dpms on mode {}x{} pos {} {}", name, width, height, x, y); // TODO run } } } }