Compare commits
4 Commits
ce5bb9f4af
...
3c9324b63a
| Author | SHA1 | Date | |
|---|---|---|---|
| 3c9324b63a | |||
| 6d859b8b9b | |||
| bec2bdc8f9 | |||
| b8c38da85b |
117
Cargo.lock
generated
117
Cargo.lock
generated
@@ -2,6 +2,18 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.14"
|
||||
@@ -10,13 +22,14 @@ checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
|
||||
|
||||
[[package]]
|
||||
name = "kdl"
|
||||
version = "4.7.1"
|
||||
version = "6.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e03e2e96c5926fe761088d66c8c2aee3a4352a2573f4eaca50043ad130af9117"
|
||||
checksum = "a927c788db5413fbaced8fc5ac08a4a341c50766cba1d363ba3e25f3db343af0"
|
||||
dependencies = [
|
||||
"miette",
|
||||
"nom",
|
||||
"num",
|
||||
"thiserror",
|
||||
"winnow",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -27,21 +40,21 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
||||
|
||||
[[package]]
|
||||
name = "miette"
|
||||
version = "5.10.0"
|
||||
version = "7.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "59bb584eaeeab6bd0226ccf3509a69d7936d148cf3d036ad350abe35e8c6856e"
|
||||
checksum = "317f146e2eb7021892722af37cf1b971f0a70c8406f487e24952667616192c64"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"miette-derive",
|
||||
"once_cell",
|
||||
"thiserror",
|
||||
"unicode-width",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "miette-derive"
|
||||
version = "5.10.0"
|
||||
version = "7.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49e7bc1560b95a3c4a25d03de42fe76ca718ab92d1a22a55b9b4cf67b3ae635c"
|
||||
checksum = "23c9b935fbe1d6cbd1dac857b54a688145e2d93f48db36010514d0f612d0ad67"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@@ -49,26 +62,77 @@ dependencies = [
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "minimal-lexical"
|
||||
version = "0.2.1"
|
||||
name = "num"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "7.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
|
||||
checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
"minimal-lexical",
|
||||
"num-bigint",
|
||||
"num-complex",
|
||||
"num-integer",
|
||||
"num-iter",
|
||||
"num-rational",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.20.2"
|
||||
name = "num-bigint"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
|
||||
checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9"
|
||||
dependencies = [
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-complex"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.46"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-iter"
|
||||
version = "0.1.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-rational"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824"
|
||||
dependencies = [
|
||||
"num-bigint",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
@@ -179,6 +243,15 @@ version = "0.1.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
|
||||
|
||||
[[package]]
|
||||
name = "winnow"
|
||||
version = "0.6.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "xdg"
|
||||
version = "2.5.2"
|
||||
|
||||
@@ -13,6 +13,6 @@ serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0.133"
|
||||
|
||||
# for config file
|
||||
kdl = "4.7.1"
|
||||
kdl = "6.1.0"
|
||||
|
||||
xdg = "2.5.2"
|
||||
91
src/config.rs
Normal file
91
src/config.rs
Normal file
@@ -0,0 +1,91 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use kdl::KdlDocument;
|
||||
|
||||
/// Configuration for swayout.
|
||||
pub struct Config {
|
||||
/// Monitor name is independent of output name. It can be anything.
|
||||
pub monitors: HashMap<String, Monitor>,
|
||||
/// 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
|
||||
/// of that monitor for this layout.
|
||||
/// Available outputs that do not match a monitor in the map are disabled.
|
||||
pub layouts: HashMap<String, HashMap<String, OutputConfig>>,
|
||||
}
|
||||
|
||||
/// Defines a monitor by make, model, and serial.
|
||||
pub struct Monitor {
|
||||
pub make: String,
|
||||
pub model: String,
|
||||
pub serial: String,
|
||||
}
|
||||
|
||||
/// Configuration for an enabled output.
|
||||
pub struct OutputConfig {
|
||||
/// mode. See man 5 sway-output
|
||||
pub mode: String,
|
||||
/// scale. See man 5 sway-output
|
||||
pub scale: String,
|
||||
/// x value for position. See man 5 sway-output
|
||||
pub x: u16,
|
||||
/// y value for position. See man 5 sway-output
|
||||
pub y: u16,
|
||||
/// transform. See man 5 sway-output
|
||||
pub transform: String,
|
||||
}
|
||||
|
||||
/// Get a `Config` from ~/.config/swayout/config.kdl.
|
||||
/// (It really uses XDG config dirs.)
|
||||
pub fn get_config() -> Config {
|
||||
let xdg_dirs = xdg::BaseDirectories::with_prefix("swayout").unwrap();
|
||||
if let Some(path) = xdg_dirs.find_config_file("config.kdl") {
|
||||
let kdl = fs::read_to_string(path).expect("error reading file");
|
||||
parse_config(kdl)
|
||||
} else {
|
||||
// no config files, use an empty rules
|
||||
Config {
|
||||
monitors: HashMap::new(),
|
||||
layouts: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
fn parse_config(kdl:String) -> Config {
|
||||
let doc:KdlDocument = kdl.parse().expect("failed to parse config KDL");
|
||||
|
||||
let monitors:HashMap<String,Monitor> = doc.nodes().iter()
|
||||
.filter(|node| node.name().value() == "monitor")
|
||||
.map(|monitor_node| {
|
||||
let name = String::from(monitor_node[0].as_string().unwrap());
|
||||
let monitor = Monitor {
|
||||
make : String::from(monitor_node["make"].as_string().unwrap()),
|
||||
model : String::from(monitor_node["model"].as_string().unwrap()),
|
||||
serial : String::from(monitor_node["serial"].as_string().unwrap()),
|
||||
};
|
||||
(name, monitor)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let layouts:HashMap<String,HashMap<String,OutputConfig>> = doc.nodes().iter()
|
||||
.filter(|node| node.name().value() == "layout")
|
||||
.map(|layout_node| {
|
||||
let layout_name = String::from(layout_node[0].as_string().unwrap());
|
||||
let monitor_name_to_output_config:HashMap<String,OutputConfig> = layout_node
|
||||
.children().unwrap().nodes().iter()
|
||||
.map(|output_node| {
|
||||
let output = String::from(output_node[0].as_string().unwrap());
|
||||
let output_config = OutputConfig {
|
||||
mode : String::from(output_node["mode"].as_string().unwrap()),
|
||||
scale : String::from(output_node["scale"].as_string().unwrap()),
|
||||
transform : String::from(output_node["transform"].as_string().unwrap()),
|
||||
x : output_node["x"].as_integer().unwrap() as u16,
|
||||
y : output_node["y"].as_integer().unwrap() as u16,
|
||||
};
|
||||
(output, output_config)
|
||||
})
|
||||
.collect();
|
||||
(layout_name,monitor_name_to_output_config)
|
||||
})
|
||||
.collect();
|
||||
|
||||
Config { monitors, layouts }
|
||||
}
|
||||
121
src/lib.rs
Normal file
121
src/lib.rs
Normal file
@@ -0,0 +1,121 @@
|
||||
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);
|
||||
}
|
||||
323
src/main.rs
323
src/main.rs
@@ -1,332 +1,13 @@
|
||||
use kdl::KdlDocument;
|
||||
use serde_json::{from_str, Value};
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
use std::process::Command;
|
||||
use std::{fs, str};
|
||||
|
||||
/// Configuration for swayout.
|
||||
struct Config {
|
||||
/// Monitor name is independent of output name. It can be anything.
|
||||
monitors: HashMap<String, Monitor>,
|
||||
/// 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
|
||||
/// of that monitor for this layout.
|
||||
/// Available outputs that do not match a monitor in the map are disabled.
|
||||
layouts: HashMap<String, HashMap<String, OutputConfig>>,
|
||||
}
|
||||
|
||||
/// Defines a monitor by make, model, and serial.
|
||||
struct Monitor {
|
||||
make: String,
|
||||
model: String,
|
||||
serial: String,
|
||||
}
|
||||
|
||||
/// Configuration for an enabled output.
|
||||
struct OutputConfig {
|
||||
/// mode. See man 5 sway-output
|
||||
mode: String,
|
||||
/// scale. See man 5 sway-output
|
||||
scale: String,
|
||||
/// x value for position. See man 5 sway-output
|
||||
x: u16,
|
||||
/// y value for position. See man 5 sway-output
|
||||
y: u16,
|
||||
/// transform. See man 5 sway-output
|
||||
transform: String,
|
||||
}
|
||||
|
||||
/// An output, as returned by `swaymsg -t get_outputs`.
|
||||
struct Output {
|
||||
/// output name, according to sway
|
||||
name: String,
|
||||
make: String,
|
||||
model: String,
|
||||
serial: String,
|
||||
output_config: OutputConfig,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() == 1 {
|
||||
// first arg is executable
|
||||
print_layout_names();
|
||||
swayout::print_layout_names();
|
||||
} else if args.len() == 2 {
|
||||
apply_layout(&args[1]);
|
||||
swayout::apply_layout(&args[1]);
|
||||
} else {
|
||||
panic!("Usage: {} [layout]", args[0].as_str());
|
||||
}
|
||||
}
|
||||
|
||||
/// Get all outputs currently available, according to `swaymsg -t get_outputs`.
|
||||
fn get_outputs() -> Vec<Output> {
|
||||
let output = Command::new("swaymsg")
|
||||
.arg("-t")
|
||||
.arg("get_outputs")
|
||||
.output()
|
||||
.expect("swaymsg -t get_outputs failed");
|
||||
let json_string = str::from_utf8(&output.stdout).unwrap();
|
||||
let json = from_str(json_string).unwrap();
|
||||
|
||||
if let Value::Array(outputs) = json {
|
||||
outputs
|
||||
.iter()
|
||||
.map(|output_json| Output {
|
||||
name: String::from(output_json["name"].as_str().unwrap()),
|
||||
make: String::from(output_json["make"].as_str().unwrap()),
|
||||
model: String::from(output_json["model"].as_str().unwrap()),
|
||||
serial: String::from(output_json["serial"].as_str().unwrap()),
|
||||
output_config: OutputConfig {
|
||||
mode: get_mode(output_json),
|
||||
scale: String::from("1.0"),
|
||||
x: 0,
|
||||
y: 0,
|
||||
transform: String::from("normal"),
|
||||
},
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
panic!("outputs json was not an array");
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse the mode from the output JSON into a string suitable for the mode param for sway-output.
|
||||
/// This will go into `OutputConfig.mode`.
|
||||
fn get_mode(output: &Value) -> String {
|
||||
if let Value::Array(modes) = &output["modes"] {
|
||||
if let Some(mode) = modes.first() {
|
||||
let width = mode["width"].as_i64().unwrap().to_string();
|
||||
let height = mode["height"].as_i64().unwrap().to_string();
|
||||
let mut out: String = String::new();
|
||||
out.push_str(&width);
|
||||
out.push_str("x");
|
||||
out.push_str(&height);
|
||||
out
|
||||
} else {
|
||||
panic!("no modes")
|
||||
}
|
||||
} else {
|
||||
panic!("modes not an array")
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a `Config` from ~/.config/swayout/config.kdl.
|
||||
/// (It really uses XDG config dirs.)
|
||||
fn get_config() -> Config {
|
||||
let xdg_dirs = xdg::BaseDirectories::with_prefix("swayout").unwrap();
|
||||
if let Some(path) = xdg_dirs.find_config_file("config.kdl") {
|
||||
let kdl = fs::read_to_string(path).expect("error reading file");
|
||||
parse_config(kdl)
|
||||
} else {
|
||||
// no config files, use an empty rules
|
||||
Config {
|
||||
monitors: HashMap::new(),
|
||||
layouts: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
fn parse_config(kdl:String) -> Config {
|
||||
let doc:KdlDocument = kdl.parse().expect("failed to parse config KDL");
|
||||
|
||||
let monitors:HashMap<String,Monitor> = doc.nodes().iter()
|
||||
.filter(|node| node.name().value() == "monitor")
|
||||
.map(|monitor_node| {
|
||||
let name = String::from(monitor_node[0].as_string().unwrap());
|
||||
let monitor = Monitor {
|
||||
make : String::from(monitor_node["make"].as_string().unwrap()),
|
||||
model : String::from(monitor_node["model"].as_string().unwrap()),
|
||||
serial : String::from(monitor_node["serial"].as_string().unwrap()),
|
||||
};
|
||||
(name, monitor)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let layouts:HashMap<String,HashMap<String,OutputConfig>> = doc.nodes().iter()
|
||||
.filter(|node| node.name().value() == "layout")
|
||||
.map(|layout_node| {
|
||||
let layout_name = String::from(layout_node[0].as_string().unwrap());
|
||||
let monitor_name_to_output_config:HashMap<String,OutputConfig> = layout_node
|
||||
.children().unwrap().nodes().iter()
|
||||
.map(|output_node| {
|
||||
let output = String::from(output_node[0].as_string().unwrap());
|
||||
let output_config = OutputConfig {
|
||||
mode : String::from(output_node["mode"].as_string().unwrap()),
|
||||
scale : String::from(output_node["scale"].as_string().unwrap()),
|
||||
transform : String::from(output_node["transform"].as_string().unwrap()),
|
||||
x : output_node["x"].as_i64().unwrap() as u16,
|
||||
y : output_node["y"].as_i64().unwrap() as u16,
|
||||
};
|
||||
(output, output_config)
|
||||
})
|
||||
.collect();
|
||||
(layout_name,monitor_name_to_output_config)
|
||||
})
|
||||
.collect();
|
||||
|
||||
Config { monitors, layouts }
|
||||
}
|
||||
|
||||
/// 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
|
||||
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.
|
||||
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);
|
||||
}
|
||||
|
||||
/// Apply the specified outputs. Enable all outputs in [outputs], disable others.
|
||||
fn apply_outputs(all_outputs: &Vec<Output>, outputs: &HashMap<&String, &OutputConfig>) {
|
||||
|
||||
// set enabled outputs first, then set disabled outputs.
|
||||
// That way if some work before an error, you have at least one output enabled.
|
||||
|
||||
// for outputs to be enabled: map of output name to config
|
||||
let mut enabled: HashMap<&String,&OutputConfig> = HashMap::new();
|
||||
// for outputs to be disabled: output names
|
||||
let mut disabled: Vec<&String> = Vec::new();
|
||||
|
||||
all_outputs.iter().for_each(|output| {
|
||||
if let Some(output_config) = outputs.get(&output.name) {
|
||||
enabled.insert(&output.name, &output_config);
|
||||
} else {
|
||||
disabled.push(&output.name);
|
||||
}
|
||||
});
|
||||
|
||||
let mut cmd = Command::new("swaymsg");
|
||||
enabled.iter().for_each(|(output_name,output_config)| {
|
||||
cmd.arg("output");
|
||||
cmd.arg(&output_name);
|
||||
cmd.arg("enable");
|
||||
cmd.arg("mode");
|
||||
cmd.arg(&output_config.mode);
|
||||
cmd.arg("scale");
|
||||
cmd.arg(&output_config.scale);
|
||||
cmd.arg("pos");
|
||||
cmd.arg(&output_config.x.to_string());
|
||||
cmd.arg(&output_config.y.to_string());
|
||||
cmd.arg("transform");
|
||||
cmd.arg(&output_config.transform);
|
||||
cmd.arg(",");
|
||||
});
|
||||
|
||||
disabled.iter().for_each(|output_name| {
|
||||
cmd.arg("output");
|
||||
cmd.arg(&output_name);
|
||||
cmd.arg("disable");
|
||||
cmd.arg(",");
|
||||
});
|
||||
|
||||
// print what we are about to do.
|
||||
cmd.get_args()
|
||||
.for_each(|arg| print!("{} ", arg.to_str().unwrap()));
|
||||
|
||||
cmd.output().expect("swaymsg output failed");
|
||||
}
|
||||
117
src/sway.rs
Normal file
117
src/sway.rs
Normal file
@@ -0,0 +1,117 @@
|
||||
use std::collections::HashMap;
|
||||
use std::process::Command;
|
||||
use std::str::from_utf8;
|
||||
use serde_json::{from_str, Value};
|
||||
use crate::config::OutputConfig;
|
||||
|
||||
/// An output, as returned by `swaymsg -t get_outputs`.
|
||||
pub struct Output {
|
||||
/// output name, according to sway
|
||||
pub name: String,
|
||||
pub make: String,
|
||||
pub model: String,
|
||||
pub serial: String,
|
||||
pub output_config: OutputConfig,
|
||||
}
|
||||
|
||||
/// Get all outputs currently available, according to `swaymsg -t get_outputs`.
|
||||
pub fn get_outputs() -> Vec<Output> {
|
||||
let output = Command::new("swaymsg")
|
||||
.arg("-t")
|
||||
.arg("get_outputs")
|
||||
.output()
|
||||
.expect("swaymsg -t get_outputs failed");
|
||||
let json_string = from_utf8(&output.stdout).unwrap();
|
||||
let json = from_str(json_string).unwrap();
|
||||
|
||||
if let Value::Array(outputs) = json {
|
||||
outputs
|
||||
.iter()
|
||||
.map(|output_json| Output {
|
||||
name: String::from(output_json["name"].as_str().unwrap()),
|
||||
make: String::from(output_json["make"].as_str().unwrap()),
|
||||
model: String::from(output_json["model"].as_str().unwrap()),
|
||||
serial: String::from(output_json["serial"].as_str().unwrap()),
|
||||
output_config: OutputConfig {
|
||||
mode: get_mode(output_json),
|
||||
scale: String::from("1.0"),
|
||||
x: 0,
|
||||
y: 0,
|
||||
transform: String::from("normal"),
|
||||
},
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
panic!("outputs json was not an array");
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse the mode from the output JSON into a string suitable for the mode param for sway-output.
|
||||
/// This will go into `OutputConfig.mode`.
|
||||
fn get_mode(output: &Value) -> String {
|
||||
if let Value::Array(modes) = &output["modes"] {
|
||||
if let Some(mode) = modes.first() {
|
||||
let width = mode["width"].as_i64().unwrap().to_string();
|
||||
let height = mode["height"].as_i64().unwrap().to_string();
|
||||
let mut out: String = String::new();
|
||||
out.push_str(&width);
|
||||
out.push_str("x");
|
||||
out.push_str(&height);
|
||||
out
|
||||
} else {
|
||||
panic!("no modes")
|
||||
}
|
||||
} else {
|
||||
panic!("modes not an array")
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply the specified outputs. Enable all outputs in [outputs], disable others.
|
||||
pub fn apply_outputs(all_outputs: &Vec<Output>, outputs: &HashMap<&String, &OutputConfig>) {
|
||||
|
||||
// set enabled outputs first, then set disabled outputs.
|
||||
// That way if some work before an error, you have at least one output enabled.
|
||||
|
||||
// for outputs to be enabled: map of output name to config
|
||||
let mut enabled: HashMap<&String,&OutputConfig> = HashMap::new();
|
||||
// for outputs to be disabled: output names
|
||||
let mut disabled: Vec<&String> = Vec::new();
|
||||
|
||||
all_outputs.iter().for_each(|output| {
|
||||
if let Some(output_config) = outputs.get(&output.name) {
|
||||
enabled.insert(&output.name, &output_config);
|
||||
} else {
|
||||
disabled.push(&output.name);
|
||||
}
|
||||
});
|
||||
|
||||
let mut cmd = Command::new("swaymsg");
|
||||
enabled.iter().for_each(|(output_name,output_config)| {
|
||||
cmd.arg("output");
|
||||
cmd.arg(&output_name);
|
||||
cmd.arg("enable");
|
||||
cmd.arg("mode");
|
||||
cmd.arg(&output_config.mode);
|
||||
cmd.arg("scale");
|
||||
cmd.arg(&output_config.scale);
|
||||
cmd.arg("pos");
|
||||
cmd.arg(&output_config.x.to_string());
|
||||
cmd.arg(&output_config.y.to_string());
|
||||
cmd.arg("transform");
|
||||
cmd.arg(&output_config.transform);
|
||||
cmd.arg(",");
|
||||
});
|
||||
|
||||
disabled.iter().for_each(|output_name| {
|
||||
cmd.arg("output");
|
||||
cmd.arg(&output_name);
|
||||
cmd.arg("disable");
|
||||
cmd.arg(",");
|
||||
});
|
||||
|
||||
// print what we are about to do.
|
||||
cmd.get_args()
|
||||
.for_each(|arg| print!("{} ", arg.to_str().unwrap()));
|
||||
|
||||
cmd.output().expect("swaymsg output failed");
|
||||
}
|
||||
Reference in New Issue
Block a user