panic if trying to enable a closed monitor
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -192,7 +192,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "swayout"
|
name = "swayout"
|
||||||
version = "1.2.0"
|
version = "1.2.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"kdl",
|
"kdl",
|
||||||
"serde",
|
"serde",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "swayout"
|
name = "swayout"
|
||||||
version = "1.2.0"
|
version = "1.2.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "A tool to configure sway outputs"
|
description = "A tool to configure sway outputs"
|
||||||
authors = ["Stephen Byrne"]
|
authors = ["Stephen Byrne"]
|
||||||
|
|||||||
52
src/lib.rs
52
src/lib.rs
@@ -133,9 +133,12 @@ pub fn print_layout_names() {
|
|||||||
/// Apply the specified layout.
|
/// Apply the specified layout.
|
||||||
/// layout_name may be the name of a layout in the config, the name of a monitor,
|
/// layout_name may be the name of a layout in the config, the name of a monitor,
|
||||||
/// or the name of an output.
|
/// 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 config = get_config();
|
||||||
let outputs = get_outputs();
|
let outputs = get_outputs();
|
||||||
|
let monitor_states = get_monitor_states(&config, &outputs);
|
||||||
|
|
||||||
// Map of output name to config for all outputs to enable.
|
// Map of output name to config for all outputs to enable.
|
||||||
// (All outputs not in this map will be disabled.)
|
// (All outputs not in this map will be disabled.)
|
||||||
@@ -147,55 +150,61 @@ pub fn apply_layout(layout_name: &String) {
|
|||||||
.iter()
|
.iter()
|
||||||
.find(|layout| &layout.name == layout_name)
|
.find(|layout| &layout.name == layout_name)
|
||||||
{
|
{
|
||||||
// The layout is defined in the rules.
|
// The layout is defined in the config.
|
||||||
layout
|
layout
|
||||||
.outputs
|
.outputs
|
||||||
.iter()
|
.iter()
|
||||||
.for_each(|(monitor_name, output_config)| {
|
.for_each(|(monitor_name, output_config)| {
|
||||||
let monitor = &config
|
let monitor_state = get_monitor_state_by_name(&monitor_states, &monitor_name)
|
||||||
.monitors
|
.unwrap_or_else(||panic!("Cannot find monitor '{}' for layout '{}'", monitor_name, layout_name));
|
||||||
.iter()
|
|
||||||
.find(|monitor| &monitor.name == monitor_name)
|
if monitor_state.is_lid_closed {
|
||||||
.unwrap();
|
panic!("Monitor '{}' is closed", monitor_name);
|
||||||
|
}
|
||||||
|
|
||||||
// find the output for the monitor to get the output name.
|
// find the output for the monitor to get the output name.
|
||||||
let output_opt = outputs
|
let output_opt = outputs
|
||||||
.iter()
|
.iter()
|
||||||
.find(|output| monitor_matches_output(monitor, output));
|
.find(|output| monitor_matches_output(monitor_state.monitor, output));
|
||||||
if let Some(output) = output_opt {
|
if let Some(output) = output_opt {
|
||||||
output_config_map.insert(&output.name, &output_config);
|
output_config_map.insert(&output.name, &output_config);
|
||||||
} else {
|
} else {
|
||||||
panic!("Missing output for monitor {monitor_name}");
|
panic!("Missing output for monitor '{monitor_name}'");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// The layout is not defined in the rules.
|
// The layout is not defined in the rules.
|
||||||
// See if it is a monitor name...
|
// See if it is a monitor name...
|
||||||
if let Some(monitor) = config
|
if let Some(monitor_state) = get_monitor_state_by_name(&monitor_states, &layout_name) {
|
||||||
.monitors
|
if monitor_state.is_lid_closed {
|
||||||
.iter()
|
panic!("Monitor '{}' is closed", &monitor_state.monitor.name);
|
||||||
.find(|monitor| &monitor.name == layout_name)
|
}
|
||||||
{
|
|
||||||
// It is a monitor name. Find the matching output...
|
// It is a monitor name. Find the matching output...
|
||||||
if let Some(output) = outputs
|
if let Some(output) = outputs
|
||||||
.iter()
|
.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);
|
output_config_map.insert(&output.name, &output.output_config);
|
||||||
} else {
|
} else {
|
||||||
panic!("could not find output for monitor {layout_name}")
|
panic!("Could not find output for monitor '{layout_name}'")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// See if it is an output name...
|
// See if it is an output name...
|
||||||
if let Some(output) = outputs.iter().find(|output| &output.name == layout_name) {
|
if let Some(output) = outputs.iter().find(|output| &output.name == layout_name) {
|
||||||
output_config_map.insert(&output.name, &output.output_config);
|
output_config_map.insert(&output.name, &output.output_config);
|
||||||
} else {
|
} 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.
|
/// Apply the first automatic layout for which all outputs are available.
|
||||||
@@ -210,8 +219,11 @@ pub fn apply_automatic() -> Option<String> {
|
|||||||
.iter()
|
.iter()
|
||||||
.find(|layout| layout.automatic)
|
.find(|layout| layout.automatic)
|
||||||
{
|
{
|
||||||
apply_layout(&layout.name);
|
if apply_layout(&layout.name) {
|
||||||
Some(String::from(&layout.name))
|
Some(String::from(&layout.name))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,9 @@ fn main() {
|
|||||||
process::exit(2)
|
process::exit(2)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
swayout::apply_layout(&args[1]);
|
if ! swayout::apply_layout(&args[1]) {
|
||||||
|
process::exit(3)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
eprintln!("Usage: {} [layout]", args[0].as_str());
|
eprintln!("Usage: {} [layout]", args[0].as_str());
|
||||||
|
|||||||
@@ -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");
|
let mut cmd = Command::new("swaymsg");
|
||||||
enabled.iter().for_each(|(output_name, output_config)| {
|
enabled.iter().for_each(|(output_name, output_config)| {
|
||||||
cmd.arg("output");
|
cmd.arg("output");
|
||||||
|
|||||||
Reference in New Issue
Block a user