Compare commits
7 Commits
3c9324b63a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 5718d8d772 | |||
| 778392f729 | |||
| 6004c1a2d3 | |||
| 818df0f7e8 | |||
| 20ec42d126 | |||
| c5f35ebf06 | |||
| 2ce2631bbc |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -192,7 +192,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "swayout"
|
||||
version = "1.0.0"
|
||||
version = "1.2.2"
|
||||
dependencies = [
|
||||
"kdl",
|
||||
"serde",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "swayout"
|
||||
version = "1.0.0"
|
||||
version = "1.2.2"
|
||||
edition = "2021"
|
||||
description = "A tool to configure sway outputs"
|
||||
authors = ["Stephen Byrne"]
|
||||
@@ -15,4 +15,4 @@ serde_json = "1.0.133"
|
||||
# for config file
|
||||
kdl = "6.1.0"
|
||||
|
||||
xdg = "2.5.2"
|
||||
xdg = "2.5.2"
|
||||
|
||||
19
README.md
19
README.md
@@ -12,7 +12,7 @@ Example `config.kdl`:
|
||||
|
||||
```kdl
|
||||
// This is my laptop's built-in screen.
|
||||
monitor "laptop" make="Unknown" model="0x403D" serial="0x00000000"
|
||||
monitor "laptop" make="Unknown" model="0x403D" serial="0x00000000" lid="LID"
|
||||
|
||||
// I usually have it hooked up to this monitor, which is the top one on my desk.
|
||||
monitor "top" make="Dell Inc." model="DELL U2415" serial="CFV9N99T0Y0U"
|
||||
@@ -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"
|
||||
|
||||
// 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 "right" mode="3840x2160" scale="1.5" x=1440 y=1120 transform="normal"
|
||||
}
|
||||
@@ -35,6 +35,9 @@ Printed layouts include:
|
||||
* All layouts defined in `config.kdl` for which the monitors are currently available. E.g.: `two4k`.
|
||||
* All monitors defined in `config.kdl` that are currently available. E.g.: `laptop`, `left`, `right`.
|
||||
* All available outputs that are not matched by a configured monitor. E.g.: `HDMI-2`.
|
||||
|
||||
If a monitor has its `lid` property is set to the name of a directory in `/proc/acpi/button/lid/`, if the `state` file
|
||||
in that directory indicates that the lid is closed, the monitor will not be considered available.
|
||||
|
||||
If you pass a layout name (or monitor name, or output name) as a command line argument to `swayout`,
|
||||
it calls `swaymsg` to configure that layout.
|
||||
@@ -48,16 +51,12 @@ set $select_layout swayout | tofi | xargs swayout
|
||||
bindsym $mod+Shift+m exec $select_layout
|
||||
```
|
||||
|
||||
## TODO
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
This could be run by `bindswitch lid:toggle exec swayout --automatic`.
|
||||
|
||||
## TODO
|
||||
|
||||
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)?
|
||||
@@ -1,23 +1,35 @@
|
||||
use kdl::KdlDocument;
|
||||
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>,
|
||||
pub monitors: Vec<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>>,
|
||||
pub layouts: Vec<Layout>,
|
||||
}
|
||||
|
||||
/// A configured layout
|
||||
pub struct Layout {
|
||||
/// Layout name
|
||||
pub name: String,
|
||||
/// If true, automatically enableable
|
||||
pub automatic: bool,
|
||||
/// Map of monitor name to OutputConfig
|
||||
pub outputs: HashMap<String, OutputConfig>,
|
||||
}
|
||||
|
||||
/// Defines a monitor by make, model, and serial.
|
||||
pub struct Monitor {
|
||||
pub name: String,
|
||||
pub make: String,
|
||||
pub model: String,
|
||||
pub serial: String,
|
||||
pub lid: Option<String>,
|
||||
}
|
||||
|
||||
/// Configuration for an enabled output.
|
||||
@@ -44,46 +56,62 @@ pub fn get_config() -> Config {
|
||||
} else {
|
||||
// no config files, use an empty rules
|
||||
Config {
|
||||
monitors: HashMap::new(),
|
||||
layouts: HashMap::new(),
|
||||
monitors: Vec::new(),
|
||||
layouts: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
fn parse_config(kdl:String) -> Config {
|
||||
let doc:KdlDocument = kdl.parse().expect("failed to parse config KDL");
|
||||
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()
|
||||
let monitors: Vec<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)
|
||||
.map(|monitor_node| Monitor {
|
||||
name: String::from(monitor_node[0].as_string().unwrap()),
|
||||
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()),
|
||||
lid: if let Some(lid) = monitor_node.get("lid") {
|
||||
Some(String::from(lid.as_string().unwrap()))
|
||||
} else {
|
||||
None
|
||||
},
|
||||
})
|
||||
.collect();
|
||||
|
||||
let layouts:HashMap<String,HashMap<String,OutputConfig>> = doc.nodes().iter()
|
||||
let layouts: Vec<Layout> = 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()
|
||||
let 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 outputs: 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,
|
||||
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)
|
||||
Layout {
|
||||
name,
|
||||
automatic,
|
||||
outputs,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
||||
245
src/lib.rs
245
src/lib.rs
@@ -1,63 +1,130 @@
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
mod config;
|
||||
use crate::config::{get_config,OutputConfig};
|
||||
use crate::config::{get_config, Config, Layout, Monitor, OutputConfig};
|
||||
|
||||
mod lid;
|
||||
use crate::lid::is_lid_closed;
|
||||
|
||||
mod sway;
|
||||
use crate::sway::{apply_outputs,get_outputs};
|
||||
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),
|
||||
/// for which all outputs are available (and lids are not closed),
|
||||
/// one for each monitor defined in the configuration file (for using just that monitor) if the lid is not closed,
|
||||
/// 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();
|
||||
fn get_layouts() -> HashSet<String> {
|
||||
let outputs = get_outputs();
|
||||
|
||||
let config = get_config();
|
||||
let monitor_states = get_monitor_states(&config, &outputs);
|
||||
|
||||
// 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: HashSet<String> = HashSet::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))
|
||||
}
|
||||
});
|
||||
get_available_layouts(&config, &monitor_states)
|
||||
.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| {
|
||||
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));
|
||||
if let Some(monitor_state) = get_monitor_state_for_output(&monitor_states, &output) {
|
||||
// do not include it if the lid is closed
|
||||
if !monitor_state.is_lid_closed {
|
||||
layout_names.insert(String::from(&monitor_state.monitor.name));
|
||||
}
|
||||
} else {
|
||||
layout_names.push(String::from(&output.name));
|
||||
layout_names.insert(String::from(&output.name));
|
||||
}
|
||||
});
|
||||
|
||||
layout_names
|
||||
}
|
||||
|
||||
/// Monitor and lid state. Created for available monitors.
|
||||
struct MonitorState<'a> {
|
||||
monitor: &'a Monitor,
|
||||
is_lid_closed: bool,
|
||||
}
|
||||
|
||||
/// Get a MonitorState for each monitor that is available (matches an output).
|
||||
fn get_monitor_states<'a, 'b>(
|
||||
config: &'a Config,
|
||||
outputs: &'b Vec<Output>,
|
||||
) -> Vec<MonitorState<'a>> {
|
||||
config
|
||||
.monitors
|
||||
.iter()
|
||||
.filter(|monitor| {
|
||||
outputs
|
||||
.iter()
|
||||
.any(|output| monitor_matches_output(&monitor, &output))
|
||||
})
|
||||
.map(|monitor| MonitorState {
|
||||
monitor,
|
||||
is_lid_closed: is_lid_closed(&monitor),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Find a monitor state that matches the supplied output.
|
||||
fn get_monitor_state_for_output<'a, 'b>(
|
||||
monitor_states: &'a Vec<MonitorState<'a>>,
|
||||
output: &'b &Output,
|
||||
) -> Option<&'a MonitorState<'a>> {
|
||||
monitor_states
|
||||
.iter()
|
||||
.find(|monitor_state| monitor_matches_output(monitor_state.monitor, output))
|
||||
}
|
||||
|
||||
/// Determine if the specified monitor matches the specified output (by make/model/serial).
|
||||
fn monitor_matches_output(monitor: &Monitor, output: &Output) -> bool {
|
||||
monitor.make == output.make && monitor.model == output.model && monitor.serial == output.serial
|
||||
}
|
||||
|
||||
/// Find a monitor state by monitor name.
|
||||
fn get_monitor_state_by_name<'a, 'b>(
|
||||
monitor_states: &'a Vec<MonitorState<'a>>,
|
||||
monitor_name: &'b String,
|
||||
) -> Option<&'a MonitorState<'a>> {
|
||||
monitor_states
|
||||
.iter()
|
||||
.find(|monitor_state| &monitor_state.monitor.name == monitor_name)
|
||||
}
|
||||
|
||||
/// Determine if the specified monitor name is available:
|
||||
/// if it is one of the known monitors and the lid is not closed.
|
||||
fn is_monitor_available<'a, 'b>(
|
||||
monitor_states: &'a Vec<MonitorState<'a>>,
|
||||
monitor_name: &'b String,
|
||||
) -> bool {
|
||||
if let Some(monitor_state) = get_monitor_state_by_name(&monitor_states, monitor_name) {
|
||||
!monitor_state.is_lid_closed
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the layouts for which all monitors are available and lids are not closed.
|
||||
fn get_available_layouts<'a, 'b, 'c>(
|
||||
config: &'a Config,
|
||||
monitor_states: &'a Vec<MonitorState<'a>>,
|
||||
) -> Vec<&'a Layout> {
|
||||
config
|
||||
.layouts
|
||||
.iter()
|
||||
.filter(|layout| {
|
||||
layout.outputs.iter().all(|(monitor_name, _output_config)| {
|
||||
is_monitor_available(monitor_states, monitor_name)
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// 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}"))
|
||||
@@ -66,56 +133,110 @@ pub fn print_layout_names() {
|
||||
/// 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();
|
||||
/// 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) -> Result<(), String> {
|
||||
let config = get_config();
|
||||
let outputs = get_outputs();
|
||||
let monitor_states = get_monitor_states(&config, &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];
|
||||
if let Some(layout) = config
|
||||
.layouts
|
||||
.iter()
|
||||
.find(|layout| &layout.name == layout_name)
|
||||
{
|
||||
// The layout is defined in the config.
|
||||
for (monitor_name, output_config) in &layout.outputs {
|
||||
// there is probably a syntactic sugar for this?
|
||||
let monitor_state = if let Some(monitor_state) =
|
||||
get_monitor_state_by_name(&monitor_states, &monitor_name)
|
||||
{
|
||||
monitor_state
|
||||
} else {
|
||||
return Err(format!(
|
||||
"Cannot find monitor '{}' for layout '{}'",
|
||||
monitor_name, layout_name
|
||||
));
|
||||
};
|
||||
|
||||
if monitor_state.is_lid_closed {
|
||||
return Err(format!("Monitor '{}' is closed.", 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
|
||||
});
|
||||
let output_opt = outputs
|
||||
.iter()
|
||||
.find(|output| monitor_matches_output(monitor_state.monitor, output));
|
||||
if let Some(output) = output_opt {
|
||||
output_config_map.insert(&output.name, &output_config);
|
||||
} else {
|
||||
panic!("Missing output for monitor {monitor_name}");
|
||||
return Err(format!("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) {
|
||||
if let Some(monitor_state) = get_monitor_state_by_name(&monitor_states, &layout_name) {
|
||||
if monitor_state.is_lid_closed {
|
||||
return Err(format!(
|
||||
"Monitor '{}' is closed.",
|
||||
&monitor_state.monitor.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
|
||||
}) {
|
||||
if let Some(output) = outputs
|
||||
.iter()
|
||||
.find(|output| monitor_matches_output(monitor_state.monitor, output))
|
||||
{
|
||||
output_config_map.insert(&output.name, &output.output_config);
|
||||
} else {
|
||||
panic!("could not find output for monitor {layout_name}")
|
||||
return Err(format!(
|
||||
"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) {
|
||||
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}")
|
||||
return Err(format!(
|
||||
"Could not find layout, monitor, or output '{}.'",
|
||||
layout_name
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
apply_outputs(&outputs, &output_config_map);
|
||||
if output_config_map.is_empty() {
|
||||
return Err("No outputs would be enabled. Do nothing.".to_string());
|
||||
}
|
||||
|
||||
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() -> Result<String,String> {
|
||||
let outputs = get_outputs();
|
||||
|
||||
let config = get_config();
|
||||
let monitor_states = get_monitor_states(&config, &outputs);
|
||||
|
||||
if let Some(layout) = get_available_layouts(&config, &monitor_states)
|
||||
.iter()
|
||||
.find(|layout| layout.automatic)
|
||||
{
|
||||
|
||||
apply_layout(&layout.name)?;
|
||||
Ok(String::from(&layout.name))
|
||||
} else {
|
||||
Err("No automatic layouts available.".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
48
src/lid.rs
Normal file
48
src/lid.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::path::PathBuf;
|
||||
use crate::config::Monitor;
|
||||
|
||||
// Determine if the lid id closed.
|
||||
// Returns true if the lid is known to be closed.
|
||||
// Returns false if the lid is known to be open, or if the lid state is not known.
|
||||
pub fn is_lid_closed(monitor: &Monitor) -> bool {
|
||||
if let Some(lid) = &monitor.lid {
|
||||
is_acpi_closed(lid)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Determine if the lid is closed.
|
||||
/// lid is the name of a directory in /proc/acpi/button/lid/.
|
||||
fn is_acpi_closed(lid: &String) -> bool {
|
||||
let mut path_buf = PathBuf::from("/proc/acpi/button/lid");
|
||||
path_buf.push(&lid);
|
||||
path_buf.push("state");
|
||||
|
||||
if let Ok(ok) = path_buf.try_exists() {
|
||||
if ok {
|
||||
File::open(path_buf).is_ok_and(|mut file| {
|
||||
let mut str = String::new();
|
||||
if let Ok(_size) = file.read_to_string(&mut str) {
|
||||
is_state_closed(str)
|
||||
} else {
|
||||
// error reading file
|
||||
false
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// no file
|
||||
false
|
||||
}
|
||||
} else {
|
||||
// error checking for file
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a /proc/acpi/button/lid/*/state file and return true if the lid is closed
|
||||
fn is_state_closed(str:String) -> bool {
|
||||
str.contains("closed")
|
||||
}
|
||||
25
src/main.rs
25
src/main.rs
@@ -1,4 +1,4 @@
|
||||
use std::env;
|
||||
use std::{env, process};
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
@@ -6,8 +6,25 @@ fn main() {
|
||||
// first arg is executable
|
||||
swayout::print_layout_names();
|
||||
} else if args.len() == 2 {
|
||||
swayout::apply_layout(&args[1]);
|
||||
let arg = &args[1];
|
||||
if arg == "--automatic" {
|
||||
match swayout::apply_automatic() {
|
||||
Ok(layout_name) => {
|
||||
println!("{}", layout_name);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("{}", e);
|
||||
process::exit(2)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if let Err(e) = swayout::apply_layout(&args[1]) {
|
||||
eprintln!("{}", e);
|
||||
process::exit(3)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
panic!("Usage: {} [layout]", args[0].as_str());
|
||||
eprintln!("Usage: {} [layout]", args[0].as_str());
|
||||
process::exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
20
src/sway.rs
20
src/sway.rs
@@ -1,8 +1,8 @@
|
||||
use crate::config::OutputConfig;
|
||||
use serde_json::{from_str, Value};
|
||||
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 {
|
||||
@@ -67,13 +67,15 @@ fn get_mode(output: &Value) -> String {
|
||||
}
|
||||
|
||||
/// Apply the specified outputs. Enable all outputs in [outputs], disable others.
|
||||
pub fn apply_outputs(all_outputs: &Vec<Output>, outputs: &HashMap<&String, &OutputConfig>) {
|
||||
|
||||
pub fn apply_outputs(
|
||||
all_outputs: &Vec<Output>,
|
||||
outputs: &HashMap<&String, &OutputConfig>,
|
||||
) -> Result<(), String> {
|
||||
// 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();
|
||||
let mut enabled: HashMap<&String, &OutputConfig> = HashMap::new();
|
||||
// for outputs to be disabled: output names
|
||||
let mut disabled: Vec<&String> = Vec::new();
|
||||
|
||||
@@ -85,8 +87,12 @@ pub fn apply_outputs(all_outputs: &Vec<Output>, outputs: &HashMap<&String, &Outp
|
||||
}
|
||||
});
|
||||
|
||||
if enabled.is_empty() {
|
||||
return Err("No enabled outputs!".to_string());
|
||||
}
|
||||
|
||||
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_name);
|
||||
cmd.arg("enable");
|
||||
@@ -114,4 +120,6 @@ pub fn apply_outputs(all_outputs: &Vec<Output>, outputs: &HashMap<&String, &Outp
|
||||
.for_each(|arg| print!("{} ", arg.to_str().unwrap()));
|
||||
|
||||
cmd.output().expect("swaymsg output failed");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user