This commit is contained in:
2023-08-23 00:53:41 +00:00
commit 0745b69d81
3 changed files with 59 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

9
Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[package]
name = "swayout"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
json = "0.12.4"

49
src/main.rs Normal file
View File

@@ -0,0 +1,49 @@
use std::process::Command;
use std::str;
use json;
use json::JsonValue;
enum SwayOutput {
Disabled(String),
Enabled(String,i32,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<SwayOutput> = Vec::new();
let json = json::parse(s).unwrap();
let json_outputs = match json {
JsonValue::Array(vec) => vec,
_ => panic!("json was not an array")
};
let size = json_outputs.len();
println!("output count: {}", size);
if size == 1 {
println!("one output, use it");
let json_output = &json_outputs[0];
//println!("output:{:#}", output);
let name = &json_output["name"];
let mode = &json_output["modes"][0];
//println!("mode:{:#}",mode);
println!("swaymsg output {} mode {}x{}", name, mode["width"], mode["height"]);
outputs.push(SwayOutput::Enabled(name.to_string(), 1, 1));
}
for output in &outputs {
match output {
SwayOutput::Disabled(name) => {
println!("swaymsg output {} disabled", name);
},
SwayOutput::Enabled(name,w,h) => {
println!("swaymsg output {} mode {}x{}", name, w, h);
}
}
}
}