use std::collections::{HashMap, HashSet}; mod config; use crate::config::{get_config, Config, Layout, OutputConfig}; mod sway; use crate::sway::{apply_outputs, get_outputs, Output}; /// 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() -> HashSet { let available_outputs = get_outputs(); let config = get_config(); let mut layout_names: HashSet = HashSet::new(); // Add each layout defined in the config file for which all outputs are available get_available_layouts(&config, &available_outputs) .iter() .for_each(|layout| { layout_names.insert(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| { monitor.make == output.make && monitor.model == output.model && monitor.serial == output.serial }); if let Some(monitor) = monitor_opt { layout_names.insert(String::from(&monitor.name)); } else { layout_names.insert(String::from(&output.name)); } }); layout_names } /// Get the names of monitors that are available (that match an available output) fn get_available_monitor_names<'a,'b>(config:&'a Config, available_outputs:&'b Vec) -> Vec<&'a String> { config.monitors.iter() .filter(|monitor| available_outputs.iter().any(|output| monitor.make == output.make && monitor.model == output.model && output.serial == monitor.serial ) ) .map(|monitor| &monitor.name) .collect() } /// Get the layouts for which all monitors are available. fn get_available_layouts<'a,'b>(config:&'a Config, available_outputs:&'b Vec) -> Vec<&'a Layout> { let available_monitor_names:Vec<&String> = get_available_monitor_names(&config, available_outputs); let mut layouts: Vec<&'a Layout> = Vec::new(); // Add each layout defined in the config file for which all outputs are available config.layouts.iter().for_each(|layout| { if layout.outputs.iter() .all(|(output_name, _output)| available_monitor_names.contains(&output_name)) { layouts.push(&layout); } }); layouts } /// 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 if let Some(layout) = rules.layouts.iter().find(|layout| &layout.name == layout_name) { // The layout is defined in the rules. layout.outputs.iter().for_each(|(monitor_name, output_config)| { let monitor = &rules.monitors.iter().find(|monitor| &monitor.name == monitor_name).unwrap(); // 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.iter().find(|monitor| &monitor.name == 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); } /// Apply the first automatic layout for which all outputs are available. /// Return the name of the layout applied or None if no automatic layout was available. pub fn apply_automatic() -> Option { let config = get_config(); let outputs = get_outputs(); if let Some(layout) = get_available_layouts(&config, &outputs) .iter() .find(|layout| layout.automatic) { apply_layout(&layout.name); Some(String::from(&layout.name)) } else { None } }