Add support for --automatic.
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.0.0"
|
version = "1.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"kdl",
|
"kdl",
|
||||||
"serde",
|
"serde",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "swayout"
|
name = "swayout"
|
||||||
version = "1.0.0"
|
version = "1.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "A tool to configure sway outputs"
|
description = "A tool to configure sway outputs"
|
||||||
authors = ["Stephen Byrne"]
|
authors = ["Stephen Byrne"]
|
||||||
@@ -15,4 +15,4 @@ serde_json = "1.0.133"
|
|||||||
# for config file
|
# for config file
|
||||||
kdl = "6.1.0"
|
kdl = "6.1.0"
|
||||||
|
|
||||||
xdg = "2.5.2"
|
xdg = "2.5.2"
|
||||||
|
|||||||
14
README.md
14
README.md
@@ -23,7 +23,7 @@ monitor "left" make="Goldstar Company Ltd" model="LG HDR 4K" serial="0x0000B9C0"
|
|||||||
monitor "right" make="Goldstar Company Ltd" model="LG HDR 4K" serial="0x0000B9BF"
|
monitor "right" make="Goldstar Company Ltd" model="LG HDR 4K" serial="0x0000B9BF"
|
||||||
|
|
||||||
// When I have "left" and "right" hooked up, I want this layout.
|
// When I have "left" and "right" hooked up, I want this layout.
|
||||||
layout "two4k" {
|
layout "two4k" automatic=#true {
|
||||||
output "left" mode="3840x2160" scale="1.5" x=0 y=0 transform="270"
|
output "left" mode="3840x2160" scale="1.5" x=0 y=0 transform="270"
|
||||||
output "right" mode="3840x2160" scale="1.5" x=1440 y=1120 transform="normal"
|
output "right" mode="3840x2160" scale="1.5" x=1440 y=1120 transform="normal"
|
||||||
}
|
}
|
||||||
@@ -48,16 +48,12 @@ set $select_layout swayout | tofi | xargs swayout
|
|||||||
bindsym $mod+Shift+m exec $select_layout
|
bindsym $mod+Shift+m exec $select_layout
|
||||||
```
|
```
|
||||||
|
|
||||||
## TODO
|
If `swayout` is called with the `--automatic` switch, the first layout with `automatic=#true` for which all outputs
|
||||||
|
are available is enabled. If no automatic layouts are available, `swayout` exits with code 2.
|
||||||
Things I might do someday:
|
|
||||||
|
|
||||||
### Automatic
|
|
||||||
|
|
||||||
Add a `automatic` option to layouts and an `--automatic` flag to `swayout`, and have it automatically enable the
|
|
||||||
first layout with `automatic=true` for which all outputs are available.
|
|
||||||
|
|
||||||
This could be run by `bindswitch lid:toggle exec swayout --automatic`.
|
This could be run by `bindswitch lid:toggle exec swayout --automatic`.
|
||||||
|
|
||||||
|
## TODO
|
||||||
|
|
||||||
How to call when an output is connected or disconnected?
|
How to call when an output is connected or disconnected?
|
||||||
Wayland events via [wayland_client](https://docs.rs/wayland-client/latest/wayland_client/index.html)?
|
Wayland events via [wayland_client](https://docs.rs/wayland-client/latest/wayland_client/index.html)?
|
||||||
@@ -5,16 +5,23 @@ use kdl::KdlDocument;
|
|||||||
/// Configuration for swayout.
|
/// Configuration for swayout.
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
/// Monitor name is independent of output name. It can be anything.
|
/// Monitor name is independent of output name. It can be anything.
|
||||||
pub monitors: HashMap<String, Monitor>,
|
pub monitors: Vec<Monitor>,
|
||||||
/// Definition of layouts: a map of the layout name to the outputs.
|
/// Definition of layouts: a map of the layout name to the outputs.
|
||||||
/// The outputs is a map of the monitor name (in `monitors`) to the configuration
|
/// The outputs is a map of the monitor name (in `monitors`) to the configuration
|
||||||
/// of that monitor for this layout.
|
/// of that monitor for this layout.
|
||||||
/// Available outputs that do not match a monitor in the map are disabled.
|
/// Available outputs that do not match a monitor in the map are disabled.
|
||||||
pub layouts: HashMap<String, HashMap<String, OutputConfig>>,
|
pub layouts: Vec<Layout>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Layout {
|
||||||
|
pub name: String,
|
||||||
|
pub automatic: bool,
|
||||||
|
pub outputs: HashMap<String,OutputConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Defines a monitor by make, model, and serial.
|
/// Defines a monitor by make, model, and serial.
|
||||||
pub struct Monitor {
|
pub struct Monitor {
|
||||||
|
pub name: String,
|
||||||
pub make: String,
|
pub make: String,
|
||||||
pub model: String,
|
pub model: String,
|
||||||
pub serial: String,
|
pub serial: String,
|
||||||
@@ -44,31 +51,31 @@ pub fn get_config() -> Config {
|
|||||||
} else {
|
} else {
|
||||||
// no config files, use an empty rules
|
// no config files, use an empty rules
|
||||||
Config {
|
Config {
|
||||||
monitors: HashMap::new(),
|
monitors: Vec::new(),
|
||||||
layouts: HashMap::new(),
|
layouts: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn parse_config(kdl:String) -> Config {
|
fn parse_config(kdl:String) -> Config {
|
||||||
let doc:KdlDocument = kdl.parse().expect("failed to parse config KDL");
|
let doc:KdlDocument = kdl.parse().expect("failed to parse config KDL");
|
||||||
|
|
||||||
let monitors:HashMap<String,Monitor> = doc.nodes().iter()
|
let monitors:Vec<Monitor> = doc.nodes().iter()
|
||||||
.filter(|node| node.name().value() == "monitor")
|
.filter(|node| node.name().value() == "monitor")
|
||||||
.map(|monitor_node| {
|
.map(|monitor_node| {
|
||||||
let name = String::from(monitor_node[0].as_string().unwrap());
|
Monitor {
|
||||||
let monitor = Monitor {
|
name : String::from(monitor_node[0].as_string().unwrap()),
|
||||||
make : String::from(monitor_node["make"].as_string().unwrap()),
|
make : String::from(monitor_node["make"].as_string().unwrap()),
|
||||||
model : String::from(monitor_node["model"].as_string().unwrap()),
|
model : String::from(monitor_node["model"].as_string().unwrap()),
|
||||||
serial : String::from(monitor_node["serial"].as_string().unwrap()),
|
serial : String::from(monitor_node["serial"].as_string().unwrap()),
|
||||||
};
|
}
|
||||||
(name, monitor)
|
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let layouts:HashMap<String,HashMap<String,OutputConfig>> = doc.nodes().iter()
|
let layouts:Vec<Layout> = doc.nodes().iter()
|
||||||
.filter(|node| node.name().value() == "layout")
|
.filter(|node| node.name().value() == "layout")
|
||||||
.map(|layout_node| {
|
.map(|layout_node| {
|
||||||
let layout_name = String::from(layout_node[0].as_string().unwrap());
|
let layout_name = String::from(layout_node[0].as_string().unwrap());
|
||||||
|
let automatic = layout_node.get("automatic").is_some_and(|v| v.as_bool().is_some_and(|a| a));
|
||||||
let monitor_name_to_output_config:HashMap<String,OutputConfig> = layout_node
|
let monitor_name_to_output_config:HashMap<String,OutputConfig> = layout_node
|
||||||
.children().unwrap().nodes().iter()
|
.children().unwrap().nodes().iter()
|
||||||
.map(|output_node| {
|
.map(|output_node| {
|
||||||
@@ -83,7 +90,11 @@ fn parse_config(kdl:String) -> Config {
|
|||||||
(output, output_config)
|
(output, output_config)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
(layout_name,monitor_name_to_output_config)
|
Layout {
|
||||||
|
name : layout_name,
|
||||||
|
automatic: automatic,
|
||||||
|
outputs: monitor_name_to_output_config
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
|||||||
91
src/lib.rs
91
src/lib.rs
@@ -1,10 +1,10 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
use crate::config::{get_config,OutputConfig};
|
use crate::config::{get_config, Config, Layout, OutputConfig};
|
||||||
|
|
||||||
mod sway;
|
mod sway;
|
||||||
use crate::sway::{apply_outputs,get_outputs};
|
use crate::sway::{apply_outputs, get_outputs, Output};
|
||||||
|
|
||||||
/// Determine the available layout names.
|
/// Determine the available layout names.
|
||||||
/// Return one layout for each layout defined in the configuration file
|
/// Return one layout for each layout defined in the configuration file
|
||||||
@@ -15,41 +15,27 @@ fn get_layouts() -> Vec<String> {
|
|||||||
let available_outputs = get_outputs();
|
let available_outputs = get_outputs();
|
||||||
let config = get_config();
|
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();
|
let mut layout_names: Vec<String> = Vec::new();
|
||||||
|
|
||||||
// Add each layout defined in the config file for which all outputs are available
|
// Add each layout defined in the config file for which all outputs are available
|
||||||
config.layouts.iter().for_each(|(layout_name, layout)| {
|
get_available_layouts(&config, &available_outputs)
|
||||||
if layout
|
.iter()
|
||||||
.iter()
|
.for_each(|layout| {
|
||||||
.all(|(output_name, _output)| available_monitor_names.contains(&output_name))
|
layout_names.push(String::from(&layout.name));
|
||||||
{
|
});
|
||||||
layout_names.push(String::from(layout_name))
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// add each individual output (by monitor name if the monitor is defined, else by output name)
|
// add each individual output (by monitor name if the monitor is defined, else by output name)
|
||||||
available_outputs.iter().for_each(|output| {
|
available_outputs.iter().for_each(|output| {
|
||||||
// if there is a monitor for the output, use the monitor name, else use the output name
|
// if there is a monitor for the output, use the monitor name, else use the output name
|
||||||
let monitor_opt = config.monitors.iter()
|
let monitor_opt = config.monitors.iter()
|
||||||
.find(|(_monitor_name, monitor)| {
|
.find(|monitor| {
|
||||||
monitor.make == output.make
|
monitor.make == output.make
|
||||||
&& monitor.model == output.model
|
&& monitor.model == output.model
|
||||||
&& monitor.serial == output.serial
|
&& monitor.serial == output.serial
|
||||||
});
|
});
|
||||||
if let Some((monitor_name, _monitor)) = monitor_opt {
|
if let Some(monitor) = monitor_opt {
|
||||||
layout_names.push(String::from(monitor_name));
|
layout_names.push(String::from(&monitor.name));
|
||||||
} else {
|
} else {
|
||||||
layout_names.push(String::from(&output.name));
|
layout_names.push(String::from(&output.name));
|
||||||
}
|
}
|
||||||
@@ -58,6 +44,38 @@ fn get_layouts() -> Vec<String> {
|
|||||||
layout_names
|
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<Output>) -> 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<Output>) -> 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
|
/// Print the name of each layout to standard out, one per line
|
||||||
pub fn print_layout_names() {
|
pub fn print_layout_names() {
|
||||||
get_layouts().iter().for_each(|layout| println!("{layout}"))
|
get_layouts().iter().for_each(|layout| println!("{layout}"))
|
||||||
@@ -75,11 +93,10 @@ pub fn apply_layout(layout_name: &String) {
|
|||||||
let mut output_config_map: HashMap<&String, &OutputConfig> = HashMap::new();
|
let mut output_config_map: HashMap<&String, &OutputConfig> = HashMap::new();
|
||||||
|
|
||||||
// First check if the layout is defined in the rules
|
// First check if the layout is defined in the rules
|
||||||
let opt_layout = rules.layouts.get(layout_name);
|
if let Some(layout) = rules.layouts.iter().find(|layout| &layout.name == layout_name) {
|
||||||
if let Some(layout) = opt_layout {
|
|
||||||
// The layout is defined in the rules.
|
// The layout is defined in the rules.
|
||||||
layout.iter().for_each(|(monitor_name, output_config)| {
|
layout.outputs.iter().for_each(|(monitor_name, output_config)| {
|
||||||
let monitor = &rules.monitors[monitor_name];
|
let monitor = &rules.monitors.iter().find(|monitor| &monitor.name == monitor_name).unwrap();
|
||||||
|
|
||||||
// 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.iter().find(|output| {
|
let output_opt = outputs.iter().find(|output| {
|
||||||
@@ -96,7 +113,7 @@ pub fn apply_layout(layout_name: &String) {
|
|||||||
} 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) = rules.monitors.get(layout_name) {
|
if let Some(monitor) = rules.monitors.iter().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.iter().find(|output| {
|
if let Some(output) = outputs.iter().find(|output| {
|
||||||
output.make == monitor.make && output.model == monitor.model
|
output.make == monitor.make && output.model == monitor.model
|
||||||
@@ -119,3 +136,19 @@ pub fn apply_layout(layout_name: &String) {
|
|||||||
|
|
||||||
apply_outputs(&outputs, &output_config_map);
|
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<String> {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/main.rs
17
src/main.rs
@@ -1,4 +1,4 @@
|
|||||||
use std::env;
|
use std::{env, process};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
@@ -6,8 +6,19 @@ fn main() {
|
|||||||
// first arg is executable
|
// first arg is executable
|
||||||
swayout::print_layout_names();
|
swayout::print_layout_names();
|
||||||
} else if args.len() == 2 {
|
} else if args.len() == 2 {
|
||||||
swayout::apply_layout(&args[1]);
|
let arg = &args[1];
|
||||||
|
if arg == "--automatic" {
|
||||||
|
if let Some(layout_name) = swayout::apply_automatic() {
|
||||||
|
println!("{}", layout_name);
|
||||||
|
} else {
|
||||||
|
eprintln!("no automatic layout available");
|
||||||
|
process::exit(2)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
swayout::apply_layout(&args[1]);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
panic!("Usage: {} [layout]", args[0].as_str());
|
eprintln!("Usage: {} [layout]", args[0].as_str());
|
||||||
|
process::exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user