show layouts and set layout
This commit is contained in:
347
src/main.rs
347
src/main.rs
@@ -1,98 +1,281 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{from_str, Value};
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
use std::process::Command;
|
||||
use std::str;
|
||||
use json;
|
||||
use json::JsonValue;
|
||||
use std::{fs, str};
|
||||
|
||||
enum SwayOutput {
|
||||
Disabled {
|
||||
name: String,
|
||||
},
|
||||
Enabled {
|
||||
name: String,
|
||||
width: i32,
|
||||
height: i32,
|
||||
x: i32,
|
||||
y: i32,
|
||||
// parse the config to this
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Rules {
|
||||
monitors: HashMap<String, Monitor>,
|
||||
// Map of layout name to : map of monitor name to output config
|
||||
layouts: HashMap<String, HashMap<String, OutputConfig>>,
|
||||
}
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Monitor {
|
||||
make: String,
|
||||
model: String,
|
||||
serial: String,
|
||||
}
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct OutputConfig {
|
||||
mode: String,
|
||||
scale: String,
|
||||
x: u16,
|
||||
y: u16,
|
||||
transform: String,
|
||||
}
|
||||
|
||||
// parse output of swaymsg -t get_outputs to this
|
||||
struct Output {
|
||||
name: String,
|
||||
make: String,
|
||||
model: String,
|
||||
serial: String,
|
||||
output_config: OutputConfig,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() == 1 {
|
||||
// first arg is executable
|
||||
print_layout_names();
|
||||
} else if args.len() == 2 {
|
||||
apply_layout(&args[1]);
|
||||
} else {
|
||||
panic!("Usage: {} [layout]", args[0].as_str());
|
||||
}
|
||||
}
|
||||
fn main() {
|
||||
|
||||
/// Get all outputs currently available
|
||||
fn get_outputs() -> Vec<Output> {
|
||||
// call `swaymsg -t get_outputs` to get outputs
|
||||
let output = Command::new("swaymsg")
|
||||
.arg("-t")
|
||||
.arg("get_outputs")
|
||||
.output()
|
||||
.expect("Failed to execute command");
|
||||
let s = str::from_utf8(&output.stdout).unwrap();
|
||||
.arg("-t")
|
||||
.arg("get_outputs")
|
||||
.output()
|
||||
.expect("swaymsg -t get_outputs failed");
|
||||
let json_string = str::from_utf8(&output.stdout).unwrap();
|
||||
let json = from_str(json_string).unwrap();
|
||||
|
||||
let mut outputs: Vec<SwayOutput> = Vec::new();
|
||||
if let Value::Array(outputs) = json {
|
||||
outputs
|
||||
.iter()
|
||||
.map(|output_json| Output {
|
||||
name: String::from(output_json["name"].as_str().unwrap()),
|
||||
make: String::from(output_json["make"].as_str().unwrap()),
|
||||
model: String::from(output_json["model"].as_str().unwrap()),
|
||||
serial: String::from(output_json["serial"].as_str().unwrap()),
|
||||
output_config: OutputConfig {
|
||||
mode: get_mode(output_json),
|
||||
scale: String::from("1.0"),
|
||||
x: 0,
|
||||
y: 0,
|
||||
transform: String::from("normal"),
|
||||
},
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
panic!("outputs json was not an array");
|
||||
}
|
||||
}
|
||||
|
||||
let json = json::parse(s).unwrap();
|
||||
|
||||
let json_outputs =
|
||||
if let JsonValue::Array(vec) = json {
|
||||
vec
|
||||
/// Parse the mode from the output JSON into a string suitable for the mode param for sway-output.
|
||||
/// This will go into OutputConfig.mode
|
||||
fn get_mode(output: &Value) -> String {
|
||||
if let Value::Array(modes) = &output["modes"] {
|
||||
if let Some(mode) = modes.first() {
|
||||
let width = mode["width"].as_i64().unwrap().to_string();
|
||||
let height = mode["height"].as_i64().unwrap().to_string();
|
||||
let mut out: String = String::new();
|
||||
out.push_str(&width);
|
||||
out.push_str("x");
|
||||
out.push_str(&height);
|
||||
out
|
||||
} else {
|
||||
panic!("json is not an array");
|
||||
};
|
||||
panic!("no modes")
|
||||
}
|
||||
} else {
|
||||
panic!("modes 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,
|
||||
/// Get the rules from ~/.config/swayout/config.toml
|
||||
fn get_rules() -> Rules {
|
||||
let xdg_dirs = xdg::BaseDirectories::with_prefix("swayout").unwrap();
|
||||
if let Some(path) = xdg_dirs.find_config_file("config.toml") {
|
||||
let toml = fs::read_to_string(path).expect("error reading file");
|
||||
toml::from_str(&toml).expect("toml parse error")
|
||||
} else {
|
||||
// no config files, use an empty rules
|
||||
Rules {
|
||||
monitors: HashMap::new(),
|
||||
layouts: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Determine the available layout names.
|
||||
/// Return one layout for each layout defined in the configuration file
|
||||
/// (for which all outputs are available),
|
||||
/// one for each monitor defined in the configuration file
|
||||
/// (for using just that monitor),
|
||||
/// and one for each available output that is not a configured monitor (for using just that output).
|
||||
fn get_layouts() -> Vec<String> {
|
||||
let available_outputs = get_outputs();
|
||||
let rules = get_rules();
|
||||
|
||||
let mut layout_names: Vec<String> = Vec::new();
|
||||
|
||||
// Get the names of monitors that are available (that match a current output)
|
||||
let mut available_monitor_names: Vec<String> = Vec::new();
|
||||
// TODO do this with iterators
|
||||
rules.monitors.iter().for_each(|(monitor_name, monitor)| {
|
||||
if available_outputs.iter().any(|output| {
|
||||
monitor.make == output.make
|
||||
&& monitor.model == output.model
|
||||
&& output.serial == monitor.serial
|
||||
}) {
|
||||
available_monitor_names.push(String::from(monitor_name));
|
||||
}
|
||||
});
|
||||
|
||||
// Add each layout defined in the config file for which all outputs are available
|
||||
rules.layouts.iter().for_each(|(layout_name, layout)| {
|
||||
if layout
|
||||
.iter()
|
||||
.all(|(output_name, _output)| available_monitor_names.contains(output_name))
|
||||
{
|
||||
layout_names.push(String::from(layout_name))
|
||||
}
|
||||
});
|
||||
|
||||
// add each individual output (by monitor name if the monitor is defined, else by output name)
|
||||
available_outputs.iter().for_each(|output| {
|
||||
// if there is a monitor for the output, use the monitor name, else use the output name
|
||||
let monitor_opt = rules.monitors.iter()
|
||||
.find(|(_monitor_name, monitor)| {
|
||||
monitor.make == output.make
|
||||
&& monitor.model == output.model
|
||||
&& monitor.serial == output.serial
|
||||
});
|
||||
if let Some((monitor_name, _monitor)) = monitor_opt {
|
||||
layout_names.push(String::from(monitor_name));
|
||||
} else {
|
||||
layout_names.push(String::from(&output.name));
|
||||
}
|
||||
});
|
||||
|
||||
layout_names
|
||||
}
|
||||
|
||||
/// Print the name of each layout to standard out, one per line
|
||||
fn print_layout_names() {
|
||||
get_layouts().iter().for_each(|layout| println!("{layout}"))
|
||||
}
|
||||
|
||||
/// Apply the specified layout.
|
||||
/// layout_name may be the name of a layout rule, the name of a monitor, or the name of an output.
|
||||
fn apply_layout(layout_name: &String) {
|
||||
let rules = get_rules();
|
||||
let outputs = get_outputs();
|
||||
|
||||
// Map of output name to config for all outputs to enable.
|
||||
// (All outputs not in this map will be disabled.)
|
||||
let mut output_config_map: HashMap<&String, &OutputConfig> = HashMap::new();
|
||||
|
||||
// First check if the layout is defined in the rules
|
||||
let opt_layout = rules.layouts.get(layout_name);
|
||||
if let Some(layout) = opt_layout {
|
||||
// The layout is defined in the rules.
|
||||
layout.iter().for_each(|(monitor_name, output_config)| {
|
||||
let monitor = &rules.monitors[monitor_name];
|
||||
|
||||
// find the output for the monitor to get the output name.
|
||||
// (You can allegedly use "<make> <model> <serial>" instead of output name.)
|
||||
let output_opt = outputs.iter().find(|output| {
|
||||
output.make == monitor.make
|
||||
&& output.model == monitor.model
|
||||
&& output.serial == monitor.serial
|
||||
});
|
||||
if let Some(output) = output_opt {
|
||||
output_config_map.insert(&output.name, &output_config);
|
||||
} else {
|
||||
panic!("Missing output for monitor {monitor_name}");
|
||||
}
|
||||
});
|
||||
} 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()
|
||||
});
|
||||
});
|
||||
// The layout is not defined in the rules.
|
||||
// See if it is a monitor name...
|
||||
if let Some(monitor) = rules.monitors.get(layout_name) {
|
||||
// It is a monitor name. Find the matching output...
|
||||
if let Some(output) = outputs.iter().find(|output| {
|
||||
output.make == monitor.make
|
||||
&& output.model == monitor.model
|
||||
&& output.serial == monitor.serial
|
||||
}) {
|
||||
output_config_map.insert(&output.name, &output.output_config);
|
||||
} else {
|
||||
panic!("could not find output for monitor {layout_name}")
|
||||
}
|
||||
} else {
|
||||
println!("unknown state, do nothing");
|
||||
// See if it is an output name...
|
||||
if let Some(output) = outputs.iter()
|
||||
.find(|output| &output.name == layout_name) {
|
||||
output_config_map.insert(&output.name, &output.output_config);
|
||||
} else {
|
||||
panic!("could not find layout, monitor, or output {layout_name}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let enabled_count = outputs.iter()
|
||||
.filter(|output| matches!(output,SwayOutput::Enabled{name:_,width:_,height:_,x:_,y:_}))
|
||||
.count();
|
||||
if enabled_count >= 1 {
|
||||
// enable then disable
|
||||
outputs.iter()
|
||||
.for_each(|output| {
|
||||
if let SwayOutput::Enabled{name,width,height,x,y} = output {
|
||||
println!("swaymsg output {} enable dpms on mode {}x{} pos {} {}",
|
||||
name, width, height, x, y);
|
||||
}
|
||||
});
|
||||
outputs.iter()
|
||||
.for_each(|output| {
|
||||
if let SwayOutput::Disabled{name} = output {
|
||||
println!("swaymsg output {} disabled", name);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
println!("No enabled outputs, do nothing");
|
||||
}
|
||||
apply_outputs(&outputs, &output_config_map);
|
||||
}
|
||||
|
||||
/// Apply the specified outputs. Enable all outputs in [outputs], disable others.
|
||||
fn apply_outputs(all_outputs: &Vec<Output>, outputs: &HashMap<&String, &OutputConfig>) {
|
||||
let mut cmd = Command::new("swaymsg");
|
||||
|
||||
// enabled first, then disabled.
|
||||
// That way if some work before an error, you at least have one output enabled.
|
||||
|
||||
let mut enabled: HashMap<&String,&OutputConfig> = HashMap::new();
|
||||
let mut disabled: Vec<&String> = Vec::new();
|
||||
|
||||
all_outputs.iter().for_each(|output| {
|
||||
if let Some(output_config) = outputs.get(&output.name) {
|
||||
enabled.insert(&output.name, &output_config);
|
||||
} else {
|
||||
disabled.push(&output.name);
|
||||
}
|
||||
});
|
||||
|
||||
enabled.iter().for_each(|(output_name,output_config)| {
|
||||
cmd.arg("output");
|
||||
cmd.arg(&output_name);
|
||||
cmd.arg("enable");
|
||||
cmd.arg("mode");
|
||||
cmd.arg(&output_config.mode);
|
||||
cmd.arg("scale");
|
||||
cmd.arg(&output_config.scale);
|
||||
cmd.arg("pos");
|
||||
cmd.arg(&output_config.x.to_string());
|
||||
cmd.arg(&output_config.y.to_string());
|
||||
cmd.arg("transform");
|
||||
cmd.arg(&output_config.transform);
|
||||
cmd.arg(",");
|
||||
});
|
||||
|
||||
disabled.iter().for_each(|output_name| {
|
||||
cmd.arg("output");
|
||||
cmd.arg(&output_name);
|
||||
cmd.arg("disable");
|
||||
cmd.arg(",");
|
||||
});
|
||||
|
||||
cmd.get_args()
|
||||
.for_each(|arg| print!("{} ", arg.to_str().unwrap()));
|
||||
|
||||
cmd.output().expect("swaymsg output failed");
|
||||
}
|
||||
Reference in New Issue
Block a user