Files
swayout/src/lib.rs
2024-12-30 03:36:05 +00:00

122 lines
4.7 KiB
Rust

use std::collections::HashMap;
mod config;
use crate::config::{get_config,OutputConfig};
mod sway;
use crate::sway::{apply_outputs,get_outputs};
/// 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 config = get_config();
// Get the names of monitors that are available (that match an available output)
let available_monitor_names:Vec<&String> = config.monitors.iter()
.filter(|(_monitor_name,monitor)|
available_outputs.iter().any(|output|
monitor.make == output.make
&& monitor.model == output.model
&& output.serial == monitor.serial
)
)
.map(|(monitor_name,_monitor)| monitor_name)
.collect();
let mut layout_names: Vec<String> = Vec::new();
// Add each layout defined in the config file for which all outputs are available
config.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 = config.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
pub 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 in the config, the name of a monitor,
/// or the name of an output.
pub fn apply_layout(layout_name: &String) {
let rules = get_config();
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.
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 {
// 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 {
// 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}")
}
}
}
apply_outputs(&outputs, &output_config_map);
}