panic if trying to enable a closed monitor

This commit is contained in:
2025-01-01 18:24:26 +00:00
parent 818df0f7e8
commit 6004c1a2d3
5 changed files with 41 additions and 23 deletions

2
Cargo.lock generated
View File

@@ -192,7 +192,7 @@ dependencies = [
[[package]]
name = "swayout"
version = "1.2.0"
version = "1.2.1"
dependencies = [
"kdl",
"serde",

View File

@@ -1,6 +1,6 @@
[package]
name = "swayout"
version = "1.2.0"
version = "1.2.1"
edition = "2021"
description = "A tool to configure sway outputs"
authors = ["Stephen Byrne"]

View File

@@ -133,9 +133,12 @@ pub fn print_layout_names() {
/// Apply the specified layout.
/// layout_name may be the name of a layout in the config, the name of a monitor,
/// or the name of an output.
pub fn apply_layout(layout_name: &String) {
/// Return true of any outputs were enabled.
/// Return false if there were no outputs to enable and thus nothing was done.
pub fn apply_layout(layout_name: &String) -> bool {
let config = get_config();
let outputs = get_outputs();
let monitor_states = get_monitor_states(&config, &outputs);
// Map of output name to config for all outputs to enable.
// (All outputs not in this map will be disabled.)
@@ -147,55 +150,61 @@ pub fn apply_layout(layout_name: &String) {
.iter()
.find(|layout| &layout.name == layout_name)
{
// The layout is defined in the rules.
// The layout is defined in the config.
layout
.outputs
.iter()
.for_each(|(monitor_name, output_config)| {
let monitor = &config
.monitors
.iter()
.find(|monitor| &monitor.name == monitor_name)
.unwrap();
let monitor_state = get_monitor_state_by_name(&monitor_states, &monitor_name)
.unwrap_or_else(||panic!("Cannot find monitor '{}' for layout '{}'", monitor_name, layout_name));
if monitor_state.is_lid_closed {
panic!("Monitor '{}' is closed", monitor_name);
}
// find the output for the monitor to get the output name.
let output_opt = outputs
.iter()
.find(|output| monitor_matches_output(monitor, output));
.find(|output| monitor_matches_output(monitor_state.monitor, output));
if let Some(output) = output_opt {
output_config_map.insert(&output.name, &output_config);
} else {
panic!("Missing output for monitor {monitor_name}");
panic!("Missing output for monitor '{monitor_name}'");
}
});
} else {
// The layout is not defined in the rules.
// See if it is a monitor name...
if let Some(monitor) = config
.monitors
.iter()
.find(|monitor| &monitor.name == layout_name)
{
if let Some(monitor_state) = get_monitor_state_by_name(&monitor_states, &layout_name) {
if monitor_state.is_lid_closed {
panic!("Monitor '{}' is closed", &monitor_state.monitor.name);
}
// It is a monitor name. Find the matching output...
if let Some(output) = outputs
.iter()
.find(|output| monitor_matches_output(monitor, output))
.find(|output| monitor_matches_output(monitor_state.monitor, output))
{
output_config_map.insert(&output.name, &output.output_config);
} else {
panic!("could not find output for monitor {layout_name}")
panic!("Could not find output for monitor '{layout_name}'")
}
} else {
// 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}")
panic!("Could not find layout, monitor, or output '{layout_name}'")
}
}
}
apply_outputs(&outputs, &output_config_map);
if ! output_config_map.is_empty() {
apply_outputs(&outputs, &output_config_map);
true
} else {
false
}
}
/// Apply the first automatic layout for which all outputs are available.
@@ -210,8 +219,11 @@ pub fn apply_automatic() -> Option<String> {
.iter()
.find(|layout| layout.automatic)
{
apply_layout(&layout.name);
Some(String::from(&layout.name))
if apply_layout(&layout.name) {
Some(String::from(&layout.name))
} else {
None
}
} else {
None
}

View File

@@ -15,7 +15,9 @@ fn main() {
process::exit(2)
}
} else {
swayout::apply_layout(&args[1]);
if ! swayout::apply_layout(&args[1]) {
process::exit(3)
}
}
} else {
eprintln!("Usage: {} [layout]", args[0].as_str());

View File

@@ -84,6 +84,10 @@ pub fn apply_outputs(all_outputs: &Vec<Output>, outputs: &HashMap<&String, &Outp
}
});
if enabled.is_empty() {
panic!("No enabled outputs!");
}
let mut cmd = Command::new("swaymsg");
enabled.iter().for_each(|(output_name, output_config)| {
cmd.arg("output");